1
|
package eu.dnetlib.data.mapreduce.hbase.broker.mapping;
|
2
|
|
3
|
import static eu.dnetlib.data.mapreduce.util.OafHbaseUtils.getKey;
|
4
|
import static eu.dnetlib.data.mapreduce.util.OafHbaseUtils.getValue;
|
5
|
|
6
|
import java.io.StringReader;
|
7
|
import java.util.List;
|
8
|
|
9
|
import org.apache.commons.lang.StringUtils;
|
10
|
import org.dom4j.Document;
|
11
|
import org.dom4j.DocumentException;
|
12
|
import org.dom4j.io.SAXReader;
|
13
|
|
14
|
import com.google.common.base.Function;
|
15
|
import com.google.common.collect.Iterables;
|
16
|
import com.google.common.collect.Lists;
|
17
|
|
18
|
import eu.dnetlib.broker.objects.ExternalReference;
|
19
|
import eu.dnetlib.broker.objects.Instance;
|
20
|
import eu.dnetlib.broker.objects.Pid;
|
21
|
import eu.dnetlib.broker.objects.Project;
|
22
|
import eu.dnetlib.data.proto.FieldTypeProtos.StructuredProperty;
|
23
|
import eu.dnetlib.data.proto.OafProtos.OafEntity;
|
24
|
import eu.dnetlib.data.proto.OafProtos.OafRel;
|
25
|
import eu.dnetlib.data.proto.ResultProtos.Result;
|
26
|
|
27
|
/**
|
28
|
* Created by claudio on 22/07/16.
|
29
|
*/
|
30
|
public abstract class ProtoMapping {
|
31
|
|
32
|
protected static List<Instance> mapInstances(final List<Result.Instance> in) {
|
33
|
return Lists.newArrayList(Iterables.transform(in, new Function<Result.Instance, Instance>() {
|
34
|
|
35
|
@Override
|
36
|
public Instance apply(final Result.Instance i) {
|
37
|
return new Instance()
|
38
|
.setHostedby(getValue(i.getHostedby()))
|
39
|
.setInstancetype(getValue(i.getInstancetype()))
|
40
|
.setLicense(getKey(i.getLicence()))
|
41
|
.setUrl(Iterables.getFirst(i.getUrlList(), ""));
|
42
|
}
|
43
|
}));
|
44
|
}
|
45
|
|
46
|
protected static List<Pid> mapPids(final List<StructuredProperty> sp) {
|
47
|
return Lists.newArrayList(Iterables.transform(sp, new Function<StructuredProperty, Pid>() {
|
48
|
|
49
|
@Override
|
50
|
public Pid apply(final StructuredProperty sp) {
|
51
|
return new Pid().setType(sp.getQualifier().getClassid()).setValue(sp.getValue());
|
52
|
}
|
53
|
}));
|
54
|
}
|
55
|
|
56
|
protected static List<ExternalReference> mapExternalRefs(final List<Result.ExternalReference> ext) {
|
57
|
return Lists.newArrayList(Iterables.transform(ext, new Function<Result.ExternalReference, ExternalReference>() {
|
58
|
|
59
|
@Override
|
60
|
public ExternalReference apply(final Result.ExternalReference e) {
|
61
|
return new ExternalReference()
|
62
|
.setUrl(e.getUrl())
|
63
|
.setType(getKey(e.getQualifier()))
|
64
|
.setRefidentifier(e.getRefidentifier())
|
65
|
.setSitename(e.getSitename());
|
66
|
}
|
67
|
}));
|
68
|
}
|
69
|
|
70
|
protected static final List<Project> mapRelatedProjects(final OafEntity oaf) {
|
71
|
|
72
|
// TODO: per ogni REL di tipo progetto creare un oggetto Project (sfruttando gli elementi nella seconda lista)
|
73
|
final Function<OafRel, Project> toProject = new Function<OafRel, Project>() {
|
74
|
|
75
|
@Override
|
76
|
public Project apply(final OafRel rel) {
|
77
|
final Project project = new Project();
|
78
|
switch (rel.getRelType()) {
|
79
|
case resultProject:
|
80
|
final eu.dnetlib.data.proto.ProjectProtos.Project.Metadata mp = rel.getCachedTarget().getProject().getMetadata();
|
81
|
final String ftree = getValue(mp.getFundingtreeList());
|
82
|
|
83
|
project.setCode(getValue(mp.getCode()));
|
84
|
project.setAcronym(getValue(mp.getAcronym()));
|
85
|
project.setTitle(getValue(mp.getTitle()));
|
86
|
|
87
|
if (StringUtils.isNotBlank(ftree)) {
|
88
|
try {
|
89
|
final Document fdoc = new SAXReader().read(new StringReader(ftree));
|
90
|
project.setFunder(fdoc.valueOf("/funder/shortname"));
|
91
|
project.setJurisdiction(fdoc.valueOf("/funder/jurisdiction"));
|
92
|
project.setFundingProgram(fdoc.valueOf("//funding_level_0/name"));
|
93
|
} catch (final DocumentException e) {
|
94
|
throw new RuntimeException(e);
|
95
|
}
|
96
|
}
|
97
|
break;
|
98
|
default:
|
99
|
break;
|
100
|
}
|
101
|
return project;
|
102
|
}
|
103
|
};
|
104
|
if (oaf.getCachedRelList() != null) { return Lists.transform(oaf.getCachedRelList(), toProject); }
|
105
|
return Lists.newArrayList();
|
106
|
|
107
|
}
|
108
|
|
109
|
}
|