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.setQueryId(c.getId() + PIPE_SEPARATOR + c.getLabel());
|
32
|
summary.setType(c.getType());
|
33
|
|
34
|
final Map<String, List<Param>> params = c.getParams();
|
35
|
if (params.containsKey(CSUMMARY_DESCRIPTION)) {
|
36
|
summary.setDescription(asCsv(params.get(CSUMMARY_DESCRIPTION)));
|
37
|
}
|
38
|
if (params.containsKey(CSUMMARY_LOGOURL)) {
|
39
|
summary.setLogoUrl(asCsv(params.get(CSUMMARY_LOGOURL)));
|
40
|
}
|
41
|
if (params.containsKey(CSUMMARY_STATUS)) {
|
42
|
summary.setStatus(CommunityStatus.valueOf(firstValue(params, CSUMMARY_STATUS)));
|
43
|
}
|
44
|
if (params.containsKey(CSUMMARY_NAME)) {
|
45
|
summary.setName(asCsv(params.get(CSUMMARY_NAME)));
|
46
|
}
|
47
|
if (params.containsKey(CSUMMARY_MANAGER)) {
|
48
|
summary.setManagers(splitValues(asValues(params.get(CSUMMARY_MANAGER)), CSV_DELIMITER));
|
49
|
}
|
50
|
if (params.containsKey(CSUMMARY_ZENODOC)) {
|
51
|
summary.setZenodoCommunity(asCsv(params.get(CSUMMARY_ZENODOC)));
|
52
|
}
|
53
|
|
54
|
|
55
|
return summary;
|
56
|
}
|
57
|
|
58
|
public static CommunityDetails asCommunityProfile(final Context c) {
|
59
|
|
60
|
final CommunityDetails p = new CommunityDetails(asCommunitySummary(c));
|
61
|
p.setLastUpdateDate(c.getLastUpdateDate());
|
62
|
final Map<String, List<Param>> params = c.getParams();
|
63
|
if (params.containsKey(CPROFILE_SUBJECT)) {
|
64
|
p.setSubjects(splitValues(asValues(params.get(CPROFILE_SUBJECT)), CSV_DELIMITER));
|
65
|
}
|
66
|
if (params.containsKey(CPROFILE_CREATIONDATE)){
|
67
|
try {
|
68
|
p.setCreationDate(org.apache.commons.lang3.time.DateUtils.parseDate(asCsv(params.get(CPROFILE_CREATIONDATE)), pattern));
|
69
|
}catch(ParseException e) {
|
70
|
e.printStackTrace();
|
71
|
}
|
72
|
}
|
73
|
|
74
|
return p;
|
75
|
}
|
76
|
|
77
|
public static CommunityProject asCommunityProject(final String communityId, final Concept c) {
|
78
|
|
79
|
final Map<String, List<Param>> p = c.getParams();
|
80
|
final CommunityProject project = new CommunityProject();
|
81
|
project.setCommunityId(communityId);
|
82
|
project.setId(StringUtils.substringAfterLast(c.getId(), ID_SEPARATOR));
|
83
|
project.setOpenaireId(firstValue(p, OPENAIRE_ID));
|
84
|
project.setFunder(firstValue(p, CPROJECT_FUNDER));
|
85
|
project.setGrantId(firstValue(p, CPROJECT_NUMBER));
|
86
|
project.setName(firstValue(p, CPROJECT_FULLNAME));
|
87
|
project.setAcronym(firstValue(p, CPROJECT_ACRONYM));
|
88
|
|
89
|
return project;
|
90
|
}
|
91
|
|
92
|
public static CommunityContentprovider asCommunityDataprovider(final String communityId, final Concept c) {
|
93
|
|
94
|
final Map<String, List<Param>> p = c.getParams();
|
95
|
final CommunityContentprovider d = new CommunityContentprovider();
|
96
|
d.setCommunityId(communityId);
|
97
|
d.setId(StringUtils.substringAfterLast(c.getId(), ID_SEPARATOR));
|
98
|
d.setOpenaireId(firstValue(p, OPENAIRE_ID));
|
99
|
d.setName(firstValue(p, CCONTENTPROVIDER_NAME));
|
100
|
d.setOfficialname(firstValue(p, CCONTENTPROVIDER_OFFICIALNAME));
|
101
|
|
102
|
return d;
|
103
|
}
|
104
|
|
105
|
public static CommunityZenodoCommunity asCommunityZenodoCommunity(final String communityId, final Concept c){
|
106
|
final CommunityZenodoCommunity z = new CommunityZenodoCommunity();
|
107
|
final Map<String, List<Param>> p = c.getParams();
|
108
|
z.setCommunityId(communityId);
|
109
|
z.setId(StringUtils.substringAfterLast(c.getId(), ID_SEPARATOR));
|
110
|
z.setZenodoid(firstValue(p,CZENODOCOMMUNITY_ID));
|
111
|
//z.setName(c.getLabel());
|
112
|
return z;
|
113
|
}
|
114
|
|
115
|
|
116
|
private static List<String> splitValues(final Stream<String> stream, final String separator) {
|
117
|
return stream.map(s -> s.split(separator))
|
118
|
.map(Arrays::asList)
|
119
|
.flatMap(List::stream)
|
120
|
.filter(StringUtils::isNotBlank)
|
121
|
.map(StringUtils::trim)
|
122
|
.collect(Collectors.toList());
|
123
|
}
|
124
|
|
125
|
private static String firstValue(final Map<String, List<Param>> p, final String paramName) {
|
126
|
final Optional<String> first = asValues(p.get(paramName)).findFirst();
|
127
|
return first.isPresent() ? first.get() : null;
|
128
|
}
|
129
|
|
130
|
private static String asCsv(final List<Param> params) {
|
131
|
return asValues(params)
|
132
|
.collect(Collectors.joining(CSV_DELIMITER));
|
133
|
}
|
134
|
|
135
|
private static Stream<String> asValues(final List<Param> params) {
|
136
|
return params == null ? Stream.empty() : params.stream()
|
137
|
.map(Param::getValue)
|
138
|
.map(StringUtils::trim)
|
139
|
.distinct();
|
140
|
}
|
141
|
|
142
|
public static String asProjectXML(final String contextId, final CommunityProject project) {
|
143
|
final Escaper esc = XmlEscapers.xmlAttributeEscaper();
|
144
|
final StringBuilder sb = new StringBuilder();
|
145
|
sb.append(
|
146
|
String.format(
|
147
|
"<concept claim='true' id='%s%s%s%s' label='%s'>\n",
|
148
|
escape(esc, contextId), PROJECTS_ID_SUFFIX, ID_SEPARATOR, escape(esc, String.valueOf(project.getId())), escape(esc, project.getAcronym())));
|
149
|
sb.append(paramXML(CPROJECT_FULLNAME, project.getName()));
|
150
|
sb.append(paramXML(CPROJECT_ACRONYM, project.getAcronym()));
|
151
|
sb.append(paramXML(CPROJECT_NUMBER, project.getGrantId()));
|
152
|
sb.append(paramXML(CPROJECT_FUNDER, project.getFunder()));
|
153
|
sb.append(paramXML(OPENAIRE_ID, project.getOpenaireId()));
|
154
|
sb.append("</concept>\n");
|
155
|
return sb.toString();
|
156
|
}
|
157
|
|
158
|
public static String asContentProviderXML(final String contextId, final CommunityContentprovider ccp) {
|
159
|
final Escaper esc = XmlEscapers.xmlAttributeEscaper();
|
160
|
final StringBuilder sb = new StringBuilder();
|
161
|
sb.append(
|
162
|
String.format(
|
163
|
"<concept claim='false' id='%s%s%s%s' label='%s'>\n",
|
164
|
escape(esc, contextId), CONTENTPROVIDERS_ID_SUFFIX, ID_SEPARATOR, escape(esc, String.valueOf(ccp.getId())), escape(esc, ccp.getName())));
|
165
|
sb.append(paramXML(OPENAIRE_ID, ccp.getOpenaireId()));
|
166
|
sb.append(paramXML(CCONTENTPROVIDER_NAME, ccp.getName()));
|
167
|
sb.append(paramXML(CCONTENTPROVIDER_OFFICIALNAME, ccp.getOfficialname()));
|
168
|
sb.append(paramXML(CCONTENTPROVIDER_ENABLED,CCONTENTPROVIDERENABLED_DEFAULT));
|
169
|
sb.append("</concept>\n");
|
170
|
return sb.toString();
|
171
|
}
|
172
|
|
173
|
public static String asZenodoCommunityXML(final String contextId, final CommunityZenodoCommunity zc) {
|
174
|
final Escaper esc = XmlEscapers.xmlAttributeEscaper();
|
175
|
final StringBuilder sb = new StringBuilder();
|
176
|
sb.append(
|
177
|
String.format(
|
178
|
"<concept claim='true' id='%s%s%s%s' label='%s'>\n",
|
179
|
escape(esc, contextId), ZENODOCOMMUNITY_ID_SUFFIX, ID_SEPARATOR, escape(esc, String.valueOf(zc.getId())), escape(esc, zc.getZenodoid())));
|
180
|
|
181
|
sb.append(paramXML(CZENODOCOMMUNITY_ID, zc.getZenodoid()));
|
182
|
sb.append("</concept>\n");
|
183
|
return sb.toString();
|
184
|
}
|
185
|
|
186
|
private static String paramXML(final String paramName, final String value) {
|
187
|
return String.format("<param name='%s'>%s</param>\n", paramName, escape(XmlEscapers.xmlContentEscaper(), value));
|
188
|
}
|
189
|
|
190
|
}
|