325 lines
12 KiB
Java
325 lines
12 KiB
Java
package eu.dnetlib.openaire.community;
|
|
|
|
import static eu.dnetlib.openaire.community.CommunityConstants.CLABEL;
|
|
import static eu.dnetlib.openaire.community.CommunityConstants.CONTENTPROVIDERS_ID_SUFFIX;
|
|
import static eu.dnetlib.openaire.community.CommunityConstants.CPROFILE_ADVANCED_CONSTRAINT;
|
|
import static eu.dnetlib.openaire.community.CommunityConstants.CPROFILE_FOS;
|
|
import static eu.dnetlib.openaire.community.CommunityConstants.CPROFILE_SDG;
|
|
import static eu.dnetlib.openaire.community.CommunityConstants.CPROFILE_SUBJECT;
|
|
import static eu.dnetlib.openaire.community.CommunityConstants.CSUMMARY_DESCRIPTION;
|
|
import static eu.dnetlib.openaire.community.CommunityConstants.CSUMMARY_LOGOURL;
|
|
import static eu.dnetlib.openaire.community.CommunityConstants.CSUMMARY_NAME;
|
|
import static eu.dnetlib.openaire.community.CommunityConstants.CSUMMARY_STATUS;
|
|
import static eu.dnetlib.openaire.community.CommunityConstants.CSUMMARY_ZENODOC;
|
|
import static eu.dnetlib.openaire.community.CommunityConstants.CSV_DELIMITER;
|
|
import static eu.dnetlib.openaire.community.CommunityConstants.ID_SEPARATOR;
|
|
import static eu.dnetlib.openaire.community.CommunityConstants.ORGANIZATION_ID_SUFFIX;
|
|
import static eu.dnetlib.openaire.community.CommunityConstants.PROJECTS_ID_SUFFIX;
|
|
import static eu.dnetlib.openaire.community.CommunityConstants.ZENODOCOMMUNITY_ID_SUFFIX;
|
|
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Optional;
|
|
import java.util.Set;
|
|
import java.util.TreeMap;
|
|
import java.util.stream.Collectors;
|
|
|
|
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.cache.annotation.CacheEvict;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import com.google.common.base.Functions;
|
|
import com.google.common.base.Joiner;
|
|
import com.google.common.collect.Lists;
|
|
import com.google.common.collect.Sets;
|
|
import com.google.gson.Gson;
|
|
|
|
import eu.dnetlib.openaire.common.ISClient;
|
|
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.CommunityOpenAIRECommunities;
|
|
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.CommunityZenodoCommunity;
|
|
import eu.dnetlib.openaire.exporter.model.community.selectioncriteria.SelectionCriteria;
|
|
|
|
@Component
|
|
@ConditionalOnProperty(value = "openaire.exporter.enable.community", havingValue = "true")
|
|
@Deprecated
|
|
public class CommunityApiCore {// implements CommunityClient{
|
|
|
|
private static final Log log = LogFactory.getLog(CommunityApiCore.class);
|
|
|
|
@Autowired
|
|
private CommunityClient cci;
|
|
|
|
@Autowired
|
|
private ISClient isClient;
|
|
|
|
@Autowired
|
|
private CommunityCommon cc;
|
|
|
|
public List<CommunitySummary> listCommunities() throws CommunityException {
|
|
return cc.listCommunities();
|
|
|
|
}
|
|
|
|
public CommunityDetails getCommunity(final String id) throws CommunityException, ResourceNotFoundException {
|
|
return cc.getCommunity(id);
|
|
|
|
}
|
|
|
|
private void removeAdvancedConstraint(final String id) throws ResourceNotFoundException, CommunityException {
|
|
cc.getCommunity(id);
|
|
isClient.updateContextParam(id, CPROFILE_ADVANCED_CONSTRAINT, "", false);
|
|
cc.removeAdvancedConstraint(id);
|
|
|
|
}
|
|
|
|
public void setCommunity(final String id, final CommunityWritableProperties details) throws CommunityException, ResourceNotFoundException {
|
|
|
|
cc.getCommunity(id); // ensure the community exists.
|
|
|
|
if (details.getShortName() != null) {
|
|
isClient.updateContextAttribute(id, CLABEL, details.getShortName());
|
|
|
|
}
|
|
if (details.getName() != null) {
|
|
isClient.updateContextParam(id, CSUMMARY_NAME, details.getName(), true);
|
|
|
|
}
|
|
if (details.getDescription() != null) {
|
|
isClient.updateContextParam(id, CSUMMARY_DESCRIPTION, details.getDescription(), true);
|
|
|
|
}
|
|
if (details.getLogoUrl() != null) {
|
|
isClient.updateContextParam(id, CSUMMARY_LOGOURL, details.getLogoUrl(), true);
|
|
|
|
}
|
|
if (details.getStatus() != null) {
|
|
isClient.updateContextParam(id, CSUMMARY_STATUS, details.getStatus().name(), true);
|
|
|
|
}
|
|
if (details.getSubjects() != null) {
|
|
isClient.updateContextParam(id, CPROFILE_SUBJECT, Joiner.on(CSV_DELIMITER).join(details.getSubjects()), true);
|
|
|
|
}
|
|
if (details.getFos() != null) {
|
|
isClient.updateContextParam(id, CPROFILE_FOS, Joiner.on(CSV_DELIMITER).join(details.getFos()), true);
|
|
|
|
}
|
|
if (details.getSdg() != null) {
|
|
isClient.updateContextParam(id, CPROFILE_SDG, Joiner.on(CSV_DELIMITER).join(details.getSdg()), true);
|
|
|
|
}
|
|
if (details.getAdvancedConstraints() != null) {
|
|
isClient.updateContextParam(id, CPROFILE_ADVANCED_CONSTRAINT, "<![CDATA[" + new Gson().toJson(details.getAdvancedConstraints()) + "]]>", false);
|
|
|
|
}
|
|
if (details.getMainZenodoCommunity() != null) {
|
|
isClient.updateContextParam(id, CSUMMARY_ZENODOC, details.getMainZenodoCommunity(), true);
|
|
}
|
|
|
|
cc.updateCommunity(id, details);
|
|
}
|
|
|
|
public List<CommunityProject> getCommunityProjects(final String id) throws CommunityException, ResourceNotFoundException {
|
|
cc.getCommunity(id); // ensure the community exists.
|
|
return cc.getCommunityInfo(id, PROJECTS_ID_SUFFIX, c -> CommunityMappingUtils.asCommunityProject(id, c));
|
|
}
|
|
|
|
private String nextId(final Integer id) {
|
|
return String.valueOf(id + 1);
|
|
}
|
|
|
|
public List<CommunityContentprovider> getCommunityContentproviders(final String id) throws CommunityException, ResourceNotFoundException {
|
|
cc.getCommunity(id); // ensure the community exists.
|
|
return cc.getCommunityInfo(id, CONTENTPROVIDERS_ID_SUFFIX, c -> CommunityMappingUtils.asCommunityDataprovider(id, c));
|
|
}
|
|
|
|
public List<CommunityZenodoCommunity> getCommunityZenodoCommunities(final String id) throws CommunityException, ResourceNotFoundException {
|
|
|
|
return cc.getCommunityZenodoCommunities(id);
|
|
}
|
|
|
|
public List<CommunityOrganization> getCommunityOrganizations(final String id) throws CommunityException, ResourceNotFoundException {
|
|
cc.getCommunity(id);
|
|
return cc.getCommunityInfo(id, ORGANIZATION_ID_SUFFIX, c -> CommunityMappingUtils.asCommunityOrganization(id, c));
|
|
}
|
|
|
|
public CommunityDetails addCommunitySubjects(final String id, final List<String> subjects) throws CommunityException, ResourceNotFoundException {
|
|
|
|
final CommunityDetails cd = new CommunityDetails();
|
|
|
|
final Set<String> current = Sets.newHashSet(cc.getCommunity(id).getSubjects());
|
|
|
|
current.addAll(subjects);
|
|
|
|
cd.setSubjects(Lists.newArrayList(current));
|
|
|
|
setCommunity(id, CommunityWritableProperties.fromDetails(cd));
|
|
|
|
return cd;
|
|
}
|
|
|
|
public CommunityDetails removeCommunitySubjects(final String id, final List<String> subjects) throws CommunityException, ResourceNotFoundException {
|
|
|
|
final CommunityDetails cd = new CommunityDetails();
|
|
|
|
final Set<String> current = Sets.newHashSet(cc.getCommunity(id).getSubjects());
|
|
|
|
current.removeAll(subjects);
|
|
|
|
cd.setSubjects(Lists.newArrayList(current));
|
|
|
|
setCommunity(id, CommunityWritableProperties.fromDetails(cd));
|
|
|
|
return cd;
|
|
}
|
|
|
|
public CommunityDetails addCommunityFOS(final String id, final List<String> foss) throws CommunityException, ResourceNotFoundException {
|
|
|
|
final CommunityDetails cd = new CommunityDetails();
|
|
|
|
final Set<String> current = Sets.newHashSet();
|
|
if (Optional.ofNullable(cc.getCommunity(id).getFos()).isPresent()) {
|
|
current.addAll(cc.getCommunity(id).getFos());
|
|
}
|
|
|
|
current.addAll(foss);
|
|
|
|
cd.setFos(Lists.newArrayList(current));
|
|
|
|
setCommunity(id, CommunityWritableProperties.fromDetails(cd));
|
|
|
|
return cd;
|
|
}
|
|
|
|
public CommunityDetails removeCommunityFOS(final String id, final List<String> foss) throws CommunityException, ResourceNotFoundException {
|
|
|
|
final CommunityDetails cd = new CommunityDetails();
|
|
|
|
final Set<String> current = Sets.newHashSet(cc.getCommunity(id).getFos());
|
|
|
|
current.removeAll(foss);
|
|
|
|
cd.setFos(Lists.newArrayList(current));
|
|
|
|
setCommunity(id, CommunityWritableProperties.fromDetails(cd));
|
|
|
|
return cd;
|
|
}
|
|
|
|
public CommunityDetails addCommunitySDG(final String id, final List<String> sdgs) throws CommunityException, ResourceNotFoundException {
|
|
|
|
final CommunityDetails cd = new CommunityDetails();
|
|
final Set<String> current = Sets.newHashSet();
|
|
if (Optional.ofNullable(cc.getCommunity(id).getSdg()).isPresent()) {
|
|
current.addAll(cc.getCommunity(id).getSdg());
|
|
}
|
|
|
|
current.addAll(sdgs);
|
|
|
|
cd.setSdg(Lists.newArrayList(current));
|
|
|
|
setCommunity(id, CommunityWritableProperties.fromDetails(cd));
|
|
|
|
return cd;
|
|
}
|
|
|
|
public CommunityDetails removeCommunitySDG(final String id, final List<String> sdgs) throws CommunityException, ResourceNotFoundException {
|
|
|
|
final CommunityDetails cd = new CommunityDetails();
|
|
|
|
final Set<String> current = Sets.newHashSet(cc.getCommunity(id).getSdg());
|
|
|
|
current.removeAll(sdgs);
|
|
|
|
cd.setSdg(Lists.newArrayList(current));
|
|
|
|
setCommunity(id, CommunityWritableProperties.fromDetails(cd));
|
|
|
|
return cd;
|
|
}
|
|
|
|
public CommunityDetails addCommunityAdvancedConstraint(final String id, final SelectionCriteria advancedCosntraint)
|
|
throws CommunityException, ResourceNotFoundException {
|
|
|
|
final CommunityDetails cd = new CommunityDetails();
|
|
|
|
cd.setAdvancedConstraints(advancedCosntraint);
|
|
|
|
setCommunity(id, CommunityWritableProperties.fromDetails(cd));
|
|
|
|
return cd;
|
|
}
|
|
|
|
public CommunityDetails removeCommunityAdvancedConstraint(final String id) throws ResourceNotFoundException, CommunityException {
|
|
|
|
removeAdvancedConstraint(id);
|
|
|
|
return new CommunityDetails();
|
|
}
|
|
|
|
@CacheEvict(value = "community-cache", allEntries = true)
|
|
public void removeCommunityZenodoCommunity(final String id, final Integer zenodoCommId) throws CommunityException, ResourceNotFoundException {
|
|
|
|
final Map<Integer, CommunityZenodoCommunity> zcomms = getZenodoCommunityMap(id);
|
|
if (!zcomms.containsKey(zenodoCommId)) {
|
|
throw new ResourceNotFoundException(String.format("Zenodo community '%s' doesn't exist within context '%s'", zenodoCommId, id));
|
|
}
|
|
isClient.removeConcept(id, id + ZENODOCOMMUNITY_ID_SUFFIX, id + ZENODOCOMMUNITY_ID_SUFFIX + ID_SEPARATOR + zenodoCommId);
|
|
|
|
cc.removeFromCategory(id, ZENODOCOMMUNITY_ID_SUFFIX, String.valueOf(zenodoCommId));
|
|
|
|
}
|
|
|
|
@CacheEvict(value = "community-cache", allEntries = true)
|
|
public CommunityZenodoCommunity addCommunityZenodoCommunity(final String id, final CommunityZenodoCommunity zc)
|
|
throws CommunityException, ResourceNotFoundException {
|
|
if (!StringUtils.equalsIgnoreCase(id, zc.getCommunityId())) { throw new CommunityException("parameters 'id' and zc.communityId must be coherent"); }
|
|
if (!StringUtils.isNotBlank(zc.getZenodoid())) { throw new CommunityException("parameter zenodoid cannot be null or empty"); }
|
|
final TreeMap<Integer, CommunityZenodoCommunity> zcs = getZenodoCommunityMap(id);
|
|
|
|
for (final CommunityZenodoCommunity czc : zcs.values()) {
|
|
if (czc.getZenodoid().equals(zc.getZenodoid())) { throw new CommunityException("Zenodo community already associated to the RCD"); }
|
|
}
|
|
|
|
zc.setId(nextId(!zcs.isEmpty() ? zcs.lastKey() : 0));
|
|
|
|
isClient.addConcept(id, id + ZENODOCOMMUNITY_ID_SUFFIX, CommunityMappingUtils.asZenodoCommunityXML(id, zc));
|
|
cc.updateZenodoCommunity(id, zc);
|
|
|
|
return zc;
|
|
}
|
|
|
|
public CommunityOpenAIRECommunities getOpenAIRECommunities(final String zenodoId) throws CommunityException, ResourceNotFoundException {
|
|
|
|
if (cci.getInverseZenodoCommunityMap().containsKey(zenodoId)) {
|
|
return new CommunityOpenAIRECommunities().setZenodoid(zenodoId)
|
|
.setOpenAirecommunitylist(cci.getInverseZenodoCommunityMap().get(zenodoId).stream().collect(Collectors.toList()));
|
|
}
|
|
return new CommunityOpenAIRECommunities();
|
|
|
|
}
|
|
|
|
// HELPERS
|
|
|
|
private TreeMap<Integer, CommunityZenodoCommunity> getZenodoCommunityMap(final String id) throws CommunityException, ResourceNotFoundException {
|
|
return getCommunityZenodoCommunities(id).stream()
|
|
.collect(Collectors.toMap(cp -> Integer.valueOf(cp.getId()), Functions.identity(), (cp1, cp2) -> {
|
|
log.warn(String.format("duplicate Zenodo community found: '%s'", cp1.getId()));
|
|
return cp2;
|
|
}, TreeMap::new));
|
|
}
|
|
|
|
}
|