|
|
|
@ -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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|