294 lines
11 KiB
Java
294 lines
11 KiB
Java
package eu.dnetlib.openaire.community;
|
|
|
|
import java.io.IOException;
|
|
import java.util.Arrays;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Set;
|
|
import java.util.TreeMap;
|
|
import java.util.function.Function;
|
|
import java.util.stream.Collectors;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
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.CommunityDetails;
|
|
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.context.Category;
|
|
import eu.dnetlib.openaire.exporter.model.context.Concept;
|
|
import eu.dnetlib.openaire.exporter.model.context.Context;
|
|
import eu.dnetlib.openaire.exporter.model.context.Param;
|
|
|
|
@Component
|
|
@Deprecated
|
|
public class CommunityCommon {
|
|
|
|
public final static Set<String> communityBlackList = Sets.newHashSet("fet-fp7", "fet-h2020");
|
|
|
|
// common
|
|
public final static String OPENAIRE_ID = "openaireId";
|
|
public final static String PIPE_SEPARATOR = "||";
|
|
public final static String ID_SEPARATOR = "::";
|
|
public final static String CSV_DELIMITER = ",";
|
|
public final static String CLABEL = "label";
|
|
|
|
// id suffixes
|
|
public final static String PROJECTS_ID_SUFFIX = ID_SEPARATOR + "projects";
|
|
public final static String CONTENTPROVIDERS_ID_SUFFIX = ID_SEPARATOR + "contentproviders";
|
|
public final static String ZENODOCOMMUNITY_ID_SUFFIX = ID_SEPARATOR + "zenodocommunities";
|
|
public final static String ORGANIZATION_ID_SUFFIX = ID_SEPARATOR + "organizations";
|
|
|
|
// community summary
|
|
public final static String CSUMMARY_DESCRIPTION = "description";
|
|
public final static String CSUMMARY_LOGOURL = "logourl";
|
|
public final static String CSUMMARY_STATUS = "status";
|
|
public final static String CSUMMARY_NAME = "name";
|
|
public final static String CSUMMARY_MANAGER = "manager";
|
|
public final static String CSUMMARY_ZENODOC = "zenodoCommunity";
|
|
|
|
// community profile
|
|
public final static String CPROFILE_SUBJECT = "subject";
|
|
public final static String CPROFILE_CREATIONDATE = "creationdate";
|
|
public final static String CPROFILE_FOS = "fos";
|
|
public final static String CPROFILE_SDG = "sdg";
|
|
public final static String CPROFILE_ADVANCED_CONSTRAINT = "advancedConstraints";
|
|
|
|
// community project
|
|
public final static String CPROJECT_FUNDER = "funder";
|
|
public final static String CPROJECT_NUMBER = "CD_PROJECT_NUMBER";
|
|
public final static String CPROJECT_FULLNAME = "projectfullname";
|
|
public final static String CPROJECT_ACRONYM = "acronym";
|
|
|
|
// community content provider
|
|
public final static String CCONTENTPROVIDER_NAME = "name";
|
|
public final static String CCONTENTPROVIDER_OFFICIALNAME = "officialname";
|
|
public final static String CCONTENTPROVIDER_ENABLED = "enabled";
|
|
public final static String CCONTENTPROVIDERENABLED_DEFAULT = "true";
|
|
public final static String CCONTENTPROVIDER_SELCRITERIA = "selcriteria";
|
|
|
|
// community zenodo community
|
|
public final static String CZENODOCOMMUNITY_ID = "zenodoid";
|
|
|
|
// community organization
|
|
public final static String CORGANIZATION_NAME = "name";
|
|
public final static String CORGANIZATION_LOGOURL = "logourl";
|
|
public final static String CORGANIZATION_WEBSITEURL = "websiteurl";
|
|
|
|
@Autowired
|
|
private ISClient isClient;
|
|
|
|
public Map<String, Context> getContextMap() throws CommunityException {
|
|
try {
|
|
return isClient.getCommunityContextMap();
|
|
} catch (final IOException e) {
|
|
throw new CommunityException(e);
|
|
}
|
|
}
|
|
|
|
public List<CommunitySummary> listCommunities() throws CommunityException {
|
|
return getContextMap().values()
|
|
.stream()
|
|
.filter(context -> !communityBlackList.contains(context.getId()))
|
|
.map(CommunityMappingUtils::asCommunitySummary)
|
|
.collect(Collectors.toList());
|
|
}
|
|
|
|
public <R> List<R> getCommunityInfo(final String id, final String idSuffix, final Function<Concept, R> mapping) throws CommunityException {
|
|
final Map<String, Context> contextMap = getContextMap();
|
|
final Context context = contextMap.get(id);
|
|
if (context != null) {
|
|
final Map<String, Category> categories = context.getCategories();
|
|
final Category category = categories.get(id + idSuffix);
|
|
if (category != null) { return category.getConcepts()
|
|
.stream()
|
|
.map(mapping)
|
|
.collect(Collectors.toList()); }
|
|
}
|
|
return Lists.newArrayList();
|
|
}
|
|
|
|
public CommunityDetails getCommunity(final String id) throws CommunityException, ResourceNotFoundException {
|
|
final Context context = getContextMap().get(id);
|
|
if (context == null || communityBlackList.contains(id)) {
|
|
// ResponseStatusException(NOT_FOUND, "Unable to find resource");
|
|
throw new ResourceNotFoundException();
|
|
}
|
|
return CommunityMappingUtils.asCommunityProfile(context);
|
|
}
|
|
|
|
public List<CommunityZenodoCommunity> getCommunityZenodoCommunities(final String id) throws CommunityException, ResourceNotFoundException {
|
|
getCommunity(id); // ensure the community exists.
|
|
return getCommunityInfo(id, ZENODOCOMMUNITY_ID_SUFFIX, c -> CommunityMappingUtils.asCommunityZenodoCommunity(id, c));
|
|
}
|
|
|
|
public void removeAdvancedConstraint(final String id) throws CommunityException {
|
|
final Context context = getContextMap().get(id);
|
|
context.getParams()
|
|
.replace(CPROFILE_ADVANCED_CONSTRAINT, Arrays.asList(new Param()
|
|
.setName(CPROFILE_ADVANCED_CONSTRAINT)
|
|
.setValue(null)));
|
|
}
|
|
|
|
public void updateCommunity(final String id, final CommunityWritableProperties community) throws CommunityException {
|
|
final Context context = getContextMap().get(id);
|
|
|
|
if (community.getShortName() != null) {
|
|
context.setLabel(community.getShortName());
|
|
}
|
|
|
|
if (community.getName() != null) {
|
|
context.getParams()
|
|
.replace(CSUMMARY_NAME, Arrays.asList(new Param()
|
|
.setValue(community.getName())
|
|
.setName(CSUMMARY_NAME)));
|
|
}
|
|
if (community.getDescription() != null) {
|
|
context.getParams()
|
|
.replace(CSUMMARY_DESCRIPTION, Arrays.asList(new Param()
|
|
.setName(CSUMMARY_DESCRIPTION)
|
|
.setValue(community.getDescription())));
|
|
}
|
|
if (community.getLogoUrl() != null) {
|
|
context.getParams()
|
|
.replace(CSUMMARY_LOGOURL, Arrays.asList(new Param()
|
|
.setName(CSUMMARY_LOGOURL)
|
|
.setValue(community.getLogoUrl())));
|
|
|
|
}
|
|
if (community.getStatus() != null) {
|
|
context.getParams()
|
|
.replace(CSUMMARY_STATUS, Arrays.asList(new Param()
|
|
.setName(CSUMMARY_STATUS)
|
|
.setValue(community.getStatus().name())));
|
|
}
|
|
if (community.getSubjects() != null) {
|
|
context.getParams()
|
|
.replace(CPROFILE_SUBJECT, Arrays.asList(new Param().setName(CPROFILE_SUBJECT)
|
|
.setValue(Joiner.on(CSV_DELIMITER)
|
|
.join(community.getSubjects()))));
|
|
}
|
|
if (community.getFos() != null) {
|
|
if (context.getParams().containsKey(CPROFILE_FOS)) {
|
|
context.getParams()
|
|
.replace(CPROFILE_FOS, Arrays.asList(new Param().setName(CPROFILE_FOS)
|
|
.setValue(Joiner.on(CSV_DELIMITER)
|
|
.join(community.getFos()))));
|
|
} else {
|
|
context.getParams()
|
|
.put(CPROFILE_FOS, Arrays.asList(new Param().setName(CPROFILE_FOS)
|
|
.setValue(Joiner.on(CSV_DELIMITER)
|
|
.join(community.getFos()))));
|
|
}
|
|
}
|
|
if (community.getSdg() != null) {
|
|
if (context.getParams().containsKey(CPROFILE_SDG)) {
|
|
context.getParams()
|
|
.replace(CPROFILE_SDG, Arrays.asList(new Param().setName(CPROFILE_SDG)
|
|
.setValue(Joiner.on(CSV_DELIMITER)
|
|
.join(community.getSdg()))));
|
|
} else {
|
|
context.getParams()
|
|
.put(CPROFILE_SDG, Arrays.asList(new Param().setName(CPROFILE_SDG)
|
|
.setValue(Joiner.on(CSV_DELIMITER)
|
|
.join(community.getSdg()))));
|
|
}
|
|
}
|
|
if (community.getAdvancedConstraints() != null) {
|
|
if (context.getParams().containsKey(CPROFILE_ADVANCED_CONSTRAINT)) {
|
|
context.getParams()
|
|
.replace(CPROFILE_ADVANCED_CONSTRAINT, Arrays.asList(new Param()
|
|
.setName(CPROFILE_ADVANCED_CONSTRAINT)
|
|
.setValue(new Gson().toJson(community.getAdvancedConstraints()))));
|
|
} else {
|
|
context.getParams()
|
|
.put(CPROFILE_ADVANCED_CONSTRAINT, Arrays.asList(new Param()
|
|
.setName(CPROFILE_ADVANCED_CONSTRAINT)
|
|
.setValue(new Gson().toJson(community.getAdvancedConstraints()))));
|
|
}
|
|
}
|
|
if (community.getMainZenodoCommunity() != null) {
|
|
context.getParams()
|
|
.replace(CSUMMARY_ZENODOC, Arrays.asList(new Param()
|
|
.setName(CSUMMARY_ZENODOC)
|
|
.setValue(community.getMainZenodoCommunity())));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public void removeFromCategory(final String communityId, final String category, final String conceptId) throws CommunityException {
|
|
final Map<String, Context> cmap = getContextMap();
|
|
final Context context = cmap.get(communityId);
|
|
final Map<String, Category> cat = context.getCategories();
|
|
|
|
final List<Concept> concepts = cat.get(communityId + category)
|
|
.getConcepts()
|
|
.stream()
|
|
.filter(c -> !c.getId().equals(communityId + category + ID_SEPARATOR + conceptId))
|
|
.collect(Collectors.toList());
|
|
|
|
cat.get(communityId + category).setConcepts(concepts);
|
|
}
|
|
|
|
public void updateZenodoCommunity(final String communityId, final CommunityZenodoCommunity zc) throws CommunityException {
|
|
final Context context = getContextMap().get(communityId);
|
|
final Category zcs = context.getCategories().get(communityId + ZENODOCOMMUNITY_ID_SUFFIX);
|
|
if (zcs.getConcepts()
|
|
.stream()
|
|
.map(c -> c.getId())
|
|
.collect(Collectors.toList())
|
|
.contains(communityId + ZENODOCOMMUNITY_ID_SUFFIX + ID_SEPARATOR + zc.getId())) {
|
|
zcs.getConcepts().forEach(concept -> {
|
|
if (concept.getId().equals(communityId + ZENODOCOMMUNITY_ID_SUFFIX + ID_SEPARATOR + zc.getId())) {
|
|
|
|
if (zc.getZenodoid() != null) {
|
|
if (concept.getParams().keySet().contains(CZENODOCOMMUNITY_ID)) {
|
|
concept.getParams()
|
|
.replace(CZENODOCOMMUNITY_ID, Arrays.asList(new Param()
|
|
.setName(CZENODOCOMMUNITY_ID)
|
|
.setValue(zc.getZenodoid())));
|
|
} else {
|
|
concept.getParams()
|
|
.put(CZENODOCOMMUNITY_ID, Arrays.asList(new Param()
|
|
.setName(CZENODOCOMMUNITY_ID)
|
|
.setValue(zc.getZenodoid())));
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
} else {
|
|
|
|
final Concept concept = new Concept();
|
|
concept.setId(communityId + ZENODOCOMMUNITY_ID_SUFFIX + ID_SEPARATOR + zc.getId());
|
|
concept.setClaim(false);
|
|
|
|
final Map<String, List<Param>> params = new TreeMap<>();
|
|
|
|
if (zc.getZenodoid() != null) {
|
|
params.put(CZENODOCOMMUNITY_ID, Arrays.asList(new Param().setValue(zc.getZenodoid()).setName(CZENODOCOMMUNITY_ID)));
|
|
concept.setLabel(zc.getZenodoid());
|
|
} else {
|
|
concept.setLabel("");
|
|
}
|
|
concept.setParams(params);
|
|
zcs.getConcepts().add(concept);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|