1
|
package eu.dnetlib.data.mapreduce.hbase.broker.mapping;
|
2
|
|
3
|
import java.io.StringReader;
|
4
|
import java.util.List;
|
5
|
import java.util.Map;
|
6
|
import java.util.stream.Collectors;
|
7
|
|
8
|
import com.google.common.collect.Iterables;
|
9
|
import com.google.common.collect.Maps;
|
10
|
import eu.dnetlib.broker.objects.*;
|
11
|
import eu.dnetlib.data.proto.FieldTypeProtos.StructuredProperty;
|
12
|
import eu.dnetlib.data.proto.OafProtos.Oaf;
|
13
|
import eu.dnetlib.data.proto.OafProtos.OafEntity;
|
14
|
import eu.dnetlib.data.proto.ProjectProtos;
|
15
|
import eu.dnetlib.data.proto.ResultProtos;
|
16
|
import eu.dnetlib.data.proto.ResultProtos.Result;
|
17
|
import org.apache.commons.lang.StringUtils;
|
18
|
import org.dom4j.Document;
|
19
|
import org.dom4j.DocumentException;
|
20
|
import org.dom4j.io.SAXReader;
|
21
|
|
22
|
import static eu.dnetlib.data.mapreduce.util.OafHbaseUtils.getKey;
|
23
|
import static eu.dnetlib.data.mapreduce.util.OafHbaseUtils.getValue;
|
24
|
|
25
|
/**
|
26
|
* Created by claudio on 22/07/16.
|
27
|
*/
|
28
|
public abstract class ProtoMapping {
|
29
|
|
30
|
protected static List<Instance> mapInstances(final List<Result.Instance> in) {
|
31
|
return in.stream()
|
32
|
.map(i -> new Instance()
|
33
|
.setHostedby(getValue(i.getHostedby()))
|
34
|
.setInstancetype(getValue(i.getInstancetype()))
|
35
|
.setLicense(getKey(i.getAccessright()))
|
36
|
.setUrl(Iterables.getFirst(i.getUrlList(), "")))
|
37
|
.collect(Collectors.toList());
|
38
|
}
|
39
|
|
40
|
protected static List<Pid> mapPids(final List<StructuredProperty> sp) {
|
41
|
return sp.stream()
|
42
|
.map(s -> new Pid().setType(s.getQualifier().getClassid()).setValue(s.getValue()))
|
43
|
.collect(Collectors.toList());
|
44
|
}
|
45
|
|
46
|
protected static Journal mapJournal(final ResultProtos.Result.Journal j) {
|
47
|
return new eu.dnetlib.broker.objects.Journal()
|
48
|
.setName(j.getName())
|
49
|
.setIssn(j.getIssnPrinted())
|
50
|
.setEissn(j.getIssnOnline())
|
51
|
.setLissn(j.getIssnLinking());
|
52
|
}
|
53
|
|
54
|
protected static List<ExternalReference> mapExternalRefs(final List<Result.ExternalReference> ext) {
|
55
|
return ext.stream()
|
56
|
.map(e -> new ExternalReference()
|
57
|
.setUrl(e.getUrl())
|
58
|
.setType(getKey(e.getQualifier()))
|
59
|
.setRefidentifier(e.getRefidentifier())
|
60
|
.setSitename(e.getSitename()))
|
61
|
.collect(Collectors.toList());
|
62
|
}
|
63
|
|
64
|
protected static final List<Project> mapRelatedProjects(final OafEntity entity) {
|
65
|
|
66
|
final Map<String, Oaf> projectMap = Maps.newHashMap();
|
67
|
for(Oaf rel : entity.getCachedOafRelList()) {
|
68
|
final OafEntity p = rel.getRel().getCachedOafTarget().getEntity();
|
69
|
projectMap.put(p.getId(), Oaf.newBuilder(rel).build());
|
70
|
}
|
71
|
|
72
|
return projectMap.values().stream()
|
73
|
.map(o -> mapRelatedProject(o.getRel().getCachedOafTarget().getEntity().getProject()))
|
74
|
.collect(Collectors.toList());
|
75
|
}
|
76
|
|
77
|
protected static final Project mapRelatedProject(final ProjectProtos.Project project) {
|
78
|
final Project p = new Project();
|
79
|
final ProjectProtos.Project.Metadata mp = project.getMetadata();
|
80
|
|
81
|
p.setCode(getValue(mp.getCode()));
|
82
|
p.setAcronym(getValue(mp.getAcronym()));
|
83
|
p.setTitle(getValue(mp.getTitle()));
|
84
|
|
85
|
final String ftree = getValue(mp.getFundingtreeList());
|
86
|
if (StringUtils.isNotBlank(ftree)) {
|
87
|
try {
|
88
|
final Document fdoc = new SAXReader().read(new StringReader(ftree));
|
89
|
p.setFunder(fdoc.valueOf("/fundingtree/funder/shortname"));
|
90
|
p.setJurisdiction(fdoc.valueOf("/fundingtree/funder/jurisdiction"));
|
91
|
p.setFundingProgram(fdoc.valueOf("//funding_level_0/name"));
|
92
|
} catch (final DocumentException e) {
|
93
|
throw new RuntimeException(e);
|
94
|
}
|
95
|
}
|
96
|
//System.out.println("ProtoMapping.mapRelatedProjects project = " + project.toJSON());
|
97
|
return p;
|
98
|
}
|
99
|
|
100
|
}
|