dnet-applications/apps/dnet-exporter-api/src/main/java/eu/dnetlib/openaire/community/CommunityService.java

555 lines
19 KiB
Java

package eu.dnetlib.openaire.community;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
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.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import eu.dnetlib.openaire.community.model.DbCommunity;
import eu.dnetlib.openaire.community.model.DbDatasource;
import eu.dnetlib.openaire.community.model.DbDatasourcePK;
import eu.dnetlib.openaire.community.model.DbOrganization;
import eu.dnetlib.openaire.community.model.DbProject;
import eu.dnetlib.openaire.community.model.DbProjectPK;
import eu.dnetlib.openaire.community.model.DbSubCommunity;
import eu.dnetlib.openaire.community.model.DbSupportOrg;
import eu.dnetlib.openaire.community.model.DbSupportOrgPK;
import eu.dnetlib.openaire.community.repository.DbCommunityRepository;
import eu.dnetlib.openaire.community.repository.DbDatasourceRepository;
import eu.dnetlib.openaire.community.repository.DbOrganizationRepository;
import eu.dnetlib.openaire.community.repository.DbProjectRepository;
import eu.dnetlib.openaire.community.repository.DbSubCommunityRepository;
import eu.dnetlib.openaire.community.repository.DbSupportOrgRepository;
import eu.dnetlib.openaire.community.utils.CommunityMappingUtils;
import eu.dnetlib.openaire.exporter.exceptions.CommunityException;
import eu.dnetlib.openaire.exporter.exceptions.ResourceNotFoundException;
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;
import eu.dnetlib.openaire.exporter.model.community.CommunitySummary;
import eu.dnetlib.openaire.exporter.model.community.CommunityWritableProperties;
import eu.dnetlib.openaire.exporter.model.community.SubCommunity;
import eu.dnetlib.openaire.exporter.model.community.selectioncriteria.SelectionCriteria;
@Service
@ConditionalOnProperty(value = "openaire.exporter.enable.community", havingValue = "true")
public class CommunityService {
@Autowired
private DbCommunityRepository dbCommunityRepository;
@Autowired
private DbProjectRepository dbProjectRepository;
@Autowired
private DbDatasourceRepository dbDatasourceRepository;
@Autowired
private DbOrganizationRepository dbOrganizationRepository;
@Autowired
private DbSupportOrgRepository dbSupportOrgRepository;
@Autowired
private DbSubCommunityRepository dbSubCommunityRepository;
private static final Log log = LogFactory.getLog(CommunityService.class);
public List<CommunitySummary> listCommunities() throws CommunityException {
try {
return dbCommunityRepository.findAll()
.stream()
.map(CommunityMappingUtils::toCommunitySummary)
.collect(Collectors.toList());
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
}
@Transactional
public CommunityDetails newCommunity(final CommunityDetails details) throws CommunityException {
if (StringUtils.isBlank(details.getId())) {
throw new CommunityException("Empty Id");
} else if (dbCommunityRepository.existsById(details.getId())) {
throw new CommunityException("Community already exists: " + details.getId());
} else {
details.setCreationDate(LocalDateTime.now());
return saveCommunity(details);
}
}
@Transactional
public CommunityDetails saveCommunity(final CommunityDetails details) throws CommunityException {
try {
details.setLastUpdateDate(LocalDateTime.now());
dbCommunityRepository.save(CommunityMappingUtils.toCommunity(details));
return getCommunity(details.getId());
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
}
@Transactional
public CommunityDetails getCommunity(final String id) throws CommunityException {
try {
final DbCommunity c = dbCommunityRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Community not found: " + id));
return CommunityMappingUtils.CommunityDetails(c);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
}
@Transactional
public void setCommunity(final String id, final CommunityWritableProperties details) throws CommunityException {
try {
final DbCommunity c = dbCommunityRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Community not found: " + id));
CommunityMappingUtils.populateCommunity(c, details);
c.setLastUpdateDate(LocalDateTime.now());
dbCommunityRepository.save(c);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
}
@Transactional
public Page<CommunityProject> getCommunityProjects(final String id, final int page, final int size) throws CommunityException {
try {
return dbProjectRepository.findByCommunity(id, PageRequest.of(page, size)).map(CommunityMappingUtils::toCommunityProject);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
}
@Transactional
public CommunityProject addCommunityProject(final String id, final CommunityProject project) throws CommunityException {
try {
final DbProject p = CommunityMappingUtils.toDbProject(id, project);
dbProjectRepository.save(p);
return project;
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
}
@Transactional
public void addCommunityProjects(final String id, final CommunityProject... projects) throws CommunityException {
try {
final List<DbProject> list = Arrays.stream(projects)
.map(p -> CommunityMappingUtils.toDbProject(id, p))
.collect(Collectors.toList());
dbProjectRepository.saveAll(list);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
}
@Transactional
public void removeCommunityProjects(final String id, final String... ids) throws CommunityException {
try {
final List<DbProjectPK> list = Arrays.stream(ids)
.map(projectId -> new DbProjectPK(id, projectId))
.collect(Collectors.toList());
dbProjectRepository.deleteAllById(list);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
}
public List<CommunityContentprovider> getCommunityContentproviders(final String id) throws CommunityException {
try {
return dbDatasourceRepository.findByCommunity(id)
.stream()
.map(CommunityMappingUtils::toCommunityContentprovider)
.collect(Collectors.toList());
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
}
@Transactional
public void addCommunityContentProviders(final String id, final CommunityContentprovider... contentproviders)
throws CommunityException {
try {
final List<DbDatasource> list = Arrays.stream(contentproviders)
.map(cp -> CommunityMappingUtils.toDbDatasource(id, cp))
.collect(Collectors.toList());
dbDatasourceRepository.saveAll(list);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
}
@Transactional
public void removeCommunityContentProviders(final String id, final String... ids) throws CommunityException {
try {
final List<DbDatasourcePK> list = Arrays.stream(ids)
.map(dsId -> new DbDatasourcePK(id, dsId))
.collect(Collectors.toList());
dbDatasourceRepository.deleteAllById(list);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
}
@Transactional
public void removeCommunityOrganizations(final String id, final String... orgNames) throws CommunityException {
try {
final List<DbSupportOrgPK> list = Arrays.stream(orgNames)
.map(name -> new DbSupportOrgPK(id, name))
.collect(Collectors.toList());
dbSupportOrgRepository.deleteAllById(list);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
}
@Transactional
public List<CommunityOrganization> getCommunityOrganizations(final String id) throws CommunityException {
try {
return dbSupportOrgRepository.findByCommunity(id)
.stream()
.map(CommunityMappingUtils::toCommunityOrganization)
.collect(Collectors.toList());
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
}
@Transactional
public void addCommunityOrganizations(final String id, final CommunityOrganization... orgs) throws CommunityException {
try {
final List<DbSupportOrg> list = Arrays.stream(orgs)
.map(o -> CommunityMappingUtils.toDbSupportOrg(id, o))
.collect(Collectors.toList());
dbSupportOrgRepository.saveAll(list);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
}
@Transactional
public void removeSubCommunity(final String id, final String subCommunityId) throws CommunityException {
try {
dbSubCommunityRepository.deleteById(subCommunityId);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
}
@Transactional
public List<SubCommunity> getSubCommunities(final String id) throws CommunityException {
try {
return dbSubCommunityRepository.findByCommunity(id)
.stream()
.map(CommunityMappingUtils::toSubCommunity)
.collect(Collectors.toList());
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
}
@Transactional
public void addSubCommunities(final SubCommunity... subs) throws CommunityException {
try {
final List<DbSubCommunity> list = Arrays.stream(subs)
.map(CommunityMappingUtils::toDbSubCommunity)
.collect(Collectors.toList());
dbSubCommunityRepository.saveAll(list);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
}
@Transactional
public CommunityDetails addCommunitySubjects(final String id, final String... subjects) throws CommunityException {
try {
return modifyElementToArrayField(id, c -> c.getSubjects(), (c, subs) -> c.setSubjects(subs), false, subjects);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
}
public CommunityDetails removeCommunitySubjects(final String id, final String... subjects) throws CommunityException {
try {
return modifyElementToArrayField(id, c -> c.getSubjects(), (c, subs) -> c.setSubjects(subs), true, subjects);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
}
public CommunityDetails addCommunityFOS(final String id, final String... foss) throws CommunityException {
try {
return modifyElementToArrayField(id, c -> c.getFos(), (c, fos) -> c.setFos(fos), false, foss);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
}
public CommunityDetails removeCommunityFOS(final String id, final String... foss) throws CommunityException {
try {
return modifyElementToArrayField(id, c -> c.getFos(), (c, fos) -> c.setFos(fos), true, foss);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
}
public CommunityDetails addCommunitySDG(final String id, final String... sdgs) throws CommunityException {
try {
return modifyElementToArrayField(id, c -> c.getSdg(), (c, sdg) -> c.setSdg(sdg), false, sdgs);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
}
public CommunityDetails removeCommunitySDG(final String id, final String... sdgs) throws CommunityException {
try {
return modifyElementToArrayField(id, c -> c.getSdg(), (c, sdg) -> c.setSdg(sdg), true, sdgs);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
}
@Transactional
public CommunityDetails addCommunityAdvancedConstraint(final String id, final SelectionCriteria advancedCosntraint)
throws CommunityException {
try {
final DbCommunity dbEntry = dbCommunityRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Community not found: " + id));
dbEntry.setAdvancedConstraints(advancedCosntraint);
dbEntry.setLastUpdateDate(LocalDateTime.now());
dbCommunityRepository.save(dbEntry);
return getCommunity(id);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
}
@Transactional
public CommunityDetails removeCommunityAdvancedConstraint(final String id) throws CommunityException {
try {
final DbCommunity dbEntry = dbCommunityRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Community not found: " + id));
dbEntry.setAdvancedConstraints(null);
dbEntry.setLastUpdateDate(LocalDateTime.now());
dbCommunityRepository.save(dbEntry);
return getCommunity(id);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
}
@Transactional
public CommunityDetails addCommunityRemoveConstraint(final String id, final SelectionCriteria removeConstraint) throws CommunityException {
try {
final DbCommunity dbEntry = dbCommunityRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Community not found: " + id));
dbEntry.setRemoveConstraints(removeConstraint);
dbEntry.setLastUpdateDate(LocalDateTime.now());
dbCommunityRepository.save(dbEntry);
return getCommunity(id);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
}
@Transactional
public CommunityDetails removeCommunityRemoveConstraint(final String id) throws CommunityException {
try {
final DbCommunity dbEntry = dbCommunityRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Community not found: " + id));
dbEntry.setRemoveConstraints(null);
dbEntry.setLastUpdateDate(LocalDateTime.now());
dbCommunityRepository.save(dbEntry);
return getCommunity(id);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
}
public CommunityDetails removeCommunityZenodoCommunity(final String id, final String zenodoCommunity, final boolean isMain)
throws CommunityException {
if (isMain) {
return updateElementToSimpleField(id, (c, val) -> c.setMainZenodoCommunity(val), null);
} else {
return modifyElementToArrayField(id, c -> c.getOtherZenodoCommunities(), (c, arr) -> c.setOtherZenodoCommunities(arr), true, zenodoCommunity);
}
}
public CommunityDetails addCommunityZenodoCommunity(final String id, final String zenodoCommunity, final boolean isMain)
throws CommunityException {
try {
if (isMain) {
return updateElementToSimpleField(id, (c, val) -> c.setMainZenodoCommunity(val), zenodoCommunity);
} else {
return modifyElementToArrayField(id, c -> c.getOtherZenodoCommunities(), (c, arr) -> c.setOtherZenodoCommunities(arr), false, zenodoCommunity);
}
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
}
@Transactional
private CommunityDetails updateElementToSimpleField(final String id,
final BiConsumer<DbCommunity, String> setter,
final String value) throws CommunityException {
try {
final DbCommunity dbEntry = dbCommunityRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Community not found: " + id));
setter.accept(dbEntry, value);
dbEntry.setLastUpdateDate(LocalDateTime.now());
dbCommunityRepository.save(dbEntry);
return getCommunity(id);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
}
@Transactional
private CommunityDetails modifyElementToArrayField(final String id,
final Function<DbCommunity, String[]> getter,
final BiConsumer<DbCommunity, String[]> setter,
final boolean remove,
final String... values) throws 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(Arrays.asList(values));
} else {
tmpList.addAll(Arrays.asList(values));
}
setter.accept(dbEntry, tmpList.toArray(new String[tmpList.size()]));
dbEntry.setLastUpdateDate(LocalDateTime.now());
dbCommunityRepository.save(dbEntry);
return getCommunity(id);
}
@Transactional
public List<String> getOpenAIRECommunitiesByZenodoId(final String zenodoId) throws CommunityException {
try {
return dbCommunityRepository.findByZenodoId(zenodoId);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
}
@Transactional
public Map<String, Set<String>> getPropagationOrganizationCommunityMap() throws CommunityException {
try {
return dbOrganizationRepository.findAll()
.stream()
.collect(Collectors.groupingBy(DbOrganization::getOrgId, Collectors.mapping(DbOrganization::getCommunity, Collectors.toSet())));
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
}
@Transactional
public Set<String> getPropagationOrganizationsForCommunity(final String communityId) throws CommunityException {
try {
return dbOrganizationRepository.findByCommunity(communityId)
.stream()
.map(DbOrganization::getOrgId)
.collect(Collectors.toSet());
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
}
@Transactional
public Set<String> addPropagationOrganizationForCommunity(final String communityId, final String... organizationIds) throws CommunityException {
try {
for (final String orgId : organizationIds) {
final DbOrganization o = new DbOrganization(communityId.trim(), orgId.trim());
dbOrganizationRepository.save(o);
}
return getPropagationOrganizationsForCommunity(communityId);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
}
@Transactional
public Set<String> removePropagationOrganizationForCommunity(final String communityId, final String... organizationIds) throws CommunityException {
try {
for (final String orgId : organizationIds) {
final DbOrganization o = new DbOrganization(communityId.trim(), orgId.trim());
dbOrganizationRepository.delete(o);
}
return getPropagationOrganizationsForCommunity(communityId);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
}
@Transactional
public void deleteCommunity(final String id, final boolean recursive) {
if (recursive) {
dbProjectRepository.deleteByCommunity(id);
dbDatasourceRepository.deleteByCommunity(id);
dbOrganizationRepository.deleteByCommunity(id);
dbSupportOrgRepository.deleteByCommunity(id);
dbSubCommunityRepository.deleteByCommunity(id);
}
dbCommunityRepository.deleteById(id);
}
}