Project

General

Profile

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.Set;
7

    
8
import com.google.common.base.Function;
9
import com.google.common.collect.Iterables;
10
import com.google.common.collect.Lists;
11
import com.google.common.collect.Maps;
12
import com.google.common.collect.Sets;
13
import eu.dnetlib.broker.objects.*;
14
import eu.dnetlib.data.proto.FieldTypeProtos.StructuredProperty;
15
import eu.dnetlib.data.proto.OafProtos.Oaf;
16
import eu.dnetlib.data.proto.OafProtos.OafEntity;
17
import eu.dnetlib.data.proto.ProjectProtos;
18
import eu.dnetlib.data.proto.ResultProtos;
19
import eu.dnetlib.data.proto.ResultProtos.Result;
20
import org.apache.commons.lang.StringUtils;
21
import org.dom4j.Document;
22
import org.dom4j.DocumentException;
23
import org.dom4j.io.SAXReader;
24

    
25
import static eu.dnetlib.data.mapreduce.util.OafHbaseUtils.getKey;
26
import static eu.dnetlib.data.mapreduce.util.OafHbaseUtils.getValue;
27

    
28
/**
29
 * Created by claudio on 22/07/16.
30
 */
31
public abstract class ProtoMapping {
32

    
33
	protected static List<Instance> mapInstances(final List<Result.Instance> in) {
34
		final Set<Instance> instances = Sets.newHashSet(Iterables.transform(in, new Function<Result.Instance, Instance>() {
35

    
36
			@Override
37
			public Instance apply(final Result.Instance i) {
38
				return new Instance()
39
						.setHostedby(getValue(i.getHostedby()))
40
						.setInstancetype(getValue(i.getInstancetype()))
41
						.setLicense(getKey(i.getLicence()))
42
						.setUrl(Iterables.getFirst(i.getUrlList(), ""));
43
			}
44
		}));
45
		return Lists.newArrayList(instances);
46
	}
47

    
48
	protected static List<Pid> mapPids(final List<StructuredProperty> sp) {
49
		return Lists.newArrayList(Iterables.transform(sp, new Function<StructuredProperty, Pid>() {
50

    
51
			@Override
52
			public Pid apply(final StructuredProperty sp) {
53
				return new Pid().setType(sp.getQualifier().getClassid()).setValue(sp.getValue());
54
			}
55
		}));
56
	}
57

    
58
	protected static Journal mapJournal(final ResultProtos.Result.Journal j) {
59
		return new eu.dnetlib.broker.objects.Journal()
60
				.setName(j.getName())
61
				.setIssn(j.getIssnPrinted())
62
				.setEissn(j.getIssnOnline())
63
				.setLissn(j.getIssnLinking());
64
	}
65

    
66
	protected static List<ExternalReference> mapExternalRefs(final List<Result.ExternalReference> ext) {
67
		return Lists.newArrayList(Iterables.transform(ext, new Function<Result.ExternalReference, ExternalReference>() {
68

    
69
			@Override
70
			public ExternalReference apply(final Result.ExternalReference e) {
71
				return new ExternalReference()
72
						.setUrl(e.getUrl())
73
						.setType(getKey(e.getQualifier()))
74
						.setRefidentifier(e.getRefidentifier())
75
						.setSitename(e.getSitename());
76
			}
77
		}));
78
	}
79

    
80
	protected static final List<Project> mapRelatedProjects(final OafEntity entity) {
81

    
82
		final Map<String, Oaf> projectMap = Maps.newHashMap();
83
		for(Oaf rel : entity.getCachedOafRelList()) {
84
			final OafEntity p = rel.getRel().getCachedOafTarget().getEntity();
85
			projectMap.put(p.getId(), Oaf.newBuilder(rel).build());
86
		}
87

    
88
		return Lists.transform(Lists.newArrayList(projectMap.values()), getProjectMappingFunction());
89
	}
90

    
91
	protected static final Project mapRelatedProject(final ProjectProtos.Project project) {
92
		final Project p = new Project();
93
		final ProjectProtos.Project.Metadata mp = project.getMetadata();
94

    
95
		p.setCode(getValue(mp.getCode()));
96
		p.setAcronym(getValue(mp.getAcronym()));
97
		p.setTitle(getValue(mp.getTitle()));
98

    
99
		final String ftree = getValue(mp.getFundingtreeList());
100
		if (StringUtils.isNotBlank(ftree)) {
101
			try {
102
				final Document fdoc = new SAXReader().read(new StringReader(ftree));
103
				p.setFunder(fdoc.valueOf("/fundingtree/funder/shortname"));
104
				p.setJurisdiction(fdoc.valueOf("/fundingtree/funder/jurisdiction"));
105
				p.setFundingProgram(fdoc.valueOf("//funding_level_0/name"));
106
			} catch (final DocumentException e) {
107
				throw new RuntimeException(e);
108
			}
109
		}
110
		//System.out.println("ProtoMapping.mapRelatedProjects project = " + project.toJSON());
111
		return p;
112
	}
113

    
114
	private static Function<Oaf, Project> getProjectMappingFunction() {
115
		return new Function<Oaf, Project>() {
116

    
117
			@Override
118
			public Project apply(final Oaf oafRel) {
119
				return mapRelatedProject(oafRel.getRel().getCachedOafTarget().getEntity().getProject());
120
			}
121
		};
122
	}
123

    
124
}
(4-4/4)