Project

General

Profile

1
package eu.dnetlib.data.transform.xml;
2

    
3
import java.util.List;
4

    
5
import com.google.common.collect.Lists;
6
import eu.dnetlib.data.proto.DNGFProtos.DNGF;
7
import eu.dnetlib.data.proto.DNGFProtos.DNGFEntity;
8
import eu.dnetlib.data.proto.DNGFProtos.DNGFEntity.Builder;
9
import eu.dnetlib.data.proto.WdsDatasetProtos.WdsDataset;
10
import eu.dnetlib.data.proto.WdsDatasetProtos.WdsDataset.GeoLocation;
11
import org.apache.commons.lang3.StringUtils;
12
import org.w3c.dom.Node;
13
import org.w3c.dom.NodeList;
14

    
15
/**
16
 * Created by claudio on 30/08/16.
17
 */
18
public class WdsToHbaseXsltFunctions extends OdfToHbaseXsltFunctions {
19

    
20
	public static String wdsDataset(
21
			final String resultId,
22
			final NodeList about,
23
			final NodeList titles,
24
			final NodeList subjects,
25
			final NodeList publisher,
26
			final NodeList descriptions,
27
			final NodeList dates,
28
			final NodeList dateaccepted,
29
			final NodeList resourceTypes,
30
			final NodeList formats,
31
			final NodeList sizes,
32
			final NodeList languages,
33
			final NodeList cobjcategory,
34
			final NodeList contributors,
35
			final NodeList rights,
36
			final NodeList version,
37
			final NodeList pidList,
38
			final NodeList geoLocations,
39
			final String provenance,
40
			final String trust,
41
			final String hostedbyId,
42
			final String hostedbyName,
43
			final String collectedfromId,
44
			final String collectedfromName,
45
			final NodeList originalIds,
46
			final String instanceUri,
47
			final String dateOfCollection,
48
			final String dateOfTransformation) {
49

    
50
		try {
51
			final DNGFEntity.Builder entity = odfDatasetProto(
52
					resultId, about, titles, subjects, publisher, descriptions, dates, dateaccepted,
53
					resourceTypes, formats, sizes, languages, cobjcategory, contributors, rights, version,
54
					pidList, hostedbyId, hostedbyName, collectedfromId, collectedfromName,
55
					originalIds, instanceUri, dateOfCollection, dateOfTransformation);
56

    
57
			if(geoLocations != null) {
58
				parseGeoLocations(entity, geoLocations);
59
			}
60

    
61
			final DNGF dngf = getOaf(entity, getDataInfo(about, provenance, trust, false, false));
62
			return base64(dngf.toByteArray());
63
		} catch (Throwable e) {
64
			e.printStackTrace(System.err);
65
			throw new RuntimeException(e);
66
		}
67
	}
68

    
69
	private static void parseGeoLocations(final Builder entity, final NodeList geoLocations) {
70
		final List<GeoLocation> geos = Lists.newArrayList();
71
		for (int i = 0; i < geoLocations.getLength(); i++) {
72
			final Node geo = geoLocations.item(i);
73
			final NodeList getlist = geo.getChildNodes();
74

    
75
			for (int j = 0; j < getlist.getLength(); j++) {
76
				final Node geoLocation = getlist.item(j);
77
				final NodeList geoChildren = geoLocation.getChildNodes();
78

    
79
				final GeoLocation.Builder g = GeoLocation.newBuilder();
80
				for (int k = 0; k < geoChildren.getLength(); k++) {
81
					final Node geoChild = geoChildren.item(k);
82

    
83
					final String value = geoChild.getTextContent();
84
					if (StringUtils.isNotBlank(value)) {
85
						switch (geoChild.getNodeName()) {
86
						case "geoLocationPoint":
87
							final String point = value.trim();
88
							g.setPoint(point);
89
							break;
90
						case "geoLocationBox":
91
							final String box = value.trim();
92
							g.addBox(box);
93
							break;
94
						case "geoLocationPlace":
95
							g.setPlace(value.trim());
96
							break;
97
						default:
98
							throw new IllegalArgumentException("invalid element: " + geoChild.getNodeName());
99
						}
100
					}
101
				}
102
				if(g.hasPlace() || g.hasPoint() || g.getBoxCount() > 0) {
103
					geos.add(g.build());
104
				}
105
			}
106
		}
107

    
108
		geos.forEach(g -> entity.getDatasetBuilder().getMetadataBuilder().addExtension(WdsDataset.geolocation, g));
109
	}
110

    
111
}
    (1-1/1)