implementation of some internal methods

This commit is contained in:
Michele Artini 2023-06-15 10:18:18 +02:00
parent 71d3ed7cee
commit c622c4cd53
4 changed files with 152 additions and 44 deletions

View File

@ -140,7 +140,7 @@ public class CommunityApiController {
})
public void deleteCommunityProject(
@PathVariable final String id,
@RequestBody final Integer projectId) throws CommunityException, ResourceNotFoundException {
@RequestBody final String projectId) throws CommunityException, ResourceNotFoundException {
communityService.removeCommunityProject(id, projectId);
}
@ -176,7 +176,7 @@ public class CommunityApiController {
})
public void deleteCommunityProjectList(
@PathVariable final String id,
@RequestBody final List<Integer> projectIdList) throws CommunityException, ResourceNotFoundException {
@RequestBody final List<String> projectIdList) throws CommunityException, ResourceNotFoundException {
communityService.removeCommunityProjectList(id, projectIdList);
}
@ -227,7 +227,7 @@ public class CommunityApiController {
})
public void removeCommunityContentprovider(
@PathVariable final String id,
@RequestBody final Integer contentproviderId) throws CommunityException, ResourceNotFoundException {
@RequestBody final String contentproviderId) throws CommunityException, ResourceNotFoundException {
communityService.removeCommunityContentProvider(id, contentproviderId);
}
@ -263,7 +263,7 @@ public class CommunityApiController {
})
public void deleteCommunityContentProvidersList(
@PathVariable final String id,
@RequestBody final List<Integer> contentProviderIdList) throws CommunityException, ResourceNotFoundException {
@RequestBody final List<String> contentProviderIdList) throws CommunityException, ResourceNotFoundException {
communityService.removeCommunityContentProviderList(id, contentProviderIdList);
}
@ -316,7 +316,7 @@ public class CommunityApiController {
})
public void removeCommunityOrganization(
@PathVariable final String id,
@RequestBody final Integer organizationId) throws CommunityException, ResourceNotFoundException {
@RequestBody final String organizationId) throws CommunityException, ResourceNotFoundException {
communityService.removeCommunityOrganization(id, organizationId);
}

View File

@ -1,13 +1,26 @@
package eu.dnetlib.openaire.community.db;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import eu.dnetlib.openaire.community.db.model.DbCommunity;
import eu.dnetlib.openaire.community.db.model.DbDatasource;
import eu.dnetlib.openaire.community.db.model.DbDatasourcePK;
import eu.dnetlib.openaire.community.db.model.DbProject;
import eu.dnetlib.openaire.community.db.model.DbProjectPK;
import eu.dnetlib.openaire.community.db.repository.DbCommunityRepository;
import eu.dnetlib.openaire.community.db.repository.DbDatasourceRepository;
import eu.dnetlib.openaire.community.db.repository.DbOrganizationRepository;
@ -29,6 +42,14 @@ import eu.dnetlib.openaire.exporter.model.community.selectioncriteria.SelectionC
@ConditionalOnProperty(value = "openaire.exporter.enable.community", havingValue = "true")
public class CommunityService {
// TODO
// 1) Aggiungere i campi membership e claim al modello delle api
// 2) Capire come gestire il campo Id (Integer) di CommunityProject
// 3) Gestire paginazione dei progetti
// 4) CommunityContentprovider sono le datasources?
// 5) Capire come gestire il campo Id (Integer) di CommunityContentprovider
// 5) Capire come gestire il campo Id (Integer) di CommunityOrganization
@Autowired
private DbCommunityRepository dbCommunityRepository;
@Autowired
@ -81,91 +102,118 @@ public class CommunityService {
return projectList;
}
public void removeCommunityProject(final String id, final Integer projectId) throws CommunityException, ResourceNotFoundException {
// dbProjectRepository.deleteById(new DbProjectPK(id, projectId));
public void removeCommunityProject(final String id, final String projectId) throws CommunityException, ResourceNotFoundException {
dbProjectRepository.deleteById(new DbProjectPK(id, projectId));
}
public void removeCommunityProjectList(final String id, final List<Integer> projectIdList) throws CommunityException, ResourceNotFoundException {
// TODO Auto-generated method stub
public void removeCommunityProjectList(final String id, final List<String> projectIdList) throws CommunityException, ResourceNotFoundException {
final List<DbProjectPK> list = projectIdList.stream()
.map(projectId -> new DbProjectPK(id, projectId))
.collect(Collectors.toList());
dbProjectRepository.deleteAllById(list);
}
public List<CommunityContentprovider> getCommunityContentproviders(final String id) throws CommunityException, ResourceNotFoundException {
// TODO Auto-generated method stub
return null;
return dbDatasourceRepository.findByCommunity(id)
.stream()
.map(ConvertionUtils::toCommunityContentprovider)
.collect(Collectors.toList());
}
public CommunityContentprovider addCommunityContentprovider(final String id, final CommunityContentprovider cp)
throws CommunityException, ResourceNotFoundException {
// TODO Auto-generated method stub
return null;
}
public void removeCommunityContentProvider(final String id, final Integer contentproviderId) throws CommunityException, ResourceNotFoundException {
// TODO Auto-generated method stub
final DbDatasource ds = ConvertionUtils.toDbDatasource(id, cp);
dbDatasourceRepository.save(ds);
return cp;
}
public List<CommunityContentprovider> addCommunityContentProvidersList(final String id, final List<CommunityContentprovider> contentprovidersList)
throws CommunityException, ResourceNotFoundException {
// TODO Auto-generated method stub
return null;
final List<DbDatasource> list = contentprovidersList.stream()
.map(cp -> ConvertionUtils.toDbDatasource(id, cp))
.collect(Collectors.toList());
dbDatasourceRepository.saveAll(list);
return contentprovidersList;
}
public void removeCommunityContentProviderList(final String id, final List<Integer> contentProviderIdList)
public void removeCommunityContentProvider(final String id, final String contentproviderId) throws CommunityException, ResourceNotFoundException {
dbDatasourceRepository.deleteById(new DbDatasourcePK(id, contentproviderId));
}
public void removeCommunityContentProviderList(final String id, final List<String> contentProviderIdList)
throws CommunityException, ResourceNotFoundException {
// TODO Auto-generated method stub
final List<DbDatasourcePK> list = contentProviderIdList.stream()
.map(dsId -> new DbDatasourcePK(id, dsId))
.collect(Collectors.toList());
dbDatasourceRepository.deleteAllById(list);
}
public void removeCommunityOrganization(final String id, final Integer organizationId) throws CommunityException, ResourceNotFoundException {
// TODO Auto-generated method stub
}
public List<CommunityZenodoCommunity> getCommunityZenodoCommunities(final String id) throws CommunityException, ResourceNotFoundException {
// TODO Auto-generated method stub
return null;
public void removeCommunityOrganization(final String id, final String organizationId) throws CommunityException, ResourceNotFoundException {
dbDatasourceRepository.deleteById(new DbDatasourcePK(id, organizationId));
}
public List<CommunityOrganization> getCommunityOrganizations(final String id) throws CommunityException, ResourceNotFoundException {
// TODO Auto-generated method stub
return null;
return dbOrganizationRepository.findByCommunity(id)
.stream()
.map(ConvertionUtils::toCommunityOrganiztion)
.collect(Collectors.toList());
}
@Transactional
public CommunityDetails addCommunitySubjects(final String id, final List<String> subjects) throws CommunityException, ResourceNotFoundException {
// TODO Auto-generated method stub
return null;
return modifyElementToArrayField(id, subjects, c -> c.getSubjects(), (c, subs) -> c.setSubjects(subs), false);
}
@Transactional
public CommunityDetails removeCommunitySubjects(final String id, final List<String> subjects) throws CommunityException, ResourceNotFoundException {
// TODO Auto-generated method stub
return null;
return modifyElementToArrayField(id, subjects, c -> c.getSubjects(), (c, subs) -> c.setSubjects(subs), true);
}
@Transactional
public CommunityDetails addCommunityFOS(final String id, final List<String> foss) throws CommunityException, ResourceNotFoundException {
// TODO Auto-generated method stub
return null;
return modifyElementToArrayField(id, foss, c -> c.getFos(), (c, fos) -> c.setFos(fos), false);
}
@Transactional
public CommunityDetails removeCommunityFOS(final String id, final List<String> foss) throws CommunityException, ResourceNotFoundException {
// TODO Auto-generated method stub
return null;
return modifyElementToArrayField(id, foss, c -> c.getFos(), (c, fos) -> c.setFos(fos), true);
}
@Transactional
public CommunityDetails addCommunitySDG(final String id, final List<String> sdgs) throws CommunityException, ResourceNotFoundException {
// TODO Auto-generated method stub
return null;
return modifyElementToArrayField(id, sdgs, c -> c.getSdg(), (c, sdg) -> c.setSdg(sdg), false);
}
@Transactional
public CommunityDetails removeCommunitySDG(final String id, final List<String> sdgs) throws CommunityException, ResourceNotFoundException {
// TODO Auto-generated method stub
return null;
return modifyElementToArrayField(id, sdgs, c -> c.getSdg(), (c, sdg) -> c.setSdg(sdg), true);
}
public CommunityDetails addCommunityAdvancedConstraint(final String id, final SelectionCriteria advancedCosntraint)
throws CommunityException, ResourceNotFoundException {
// TODO Auto-generated method stub
return null;
try {
final DbCommunity dbEntry = dbCommunityRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Community not found: " + id));
dbEntry.setAdvancedConstraints(new ObjectMapper().writeValueAsString(advancedCosntraint));
dbCommunityRepository.save(dbEntry);
return getCommunity(id);
} catch (final JsonProcessingException e) {
throw new CommunityException("Error converting json");
}
}
public CommunityDetails removeCommunityAdvancedConstraint(final String id) throws ResourceNotFoundException, CommunityException {
final DbCommunity dbEntry = dbCommunityRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Community not found: " + id));
dbEntry.setAdvancedConstraints(null);
dbCommunityRepository.save(dbEntry);
return getCommunity(id);
}
public List<CommunityZenodoCommunity> getCommunityZenodoCommunities(final String id) throws CommunityException, ResourceNotFoundException {
// TODO Auto-generated method stub
return null;
}
@ -190,4 +238,33 @@ public class CommunityService {
// TODO Auto-generated method stub
return null;
}
private CommunityDetails modifyElementToArrayField(final String id,
final List<String> values,
final Function<DbCommunity, String[]> getter,
final BiConsumer<DbCommunity, String[]> setter,
final boolean remove) throws ResourceNotFoundException, CommunityException {
final DbCommunity dbEntry = dbCommunityRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Community not found: " + id));
final Set<String> tmpList = new LinkedHashSet<>();
final String[] oldValues = getter.apply(dbEntry);
if (oldValues != null) {
for (final String s : oldValues) {
tmpList.add(s);
}
}
if (remove) {
tmpList.removeAll(values);
} else {
tmpList.addAll(values);
}
setter.accept(dbEntry, tmpList.toArray(new String[tmpList.size()]));
dbCommunityRepository.save(dbEntry);
return getCommunity(id);
}
}

View File

@ -1,7 +1,11 @@
package eu.dnetlib.openaire.community.db;
import eu.dnetlib.openaire.community.db.model.DbCommunity;
import eu.dnetlib.openaire.community.db.model.DbDatasource;
import eu.dnetlib.openaire.community.db.model.DbOrganization;
import eu.dnetlib.openaire.community.db.model.DbProject;
import eu.dnetlib.openaire.exporter.model.community.CommunityContentprovider;
import eu.dnetlib.openaire.exporter.model.community.CommunityOrganization;
import eu.dnetlib.openaire.exporter.model.community.CommunityProject;
import eu.dnetlib.openaire.exporter.model.community.CommunitySummary;
@ -22,4 +26,24 @@ public class ConvertionUtils {
return null;
}
public static CommunityContentprovider toCommunityContentprovider(final DbDatasource dbEntry) {
// TODO
return null;
}
public static DbDatasource toDbDatasource(final String id, final CommunityContentprovider provider) {
// TODO Auto-generated method stub
return null;
}
public static CommunityOrganization toCommunityOrganiztion(final DbOrganization dbEntry) {
// TODO
return null;
}
public static DbOrganization toDbOrganization(final String id, final CommunityOrganization org) {
// TODO Auto-generated method stub
return null;
}
}

View File

@ -11,6 +11,13 @@ public class DbDatasourcePK implements Serializable {
private String dsId;
public DbDatasourcePK() {}
public DbDatasourcePK(final String community, final String dsId) {
this.community = community;
this.dsId = dsId;
}
public String getCommunity() {
return community;
}