Project

General

Profile

1
package eu.dnetlib.openaire.exporter.community;
2

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

    
9
import com.google.common.base.Functions;
10
import com.google.common.base.Joiner;
11
import com.google.common.collect.Lists;
12
import eu.dnetlib.openaire.exporter.datasource.clients.ISClient;
13
import eu.dnetlib.openaire.exporter.funders.context.Category;
14
import eu.dnetlib.openaire.exporter.funders.context.Concept;
15
import eu.dnetlib.openaire.exporter.funders.context.Context;
16
import org.apache.commons.lang3.StringUtils;
17
import org.apache.commons.logging.Log;
18
import org.apache.commons.logging.LogFactory;
19
import org.springframework.beans.factory.annotation.Autowired;
20
import org.springframework.stereotype.Component;
21

    
22
import static eu.dnetlib.openaire.exporter.community.CommunityConstants.*;
23

    
24
@Component
25
public class CommunityApiCore {
26

    
27
	private static final Log log = LogFactory.getLog(CommunityApiCore.class);
28

    
29
	@Autowired
30
	private ISClient isClient;
31

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

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

    
47
	public void setCommunity(final String id, final CommunityWritableProperties details) throws CommunityException, CommunityNotFoundException {
48

    
49
		getCommunity(id); // ensure the community exists.
50

    
51
		isClient.updateContextAttribute(id, CLABEL, details.getShortName());
52
		isClient.updateContextParam(id, CSUMMARY_NAME, details.getName());
53
		isClient.updateContextParam(id, CSUMMARY_DESCRIPTION, details.getDescription());
54
		isClient.updateContextParam(id, CSUMMARY_LOGOURL, details.getLogoUrl());
55
		isClient.updateContextParam(id, CSUMMARY_MANAGER, Joiner.on(CSV_DELIMITER).join(details.getManagers()));
56
		isClient.updateContextParam(id, CPROFILE_SUBJECT, Joiner.on(CSV_DELIMITER).join(details.getSubjects()));
57
	}
58

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

    
64
	public void addCommunityProject(final String id, final CommunityProject project) throws CommunityException, CommunityNotFoundException {
65
		if (!StringUtils.equalsIgnoreCase(id, project.getCommunityId())) {
66
			throw new CommunityException("parameters 'id' and project.communityId must be coherent");
67
		}
68

    
69
		if (StringUtils.isBlank(project.getId())) {
70
			throw new CommunityException("parameter 'id' must be non empty");
71
		}
72

    
73
		final Map<String, CommunityProject> projects = getCommunityProjectMap(id);
74

    
75
		if (projects.containsKey(project.getId())) {
76
			throw new CommunityException(String.format("project '%s' already defined in context '%s'", project.getId(), id));
77
		}
78

    
79
		isClient.addConcept(id, id + PROJECTS_ID_SUFFIX, CommunityMappingUtils.asProjectXML(id, project));
80
	}
81

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

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

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

    
103
		if (StringUtils.isBlank(cp.getId())) {
104
			throw new CommunityException("parameter 'id' must be non empty");
105
		}
106

    
107
		final Map<String, CommunityContentprovider> cps = getCommunityContentproviderMap(id);
108

    
109
		if (cps.containsKey(cp.getId())) {
110
			throw new CommunityException(String.format("content provider '%s' already defined in context '%s'", cp.getId(), id));
111
		}
112
		isClient.addConcept(id, id + CONTENTPROVIDERS_ID_SUFFIX, CommunityMappingUtils.asContentProviderXML(id, cp));
113
	}
114

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

    
126
	// HELPERS
127

    
128
	private Map<String, CommunityProject> getCommunityProjectMap(final String id) throws CommunityException, CommunityNotFoundException {
129
		return getCommunityProjects(id).stream()
130
				.collect(Collectors.toMap(
131
						CommunityProject::getId,
132
						Functions.identity(),
133
						(p1, p2) -> {
134
							log.warn("duplicate project found! " + p1.getId());
135
							return p2;
136
						}));
137
	}
138

    
139
	private Map<String, CommunityContentprovider> getCommunityContentproviderMap(final String id) throws CommunityException, CommunityNotFoundException {
140
		return getCommunityContentproviders(id).stream()
141
				.collect(Collectors.toMap(
142
						CommunityContentprovider::getId,
143
						Functions.identity(),
144
						(cp1, cp2) -> {
145
							log.warn("duplicate content provider found! " + cp1.getId());
146
							return cp2;
147
						}));
148
	}
149

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

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

    
173
}
(2-2/11)