1
|
package eu.dnetlib.pace;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.io.StringWriter;
|
5
|
import java.util.ArrayList;
|
6
|
import java.util.LinkedList;
|
7
|
import java.util.List;
|
8
|
import java.util.stream.Collectors;
|
9
|
import java.util.stream.IntStream;
|
10
|
|
11
|
import com.google.common.collect.Lists;
|
12
|
import com.google.gson.Gson;
|
13
|
import eu.dnetlib.data.mapreduce.util.OafTest;
|
14
|
import eu.dnetlib.data.proto.FieldTypeProtos.Author;
|
15
|
import eu.dnetlib.data.proto.FieldTypeProtos.Qualifier;
|
16
|
import eu.dnetlib.data.proto.FieldTypeProtos.StructuredProperty;
|
17
|
import eu.dnetlib.data.proto.FieldTypeProtos.StructuredProperty.Builder;
|
18
|
import eu.dnetlib.data.proto.OafProtos.Oaf;
|
19
|
import eu.dnetlib.data.proto.OafProtos.OafEntity;
|
20
|
import eu.dnetlib.data.proto.OrganizationProtos.Organization;
|
21
|
import eu.dnetlib.data.proto.ResultProtos.Result;
|
22
|
import eu.dnetlib.pace.config.Config;
|
23
|
import eu.dnetlib.pace.config.DedupConfig;
|
24
|
import eu.dnetlib.pace.config.Type;
|
25
|
import eu.dnetlib.pace.model.Field;
|
26
|
import eu.dnetlib.pace.model.FieldValueImpl;
|
27
|
import eu.dnetlib.pace.model.MapDocument;
|
28
|
import eu.dnetlib.pace.model.ProtoDocumentBuilder;
|
29
|
import eu.dnetlib.pace.model.gt.GTAuthor;
|
30
|
|
31
|
import org.apache.commons.io.IOUtils;
|
32
|
import org.apache.commons.lang.RandomStringUtils;
|
33
|
import org.apache.commons.lang.StringUtils;
|
34
|
|
35
|
public abstract class AbstractProtoPaceTest extends OafTest {
|
36
|
|
37
|
protected DedupConfig getResultFullConf() {
|
38
|
return DedupConfig.load(readFromClasspath("/eu/dnetlib/pace/result.full.pace.conf"));
|
39
|
}
|
40
|
|
41
|
protected DedupConfig getResultSimpleConf() {
|
42
|
return DedupConfig.load(readFromClasspath("/eu/dnetlib/pace/result.simple.pace.conf"));
|
43
|
}
|
44
|
|
45
|
protected DedupConfig getResultConf() {
|
46
|
return DedupConfig.load(readFromClasspath("/eu/dnetlib/pace/result.pace.conf"));
|
47
|
}
|
48
|
|
49
|
protected DedupConfig getOrganizationSimpleConf() {
|
50
|
return DedupConfig.load(readFromClasspath("/eu/dnetlib/pace/organization.pace.conf"));
|
51
|
}
|
52
|
|
53
|
protected DedupConfig getResultAuthorsConf() {
|
54
|
return DedupConfig.load(readFromClasspath("/eu/dnetlib/pace/result.authors.pace.conf"));
|
55
|
}
|
56
|
|
57
|
protected DedupConfig getResultProdConf() {
|
58
|
return DedupConfig.load(readFromClasspath("/eu/dnetlib/pace/result.prod.pace.conf"));
|
59
|
}
|
60
|
|
61
|
protected MapDocument author(final Config conf, final String id, final Oaf oaf) {
|
62
|
return ProtoDocumentBuilder.newInstance(id, oaf.getEntity(), conf.model());
|
63
|
}
|
64
|
|
65
|
protected GTAuthor getGTAuthor(final String path) {
|
66
|
|
67
|
final Gson gson = new Gson();
|
68
|
|
69
|
final String json = readFromClasspath(path);
|
70
|
|
71
|
final GTAuthor gta = gson.fromJson(json, GTAuthor.class);
|
72
|
|
73
|
return gta;
|
74
|
}
|
75
|
|
76
|
protected String readFromClasspath(final String filename) {
|
77
|
final StringWriter sw = new StringWriter();
|
78
|
try {
|
79
|
IOUtils.copy(getClass().getResourceAsStream(filename), sw);
|
80
|
return sw.toString();
|
81
|
} catch (final IOException e) {
|
82
|
throw new RuntimeException("cannot load resource from classpath: " + filename);
|
83
|
}
|
84
|
}
|
85
|
|
86
|
protected MapDocument result(final Config config, final String id, final String title) {
|
87
|
return result(config, id, title, null, new ArrayList<>(), null);
|
88
|
}
|
89
|
|
90
|
protected MapDocument result(final Config config, final String id, final String title, final String date) {
|
91
|
return result(config, id, title, date, new ArrayList<>(), null);
|
92
|
}
|
93
|
|
94
|
protected MapDocument result(final Config config, final String id, final String title, final String date, final List<String> pid) {
|
95
|
return result(config, id, title, date, pid, null);
|
96
|
}
|
97
|
|
98
|
protected MapDocument result(final Config config, final String id, final String title, final String date, final String pid) {
|
99
|
return result(config, id, title, date, pid, null);
|
100
|
}
|
101
|
|
102
|
protected MapDocument result(final Config config, final String id, final String title, final String date, final String pid, final List<String> authors) {
|
103
|
return result(config, id, title, date, Lists.newArrayList(pid), authors);
|
104
|
}
|
105
|
|
106
|
protected MapDocument result(final Config config, final String id, final String title, final String date, final List<String> pid, final List<String> authors) {
|
107
|
final Result.Metadata.Builder metadata = Result.Metadata.newBuilder();
|
108
|
if (!StringUtils.isBlank(title)) {
|
109
|
metadata.addTitle(getStruct(title, getQualifier("main title", "dnet:titles")));
|
110
|
metadata.addTitle(getStruct(RandomStringUtils.randomAlphabetic(10), getQualifier("alternative title", "dnet:titles")));
|
111
|
}
|
112
|
if (!StringUtils.isBlank(date)) {
|
113
|
metadata.setDateofacceptance(sf(date));
|
114
|
}
|
115
|
|
116
|
final OafEntity.Builder entity = oafEntity(id, eu.dnetlib.data.proto.TypeProtos.Type.result);
|
117
|
final Result.Builder result = Result.newBuilder().setMetadata(metadata);
|
118
|
|
119
|
if (authors != null) {
|
120
|
result.getMetadataBuilder().addAllAuthor(
|
121
|
IntStream.range(0, authors.size())
|
122
|
.mapToObj(i -> author(authors.get(i), i))
|
123
|
.collect(Collectors.toCollection(LinkedList::new)));
|
124
|
}
|
125
|
|
126
|
entity.setResult(result);
|
127
|
|
128
|
if (pid != null) {
|
129
|
for(String p : pid) {
|
130
|
if (!StringUtils.isBlank(p)) {
|
131
|
entity.addPid(sp(p, "doi"));
|
132
|
//entity.addPid(sp(RandomStringUtils.randomAlphabetic(10), "oai"));
|
133
|
}
|
134
|
}
|
135
|
}
|
136
|
|
137
|
final OafEntity build = entity.build();
|
138
|
return ProtoDocumentBuilder.newInstance(id, build, config.model());
|
139
|
}
|
140
|
|
141
|
private Author author(final String s, int rank) {
|
142
|
final eu.dnetlib.pace.model.Person p = new eu.dnetlib.pace.model.Person(s, false);
|
143
|
final Author.Builder author = Author.newBuilder();
|
144
|
if (p.isAccurate()) {
|
145
|
author.setName(p.getNormalisedFirstName());
|
146
|
author.setSurname(p.getNormalisedSurname());
|
147
|
}
|
148
|
author.setFullname(p.getNormalisedFullname());
|
149
|
author.setRank(rank);
|
150
|
|
151
|
return author.build();
|
152
|
}
|
153
|
|
154
|
private OafEntity.Builder oafEntity(final String id, final eu.dnetlib.data.proto.TypeProtos.Type type) {
|
155
|
final OafEntity.Builder entity = OafEntity.newBuilder().setId(id).setType(type);
|
156
|
return entity;
|
157
|
}
|
158
|
|
159
|
protected MapDocument organization(final Config config, final String id, final String legalName) {
|
160
|
return organization(config, id, legalName, null);
|
161
|
}
|
162
|
|
163
|
protected MapDocument organization(final Config config, final String id, final String legalName, final String legalShortName) {
|
164
|
final Organization.Metadata.Builder metadata = Organization.Metadata.newBuilder();
|
165
|
if (legalName != null) {
|
166
|
metadata.setLegalname(sf(legalName));
|
167
|
}
|
168
|
if (legalShortName != null) {
|
169
|
metadata.setLegalshortname(sf(legalShortName));
|
170
|
}
|
171
|
|
172
|
final OafEntity.Builder entity = oafEntity(id, eu.dnetlib.data.proto.TypeProtos.Type.result);
|
173
|
entity.setOrganization(Organization.newBuilder().setMetadata(metadata));
|
174
|
|
175
|
return ProtoDocumentBuilder.newInstance(id, entity.build(), config.model());
|
176
|
}
|
177
|
|
178
|
private StructuredProperty sp(final String pid, final String type) {
|
179
|
final Builder pidSp =
|
180
|
StructuredProperty.newBuilder().setValue(pid)
|
181
|
.setQualifier(Qualifier.newBuilder().setClassid(type).setClassname(type).setSchemeid("dnet:pid_types").setSchemename("dnet:pid_types"));
|
182
|
return pidSp.build();
|
183
|
}
|
184
|
|
185
|
protected Field title(final String s) {
|
186
|
return new FieldValueImpl(Type.String, "title", s);
|
187
|
}
|
188
|
|
189
|
protected static StructuredProperty.Builder getStruct(final String value, final Qualifier.Builder qualifier) {
|
190
|
return StructuredProperty.newBuilder().setValue(value).setQualifier(qualifier);
|
191
|
}
|
192
|
|
193
|
/*
|
194
|
* protected static StringField.Builder sf(final String s) { return StringField.newBuilder().setValue(s); }
|
195
|
*
|
196
|
* protected static Qualifier.Builder getQualifier(final String classname, final String schemename) { return
|
197
|
* Qualifier.newBuilder().setClassid(classname).setClassname(classname).setSchemeid(schemename).setSchemename(schemename); }
|
198
|
*/
|
199
|
|
200
|
}
|