new mapping method

This commit is contained in:
Michele Artini 2023-06-27 09:55:39 +02:00
parent 81404e3ee9
commit 566e94d67b
3 changed files with 128 additions and 35 deletions

View File

@ -27,7 +27,7 @@ import eu.dnetlib.openaire.community.db.repository.DbDatasourceRepository;
import eu.dnetlib.openaire.community.db.repository.DbOrganizationRepository;
import eu.dnetlib.openaire.community.db.repository.DbProjectRepository;
import eu.dnetlib.openaire.community.db.repository.DbSupportOrgRepository;
import eu.dnetlib.openaire.community.db.utils.ConvertionUtils;
import eu.dnetlib.openaire.community.db.utils.CommunityMappingUtils;
import eu.dnetlib.openaire.exporter.exceptions.CommunityException;
import eu.dnetlib.openaire.exporter.exceptions.ResourceNotFoundException;
import eu.dnetlib.openaire.exporter.model.community.CommunityContentprovider;
@ -47,9 +47,8 @@ public class CommunityService {
// 1.1) Completare CommunityImporterController
// 1.2) Considerare anche le Subcommunities: importazione tramite profili
// 2) Subcommunities: visualizzazione tramite le context api
// 3) Implemtare il metodo CommunityService.setCommunity
// 4) ricontrollare se tutti i campi del database sono esposti dalle api (modello + mapping)
// 5) Verificare Tickets: #8835, #8854, #6483, #3259, #3494
// 3) Come ritornare i campi delle community_orgs ?
// 4) Verificare Tickets: #8835, #8854, #6483, #3259, #3494
@Autowired
private DbCommunityRepository dbCommunityRepository;
@ -65,30 +64,33 @@ public class CommunityService {
public List<CommunitySummary> listCommunities() throws CommunityException {
return dbCommunityRepository.findAll()
.stream()
.map(ConvertionUtils::toCommunitySummary)
.map(CommunityMappingUtils::toCommunitySummary)
.collect(Collectors.toList());
}
public CommunityDetails saveCommunity(final CommunityDetails details) throws CommunityException {
dbCommunityRepository.save(ConvertionUtils.toCommunity(details));
dbCommunityRepository.save(CommunityMappingUtils.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);
return CommunityMappingUtils.CommunityDetails(c);
}
@Transactional
public void setCommunity(final String id, final CommunityWritableProperties details) throws CommunityException, ResourceNotFoundException {
// TODO Auto-generated method stub
final DbCommunity c = dbCommunityRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Community not found: " + id));
CommunityMappingUtils.populateCommunity(c, details);
dbCommunityRepository.save(c);
}
public Page<CommunityProject> getCommunityProjects(final String id, final int page, final int size) throws CommunityException, ResourceNotFoundException {
return dbProjectRepository.findByCommunity(id, PageRequest.of(page, size)).map(ConvertionUtils::toCommunityProject);
return dbProjectRepository.findByCommunity(id, PageRequest.of(page, size)).map(CommunityMappingUtils::toCommunityProject);
}
public CommunityProject addCommunityProject(final String id, final CommunityProject project) throws CommunityException, ResourceNotFoundException {
final DbProject p = ConvertionUtils.toDbProject(id, project);
final DbProject p = CommunityMappingUtils.toDbProject(id, project);
dbProjectRepository.save(p);
return project;
}
@ -97,7 +99,7 @@ public class CommunityService {
throws CommunityException, ResourceNotFoundException {
final List<DbProject> list = projectList.stream()
.map(p -> ConvertionUtils.toDbProject(id, p))
.map(p -> CommunityMappingUtils.toDbProject(id, p))
.collect(Collectors.toList());
dbProjectRepository.saveAll(list);
@ -119,13 +121,13 @@ public class CommunityService {
public List<CommunityContentprovider> getCommunityContentproviders(final String id) throws CommunityException, ResourceNotFoundException {
return dbDatasourceRepository.findByCommunity(id)
.stream()
.map(ConvertionUtils::toCommunityContentprovider)
.map(CommunityMappingUtils::toCommunityContentprovider)
.collect(Collectors.toList());
}
public CommunityContentprovider addCommunityContentprovider(final String id, final CommunityContentprovider cp)
throws CommunityException, ResourceNotFoundException {
final DbDatasource ds = ConvertionUtils.toDbDatasource(id, cp);
final DbDatasource ds = CommunityMappingUtils.toDbDatasource(id, cp);
dbDatasourceRepository.save(ds);
return cp;
}
@ -134,7 +136,7 @@ public class CommunityService {
throws CommunityException, ResourceNotFoundException {
final List<DbDatasource> list = contentprovidersList.stream()
.map(cp -> ConvertionUtils.toDbDatasource(id, cp))
.map(cp -> CommunityMappingUtils.toDbDatasource(id, cp))
.collect(Collectors.toList());
dbDatasourceRepository.saveAll(list);
@ -161,13 +163,13 @@ public class CommunityService {
public List<CommunityOrganization> getCommunityOrganizations(final String id) throws CommunityException, ResourceNotFoundException {
return dbSupportOrgRepository.findByCommunity(id)
.stream()
.map(ConvertionUtils::toCommunityOrganiztion)
.map(CommunityMappingUtils::toCommunityOrganiztion)
.collect(Collectors.toList());
}
public CommunityOrganization addCommunityOrganization(final String id, final CommunityOrganization organization)
throws CommunityException, ResourceNotFoundException {
final DbSupportOrg o = ConvertionUtils.toDbSupportOrg(id, organization);
final DbSupportOrg o = CommunityMappingUtils.toDbSupportOrg(id, organization);
dbSupportOrgRepository.save(o);
return organization;
}
@ -176,7 +178,7 @@ public class CommunityService {
throws CommunityException, ResourceNotFoundException {
final List<DbSupportOrg> list = orgList.stream()
.map(o -> ConvertionUtils.toDbSupportOrg(id, o))
.map(o -> CommunityMappingUtils.toDbSupportOrg(id, o))
.collect(Collectors.toList());
dbSupportOrgRepository.saveAll(list);

View File

@ -5,6 +5,7 @@ import java.util.Arrays;
import java.util.List;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import eu.dnetlib.openaire.community.db.model.DbCommunity;
import eu.dnetlib.openaire.community.db.model.DbDatasource;
@ -15,8 +16,9 @@ import eu.dnetlib.openaire.exporter.model.community.CommunityDetails;
import eu.dnetlib.openaire.exporter.model.community.CommunityOrganization;
import eu.dnetlib.openaire.exporter.model.community.CommunityProject;
import eu.dnetlib.openaire.exporter.model.community.CommunitySummary;
import eu.dnetlib.openaire.exporter.model.community.CommunityWritableProperties;
public class ConvertionUtils {
public class CommunityMappingUtils {
public final static String PIPE_SEPARATOR = "||";
@ -33,6 +35,7 @@ public class ConvertionUtils {
c.setShortName(details.getShortName());
c.setDescription(details.getDescription());
c.setStatus(details.getStatus());
c.setLogoUrl(details.getLogoUrl());
c.setMembership(details.getMembership());
c.setType(details.getType());
c.setClaim(details.getClaim());
@ -45,12 +48,57 @@ public class ConvertionUtils {
c.setOtherZenodoCommunities(toStringArray(details.getOtherZenodoCommunities()));
c.setCreationDate(ObjectUtils.firstNonNull(details.getCreationDate(), LocalDateTime.now()));
c.setLastUpdateDate(LocalDateTime.now());
c.setLogoUrl(details.getLogoUrl());
return c;
}
private static String[] toStringArray(final List<String> list) {
return list.toArray(new String[list.size()]);
public static void populateCommunity(final DbCommunity c, final CommunityWritableProperties details) {
if (StringUtils.isNotBlank(details.getName())) {
c.setName(details.getName());
}
if (StringUtils.isNotBlank(details.getShortName())) {
c.setShortName(details.getShortName());
}
if (StringUtils.isNotBlank(details.getDescription())) {
c.setDescription(details.getDescription());
}
if (details.getStatus() != null) {
c.setStatus(details.getStatus());
}
if (details.getMembership() != null) {
c.setMembership(details.getMembership());
}
if (details.getType() != null) {
c.setType(details.getType());
}
if (details.getClaim() != null) {
c.setClaim(details.getClaim());
}
if (StringUtils.isNotBlank(details.getLogoUrl())) {
c.setLogoUrl(details.getLogoUrl());
}
if (details.getFos() != null) {
c.setFos(toStringArray(details.getFos()));
}
if (details.getSdg() != null) {
c.setSdg(toStringArray(details.getSdg()));
}
if (details.getSubjects() != null) {
c.setSubjects(toStringArray(details.getSubjects()));
}
if (details.getAdvancedConstraints() != null) {
c.setAdvancedConstraints(details.getAdvancedConstraints());
}
if (details.getRemoveConstraints() != null) {
c.setRemoveConstraints(details.getRemoveConstraints());
}
if (StringUtils.isNotBlank(details.getMainZenodoCommunity())) {
c.setMainZenodoCommunity(details.getMainZenodoCommunity());
}
if (details.getOtherZenodoCommunities() != null) {
c.setOtherZenodoCommunities(toStringArray(details.getOtherZenodoCommunities()));
}
c.setLastUpdateDate(LocalDateTime.now());
}
public static CommunityDetails CommunityDetails(final DbCommunity c) {
@ -141,4 +189,7 @@ public class ConvertionUtils {
return dbo;
}
private static String[] toStringArray(final List<String> list) {
return list.toArray(new String[list.size()]);
}
}

View File

@ -34,26 +34,26 @@ public class CommunityWritableProperties {
@Schema(description = "Advanced constraint for the association of results to the community")
private SelectionCriteria advancedConstraints;
@Schema(description = "Constraint for removing")
private SelectionCriteria removeConstraints;
@Schema(description = "status of the community, drives its visibility")
private CommunityStatus status;
@Schema(description = "id of the main Zenodo community")
private String mainZenodoCommunity;
public static CommunityWritableProperties fromDetails(final CommunityDetails details) {
final CommunityWritableProperties p = new CommunityWritableProperties();
p.setName(details.getName());
p.setShortName(details.getShortName());
p.setDescription(details.getDescription());
p.setLogoUrl(details.getLogoUrl());
p.setSubjects(details.getSubjects());
p.setStatus(details.getStatus());
p.setMainZenodoCommunity(details.getZenodoCommunity());
p.setFos(details.getFos());
p.setSdg(details.getSdg());
p.setAdvancedConstraints(details.getAdvancedConstraints());
return p;
}
@Schema(description = "identifiers of the other Zenodo community")
private List<String> otherZenodoCommunities;
@Schema(description = "membership of the community")
private CommunityMembershipType membership;
@Schema(description = "type of the community")
private CommunityType type;
@Schema(description = "type of supported claim")
private CommunityClaimType claim;
public List<String> getFos() {
return fos;
@ -134,4 +134,44 @@ public class CommunityWritableProperties {
public void setMainZenodoCommunity(final String mainZenodoCommunity) {
this.mainZenodoCommunity = mainZenodoCommunity;
}
public CommunityMembershipType getMembership() {
return membership;
}
public void setMembership(final CommunityMembershipType membership) {
this.membership = membership;
}
public CommunityType getType() {
return type;
}
public void setType(final CommunityType type) {
this.type = type;
}
public CommunityClaimType getClaim() {
return claim;
}
public void setClaim(final CommunityClaimType claim) {
this.claim = claim;
}
public SelectionCriteria getRemoveConstraints() {
return removeConstraints;
}
public void setRemoveConstraints(final SelectionCriteria removeConstraints) {
this.removeConstraints = removeConstraints;
}
public List<String> getOtherZenodoCommunities() {
return otherZenodoCommunities;
}
public void setOtherZenodoCommunities(final List<String> otherZenodoCommunities) {
this.otherZenodoCommunities = otherZenodoCommunities;
}
}