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 com.google.protobuf.Message.Builder;
|
9
|
import eu.dnetlib.data.mapreduce.util.OafRowKeyDecoder;
|
10
|
import eu.dnetlib.data.proto.FieldTypeProtos.StructuredProperty;
|
11
|
import eu.dnetlib.data.proto.OafProtos.Oaf;
|
12
|
import eu.dnetlib.data.proto.OafProtos.OafEntity;
|
13
|
import eu.dnetlib.data.proto.OafProtos.OafRel;
|
14
|
import eu.dnetlib.data.proto.PersonProtos.Person;
|
15
|
import eu.dnetlib.data.proto.PersonProtos.Person.CoAuthor;
|
16
|
import eu.dnetlib.data.proto.RelMetadataProtos.RelMetadata;
|
17
|
import eu.dnetlib.data.proto.RelTypeProtos.RelType;
|
18
|
import eu.dnetlib.data.proto.RelTypeProtos.SubRelType;
|
19
|
import eu.dnetlib.data.proto.TypeProtos.Type;
|
20
|
import org.apache.commons.lang.StringUtils;
|
21
|
import org.w3c.dom.Node;
|
22
|
import org.w3c.dom.NodeList;
|
23
|
|
24
|
/**
|
25
|
* Created by claudio on 01/12/15.
|
26
|
*/
|
27
|
public class CommonDNetXsltFunctions extends AbstractDNetXsltFunctions {
|
28
|
|
29
|
private static final int MAX_COAUTHORS = 50;
|
30
|
|
31
|
// dnet:oafPersonResultFromDMF($resultId, $oafPerson, position(), "sysimport:crosswalk:repository", "0.9")
|
32
|
public static String personResult_Authorship(
|
33
|
final String source,
|
34
|
final String target,
|
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, target, RelType.personResult.name(), SubRelType.authorship.name(), relClass, provenanceAction, trust, about, params);
|
44
|
}
|
45
|
|
46
|
public static String rel(
|
47
|
final String source,
|
48
|
final String target,
|
49
|
final String relType,
|
50
|
final String subRelType,
|
51
|
final String relClass,
|
52
|
final String provenanceAction,
|
53
|
final String trust) {
|
54
|
return rel(source, target, relType, subRelType, relClass, provenanceAction, trust, null, null);
|
55
|
}
|
56
|
|
57
|
public static String rel(
|
58
|
final String source,
|
59
|
final String target,
|
60
|
final String relType,
|
61
|
final String subRelType,
|
62
|
final String relClass,
|
63
|
final String provenanceAction,
|
64
|
final String trust,
|
65
|
final NodeList about) {
|
66
|
return rel(source, target, relType, subRelType, relClass, provenanceAction, trust, about, null);
|
67
|
}
|
68
|
|
69
|
// dnet:oafRel($resultId, $datasetId, 'resultResult', 'part' 'isPartOf', $about)
|
70
|
public static String rel(
|
71
|
final String source,
|
72
|
final String target,
|
73
|
final String relType,
|
74
|
final String subRelType,
|
75
|
final String relClass,
|
76
|
final String provenanceAction,
|
77
|
final String trust,
|
78
|
final NodeList about,
|
79
|
final Map<String, String> params) {
|
80
|
try {
|
81
|
final String eSource = OafRowKeyDecoder.decode(source).getKey();
|
82
|
final String eTarget = OafRowKeyDecoder.decode(target).getKey();
|
83
|
|
84
|
final RelType rType = RelType.valueOf(relType);
|
85
|
final SubRelType srType = SubRelType.valueOf(subRelType);
|
86
|
|
87
|
final RelMetadata.Builder metadata = RelMetadata.newBuilder().setSemantics(getSimpleQualifier(relClass, getVocabularyName(rType)));
|
88
|
|
89
|
final Builder subRel = getSubRelBuilder(metadata, srType, params);
|
90
|
|
91
|
final OafRel.Builder rel = getRelBuilder(rType, srType, getRel(eSource, eTarget, rType, srType, relClass, false), subRel);
|
92
|
|
93
|
final Oaf oaf = getOaf(rel, getDataInfo(about, provenanceAction, trust, false, false));
|
94
|
return base64(oaf.toByteArray());
|
95
|
} catch (Exception e) {
|
96
|
e.printStackTrace(System.err);
|
97
|
throw new RuntimeException(e);
|
98
|
}
|
99
|
}
|
100
|
|
101
|
public static String person(
|
102
|
final String personId,
|
103
|
final NodeList about,
|
104
|
final String provenanceAction,
|
105
|
final String trust,
|
106
|
final String collectedFromId,
|
107
|
final String collectedFromName,
|
108
|
final String originalId,
|
109
|
final String dateOfCollection,
|
110
|
final String dateOfTransformation,
|
111
|
final String fullname,
|
112
|
final String nameIdentifier,
|
113
|
final String nameIdentifierScheme) {
|
114
|
return person(personId, fullname, nameIdentifier, nameIdentifierScheme, null, null, null, provenanceAction, trust, about, collectedFromId,
|
115
|
collectedFromName, originalId, dateOfCollection, dateOfTransformation);
|
116
|
}
|
117
|
|
118
|
// $personId, $about, $provenance, $trust, $collectedfromid, $collectedfromname, $originalPersonId, $dateofcollection, normalize-space(.))
|
119
|
public static String person(
|
120
|
final String personId,
|
121
|
final String fullname,
|
122
|
final String nameIdentifier,
|
123
|
final String nameIdentifierScheme,
|
124
|
final NodeList authors,
|
125
|
final String namespaceprefix,
|
126
|
final String objIdentifier,
|
127
|
final String provenanceAction,
|
128
|
final String trust,
|
129
|
final NodeList about,
|
130
|
final String collectedFromId,
|
131
|
final String collectedFromName,
|
132
|
final String originalId,
|
133
|
final String dateOfCollection,
|
134
|
final String dateOfTransformation) {
|
135
|
try {
|
136
|
final String entityId = OafRowKeyDecoder.decode(personId).getKey();
|
137
|
|
138
|
final Person.Builder person = Person.newBuilder();
|
139
|
final Person.Metadata.Builder metadata = getMetadata(fullname);
|
140
|
|
141
|
if (authors != null) {
|
142
|
for (int i = 0; (i < authors.getLength()) && (i < MAX_COAUTHORS); i++) {
|
143
|
final Node node = authors.item(i);
|
144
|
|
145
|
final String name = StringUtils.trim(node.getTextContent());
|
146
|
if (!name.equals(fullname)) {
|
147
|
|
148
|
final CoAuthor.Builder coAuthor = CoAuthor.newBuilder();
|
149
|
|
150
|
coAuthor.setId(oafId("person", namespaceprefix, objIdentifier + "::" + name));
|
151
|
coAuthor.setMetadata(getMetadata(name));
|
152
|
|
153
|
person.addCoauthor(coAuthor);
|
154
|
}
|
155
|
}
|
156
|
}
|
157
|
|
158
|
final List<StructuredProperty> pids = Lists.newArrayList();
|
159
|
if (StringUtils.isNotBlank(nameIdentifier) && StringUtils.isNotBlank(nameIdentifierScheme)) {
|
160
|
pids.add(getStructuredProperty(nameIdentifier, nameIdentifierScheme, nameIdentifierScheme, "dnet:pid_types", "dnet:pid_types"));
|
161
|
}
|
162
|
|
163
|
final OafEntity.Builder entity =
|
164
|
getEntity(Type.person, entityId, getKV(collectedFromId, collectedFromName), Lists.newArrayList(originalId), dateOfCollection,
|
165
|
dateOfTransformation, pids)
|
166
|
.setPerson(person.setMetadata(metadata));
|
167
|
|
168
|
final Oaf oaf = getOaf(entity, getDataInfo(about, provenanceAction, trust, false, false));
|
169
|
return base64(oaf.toByteArray());
|
170
|
} catch (final Throwable e) {
|
171
|
System.err.println("personId: " + personId);
|
172
|
System.err.println("fullname: " + fullname);
|
173
|
System.err.println("provenanceAction: " + provenanceAction);
|
174
|
System.err.println("trust: " + trust);
|
175
|
System.err.println("collectedFromId: " + collectedFromId);
|
176
|
System.err.println("collectedFromName: " + collectedFromName);
|
177
|
System.err.println("originalId: " + originalId);
|
178
|
System.err.println("dateOfCollection: " + dateOfCollection);
|
179
|
e.printStackTrace();
|
180
|
throw new RuntimeException(e);
|
181
|
}
|
182
|
}
|
183
|
|
184
|
private static Person.Metadata.Builder getMetadata(final String fullname) {
|
185
|
final Person.Metadata.Builder metadata = Person.Metadata.newBuilder();
|
186
|
|
187
|
metadata.setFullname(sf(fullname));
|
188
|
|
189
|
final eu.dnetlib.pace.model.Person p = new eu.dnetlib.pace.model.Person(fullname, false);
|
190
|
if (p.isAccurate()) {
|
191
|
metadata.setFirstname(sf(p.getNormalisedFirstName()));
|
192
|
metadata.clearSecondnames().addSecondnames(sf(p.getNormalisedSurname()));
|
193
|
// metadata.setFullname(sf(p.getNormalisedFullname()));
|
194
|
}
|
195
|
return metadata;
|
196
|
}
|
197
|
|
198
|
}
|