1
|
package eu.dnetlib.data.transform.xml;
|
2
|
|
3
|
import java.util.List;
|
4
|
import java.util.Map;
|
5
|
|
6
|
import com.google.common.collect.Lists;
|
7
|
import com.google.common.collect.Maps;
|
8
|
import eu.dnetlib.data.mapreduce.util.DNGFRowKeyDecoder;
|
9
|
import eu.dnetlib.data.proto.DNGFProtos.DNGF;
|
10
|
import eu.dnetlib.data.proto.DNGFProtos.DNGFEntity;
|
11
|
import eu.dnetlib.data.proto.DNGFProtos.DNGFRel;
|
12
|
import eu.dnetlib.data.proto.DatasetProtos;
|
13
|
import eu.dnetlib.data.proto.FieldTypeProtos.Qualifier;
|
14
|
import eu.dnetlib.data.proto.FieldTypeProtos.StructuredProperty;
|
15
|
import eu.dnetlib.data.proto.PersonProtos.Person;
|
16
|
import eu.dnetlib.data.proto.PersonProtos.Person.CoAuthor;
|
17
|
import eu.dnetlib.data.proto.TypeProtos.Type;
|
18
|
import org.apache.commons.lang3.StringUtils;
|
19
|
import org.w3c.dom.NamedNodeMap;
|
20
|
import org.w3c.dom.Node;
|
21
|
import org.w3c.dom.NodeList;
|
22
|
|
23
|
/**
|
24
|
* Created by claudio on 01/12/15.
|
25
|
*/
|
26
|
public class CommonDNetXsltFunctions extends AbstractDNetXsltFunctions {
|
27
|
|
28
|
private static final int MAX_COAUTHORS = 50;
|
29
|
|
30
|
public static String authorship(
|
31
|
final String source,
|
32
|
final String sourceType,
|
33
|
final String target,
|
34
|
final String targetType,
|
35
|
final int rank,
|
36
|
final String relClass,
|
37
|
final String provenanceAction,
|
38
|
final String trust,
|
39
|
final NodeList about) {
|
40
|
|
41
|
final Map<String, String> params = Maps.newHashMap();
|
42
|
params.put("rank", "" + rank);
|
43
|
return rel(source, sourceType, target, targetType, relClass, "", provenanceAction, trust, about, params);
|
44
|
}
|
45
|
|
46
|
public static String rel(
|
47
|
final String source,
|
48
|
final String sourceType,
|
49
|
final String target,
|
50
|
final String targetType,
|
51
|
final String relTypeId,
|
52
|
final String relTypeScheme,
|
53
|
final String provenanceAction,
|
54
|
final String trust) {
|
55
|
return rel(source, sourceType, target, targetType, relTypeId, relTypeScheme, provenanceAction, trust, null, null);
|
56
|
}
|
57
|
|
58
|
public static String rel(
|
59
|
final String source,
|
60
|
final String sourceType,
|
61
|
final String target,
|
62
|
final String targetType,
|
63
|
final String relTypeId,
|
64
|
final String relTypeScheme,
|
65
|
final String provenanceAction,
|
66
|
final String trust,
|
67
|
final NodeList about) {
|
68
|
return rel(source, sourceType, target, targetType, relTypeId, relTypeScheme, provenanceAction, trust, about, null);
|
69
|
}
|
70
|
|
71
|
// dnet:oafRel($resultId, $datasetId, 'resultResult', 'part' 'isPartOf', $about)
|
72
|
public static String rel(
|
73
|
final String source,
|
74
|
final String sourceType,
|
75
|
final String target,
|
76
|
final String targetType,
|
77
|
final String relTypeId,
|
78
|
final String relTypeScheme,
|
79
|
final String provenanceAction,
|
80
|
final String trust,
|
81
|
final NodeList about,
|
82
|
final Map<String, String> params) {
|
83
|
try {
|
84
|
final String eSource = DNGFRowKeyDecoder.decode(source).getKey();
|
85
|
final String eTarget = DNGFRowKeyDecoder.decode(target).getKey();
|
86
|
|
87
|
final Qualifier.Builder relType = getSimpleQualifier(relTypeId, relTypeScheme);
|
88
|
final DNGFRel.Builder rel = getRel(eSource, Type.valueOf(sourceType), eTarget, Type.valueOf(targetType), relType.build(), false);
|
89
|
|
90
|
final DNGF oaf = getOaf(rel, getDataInfo(about, provenanceAction, trust, false, false));
|
91
|
return base64(oaf.toByteArray());
|
92
|
} catch (Throwable e) {
|
93
|
e.printStackTrace(System.err);
|
94
|
throw new RuntimeException(e);
|
95
|
}
|
96
|
}
|
97
|
|
98
|
public static String person(
|
99
|
final String personId,
|
100
|
final NodeList about,
|
101
|
final String provenanceAction,
|
102
|
final String trust,
|
103
|
final String collectedFromId,
|
104
|
final String collectedFromName,
|
105
|
final String originalId,
|
106
|
final String dateOfCollection,
|
107
|
final String dateOfTransformation,
|
108
|
final String fullname,
|
109
|
final String nameIdentifier,
|
110
|
final String nameIdentifierScheme) {
|
111
|
return person(personId, fullname, nameIdentifier, nameIdentifierScheme, null, null, null, provenanceAction, trust, about, collectedFromId,
|
112
|
collectedFromName, originalId, dateOfCollection, dateOfTransformation);
|
113
|
}
|
114
|
|
115
|
// $personId, $about, $provenance, $trust, $collectedfromid, $collectedfromname, $originalPersonId, $dateofcollection, normalize-space(.))
|
116
|
public static String person(
|
117
|
final String personId,
|
118
|
final String fullname,
|
119
|
final String nameIdentifier,
|
120
|
final String nameIdentifierScheme,
|
121
|
final NodeList authors,
|
122
|
final String namespaceprefix,
|
123
|
final String objIdentifier,
|
124
|
final String provenanceAction,
|
125
|
final String trust,
|
126
|
final NodeList about,
|
127
|
final String collectedFromId,
|
128
|
final String collectedFromName,
|
129
|
final String originalId,
|
130
|
final String dateOfCollection,
|
131
|
final String dateOfTransformation) {
|
132
|
try {
|
133
|
final String entityId = DNGFRowKeyDecoder.decode(personId).getKey();
|
134
|
|
135
|
final Person.Builder person = Person.newBuilder();
|
136
|
final Person.Metadata.Builder metadata = getMetadata(fullname);
|
137
|
|
138
|
if (authors != null) {
|
139
|
for (int i = 0; (i < authors.getLength()) && (i < MAX_COAUTHORS); i++) {
|
140
|
final Node node = authors.item(i);
|
141
|
|
142
|
final String name = StringUtils.trim(node.getTextContent());
|
143
|
if (!name.equals(fullname)) {
|
144
|
|
145
|
final CoAuthor.Builder coAuthor = CoAuthor.newBuilder();
|
146
|
|
147
|
coAuthor.setId(oafId("person", namespaceprefix, objIdentifier + "::" + name));
|
148
|
coAuthor.setMetadata(getMetadata(name));
|
149
|
|
150
|
person.addCoauthor(coAuthor);
|
151
|
}
|
152
|
}
|
153
|
}
|
154
|
|
155
|
final List<StructuredProperty> pids = Lists.newArrayList();
|
156
|
if (StringUtils.isNotBlank(nameIdentifier) && StringUtils.isNotBlank(nameIdentifierScheme)) {
|
157
|
pids.add(getStructuredProperty(nameIdentifier, nameIdentifierScheme, nameIdentifierScheme, "dnet:pid_types", "dnet:pid_types"));
|
158
|
}
|
159
|
|
160
|
final DNGFEntity.Builder entity =
|
161
|
getEntity(Type.person, entityId, getKV(collectedFromId, collectedFromName), Lists.newArrayList(originalId), dateOfCollection,
|
162
|
dateOfTransformation, pids)
|
163
|
.setPerson(person.setMetadata(metadata));
|
164
|
|
165
|
final DNGF oaf = getOaf(entity, getDataInfo(about, provenanceAction, trust, false, false));
|
166
|
return base64(oaf.toByteArray());
|
167
|
} catch (final Throwable e) {
|
168
|
System.err.println("personId: " + personId);
|
169
|
System.err.println("fullname: " + fullname);
|
170
|
System.err.println("provenanceAction: " + provenanceAction);
|
171
|
System.err.println("trust: " + trust);
|
172
|
System.err.println("collectedFromId: " + collectedFromId);
|
173
|
System.err.println("collectedFromName: " + collectedFromName);
|
174
|
System.err.println("originalId: " + originalId);
|
175
|
System.err.println("dateOfCollection: " + dateOfCollection);
|
176
|
e.printStackTrace();
|
177
|
throw new RuntimeException(e);
|
178
|
}
|
179
|
}
|
180
|
|
181
|
private static Person.Metadata.Builder getMetadata(final String fullname) {
|
182
|
final Person.Metadata.Builder metadata = Person.Metadata.newBuilder();
|
183
|
|
184
|
metadata.setFullname(sf(fullname));
|
185
|
|
186
|
final eu.dnetlib.pace.model.Person p = new eu.dnetlib.pace.model.Person(fullname, false);
|
187
|
if (p.isAccurate()) {
|
188
|
metadata.setFirstname(sf(p.getNormalisedFirstName()));
|
189
|
metadata.clearSecondnames().addSecondnames(sf(p.getNormalisedSurname()));
|
190
|
// metadata.setFullname(sf(p.getNormalisedFullname()));
|
191
|
}
|
192
|
return metadata;
|
193
|
}
|
194
|
|
195
|
protected static void manageTitle(NodeList titles, DatasetProtos.Dataset.Metadata.Builder metadataProto) {
|
196
|
for (int i = 0; i < titles.getLength(); i++) {
|
197
|
Node currentNode = titles.item(i);
|
198
|
NodeList childNodes = currentNode.getChildNodes();
|
199
|
if (childNodes.getLength() > 0) {
|
200
|
String titleValue = childNodes.item(0).getNodeValue();
|
201
|
String classname = "main title";
|
202
|
String classid = "main title";
|
203
|
if (currentNode.hasAttributes()) {
|
204
|
NamedNodeMap attributes = currentNode.getAttributes();
|
205
|
Node titleType = attributes.getNamedItem("titleType");
|
206
|
|
207
|
if (titleType != null && titleType.getNodeValue().equals("AlternativeTitle")) {
|
208
|
classname = "alternative title";
|
209
|
classid = "alternative title";
|
210
|
}
|
211
|
if (titleType != null && titleType.getNodeValue().equals("Subtitle")) {
|
212
|
classname = "subtitle";
|
213
|
classid = "subtitle";
|
214
|
}
|
215
|
if (titleType != null && titleType.getNodeValue().equals("TranslatedTitle")) {
|
216
|
classname = "translated title";
|
217
|
classid = "translated title";
|
218
|
}
|
219
|
}
|
220
|
addField(metadataProto, DatasetProtos.Dataset.Metadata.getDescriptor().findFieldByName("title"),
|
221
|
getStructuredProperty(titleValue, classname, classid, "dnet:dataCite_title", "dnet:dataCite_title"));
|
222
|
}
|
223
|
}
|
224
|
}
|
225
|
|
226
|
protected static void manageDate(NodeList dates, DatasetProtos.Dataset.Metadata.Builder metadataProto) {
|
227
|
for (int i = 0; i < dates.getLength(); i++) {
|
228
|
Node currentNode = dates.item(i);
|
229
|
if (currentNode != null && currentNode.hasAttributes() && currentNode.hasChildNodes()) {
|
230
|
String dateAttribute = currentNode.getAttributes().getNamedItem("dateType").getNodeValue();
|
231
|
String dateValue = currentNode.getChildNodes().item(0).getNodeValue();
|
232
|
String protoAttribute = "relevantdate";
|
233
|
if ("Accepted".equals(dateAttribute)) {
|
234
|
protoAttribute = "dateofacceptance";
|
235
|
} else if ("Issued".equals(dateAttribute)) {
|
236
|
protoAttribute = "storagedate";
|
237
|
} else if ("Updated".equals(dateAttribute)) {
|
238
|
protoAttribute = "lastmetadataupdate";
|
239
|
} else if ("Available".equals(dateAttribute)) {
|
240
|
protoAttribute = "embargoenddate";
|
241
|
}
|
242
|
if (protoAttribute.equals("relevantdate") == false) {
|
243
|
addField(metadataProto, DatasetProtos.Dataset.Metadata.getDescriptor().findFieldByName(protoAttribute), dateValue);
|
244
|
} else {
|
245
|
addField(metadataProto, DatasetProtos.Dataset.Metadata.getDescriptor().findFieldByName(protoAttribute),
|
246
|
getStructuredProperty(dateValue, "UNKNOWN", "UNKNOWN", "dnet:dataCite_date", "dnet:dataCite_date"));
|
247
|
}
|
248
|
}
|
249
|
}
|
250
|
}
|
251
|
|
252
|
}
|