Project

General

Profile

1
package eu.dnetlib.openaire.community;
2

    
3
import java.io.IOException;
4
import java.util.List;
5
import java.util.Map;
6
import java.util.TreeMap;
7
import java.util.function.Function;
8
import java.util.stream.Collectors;
9

    
10
import com.google.common.base.Functions;
11
import com.google.common.base.Joiner;
12
import com.google.common.collect.Lists;
13
import eu.dnetlib.openaire.common.ISClient;
14
import eu.dnetlib.openaire.context.Category;
15
import eu.dnetlib.openaire.context.Concept;
16
import eu.dnetlib.openaire.context.Context;
17
import org.apache.commons.collections.MapUtils;
18
import org.apache.commons.lang3.StringUtils;
19
import org.apache.commons.logging.Log;
20
import org.apache.commons.logging.LogFactory;
21
import org.springframework.beans.factory.annotation.Autowired;
22
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
23
import org.springframework.stereotype.Component;
24

    
25
import static eu.dnetlib.openaire.community.CommunityConstants.*;
26

    
27
@Component
28
@ConditionalOnProperty(value = "openaire.exporter.enable.community", havingValue = "true")
29
public class CommunityApiCore {
30

    
31
	private static final Log log = LogFactory.getLog(CommunityApiCore.class);
32

    
33
	@Autowired
34
	private ISClient isClient;
35

    
36
	public List<CommunitySummary> listCommunities() throws CommunityException {
37
		return getContextMap().values().stream()
38
				.filter(context -> !communityBlackList.contains(context.getId()))
39
				.map(CommunityMappingUtils::asCommunitySummary)
40
				.collect(Collectors.toList());
41
	}
42

    
43
	public CommunityDetails getCommunity(final String id) throws CommunityException, CommunityNotFoundException {
44
		final Context context = getContextMap().get(id);
45
		if (context == null || CommunityConstants.communityBlackList.contains(id)) {
46
			throw new CommunityNotFoundException(String.format("community '%s' does not exist", id));
47
		}
48
		return CommunityMappingUtils.asCommunityProfile(context);
49
	}
50

    
51
	public void setCommunity(final String id, final CommunityWritableProperties details) throws CommunityException, CommunityNotFoundException {
52

    
53
		getCommunity(id); // ensure the community exists.
54

    
55
		isClient.updateContextAttribute(id, CLABEL, details.getShortName());
56
		isClient.updateContextParam(id, CSUMMARY_NAME, details.getName());
57
		isClient.updateContextParam(id, CSUMMARY_DESCRIPTION, details.getDescription());
58
		isClient.updateContextParam(id, CSUMMARY_LOGOURL, details.getLogoUrl());
59
		isClient.updateContextParam(id, CSUMMARY_MANAGER, Joiner.on(CSV_DELIMITER).join(details.getManagers()));
60
		isClient.updateContextParam(id, CPROFILE_SUBJECT, Joiner.on(CSV_DELIMITER).join(details.getSubjects()));
61
	}
62

    
63
	public List<CommunityProject> getCommunityProjects(final String id) throws CommunityException, CommunityNotFoundException {
64
		getCommunity(id); // ensure the community exists.
65
		return _getCommunityInfo(id, PROJECTS_ID_SUFFIX, c -> CommunityMappingUtils.asCommunityProject(id, c));
66
	}
67

    
68
	public CommunityProject addCommunityProject(final String id, final CommunityProject project) throws CommunityException, CommunityNotFoundException {
69
		if (!StringUtils.equalsIgnoreCase(id, project.getCommunityId())) {
70
			throw new CommunityException("parameters 'id' and project.communityId must be coherent");
71
		}
72

    
73
		final TreeMap<Integer, CommunityProject> projects = getCommunityProjectMap(id);
74
		project.setId(nextId(projects != null ? projects.lastKey() : 0));
75

    
76
		isClient.addConcept(id, id + PROJECTS_ID_SUFFIX, CommunityMappingUtils.asProjectXML(id, project));
77

    
78
		return project;
79
	}
80

    
81
	private String nextId(final Integer id) {
82
		return String.valueOf(id + 1);
83
	}
84

    
85
	public void removeCommunityProject(final String id, final Integer projectId) throws CommunityException, CommunityNotFoundException {
86
		final Map<Integer, CommunityProject> projects = getCommunityProjectMap(id);
87
		if (!projects.containsKey(projectId)) {
88
			throw new CommunityNotFoundException(String.format("project '%s' doesn't exist within context '%s'", projectId, id));
89
		}
90
		isClient.removeConcept(
91
				id,
92
				id + PROJECTS_ID_SUFFIX,
93
				id + PROJECTS_ID_SUFFIX + ID_SEPARATOR + projectId);
94
	}
95

    
96
	public List<CommunityContentprovider> getCommunityContentproviders(final String id) throws CommunityException, CommunityNotFoundException {
97
		getCommunity(id); // ensure the community exists.
98
		return _getCommunityInfo(id, CONTENTPROVIDERS_ID_SUFFIX, c -> CommunityMappingUtils.asCommunityDataprovider(id, c));
99
	}
100

    
101
	public CommunityContentprovider addCommunityContentprovider(final String id, final CommunityContentprovider cp) throws CommunityException, CommunityNotFoundException {
102
		if (!StringUtils.equalsIgnoreCase(id, cp.getCommunityId())) {
103
			throw new CommunityException("parameters 'id' and cp.communityId must be coherent");
104
		}
105

    
106
		final TreeMap<Integer, CommunityContentprovider> cps = getCommunityContentproviderMap(id);
107
		cp.setId(nextId(MapUtils.isNotEmpty(cps) ? cps.lastKey() : 0));
108

    
109
		isClient.addConcept(id, id + CONTENTPROVIDERS_ID_SUFFIX, CommunityMappingUtils.asContentProviderXML(id, cp));
110

    
111
		return cp;
112
	}
113

    
114
	public void removeCommunityContentProvider(final String id, final Integer contentproviderId) throws CommunityException, CommunityNotFoundException {
115
		final Map<Integer, CommunityContentprovider> providers = getCommunityContentproviderMap(id);
116
		if (!providers.containsKey(contentproviderId)) {
117
			throw new CommunityNotFoundException(String.format("content provider '%s' doesn't exist within context '%s'", contentproviderId, id));
118
		}
119
		isClient.removeConcept(
120
				id,
121
				id + CONTENTPROVIDERS_ID_SUFFIX,
122
				id + CONTENTPROVIDERS_ID_SUFFIX + ID_SEPARATOR + contentproviderId);
123
	}
124

    
125
	// HELPERS
126

    
127
	private TreeMap<Integer, CommunityProject> getCommunityProjectMap(final String id) throws CommunityException, CommunityNotFoundException {
128
		return getCommunityProjects(id).stream()
129
				.collect(Collectors.toMap(
130
						p -> Integer.valueOf(p.getId()),
131
						Functions.identity(),
132
						(p1, p2) -> {
133
							log.warn(String.format("duplicate project found: '%s'", p1.getId()));
134
							return p2;
135
						},
136
						TreeMap::new));
137
	}
138

    
139
	private TreeMap<Integer, CommunityContentprovider> getCommunityContentproviderMap(final String id) throws CommunityException, CommunityNotFoundException {
140
		return getCommunityContentproviders(id).stream()
141
				.collect(Collectors.toMap(
142
						cp -> Integer.valueOf(cp.getId()),
143
						Functions.identity(),
144
						(cp1, cp2) -> {
145
							log.warn(String.format("duplicate content provider found: '%s'", cp1.getId()));
146
							return cp2;
147
						},
148
						TreeMap::new));
149
	}
150

    
151
	private Map<String, Context> getContextMap() throws CommunityException {
152
		try {
153
			return isClient.getCommunityContextMap();
154
		} catch (IOException e) {
155
			throw new CommunityException(e);
156
		}
157
	}
158

    
159
	private <R> List<R> _getCommunityInfo(final String id, final String idSuffix, final Function<Concept, R> mapping) throws CommunityException {
160
		final Map<String, Context> contextMap = getContextMap();
161
		final Context context = contextMap.get(id);
162
		if (context != null) {
163
			final Map<String, Category> categories = context.getCategories();
164
			final Category category = categories.get(id + idSuffix);
165
			if (category != null) {
166
				return category.getConcepts().stream()
167
						.map(mapping)
168
						.collect(Collectors.toList());
169
			}
170
		}
171
		return Lists.newArrayList();
172
	}
173

    
174
}
(2-2/11)