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