1
|
package eu.dnetlib.openaire.community;
|
2
|
|
3
|
import java.text.ParseException;
|
4
|
import java.util.Arrays;
|
5
|
import java.util.List;
|
6
|
import java.util.Map;
|
7
|
import java.util.Optional;
|
8
|
import java.util.stream.Collectors;
|
9
|
import java.util.stream.Stream;
|
10
|
|
11
|
import com.google.common.escape.Escaper;
|
12
|
import com.google.common.xml.XmlEscapers;
|
13
|
import eu.dnetlib.openaire.context.Concept;
|
14
|
import eu.dnetlib.openaire.context.Context;
|
15
|
import eu.dnetlib.openaire.context.Param;
|
16
|
import org.apache.commons.lang3.StringUtils;
|
17
|
import org.apache.commons.logging.Log;
|
18
|
import org.apache.commons.logging.LogFactory;
|
19
|
|
20
|
import static eu.dnetlib.openaire.common.Utils.escape;
|
21
|
import static eu.dnetlib.openaire.community.CommunityConstants.*;
|
22
|
|
23
|
public class CommunityMappingUtils {
|
24
|
private final static String pattern = "yyyy-MM-dd'T'hh:mm:ss";
|
25
|
private static final Log log = LogFactory.getLog(CommunityMappingUtils.class);
|
26
|
public static CommunitySummary asCommunitySummary(final Context c) {
|
27
|
final CommunitySummary summary = new CommunitySummary();
|
28
|
|
29
|
summary.setId(c.getId());
|
30
|
summary.setShortName(c.getLabel());
|
31
|
summary.setLastUpdateDate(c.getLastUpdateDate());
|
32
|
summary.setCreationDate(c.getCreationDate());
|
33
|
summary.setQueryId(c.getId() + PIPE_SEPARATOR + c.getLabel());
|
34
|
summary.setType(c.getType());
|
35
|
|
36
|
final Map<String, List<Param>> params = c.getParams();
|
37
|
if (params.containsKey(CSUMMARY_DESCRIPTION)) {
|
38
|
summary.setDescription(asCsv(params.get(CSUMMARY_DESCRIPTION)));
|
39
|
}
|
40
|
if (params.containsKey(CSUMMARY_LOGOURL)) {
|
41
|
summary.setLogoUrl(asCsv(params.get(CSUMMARY_LOGOURL)));
|
42
|
}
|
43
|
if (params.containsKey(CSUMMARY_STATUS)) {
|
44
|
summary.setStatus(CommunityStatus.valueOf(firstValue(params, CSUMMARY_STATUS)));
|
45
|
}
|
46
|
if (params.containsKey(CSUMMARY_NAME)) {
|
47
|
summary.setName(asCsv(params.get(CSUMMARY_NAME)));
|
48
|
}
|
49
|
if (params.containsKey(CSUMMARY_MANAGER)) {
|
50
|
summary.setManagers(splitValues(asValues(params.get(CSUMMARY_MANAGER)), CSV_DELIMITER));
|
51
|
}
|
52
|
if (params.containsKey(CSUMMARY_ZENODOC)) {
|
53
|
summary.setZenodoCommunity(asCsv(params.get(CSUMMARY_ZENODOC)));
|
54
|
}
|
55
|
|
56
|
|
57
|
return summary;
|
58
|
}
|
59
|
|
60
|
public static CommunityDetails asCommunityProfile(final Context c) {
|
61
|
|
62
|
final CommunityDetails p = new CommunityDetails(asCommunitySummary(c));
|
63
|
p.setLastUpdateDate(c.getLastUpdateDate());
|
64
|
final Map<String, List<Param>> params = c.getParams();
|
65
|
if (params.containsKey(CPROFILE_SUBJECT)) {
|
66
|
p.setSubjects(splitValues(asValues(params.get(CPROFILE_SUBJECT)), CSV_DELIMITER));
|
67
|
}
|
68
|
if (params.containsKey(CPROFILE_CREATIONDATE)){
|
69
|
try {
|
70
|
p.setCreationDate(org.apache.commons.lang3.time.DateUtils.parseDate(asCsv(params.get(CPROFILE_CREATIONDATE)), pattern));
|
71
|
}catch(ParseException e) {
|
72
|
e.printStackTrace();
|
73
|
}
|
74
|
}
|
75
|
|
76
|
return p;
|
77
|
}
|
78
|
|
79
|
public static CommunityProject asCommunityProject(final String communityId, final Concept c) {
|
80
|
|
81
|
final Map<String, List<Param>> p = c.getParams();
|
82
|
final CommunityProject project = new CommunityProject();
|
83
|
project.setCommunityId(communityId);
|
84
|
project.setId(StringUtils.substringAfterLast(c.getId(), ID_SEPARATOR));
|
85
|
project.setOpenaireId(firstValue(p, OPENAIRE_ID));
|
86
|
project.setFunder(firstValue(p, CPROJECT_FUNDER));
|
87
|
project.setGrantId(firstValue(p, CPROJECT_NUMBER));
|
88
|
project.setName(firstValue(p, CPROJECT_FULLNAME));
|
89
|
project.setAcronym(firstValue(p, CPROJECT_ACRONYM));
|
90
|
|
91
|
return project;
|
92
|
}
|
93
|
|
94
|
public static CommunityContentprovider asCommunityDataprovider(final String communityId, final Concept c) {
|
95
|
|
96
|
final Map<String, List<Param>> p = c.getParams();
|
97
|
final CommunityContentprovider d = new CommunityContentprovider();
|
98
|
d.setCommunityId(communityId);
|
99
|
d.setId(StringUtils.substringAfterLast(c.getId(), ID_SEPARATOR));
|
100
|
d.setOpenaireId(firstValue(p, OPENAIRE_ID));
|
101
|
d.setName(firstValue(p, CCONTENTPROVIDER_NAME));
|
102
|
d.setOfficialname(firstValue(p, CCONTENTPROVIDER_OFFICIALNAME));
|
103
|
|
104
|
return d;
|
105
|
}
|
106
|
|
107
|
public static CommunityZenodoCommunity asCommunityZenodoCommunity(final String communityId, final Concept c){
|
108
|
final CommunityZenodoCommunity z = new CommunityZenodoCommunity();
|
109
|
final Map<String, List<Param>> p = c.getParams();
|
110
|
z.setCommunityId(communityId);
|
111
|
z.setId(StringUtils.substringAfterLast(c.getId(), ID_SEPARATOR));
|
112
|
z.setZenodoid(firstValue(p,CZENODOCOMMUNITY_ID));
|
113
|
//z.setName(c.getLabel());
|
114
|
return z;
|
115
|
}
|
116
|
|
117
|
|
118
|
public static CommunityOrganization asCommunityOrganization(String id, Concept c) {
|
119
|
final Map<String, List<Param>> p = c.getParams();
|
120
|
final CommunityOrganization o = new CommunityOrganization();
|
121
|
o.setCommunityId(id);
|
122
|
o.setId(StringUtils.substringAfterLast(c.getId(), ID_SEPARATOR));
|
123
|
o.setName(firstValue(p,CORGANIZATION_NAME));
|
124
|
o.setLogo_url(firstValue(p,CORGANIZATION_LOGOURL));
|
125
|
o.setWebsite_url(firstValue(p,CORGANIZATION_WEBSITEURL));
|
126
|
return o;
|
127
|
}
|
128
|
|
129
|
private static List<String> splitValues(final Stream<String> stream, final String separator) {
|
130
|
return stream.map(s -> s.split(separator))
|
131
|
.map(Arrays::asList)
|
132
|
.flatMap(List::stream)
|
133
|
.filter(StringUtils::isNotBlank)
|
134
|
.map(StringUtils::trim)
|
135
|
.collect(Collectors.toList());
|
136
|
}
|
137
|
|
138
|
private static String firstValue(final Map<String, List<Param>> p, final String paramName) {
|
139
|
final Optional<String> first = asValues(p.get(paramName)).findFirst();
|
140
|
return first.isPresent() ? first.get() : null;
|
141
|
}
|
142
|
|
143
|
private static String asCsv(final List<Param> params) {
|
144
|
return asValues(params)
|
145
|
.collect(Collectors.joining(CSV_DELIMITER));
|
146
|
}
|
147
|
|
148
|
private static Stream<String> asValues(final List<Param> params) {
|
149
|
return params == null ? Stream.empty() : params.stream()
|
150
|
.map(Param::getValue)
|
151
|
.map(StringUtils::trim)
|
152
|
.distinct();
|
153
|
}
|
154
|
|
155
|
public static String asProjectXML(final String contextId, final CommunityProject project) {
|
156
|
final Escaper esc = XmlEscapers.xmlAttributeEscaper();
|
157
|
final StringBuilder sb = new StringBuilder();
|
158
|
sb.append(
|
159
|
String.format(
|
160
|
"<concept claim='false' id='%s%s%s%s' label='%s'>\n",
|
161
|
escape(esc, contextId), PROJECTS_ID_SUFFIX, ID_SEPARATOR, escape(esc, String.valueOf(project.getId())), escape(esc, project.getAcronym())));
|
162
|
sb.append(paramXML(CPROJECT_FULLNAME, project.getName()));
|
163
|
sb.append(paramXML(CPROJECT_ACRONYM, project.getAcronym()));
|
164
|
sb.append(paramXML(CPROJECT_NUMBER, project.getGrantId()));
|
165
|
sb.append(paramXML(CPROJECT_FUNDER, project.getFunder()));
|
166
|
sb.append(paramXML(OPENAIRE_ID, project.getOpenaireId()));
|
167
|
sb.append("</concept>\n");
|
168
|
return sb.toString();
|
169
|
}
|
170
|
|
171
|
public static String asContentProviderXML(final String contextId, final CommunityContentprovider ccp) {
|
172
|
final Escaper esc = XmlEscapers.xmlAttributeEscaper();
|
173
|
final StringBuilder sb = new StringBuilder();
|
174
|
sb.append(
|
175
|
String.format(
|
176
|
"<concept claim='false' id='%s%s%s%s' label='%s'>\n",
|
177
|
escape(esc, contextId), CONTENTPROVIDERS_ID_SUFFIX, ID_SEPARATOR, escape(esc, String.valueOf(ccp.getId())), escape(esc, ccp.getName())));
|
178
|
sb.append(paramXML(OPENAIRE_ID, ccp.getOpenaireId()));
|
179
|
sb.append(paramXML(CCONTENTPROVIDER_NAME, ccp.getName()));
|
180
|
sb.append(paramXML(CCONTENTPROVIDER_OFFICIALNAME, ccp.getOfficialname()));
|
181
|
sb.append(paramXML(CCONTENTPROVIDER_ENABLED,CCONTENTPROVIDERENABLED_DEFAULT));
|
182
|
sb.append("</concept>\n");
|
183
|
return sb.toString();
|
184
|
}
|
185
|
|
186
|
public static String asZenodoCommunityXML(final String contextId, final CommunityZenodoCommunity zc) {
|
187
|
final Escaper esc = XmlEscapers.xmlAttributeEscaper();
|
188
|
final StringBuilder sb = new StringBuilder();
|
189
|
sb.append(
|
190
|
String.format(
|
191
|
"<concept claim='false' id='%s%s%s%s' label='%s'>\n",
|
192
|
escape(esc, contextId), ZENODOCOMMUNITY_ID_SUFFIX, ID_SEPARATOR, escape(esc, String.valueOf(zc.getId())), escape(esc, zc.getZenodoid())));
|
193
|
|
194
|
sb.append(paramXML(CZENODOCOMMUNITY_ID, zc.getZenodoid()));
|
195
|
sb.append("</concept>\n");
|
196
|
return sb.toString();
|
197
|
}
|
198
|
|
199
|
|
200
|
public static String asOrganizationXML(final String contextId, CommunityOrganization organization) {
|
201
|
final Escaper esc = XmlEscapers.xmlAttributeEscaper();
|
202
|
final StringBuilder sb = new StringBuilder();
|
203
|
sb.append(
|
204
|
String.format(
|
205
|
"<concept claim='false' id='%s%s%s%s' label='%s'>\n",
|
206
|
escape(esc, contextId), ORGANIZATION_ID_SUFFIX, ID_SEPARATOR, escape(esc, String.valueOf(organization.getId())), escape(esc, organization.getName())));
|
207
|
sb.append(paramXML(CORGANIZATION_NAME, organization.getName()));
|
208
|
sb.append(paramXML(CORGANIZATION_LOGOURL, organization.getLogo_url()));
|
209
|
sb.append(paramXML(CORGANIZATION_WEBSITEURL,organization.getWebsite_url()));
|
210
|
sb.append("</concept>\n");
|
211
|
return sb.toString();
|
212
|
}
|
213
|
|
214
|
|
215
|
private static String paramXML(final String paramName, final String value) {
|
216
|
return String.format("<param name='%s'>%s</param>\n", paramName, escape(XmlEscapers.xmlContentEscaper(), value));
|
217
|
}
|
218
|
|
219
|
|
220
|
|
221
|
}
|