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
|
//isClient.updateContextParam(id, CSUMMARY_ZENODOC, details.getZenodoCommunity());
|
60
|
|
61
|
if (details.getManagers() != null) {
|
62
|
isClient.updateContextParam(id, CSUMMARY_MANAGER, Joiner.on(CSV_DELIMITER).join(details.getManagers()));
|
63
|
}
|
64
|
if (details.getSubjects() != null) {
|
65
|
isClient.updateContextParam(id, CPROFILE_SUBJECT, Joiner.on(CSV_DELIMITER).join(details.getSubjects()));
|
66
|
}
|
67
|
}
|
68
|
|
69
|
public List<CommunityProject> getCommunityProjects(final String id) throws CommunityException, CommunityNotFoundException {
|
70
|
getCommunity(id); // ensure the community exists.
|
71
|
return _getCommunityInfo(id, PROJECTS_ID_SUFFIX, c -> CommunityMappingUtils.asCommunityProject(id, c));
|
72
|
}
|
73
|
|
74
|
public CommunityProject addCommunityProject(final String id, final CommunityProject project) throws CommunityException, CommunityNotFoundException {
|
75
|
if (!StringUtils.equalsIgnoreCase(id, project.getCommunityId())) {
|
76
|
throw new CommunityException("parameters 'id' and project.communityId must be coherent");
|
77
|
}
|
78
|
|
79
|
final TreeMap<Integer, CommunityProject> projects = getCommunityProjectMap(id);
|
80
|
project.setId(nextId(projects != null ? projects.lastKey() : 0));
|
81
|
|
82
|
isClient.addConcept(id, id + PROJECTS_ID_SUFFIX, CommunityMappingUtils.asProjectXML(id, project));
|
83
|
|
84
|
return project;
|
85
|
}
|
86
|
|
87
|
private String nextId(final Integer id) {
|
88
|
return String.valueOf(id + 1);
|
89
|
}
|
90
|
|
91
|
public void removeCommunityProject(final String id, final Integer projectId) throws CommunityException, CommunityNotFoundException {
|
92
|
final Map<Integer, CommunityProject> projects = getCommunityProjectMap(id);
|
93
|
if (!projects.containsKey(projectId)) {
|
94
|
throw new CommunityNotFoundException(String.format("project '%s' doesn't exist within context '%s'", projectId, id));
|
95
|
}
|
96
|
isClient.removeConcept(
|
97
|
id,
|
98
|
id + PROJECTS_ID_SUFFIX,
|
99
|
id + PROJECTS_ID_SUFFIX + ID_SEPARATOR + projectId);
|
100
|
}
|
101
|
|
102
|
public List<CommunityContentprovider> getCommunityContentproviders(final String id) throws CommunityException, CommunityNotFoundException {
|
103
|
getCommunity(id); // ensure the community exists.
|
104
|
return _getCommunityInfo(id, CONTENTPROVIDERS_ID_SUFFIX, c -> CommunityMappingUtils.asCommunityDataprovider(id, c));
|
105
|
}
|
106
|
|
107
|
public CommunityContentprovider addCommunityContentprovider(final String id, final CommunityContentprovider cp) throws CommunityException, CommunityNotFoundException {
|
108
|
if (!StringUtils.equalsIgnoreCase(id, cp.getCommunityId())) {
|
109
|
throw new CommunityException("parameters 'id' and cp.communityId must be coherent");
|
110
|
}
|
111
|
|
112
|
final TreeMap<Integer, CommunityContentprovider> cps = getCommunityContentproviderMap(id);
|
113
|
cp.setId(nextId(MapUtils.isNotEmpty(cps) ? cps.lastKey() : 0));
|
114
|
|
115
|
isClient.addConcept(id, id + CONTENTPROVIDERS_ID_SUFFIX, CommunityMappingUtils.asContentProviderXML(id, cp));
|
116
|
|
117
|
return cp;
|
118
|
}
|
119
|
|
120
|
public void removeCommunityContentProvider(final String id, final Integer contentproviderId) throws CommunityException, CommunityNotFoundException {
|
121
|
final Map<Integer, CommunityContentprovider> providers = getCommunityContentproviderMap(id);
|
122
|
if (!providers.containsKey(contentproviderId)) {
|
123
|
throw new CommunityNotFoundException(String.format("content provider '%s' doesn't exist within context '%s'", contentproviderId, id));
|
124
|
}
|
125
|
isClient.removeConcept(
|
126
|
id,
|
127
|
id + CONTENTPROVIDERS_ID_SUFFIX,
|
128
|
id + CONTENTPROVIDERS_ID_SUFFIX + ID_SEPARATOR + contentproviderId);
|
129
|
}
|
130
|
|
131
|
public CommunityDetails addCommunitySubjects(final String id, final List<String> subjects) throws CommunityException, CommunityNotFoundException {
|
132
|
|
133
|
final CommunityDetails details = getCommunity(id);
|
134
|
|
135
|
final Set<String> current = Sets.newHashSet(details.getSubjects());
|
136
|
|
137
|
current.addAll(subjects);
|
138
|
|
139
|
details.setSubjects(Lists.newArrayList(current));
|
140
|
|
141
|
setCommunity(id, CommunityWritableProperties.fromDetails(details));
|
142
|
|
143
|
return details;
|
144
|
}
|
145
|
|
146
|
public CommunityDetails removeCommunitySubjects(final String id, final List<String> subjects) throws CommunityException, CommunityNotFoundException {
|
147
|
|
148
|
final CommunityDetails details = getCommunity(id);
|
149
|
|
150
|
final Set<String> current = Sets.newHashSet(details.getSubjects());
|
151
|
|
152
|
current.removeAll(subjects);
|
153
|
|
154
|
details.setSubjects(Lists.newArrayList(current));
|
155
|
|
156
|
setCommunity(id, CommunityWritableProperties.fromDetails(details));
|
157
|
|
158
|
return details;
|
159
|
}
|
160
|
|
161
|
// HELPERS
|
162
|
|
163
|
private TreeMap<Integer, CommunityProject> getCommunityProjectMap(final String id) throws CommunityException, CommunityNotFoundException {
|
164
|
return getCommunityProjects(id).stream()
|
165
|
.collect(Collectors.toMap(
|
166
|
p -> Integer.valueOf(p.getId()),
|
167
|
Functions.identity(),
|
168
|
(p1, p2) -> {
|
169
|
log.warn(String.format("duplicate project found: '%s'", p1.getId()));
|
170
|
return p2;
|
171
|
},
|
172
|
TreeMap::new));
|
173
|
}
|
174
|
|
175
|
private TreeMap<Integer, CommunityContentprovider> getCommunityContentproviderMap(final String id) throws CommunityException, CommunityNotFoundException {
|
176
|
return getCommunityContentproviders(id).stream()
|
177
|
.collect(Collectors.toMap(
|
178
|
cp -> Integer.valueOf(cp.getId()),
|
179
|
Functions.identity(),
|
180
|
(cp1, cp2) -> {
|
181
|
log.warn(String.format("duplicate content provider found: '%s'", cp1.getId()));
|
182
|
return cp2;
|
183
|
},
|
184
|
TreeMap::new));
|
185
|
}
|
186
|
|
187
|
private Map<String, Context> getContextMap() throws CommunityException {
|
188
|
try {
|
189
|
return isClient.getCommunityContextMap();
|
190
|
} catch (IOException e) {
|
191
|
throw new CommunityException(e);
|
192
|
}
|
193
|
}
|
194
|
|
195
|
private <R> List<R> _getCommunityInfo(final String id, final String idSuffix, final Function<Concept, R> mapping) throws CommunityException {
|
196
|
final Map<String, Context> contextMap = getContextMap();
|
197
|
final Context context = contextMap.get(id);
|
198
|
if (context != null) {
|
199
|
final Map<String, Category> categories = context.getCategories();
|
200
|
final Category category = categories.get(id + idSuffix);
|
201
|
if (category != null) {
|
202
|
return category.getConcepts().stream()
|
203
|
.map(mapping)
|
204
|
.collect(Collectors.toList());
|
205
|
}
|
206
|
}
|
207
|
return Lists.newArrayList();
|
208
|
}
|
209
|
|
210
|
}
|