Revision 48991
Added by Claudio Atzori over 6 years ago
CommonDNetXsltFunctions.java | ||
---|---|---|
1 | 1 |
package eu.dnetlib.data.transform.xml; |
2 | 2 |
|
3 |
import java.util.List; |
|
4 | 3 |
import java.util.Map; |
5 | 4 |
|
6 |
import com.google.common.collect.Lists; |
|
7 |
import com.google.common.collect.Maps; |
|
8 | 5 |
import com.google.protobuf.Message.Builder; |
9 | 6 |
import eu.dnetlib.data.mapreduce.util.OafRowKeyDecoder; |
10 |
import eu.dnetlib.data.proto.FieldTypeProtos.StructuredProperty; |
|
11 | 7 |
import eu.dnetlib.data.proto.OafProtos.Oaf; |
12 |
import eu.dnetlib.data.proto.OafProtos.OafEntity; |
|
13 | 8 |
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 | 9 |
import eu.dnetlib.data.proto.RelMetadataProtos.RelMetadata; |
17 | 10 |
import eu.dnetlib.data.proto.RelTypeProtos.RelType; |
18 | 11 |
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 | 12 |
import org.w3c.dom.NodeList; |
23 | 13 |
|
24 | 14 |
/** |
... | ... | |
28 | 18 |
|
29 | 19 |
private static final int MAX_COAUTHORS = 50; |
30 | 20 |
|
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 collectedFromId, |
|
38 |
final String collectedFromName, |
|
39 |
final String provenanceAction, |
|
40 |
final String trust, |
|
41 |
final NodeList about) { |
|
42 |
|
|
43 |
final Map<String, String> params = Maps.newHashMap(); |
|
44 |
params.put("rank", "" + rank); |
|
45 |
return rel(source, target, RelType.personResult.name(), SubRelType.authorship.name(), relClass, collectedFromId, collectedFromName, provenanceAction, trust, about, params); |
|
46 |
} |
|
47 |
|
|
48 | 21 |
public static String rel( |
49 | 22 |
final String source, |
50 | 23 |
final String target, |
... | ... | |
117 | 90 |
} |
118 | 91 |
} |
119 | 92 |
|
120 |
public static String person( |
|
121 |
final String personId, |
|
122 |
final NodeList about, |
|
123 |
final String provenanceAction, |
|
124 |
final String trust, |
|
125 |
final String collectedFromId, |
|
126 |
final String collectedFromName, |
|
127 |
final String originalId, |
|
128 |
final String dateOfCollection, |
|
129 |
final String dateOfTransformation, |
|
130 |
final String fullname, |
|
131 |
final String nameIdentifier, |
|
132 |
final String nameIdentifierScheme) { |
|
133 |
return person(personId, fullname, nameIdentifier, nameIdentifierScheme, null, null, null, provenanceAction, trust, about, collectedFromId, |
|
134 |
collectedFromName, originalId, dateOfCollection, dateOfTransformation); |
|
135 |
} |
|
136 |
|
|
137 |
// $personId, $about, $provenance, $trust, $collectedfromid, $collectedfromname, $originalPersonId, $dateofcollection, normalize-space(.)) |
|
138 |
public static String person( |
|
139 |
final String personId, |
|
140 |
final String fullname, |
|
141 |
final String nameIdentifier, |
|
142 |
final String nameIdentifierScheme, |
|
143 |
final NodeList authors, |
|
144 |
final String namespaceprefix, |
|
145 |
final String objIdentifier, |
|
146 |
final String provenanceAction, |
|
147 |
final String trust, |
|
148 |
final NodeList about, |
|
149 |
final String collectedFromId, |
|
150 |
final String collectedFromName, |
|
151 |
final String originalId, |
|
152 |
final String dateOfCollection, |
|
153 |
final String dateOfTransformation) { |
|
154 |
try { |
|
155 |
final String entityId = OafRowKeyDecoder.decode(personId).getKey(); |
|
156 |
|
|
157 |
final Person.Builder person = Person.newBuilder(); |
|
158 |
final Person.Metadata.Builder metadata = getMetadata(fullname); |
|
159 |
|
|
160 |
if (authors != null) { |
|
161 |
for (int i = 0; (i < authors.getLength()) && (i < MAX_COAUTHORS); i++) { |
|
162 |
final Node node = authors.item(i); |
|
163 |
|
|
164 |
final String name = StringUtils.trim(node.getTextContent()); |
|
165 |
if (!name.equals(fullname)) { |
|
166 |
|
|
167 |
final CoAuthor.Builder coAuthor = CoAuthor.newBuilder(); |
|
168 |
|
|
169 |
coAuthor.setId(oafId("person", namespaceprefix, objIdentifier + "::" + name)); |
|
170 |
coAuthor.setMetadata(getMetadata(name)); |
|
171 |
|
|
172 |
person.addCoauthor(coAuthor); |
|
173 |
} |
|
174 |
} |
|
175 |
} |
|
176 |
|
|
177 |
final List<StructuredProperty> pids = Lists.newArrayList(); |
|
178 |
if (StringUtils.isNotBlank(nameIdentifier) && StringUtils.isNotBlank(nameIdentifierScheme)) { |
|
179 |
pids.add(getStructuredProperty(nameIdentifier, nameIdentifierScheme, nameIdentifierScheme, "dnet:pid_types", "dnet:pid_types")); |
|
180 |
} |
|
181 |
|
|
182 |
final OafEntity.Builder entity = |
|
183 |
getEntity(Type.person, entityId, getKV(collectedFromId, collectedFromName), Lists.newArrayList(originalId), dateOfCollection, |
|
184 |
dateOfTransformation, pids) |
|
185 |
.setPerson(person.setMetadata(metadata)); |
|
186 |
|
|
187 |
final Oaf oaf = getOaf(entity, getDataInfo(about, provenanceAction, trust, false, false)); |
|
188 |
return base64(oaf.toByteArray()); |
|
189 |
} catch (final Throwable e) { |
|
190 |
System.err.println("personId: " + personId); |
|
191 |
System.err.println("fullname: " + fullname); |
|
192 |
System.err.println("provenanceAction: " + provenanceAction); |
|
193 |
System.err.println("trust: " + trust); |
|
194 |
System.err.println("collectedFromId: " + collectedFromId); |
|
195 |
System.err.println("collectedFromName: " + collectedFromName); |
|
196 |
System.err.println("originalId: " + originalId); |
|
197 |
System.err.println("dateOfCollection: " + dateOfCollection); |
|
198 |
e.printStackTrace(); |
|
199 |
throw new RuntimeException(e); |
|
200 |
} |
|
201 |
} |
|
202 |
|
|
203 |
private static Person.Metadata.Builder getMetadata(final String fullname) { |
|
204 |
final Person.Metadata.Builder metadata = Person.Metadata.newBuilder(); |
|
205 |
|
|
206 |
metadata.setFullname(sf(fullname)); |
|
207 |
|
|
208 |
final eu.dnetlib.pace.model.Person p = new eu.dnetlib.pace.model.Person(fullname, false); |
|
209 |
if (p.isAccurate()) { |
|
210 |
metadata.setFirstname(sf(p.getNormalisedFirstName())); |
|
211 |
metadata.clearSecondnames().addSecondnames(sf(p.getNormalisedSurname())); |
|
212 |
// metadata.setFullname(sf(p.getNormalisedFullname())); |
|
213 |
} |
|
214 |
return metadata; |
|
215 |
} |
|
216 |
|
|
217 | 93 |
} |
Also available in: Unified diff
depending on protobuf specific version, getting rid of person entities, updated solrj version