new_model_for_communities #15
|
@ -0,0 +1,54 @@
|
|||
package eu.dnetlib.openaire.community;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import eu.dnetlib.openaire.community.db.CommunityService;
|
||||
import eu.dnetlib.openaire.exporter.exceptions.CommunityException;
|
||||
import eu.dnetlib.openaire.exporter.model.community.CommunityContentprovider;
|
||||
import eu.dnetlib.openaire.exporter.model.community.CommunityDetails;
|
||||
import eu.dnetlib.openaire.exporter.model.community.CommunityOrganization;
|
||||
import eu.dnetlib.openaire.exporter.model.community.CommunityProject;
|
||||
|
||||
@RestController
|
||||
public class CommunityImporterController {
|
||||
|
||||
@Autowired
|
||||
private CommunityService service;
|
||||
|
||||
@Autowired
|
||||
private CommunityApiCore core;
|
||||
|
||||
@GetMapping("/community_importer/import")
|
||||
public List<String> importProfiles() throws CommunityException {
|
||||
final List<String> list = core.listCommunities()
|
||||
.stream()
|
||||
.map(c -> c.getId())
|
||||
.collect(Collectors.toList());
|
||||
|
||||
list.forEach(this::importCommunity);
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
private void importCommunity(final String id) {
|
||||
try {
|
||||
final CommunityDetails community = core.getCommunity(id);
|
||||
final List<CommunityContentprovider> datasources = core.getCommunityContentproviders(id);
|
||||
final List<CommunityProject> projects = core.getCommunityProjects(id);
|
||||
final List<CommunityOrganization> orgs = core.getCommunityOrganizations(id);
|
||||
|
||||
service.saveCommunity(community);
|
||||
service.addCommunityProjectList(id, projects);
|
||||
service.addCommunityContentProvidersList(id, datasources);
|
||||
service.addCommunityOrganizationList(id, orgs);
|
||||
// TODO MANAGE new fields and tables
|
||||
} catch (final Exception e) {
|
||||
throw new RuntimeException("Error importing community: " + id, e);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -75,6 +75,12 @@ public class CommunityService {
|
|||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
// TODO perche' questo metodo non esisteva prima?
|
||||
public CommunityDetails saveCommunity(final CommunityDetails details) throws CommunityException {
|
||||
dbCommunityRepository.save(ConvertionUtils.toCommunity(details));
|
||||
return getCommunity(details.getId());
|
||||
}
|
||||
|
||||
public CommunityDetails getCommunity(final String id) throws CommunityException, ResourceNotFoundException {
|
||||
final DbCommunity c = dbCommunityRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Community not found: " + id));
|
||||
return ConvertionUtils.CommunityDetails(c);
|
||||
|
@ -106,7 +112,7 @@ public class CommunityService {
|
|||
|
||||
dbProjectRepository.saveAll(list);
|
||||
|
||||
return projectList;
|
||||
return getCommunityProjects(id);
|
||||
}
|
||||
|
||||
public void removeCommunityProject(final String id, final String projectId) throws CommunityException, ResourceNotFoundException {
|
||||
|
@ -143,7 +149,7 @@ public class CommunityService {
|
|||
|
||||
dbDatasourceRepository.saveAll(list);
|
||||
|
||||
return contentprovidersList;
|
||||
return getCommunityContentproviders(id);
|
||||
}
|
||||
|
||||
public void removeCommunityContentProvider(final String id, final String contentproviderId) throws CommunityException, ResourceNotFoundException {
|
||||
|
@ -176,6 +182,18 @@ public class CommunityService {
|
|||
return organization;
|
||||
}
|
||||
|
||||
public List<CommunityOrganization> addCommunityOrganizationList(final String id, final List<CommunityOrganization> orgList)
|
||||
throws CommunityException, ResourceNotFoundException {
|
||||
|
||||
final List<DbOrganization> list = orgList.stream()
|
||||
.map(o -> ConvertionUtils.toDbOrganization(id, o))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
dbOrganizationRepository.saveAll(list);
|
||||
|
||||
return getCommunityOrganizations(id);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public CommunityDetails addCommunitySubjects(final String id, final List<String> subjects) throws CommunityException, ResourceNotFoundException {
|
||||
return modifyElementToArrayField(id, subjects, c -> c.getSubjects(), (c, subs) -> c.setSubjects(subs), false);
|
||||
|
|
|
@ -23,6 +23,11 @@ public class ConvertionUtils {
|
|||
return summary;
|
||||
}
|
||||
|
||||
public static DbCommunity toCommunity(final CommunityDetails details) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
public static CommunityDetails CommunityDetails(final DbCommunity c) {
|
||||
|
||||
final CommunityDetails details = new CommunityDetails();
|
||||
|
|
Loading…
Reference in New Issue