1
|
package eu.dnetlib.openaire.community;
|
2
|
|
3
|
import com.google.common.collect.Lists;
|
4
|
import eu.dnetlib.openaire.common.ISClient;
|
5
|
import eu.dnetlib.openaire.context.Category;
|
6
|
import eu.dnetlib.openaire.context.Concept;
|
7
|
import eu.dnetlib.openaire.context.Context;
|
8
|
import org.springframework.beans.factory.annotation.Autowired;
|
9
|
import org.springframework.stereotype.Component;
|
10
|
|
11
|
import java.io.IOException;
|
12
|
import java.text.ParseException;
|
13
|
import java.util.List;
|
14
|
import java.util.Map;
|
15
|
import java.util.function.Function;
|
16
|
import java.util.stream.Collectors;
|
17
|
|
18
|
import static eu.dnetlib.openaire.community.CommunityConstants.ZENODOCOMMUNITY_ID_SUFFIX;
|
19
|
import static eu.dnetlib.openaire.community.CommunityConstants.communityBlackList;
|
20
|
|
21
|
@Component
|
22
|
public class CommunityCommon {
|
23
|
|
24
|
@Autowired
|
25
|
private ISClient isClient;
|
26
|
|
27
|
public Map<String, Context> getContextMap() throws CommunityException {
|
28
|
try {
|
29
|
return isClient.getCommunityContextMap();
|
30
|
} catch (IOException e) {
|
31
|
throw new CommunityException(e);
|
32
|
}
|
33
|
}
|
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
|
|
43
|
public <R> List<R> getCommunityInfo(final String id, final String idSuffix, final Function<Concept, R> mapping) throws CommunityException {
|
44
|
final Map<String, Context> contextMap = getContextMap();
|
45
|
final Context context = contextMap.get(id);
|
46
|
if (context != null) {
|
47
|
final Map<String, Category> categories = context.getCategories();
|
48
|
final Category category = categories.get(id + idSuffix);
|
49
|
if (category != null) {
|
50
|
return category.getConcepts().stream()
|
51
|
.map(mapping)
|
52
|
.collect(Collectors.toList());
|
53
|
}
|
54
|
}
|
55
|
return Lists.newArrayList();
|
56
|
}
|
57
|
|
58
|
public CommunityDetails getCommunity(final String id) throws CommunityException, CommunityNotFoundException {
|
59
|
final Context context = getContextMap().get(id);
|
60
|
if (context == null || CommunityConstants.communityBlackList.contains(id)) {
|
61
|
throw new CommunityNotFoundException(String.format("community '%s' does not exist", id));
|
62
|
}
|
63
|
return CommunityMappingUtils.asCommunityProfile(context);
|
64
|
}
|
65
|
|
66
|
public List<CommunityZenodoCommunity> getCommunityZenodoCommunities(final String id) throws CommunityException, CommunityNotFoundException {
|
67
|
getCommunity(id); // ensure the community exists.
|
68
|
return getCommunityInfo(id, ZENODOCOMMUNITY_ID_SUFFIX, c -> CommunityMappingUtils.asCommunityZenodoCommunity(id, c));
|
69
|
}
|
70
|
}
|