1
|
package eu.dnetlib.openaire.exporter.community;
|
2
|
|
3
|
import java.util.List;
|
4
|
import java.util.Map;
|
5
|
import java.util.Optional;
|
6
|
import java.util.stream.Collectors;
|
7
|
import java.util.stream.Stream;
|
8
|
|
9
|
import com.google.common.escape.Escaper;
|
10
|
import com.google.common.xml.XmlEscapers;
|
11
|
import eu.dnetlib.openaire.exporter.funders.context.Concept;
|
12
|
import eu.dnetlib.openaire.exporter.funders.context.Context;
|
13
|
import eu.dnetlib.openaire.exporter.funders.context.Param;
|
14
|
import org.apache.commons.lang3.StringUtils;
|
15
|
|
16
|
import static eu.dnetlib.openaire.exporter.community.CommunityConstants.*;
|
17
|
|
18
|
public class CommunityMappingUtils {
|
19
|
|
20
|
public static CommunitySummary asCommunitySummary(final Context c) {
|
21
|
final CommunitySummary summary = new CommunitySummary();
|
22
|
|
23
|
summary.setId(c.getId());
|
24
|
summary.setShortName(c.getLabel());
|
25
|
summary.setQueryId(c.getId() + PIPE_SEPARATOR + c.getLabel());
|
26
|
|
27
|
final Map<String, List<Param>> params = c.getParams();
|
28
|
if (params.containsKey(CSUMMARY_DESCRIPTION)) {
|
29
|
summary.setDescription(asCsv(params.get(CSUMMARY_DESCRIPTION)));
|
30
|
}
|
31
|
if (params.containsKey(CSUMMARY_LOGOURL)) {
|
32
|
summary.setLogoUrl(asCsv(params.get(CSUMMARY_LOGOURL)));
|
33
|
}
|
34
|
if (params.containsKey(CSUMMARY_NAME)) {
|
35
|
summary.setName(asCsv(params.get(CSUMMARY_NAME)));
|
36
|
}
|
37
|
if (params.containsKey(CSUMMARY_MANAGER)) {
|
38
|
summary.setManagers(asValues(params.get(CSUMMARY_MANAGER)).collect(Collectors.toList()));
|
39
|
}
|
40
|
|
41
|
return summary;
|
42
|
}
|
43
|
|
44
|
public static CommunityProfile asCommunityProfile(final Context c) {
|
45
|
|
46
|
final CommunityProfile p = new CommunityProfile(asCommunitySummary(c));
|
47
|
p.setType(c.getType());
|
48
|
p.setCreationDate(c.getDateofcreation());
|
49
|
final Map<String, List<Param>> params = c.getParams();
|
50
|
if (params.containsKey(CPROFILE_SUBJECT)) {
|
51
|
p.setSubjects(asValues(params.get(CPROFILE_SUBJECT)).collect(Collectors.toList()));
|
52
|
}
|
53
|
|
54
|
return p;
|
55
|
}
|
56
|
|
57
|
public static CommunityProject asCommunityProject(final Concept c) {
|
58
|
|
59
|
final Map<String, List<Param>> p = c.getParams();
|
60
|
final CommunityProject project = new CommunityProject();
|
61
|
project.setOpenaireid(firstValue(p, OPENAIRE_ID));
|
62
|
project.setFunder(firstValue(p, CPROJECT_FUNDER));
|
63
|
project.setGrantId(firstValue(p, CPROJECT_NUMBER));
|
64
|
project.setName(firstValue(p, CPROJECT_FULLNAME));
|
65
|
project.setAcronym(firstValue(p, CPROJECT_ACRONYM));
|
66
|
|
67
|
return project;
|
68
|
}
|
69
|
|
70
|
public static CommunityContentprovider asCommunityDataprovider(final Concept c) {
|
71
|
|
72
|
final Map<String, List<Param>> p = c.getParams();
|
73
|
final CommunityContentprovider d = new CommunityContentprovider();
|
74
|
d.setOpenaireid(firstValue(p, OPENAIRE_ID));
|
75
|
d.setName(firstValue(p, CCONTENTPROVIDER_NAME));
|
76
|
d.setOfficialname(firstValue(p, CCONTENTPROVIDER_OFFICIALNAME));
|
77
|
|
78
|
return d;
|
79
|
}
|
80
|
|
81
|
private static String firstValue(final Map<String, List<Param>> p, final String paramName) {
|
82
|
final Optional<String> first = asValues(p.get(paramName)).findFirst();
|
83
|
return first.isPresent() ? first.get() : null;
|
84
|
}
|
85
|
|
86
|
private static String asCsv(final List<Param> params) {
|
87
|
return asValues(params)
|
88
|
.collect(Collectors.joining(CSV_DELIMITER));
|
89
|
}
|
90
|
|
91
|
private static Stream<String> asValues(final List<Param> params) {
|
92
|
return params == null ? Stream.empty() : params.stream()
|
93
|
.map(Param::getValue)
|
94
|
.distinct();
|
95
|
}
|
96
|
|
97
|
public static String asProjectXML(final String contextId, final CommunityProject project) {
|
98
|
final Escaper esc = XmlEscapers.xmlAttributeEscaper();
|
99
|
final StringBuilder sb = new StringBuilder();
|
100
|
sb.append(
|
101
|
String.format(
|
102
|
"<concept claim='true' id='%s%s%s%s' label='%s'>\n",
|
103
|
escape(esc, contextId), PROJECTS_ID_SUFFIX, ID_SEPARATOR, escape(esc, project.getOpenaireid()), escape(esc, project.getAcronym())));
|
104
|
sb.append(paramXML(CPROJECT_FULLNAME, project.getName()));
|
105
|
sb.append(paramXML(CPROJECT_NUMBER, project.getGrantId()));
|
106
|
sb.append(paramXML(CPROJECT_FUNDER, project.getFunder()));
|
107
|
sb.append(paramXML(OPENAIRE_ID, project.getOpenaireid()));
|
108
|
sb.append("</concept>\n");
|
109
|
return sb.toString();
|
110
|
}
|
111
|
|
112
|
public static String asContentProviderXML(final String contextId, final CommunityContentprovider ccp) {
|
113
|
final Escaper esc = XmlEscapers.xmlAttributeEscaper();
|
114
|
final StringBuilder sb = new StringBuilder();
|
115
|
sb.append(
|
116
|
String.format(
|
117
|
"<concept claim='true' id='%s%s%s%s' label='%s'>\n",
|
118
|
escape(esc, contextId), CONTENTPROVIDERS_ID_SUFFIX, ID_SEPARATOR, escape(esc, ccp.getOpenaireid()), escape(esc, ccp.getName())));
|
119
|
sb.append(paramXML(OPENAIRE_ID, ccp.getOpenaireid()));
|
120
|
sb.append(paramXML(CCONTENTPROVIDER_NAME, ccp.getName()));
|
121
|
sb.append(paramXML(CCONTENTPROVIDER_OFFICIALNAME, ccp.getOfficialname()));
|
122
|
sb.append("</concept>\n");
|
123
|
return sb.toString();
|
124
|
}
|
125
|
|
126
|
private static String paramXML(final String paramName, final String value) {
|
127
|
return String.format("<param name='%s'>%s</param>\n", paramName, escape(XmlEscapers.xmlContentEscaper(), value));
|
128
|
}
|
129
|
|
130
|
private static String escape(final Escaper esc, final String value) {
|
131
|
return StringUtils.isNotBlank(value) ? esc.escape(value) : "";
|
132
|
}
|
133
|
}
|