Revision 54556
Added by Miriam Baglioni over 5 years ago
CommunityApiCore.java | ||
---|---|---|
1 | 1 |
package eu.dnetlib.openaire.community; |
2 | 2 |
|
3 |
import java.io.IOException; |
|
4 |
import java.util.*; |
|
5 |
import java.util.function.Function; |
|
6 |
import java.util.stream.Collectors; |
|
7 |
|
|
8 | 3 |
import com.google.common.base.Functions; |
9 | 4 |
import com.google.common.base.Joiner; |
10 | 5 |
import com.google.common.collect.Lists; |
... | ... | |
21 | 16 |
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
22 | 17 |
import org.springframework.stereotype.Component; |
23 | 18 |
|
19 |
import java.io.IOException; |
|
20 |
import java.util.*; |
|
21 |
import java.util.function.Function; |
|
22 |
import java.util.stream.Collectors; |
|
23 |
|
|
24 | 24 |
import static eu.dnetlib.openaire.community.CommunityConstants.*; |
25 | 25 |
|
26 | 26 |
@Component |
27 | 27 |
@ConditionalOnProperty(value = "openaire.exporter.enable.community", havingValue = "true") |
28 |
public class CommunityApiCore { |
|
28 |
public class CommunityApiCore {//implements CommunityClient{
|
|
29 | 29 |
|
30 | 30 |
private static final Log log = LogFactory.getLog(CommunityApiCore.class); |
31 | 31 |
|
32 | 32 |
@Autowired |
33 |
private CommunityClient cci; |
|
34 |
|
|
35 |
@Autowired |
|
33 | 36 |
private ISClient isClient; |
34 | 37 |
|
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()); |
|
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()); |
|
40 | 48 |
} |
41 | 49 |
|
42 | 50 |
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); |
|
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); |
|
48 | 57 |
} |
49 | 58 |
|
50 | 59 |
public void setCommunity(final String id, final CommunityWritableProperties details) throws CommunityException, CommunityNotFoundException { |
51 | 60 |
|
52 |
getCommunity(id); // ensure the community exists. |
|
61 |
cc.getCommunity(id); // ensure the community exists.
|
|
53 | 62 |
|
54 | 63 |
if(details.getShortName() != null) { |
55 | 64 |
isClient.updateContextAttribute(id, CLABEL, details.getShortName()); |
... | ... | |
67 | 76 |
isClient.updateContextParam(id, CSUMMARY_STATUS, details.getStatus().name()); |
68 | 77 |
} |
69 | 78 |
|
70 |
|
|
71 | 79 |
if (details.getManagers() != null) { |
72 | 80 |
isClient.updateContextParam(id, CSUMMARY_MANAGER, Joiner.on(CSV_DELIMITER).join(details.getManagers())); |
73 | 81 |
} |
... | ... | |
77 | 85 |
} |
78 | 86 |
|
79 | 87 |
public List<CommunityProject> getCommunityProjects(final String id) throws CommunityException, CommunityNotFoundException { |
80 |
getCommunity(id); // ensure the community exists. |
|
81 |
return _getCommunityInfo(id, PROJECTS_ID_SUFFIX, c -> CommunityMappingUtils.asCommunityProject(id, c));
|
|
88 |
cc.getCommunity(id); // ensure the community exists.
|
|
89 |
return cc.getCommunityInfo(id, PROJECTS_ID_SUFFIX, c -> CommunityMappingUtils.asCommunityProject(id, c));
|
|
82 | 90 |
} |
83 | 91 |
|
84 | 92 |
public CommunityProject addCommunityProject(final String id, final CommunityProject project) throws CommunityException, CommunityNotFoundException { |
... | ... | |
110 | 118 |
} |
111 | 119 |
|
112 | 120 |
public List<CommunityContentprovider> getCommunityContentproviders(final String id) throws CommunityException, CommunityNotFoundException { |
113 |
getCommunity(id); // ensure the community exists. |
|
114 |
return _getCommunityInfo(id, CONTENTPROVIDERS_ID_SUFFIX, c -> CommunityMappingUtils.asCommunityDataprovider(id, c));
|
|
121 |
cc.getCommunity(id); // ensure the community exists.
|
|
122 |
return cc.getCommunityInfo(id, CONTENTPROVIDERS_ID_SUFFIX, c -> CommunityMappingUtils.asCommunityDataprovider(id, c));
|
|
115 | 123 |
} |
116 | 124 |
|
117 | 125 |
public CommunityContentprovider addCommunityContentprovider(final String id, final CommunityContentprovider cp) throws CommunityException, CommunityNotFoundException { |
... | ... | |
140 | 148 |
|
141 | 149 |
|
142 | 150 |
public List<CommunityZenodoCommunity> getCommunityZenodoCommunities(final String id) throws CommunityException, CommunityNotFoundException { |
143 |
getCommunity(id); // ensure the community exists. |
|
144 |
return _getCommunityInfo(id, ZENODOCOMMUNITY_ID_SUFFIX, c -> CommunityMappingUtils.asCommunityZenodoCommunity(id, c)); |
|
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); |
|
145 | 154 |
} |
146 | 155 |
|
147 | 156 |
|
148 | 157 |
public CommunityDetails addCommunitySubjects(final String id, final List<String> subjects) throws CommunityException, CommunityNotFoundException { |
149 | 158 |
|
150 |
final CommunityDetails details = getCommunity(id); |
|
159 |
final CommunityDetails details = cc.getCommunity(id);
|
|
151 | 160 |
|
152 | 161 |
final Set<String> current = Sets.newHashSet(details.getSubjects()); |
153 | 162 |
|
... | ... | |
162 | 171 |
|
163 | 172 |
public CommunityDetails removeCommunitySubjects(final String id, final List<String> subjects) throws CommunityException, CommunityNotFoundException { |
164 | 173 |
|
165 |
final CommunityDetails details = getCommunity(id); |
|
174 |
final CommunityDetails details = cc.getCommunity(id);
|
|
166 | 175 |
|
167 | 176 |
final Set<String> current = Sets.newHashSet(details.getSubjects()); |
168 | 177 |
|
... | ... | |
185 | 194 |
id, |
186 | 195 |
id + ZENODOCOMMUNITY_ID_SUFFIX, |
187 | 196 |
id + ZENODOCOMMUNITY_ID_SUFFIX + ID_SEPARATOR + zenodoCommId); |
197 |
|
|
198 |
|
|
199 |
cci.dropCache(); |
|
188 | 200 |
} |
189 | 201 |
|
190 | 202 |
public CommunityZenodoCommunity addCommunityZenodoCommunity(final String id, final CommunityZenodoCommunity zc) throws CommunityException, CommunityNotFoundException { |
... | ... | |
205 | 217 |
zc.setId(nextId(MapUtils.isNotEmpty(zcs) ? zcs.lastKey() : 0)); |
206 | 218 |
|
207 | 219 |
isClient.addConcept(id, id + ZENODOCOMMUNITY_ID_SUFFIX, CommunityMappingUtils.asZenodoCommunityXML(id, zc)); |
208 |
|
|
220 |
cci.dropCache(); |
|
209 | 221 |
return zc; |
210 | 222 |
} |
211 | 223 |
|
224 |
public List<String> getOpenAIRECommunities(String zenodoId) throws IOException { |
|
225 |
|
|
226 |
if(cci.getInverseZenodoCommunityMap().containsKey(zenodoId)) |
|
227 |
return cci.getInverseZenodoCommunityMap().get(zenodoId).stream().collect(Collectors.toList()); |
|
228 |
return new ArrayList<>(); |
|
229 |
|
|
230 |
} |
|
231 |
|
|
212 | 232 |
// HELPERS |
213 | 233 |
|
214 | 234 |
private TreeMap<Integer, CommunityProject> getCommunityProjectMap(final String id) throws CommunityException, CommunityNotFoundException { |
... | ... | |
255 | 275 |
} |
256 | 276 |
} |
257 | 277 |
|
258 |
private <R> List<R> _getCommunityInfo(final String id, final String idSuffix, final Function<Concept, R> mapping) throws CommunityException { |
|
259 |
final Map<String, Context> contextMap = getContextMap(); |
|
260 |
final Context context = contextMap.get(id); |
|
261 |
if (context != null) { |
|
262 |
final Map<String, Category> categories = context.getCategories(); |
|
263 |
final Category category = categories.get(id + idSuffix); |
|
264 |
if (category != null) { |
|
265 |
return category.getConcepts().stream() |
|
266 |
.map(mapping) |
|
267 |
.collect(Collectors.toList()); |
|
268 |
} |
|
269 |
} |
|
270 |
return Lists.newArrayList(); |
|
271 |
} |
|
272 | 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 |
|
|
273 | 296 |
} |
Also available in: Unified diff
refactoring and addition of lastupdatedate field in the json.
the value for the field creationDate of the json is found in the context parameter with name creationdate