Project

General

Profile

1 51088 claudio.at
package eu.dnetlib.openaire.community;
2 50853 claudio.at
3
import java.io.IOException;
4
import java.util.List;
5
import java.util.Map;
6 51298 claudio.at
import java.util.TreeMap;
7 50853 claudio.at
import java.util.function.Function;
8
import java.util.stream.Collectors;
9
10 51046 claudio.at
import com.google.common.base.Functions;
11 50923 claudio.at
import com.google.common.base.Joiner;
12 50853 claudio.at
import com.google.common.collect.Lists;
13 51088 claudio.at
import eu.dnetlib.openaire.common.ISClient;
14
import eu.dnetlib.openaire.context.Category;
15
import eu.dnetlib.openaire.context.Concept;
16
import eu.dnetlib.openaire.context.Context;
17 51298 claudio.at
import org.apache.commons.collections.MapUtils;
18 51046 claudio.at
import org.apache.commons.lang3.StringUtils;
19 51047 claudio.at
import org.apache.commons.logging.Log;
20
import org.apache.commons.logging.LogFactory;
21 50853 claudio.at
import org.springframework.beans.factory.annotation.Autowired;
22 51155 claudio.at
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
23 50853 claudio.at
import org.springframework.stereotype.Component;
24
25 51088 claudio.at
import static eu.dnetlib.openaire.community.CommunityConstants.*;
26 50853 claudio.at
27
@Component
28 51155 claudio.at
@ConditionalOnProperty(value = "openaire.exporter.enable.community", havingValue = "true")
29 50853 claudio.at
public class CommunityApiCore {
30
31 51047 claudio.at
	private static final Log log = LogFactory.getLog(CommunityApiCore.class);
32
33 50853 claudio.at
	@Autowired
34
	private ISClient isClient;
35
36 50923 claudio.at
	public List<CommunitySummary> listCommunities() throws CommunityException {
37 50853 claudio.at
		return getContextMap().values().stream()
38 51046 claudio.at
				.filter(context -> !communityBlackList.contains(context.getId()))
39 50853 claudio.at
				.map(CommunityMappingUtils::asCommunitySummary)
40
				.collect(Collectors.toList());
41
	}
42
43 50923 claudio.at
	public CommunityDetails getCommunity(final String id) throws CommunityException, CommunityNotFoundException {
44
		final Context context = getContextMap().get(id);
45 51046 claudio.at
		if (context == null || CommunityConstants.communityBlackList.contains(id)) {
46
			throw new CommunityNotFoundException(String.format("community '%s' does not exist", id));
47 50923 claudio.at
		}
48
		return CommunityMappingUtils.asCommunityProfile(context);
49 50853 claudio.at
	}
50
51 50923 claudio.at
	public void setCommunity(final String id, final CommunityWritableProperties details) throws CommunityException, CommunityNotFoundException {
52
53
		getCommunity(id); // ensure the community exists.
54
55
		isClient.updateContextAttribute(id, CLABEL, details.getShortName());
56
		isClient.updateContextParam(id, CSUMMARY_NAME, details.getName());
57
		isClient.updateContextParam(id, CSUMMARY_DESCRIPTION, details.getDescription());
58
		isClient.updateContextParam(id, CSUMMARY_LOGOURL, details.getLogoUrl());
59
		isClient.updateContextParam(id, CSUMMARY_MANAGER, Joiner.on(CSV_DELIMITER).join(details.getManagers()));
60
		isClient.updateContextParam(id, CPROFILE_SUBJECT, Joiner.on(CSV_DELIMITER).join(details.getSubjects()));
61
	}
62
63
	public List<CommunityProject> getCommunityProjects(final String id) throws CommunityException, CommunityNotFoundException {
64
		getCommunity(id); // ensure the community exists.
65 51011 claudio.at
		return _getCommunityInfo(id, PROJECTS_ID_SUFFIX, c -> CommunityMappingUtils.asCommunityProject(id, c));
66 50853 claudio.at
	}
67
68 51298 claudio.at
	public CommunityProject addCommunityProject(final String id, final CommunityProject project) throws CommunityException, CommunityNotFoundException {
69 51046 claudio.at
		if (!StringUtils.equalsIgnoreCase(id, project.getCommunityId())) {
70
			throw new CommunityException("parameters 'id' and project.communityId must be coherent");
71
		}
72
73 51298 claudio.at
		final TreeMap<Integer, CommunityProject> projects = getCommunityProjectMap(id);
74
		project.setId(nextId(projects != null ? projects.lastKey() : 0));
75 51046 claudio.at
76 51298 claudio.at
		isClient.addConcept(id, id + PROJECTS_ID_SUFFIX, CommunityMappingUtils.asProjectXML(id, project));
77 51046 claudio.at
78 51298 claudio.at
		return project;
79
	}
80 51046 claudio.at
81 51298 claudio.at
	private String nextId(final Integer id) {
82
		return String.valueOf(id + 1);
83 50853 claudio.at
	}
84
85 51298 claudio.at
	public void removeCommunityProject(final String id, final Integer projectId) throws CommunityException, CommunityNotFoundException {
86
		final Map<Integer, CommunityProject> projects = getCommunityProjectMap(id);
87 51046 claudio.at
		if (!projects.containsKey(projectId)) {
88
			throw new CommunityNotFoundException(String.format("project '%s' doesn't exist within context '%s'", projectId, id));
89
		}
90 50853 claudio.at
		isClient.removeConcept(
91
				id,
92
				id + PROJECTS_ID_SUFFIX,
93
				id + PROJECTS_ID_SUFFIX + ID_SEPARATOR + projectId);
94
	}
95
96 50923 claudio.at
	public List<CommunityContentprovider> getCommunityContentproviders(final String id) throws CommunityException, CommunityNotFoundException {
97
		getCommunity(id); // ensure the community exists.
98 51011 claudio.at
		return _getCommunityInfo(id, CONTENTPROVIDERS_ID_SUFFIX, c -> CommunityMappingUtils.asCommunityDataprovider(id, c));
99 50853 claudio.at
	}
100
101 51298 claudio.at
	public CommunityContentprovider addCommunityContentprovider(final String id, final CommunityContentprovider cp) throws CommunityException, CommunityNotFoundException {
102 51046 claudio.at
		if (!StringUtils.equalsIgnoreCase(id, cp.getCommunityId())) {
103
			throw new CommunityException("parameters 'id' and cp.communityId must be coherent");
104
		}
105
106 51298 claudio.at
		final TreeMap<Integer, CommunityContentprovider> cps = getCommunityContentproviderMap(id);
107
		cp.setId(nextId(MapUtils.isNotEmpty(cps) ? cps.lastKey() : 0));
108 51046 claudio.at
109 51298 claudio.at
		isClient.addConcept(id, id + CONTENTPROVIDERS_ID_SUFFIX, CommunityMappingUtils.asContentProviderXML(id, cp));
110 51046 claudio.at
111 51298 claudio.at
		return cp;
112 50853 claudio.at
	}
113
114 51298 claudio.at
	public void removeCommunityContentProvider(final String id, final Integer contentproviderId) throws CommunityException, CommunityNotFoundException {
115
		final Map<Integer, CommunityContentprovider> providers = getCommunityContentproviderMap(id);
116 51046 claudio.at
		if (!providers.containsKey(contentproviderId)) {
117
			throw new CommunityNotFoundException(String.format("content provider '%s' doesn't exist within context '%s'", contentproviderId, id));
118
		}
119 50853 claudio.at
		isClient.removeConcept(
120
				id,
121
				id + CONTENTPROVIDERS_ID_SUFFIX,
122
				id + CONTENTPROVIDERS_ID_SUFFIX + ID_SEPARATOR + contentproviderId);
123
	}
124
125
	// HELPERS
126
127 51298 claudio.at
	private TreeMap<Integer, CommunityProject> getCommunityProjectMap(final String id) throws CommunityException, CommunityNotFoundException {
128 51046 claudio.at
		return getCommunityProjects(id).stream()
129
				.collect(Collectors.toMap(
130 51298 claudio.at
						p -> Integer.valueOf(p.getId()),
131 51047 claudio.at
						Functions.identity(),
132
						(p1, p2) -> {
133 51298 claudio.at
							log.warn(String.format("duplicate project found: '%s'", p1.getId()));
134 51047 claudio.at
							return p2;
135 51298 claudio.at
						},
136
						TreeMap::new));
137 51046 claudio.at
	}
138
139 51298 claudio.at
	private TreeMap<Integer, CommunityContentprovider> getCommunityContentproviderMap(final String id) throws CommunityException, CommunityNotFoundException {
140 51046 claudio.at
		return getCommunityContentproviders(id).stream()
141
				.collect(Collectors.toMap(
142 51298 claudio.at
						cp -> Integer.valueOf(cp.getId()),
143 51047 claudio.at
						Functions.identity(),
144
						(cp1, cp2) -> {
145 51298 claudio.at
							log.warn(String.format("duplicate content provider found: '%s'", cp1.getId()));
146 51047 claudio.at
							return cp2;
147 51298 claudio.at
						},
148
						TreeMap::new));
149 51046 claudio.at
	}
150
151 50923 claudio.at
	private Map<String, Context> getContextMap() throws CommunityException {
152 50853 claudio.at
		try {
153
			return isClient.getCommunityContextMap();
154
		} catch (IOException e) {
155 50923 claudio.at
			throw new CommunityException(e);
156 50853 claudio.at
		}
157
	}
158
159 50923 claudio.at
	private <R> List<R> _getCommunityInfo(final String id, final String idSuffix, final Function<Concept, R> mapping) throws CommunityException {
160 50853 claudio.at
		final Map<String, Context> contextMap = getContextMap();
161
		final Context context = contextMap.get(id);
162
		if (context != null) {
163
			final Map<String, Category> categories = context.getCategories();
164
			final Category category = categories.get(id + idSuffix);
165
			if (category != null) {
166
				return category.getConcepts().stream()
167
						.map(mapping)
168
						.collect(Collectors.toList());
169
			}
170
		}
171
		return Lists.newArrayList();
172
	}
173
174
}