1
|
package eu.dnetlib.openaire.community;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.util.*;
|
5
|
import java.util.function.Function;
|
6
|
import java.util.stream.Collectors;
|
7
|
|
8
|
import com.google.common.base.Functions;
|
9
|
import com.google.common.base.Joiner;
|
10
|
import com.google.common.collect.Lists;
|
11
|
import com.google.common.collect.Sets;
|
12
|
import eu.dnetlib.openaire.common.ISClient;
|
13
|
import eu.dnetlib.openaire.context.Category;
|
14
|
import eu.dnetlib.openaire.context.Concept;
|
15
|
import eu.dnetlib.openaire.context.Context;
|
16
|
import org.apache.commons.collections.MapUtils;
|
17
|
import org.apache.commons.lang3.StringUtils;
|
18
|
import org.apache.commons.logging.Log;
|
19
|
import org.apache.commons.logging.LogFactory;
|
20
|
import org.springframework.beans.factory.annotation.Autowired;
|
21
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
22
|
import org.springframework.stereotype.Component;
|
23
|
|
24
|
import static eu.dnetlib.openaire.community.CommunityConstants.*;
|
25
|
|
26
|
@Component
|
27
|
@ConditionalOnProperty(value = "openaire.exporter.enable.community", havingValue = "true")
|
28
|
public class CommunityApiCore {
|
29
|
|
30
|
private static final Log log = LogFactory.getLog(CommunityApiCore.class);
|
31
|
|
32
|
@Autowired
|
33
|
private ISClient isClient;
|
34
|
|
35
|
public List<CommunitySummary> listCommunities() throws CommunityException {
|
36
|
return getContextMap().values().stream()
|
37
|
.filter(context -> !communityBlackList.contains(context.getId()))
|
38
|
.map(CommunityMappingUtils::asCommunitySummary)
|
39
|
.collect(Collectors.toList());
|
40
|
}
|
41
|
|
42
|
public CommunityDetails getCommunity(final String id) throws CommunityException, CommunityNotFoundException {
|
43
|
final Context context = getContextMap().get(id);
|
44
|
if (context == null || CommunityConstants.communityBlackList.contains(id)) {
|
45
|
throw new CommunityNotFoundException(String.format("community '%s' does not exist", id));
|
46
|
}
|
47
|
return CommunityMappingUtils.asCommunityProfile(context);
|
48
|
}
|
49
|
|
50
|
public void setCommunity(final String id, final CommunityWritableProperties details) throws CommunityException, CommunityNotFoundException {
|
51
|
|
52
|
getCommunity(id); // ensure the community exists.
|
53
|
|
54
|
isClient.updateContextAttribute(id, CLABEL, details.getShortName());
|
55
|
isClient.updateContextParam(id, CSUMMARY_NAME, details.getName());
|
56
|
isClient.updateContextParam(id, CSUMMARY_DESCRIPTION, details.getDescription());
|
57
|
isClient.updateContextParam(id, CSUMMARY_LOGOURL, details.getLogoUrl());
|
58
|
isClient.updateContextParam(id, CSUMMARY_STATUS, details.getStatus().name());
|
59
|
|
60
|
if (details.getManagers() != null) {
|
61
|
isClient.updateContextParam(id, CSUMMARY_MANAGER, Joiner.on(CSV_DELIMITER).join(details.getManagers()));
|
62
|
}
|
63
|
if (details.getSubjects() != null) {
|
64
|
isClient.updateContextParam(id, CPROFILE_SUBJECT, Joiner.on(CSV_DELIMITER).join(details.getSubjects()));
|
65
|
}
|
66
|
}
|
67
|
|
68
|
public List<CommunityProject> getCommunityProjects(final String id) throws CommunityException, CommunityNotFoundException {
|
69
|
getCommunity(id); // ensure the community exists.
|
70
|
return _getCommunityInfo(id, PROJECTS_ID_SUFFIX, c -> CommunityMappingUtils.asCommunityProject(id, c));
|
71
|
}
|
72
|
|
73
|
public CommunityProject addCommunityProject(final String id, final CommunityProject project) throws CommunityException, CommunityNotFoundException {
|
74
|
if (!StringUtils.equalsIgnoreCase(id, project.getCommunityId())) {
|
75
|
throw new CommunityException("parameters 'id' and project.communityId must be coherent");
|
76
|
}
|
77
|
|
78
|
final TreeMap<Integer, CommunityProject> projects = getCommunityProjectMap(id);
|
79
|
project.setId(nextId(projects != null ? projects.lastKey() : 0));
|
80
|
|
81
|
isClient.addConcept(id, id + PROJECTS_ID_SUFFIX, CommunityMappingUtils.asProjectXML(id, project));
|
82
|
|
83
|
return project;
|
84
|
}
|
85
|
|
86
|
private String nextId(final Integer id) {
|
87
|
return String.valueOf(id + 1);
|
88
|
}
|
89
|
|
90
|
public void removeCommunityProject(final String id, final Integer projectId) throws CommunityException, CommunityNotFoundException {
|
91
|
final Map<Integer, CommunityProject> projects = getCommunityProjectMap(id);
|
92
|
if (!projects.containsKey(projectId)) {
|
93
|
throw new CommunityNotFoundException(String.format("project '%s' doesn't exist within context '%s'", projectId, id));
|
94
|
}
|
95
|
isClient.removeConcept(
|
96
|
id,
|
97
|
id + PROJECTS_ID_SUFFIX,
|
98
|
id + PROJECTS_ID_SUFFIX + ID_SEPARATOR + projectId);
|
99
|
}
|
100
|
|
101
|
public List<CommunityContentprovider> getCommunityContentproviders(final String id) throws CommunityException, CommunityNotFoundException {
|
102
|
getCommunity(id); // ensure the community exists.
|
103
|
return _getCommunityInfo(id, CONTENTPROVIDERS_ID_SUFFIX, c -> CommunityMappingUtils.asCommunityDataprovider(id, c));
|
104
|
}
|
105
|
|
106
|
public CommunityContentprovider addCommunityContentprovider(final String id, final CommunityContentprovider cp) throws CommunityException, CommunityNotFoundException {
|
107
|
if (!StringUtils.equalsIgnoreCase(id, cp.getCommunityId())) {
|
108
|
throw new CommunityException("parameters 'id' and cp.communityId must be coherent");
|
109
|
}
|
110
|
|
111
|
final TreeMap<Integer, CommunityContentprovider> cps = getCommunityContentproviderMap(id);
|
112
|
cp.setId(nextId(MapUtils.isNotEmpty(cps) ? cps.lastKey() : 0));
|
113
|
|
114
|
isClient.addConcept(id, id + CONTENTPROVIDERS_ID_SUFFIX, CommunityMappingUtils.asContentProviderXML(id, cp));
|
115
|
|
116
|
return cp;
|
117
|
}
|
118
|
|
119
|
public void removeCommunityContentProvider(final String id, final Integer contentproviderId) throws CommunityException, CommunityNotFoundException {
|
120
|
final Map<Integer, CommunityContentprovider> providers = getCommunityContentproviderMap(id);
|
121
|
if (!providers.containsKey(contentproviderId)) {
|
122
|
throw new CommunityNotFoundException(String.format("content provider '%s' doesn't exist within context '%s'", contentproviderId, id));
|
123
|
}
|
124
|
isClient.removeConcept(
|
125
|
id,
|
126
|
id + CONTENTPROVIDERS_ID_SUFFIX,
|
127
|
id + CONTENTPROVIDERS_ID_SUFFIX + ID_SEPARATOR + contentproviderId);
|
128
|
}
|
129
|
|
130
|
|
131
|
public List<CommunityZenodoCommunity> getCommunityZenodoCommunities(final String id) throws CommunityException, CommunityNotFoundException {
|
132
|
getCommunity(id); // ensure the community exists.
|
133
|
return _getCommunityInfo(id, ZENODOCOMMUNITY_ID_SUFFIX, c -> CommunityMappingUtils.asCommunityZenodoCommunity(id, c));
|
134
|
}
|
135
|
|
136
|
|
137
|
public CommunityDetails addCommunitySubjects(final String id, final List<String> subjects) throws CommunityException, CommunityNotFoundException {
|
138
|
|
139
|
final CommunityDetails details = getCommunity(id);
|
140
|
|
141
|
final Set<String> current = Sets.newHashSet(details.getSubjects());
|
142
|
|
143
|
current.addAll(subjects);
|
144
|
|
145
|
details.setSubjects(Lists.newArrayList(current));
|
146
|
|
147
|
setCommunity(id, CommunityWritableProperties.fromDetails(details));
|
148
|
|
149
|
return details;
|
150
|
}
|
151
|
|
152
|
public CommunityDetails removeCommunitySubjects(final String id, final List<String> subjects) throws CommunityException, CommunityNotFoundException {
|
153
|
|
154
|
final CommunityDetails details = getCommunity(id);
|
155
|
|
156
|
final Set<String> current = Sets.newHashSet(details.getSubjects());
|
157
|
|
158
|
current.removeAll(subjects);
|
159
|
|
160
|
details.setSubjects(Lists.newArrayList(current));
|
161
|
|
162
|
setCommunity(id, CommunityWritableProperties.fromDetails(details));
|
163
|
|
164
|
return details;
|
165
|
}
|
166
|
|
167
|
public void removeCommunityZenodoCommunity(final String id, final Integer zenodoCommId) throws CommunityException, CommunityNotFoundException {
|
168
|
|
169
|
final Map<Integer, CommunityZenodoCommunity> zcomms = getZenodoCommunityMap(id);
|
170
|
if (!zcomms.containsKey(zenodoCommId)) {
|
171
|
throw new CommunityNotFoundException(String.format("Zenodo community '%s' doesn't exist within context '%s'", zenodoCommId, id));
|
172
|
}
|
173
|
isClient.removeConcept(
|
174
|
id,
|
175
|
id + ZENODOCOMMUNITY_ID_SUFFIX,
|
176
|
id + ZENODOCOMMUNITY_ID_SUFFIX + ID_SEPARATOR + zenodoCommId);
|
177
|
}
|
178
|
|
179
|
public CommunityZenodoCommunity addCommunityZenodoCommunity(final String id, final CommunityZenodoCommunity zc) throws CommunityException, CommunityNotFoundException {
|
180
|
if (!StringUtils.equalsIgnoreCase(id, zc.getCommunityId())) {
|
181
|
throw new CommunityException("parameters 'id' and zc.communityId must be coherent");
|
182
|
}
|
183
|
|
184
|
final TreeMap<Integer, CommunityContentprovider> zcs = getCommunityContentproviderMap(id);
|
185
|
zc.setId(nextId(MapUtils.isNotEmpty(zcs) ? zcs.lastKey() : 0));
|
186
|
|
187
|
isClient.addConcept(id, id + ZENODOCOMMUNITY_ID_SUFFIX, CommunityMappingUtils.asZenodoCommunityXML(id, zc));
|
188
|
|
189
|
return zc;
|
190
|
}
|
191
|
|
192
|
// HELPERS
|
193
|
|
194
|
private TreeMap<Integer, CommunityProject> getCommunityProjectMap(final String id) throws CommunityException, CommunityNotFoundException {
|
195
|
return getCommunityProjects(id).stream()
|
196
|
.collect(Collectors.toMap(
|
197
|
p -> Integer.valueOf(p.getId()),
|
198
|
Functions.identity(),
|
199
|
(p1, p2) -> {
|
200
|
log.warn(String.format("duplicate project found: '%s'", p1.getId()));
|
201
|
return p2;
|
202
|
},
|
203
|
TreeMap::new));
|
204
|
}
|
205
|
|
206
|
private TreeMap<Integer, CommunityContentprovider> getCommunityContentproviderMap(final String id) throws CommunityException, CommunityNotFoundException {
|
207
|
return getCommunityContentproviders(id).stream()
|
208
|
.collect(Collectors.toMap(
|
209
|
cp -> Integer.valueOf(cp.getId()),
|
210
|
Functions.identity(),
|
211
|
(cp1, cp2) -> {
|
212
|
log.warn(String.format("duplicate content provider found: '%s'", cp1.getId()));
|
213
|
return cp2;
|
214
|
},
|
215
|
TreeMap::new));
|
216
|
}
|
217
|
|
218
|
private TreeMap<Integer,CommunityZenodoCommunity> getZenodoCommunityMap(final String id) throws CommunityException, CommunityNotFoundException{
|
219
|
return getCommunityZenodoCommunities(id).stream()
|
220
|
.collect(Collectors.toMap(
|
221
|
cp -> Integer.valueOf(cp.getId()),
|
222
|
Functions.identity(),
|
223
|
(cp1, cp2) -> {
|
224
|
log.warn(String.format("duplicate Zenodo community found: '%s'", cp1.getId()));
|
225
|
return cp2;
|
226
|
},
|
227
|
TreeMap::new));
|
228
|
}
|
229
|
|
230
|
private Map<String, Context> getContextMap() throws CommunityException {
|
231
|
try {
|
232
|
return isClient.getCommunityContextMap();
|
233
|
} catch (IOException e) {
|
234
|
throw new CommunityException(e);
|
235
|
}
|
236
|
}
|
237
|
|
238
|
private <R> List<R> _getCommunityInfo(final String id, final String idSuffix, final Function<Concept, R> mapping) throws CommunityException {
|
239
|
final Map<String, Context> contextMap = getContextMap();
|
240
|
final Context context = contextMap.get(id);
|
241
|
if (context != null) {
|
242
|
final Map<String, Category> categories = context.getCategories();
|
243
|
final Category category = categories.get(id + idSuffix);
|
244
|
if (category != null) {
|
245
|
return category.getConcepts().stream()
|
246
|
.map(mapping)
|
247
|
.collect(Collectors.toList());
|
248
|
}
|
249
|
}
|
250
|
return Lists.newArrayList();
|
251
|
}
|
252
|
|
253
|
}
|