Project

General

Profile

1
package eu.dnetlib.openaire.context;
2

    
3
import java.io.StringReader;
4
import java.text.ParseException;
5
import java.util.*;
6
import java.util.stream.Collectors;
7

    
8
import com.google.common.base.Functions;
9
import com.google.common.collect.Lists;
10
import eu.dnetlib.openaire.funders.domain.FunderDetails;
11
import org.apache.commons.lang3.BooleanUtils;
12
import org.apache.commons.lang3.StringUtils;
13
import org.apache.commons.lang3.time.DateUtils;
14
import org.dom4j.Document;
15
import org.dom4j.DocumentException;
16
import org.dom4j.Element;
17
import org.dom4j.io.SAXReader;
18

    
19
public class ContextMappingUtils {
20

    
21
	private static final List<String> DATE_PATTERN = Lists.newArrayList(
22
			"yyyy-MM-dd'T'hh:mm:ss", "yyyy-MM-dd'T'hh:mm:ssXXX", "yyyy-MM-dd'T'hh:mm:ss+00:00");
23

    
24
	public static Context parseContext(final String s, final Queue<Throwable> errors) {
25

    
26
		final SAXReader reader = new SAXReader();
27
		final Document doc;
28
		try {
29
			doc = reader.read(new StringReader(s));
30
		} catch (DocumentException e) {
31
			errors.add(e);
32
			return new Context();
33
		}
34
		try {
35
			final Element eContext = (Element) doc.selectSingleNode("/RESOURCE_PROFILE/BODY/CONFIGURATION/context");
36
			return new Context()
37
					.setId(eContext.attributeValue("id"))
38
					.setLabel(eContext.attributeValue("label"))
39
					.setType(eContext.attributeValue("type"))
40
					.setLastUpdateDate(asDate(doc.valueOf("/RESOURCE_PROFILE/HEADER/DATE_OF_CREATION/@value")))
41
					//.setDateofcreation() in reality the field named CreationDate is modified each time the context profile is updated
42
					//the creation date will be added in the param elements of the community
43
					.setParams(parseParams(eContext))
44
					.setCategories(parseCategories(eContext));
45
		} catch (Throwable e) {
46
			errors.add(e);
47
			return new Context();
48
		}
49
	}
50

    
51
	private static Date asDate(final String s) {
52
		for(String pattern : DATE_PATTERN) {
53
			try {
54
				return DateUtils.parseDate(s, pattern);
55
			} catch (final ParseException e) {}
56
		}
57
		return null;
58
	}
59

    
60
	private static Map<String, Category> parseCategories(final Element eContext) {
61
		final List<Element> eCategory = (List<Element>) eContext.selectNodes("//category");
62
		return eCategory.stream()
63
				.map(eCat -> new Category()
64
						.setClaim(getClaim(eCat))
65
						.setId(eCat.attributeValue("id"))
66
						.setLabel(eCat.attributeValue("label"))
67
						.setParams(parseParams(eCat))
68
						.setConcepts(parseConcepts(eCat)))
69
				.collect(Collectors.toMap(
70
						Category::getId,
71
						Functions.identity()
72
				));
73
	}
74

    
75
	private static List<Concept> parseConcepts(final Element eCategory) {
76
		final List<Element> eConcepts = (List<Element>) eCategory.selectNodes("./concept");
77
		return eConcepts.stream()
78
				.map(eCon -> new Concept()
79
						.setClaim(getClaim(eCon))
80
						.setId(eCon.attributeValue("id"))
81
						.setLabel(eCon.attributeValue("label"))
82
						.setParams(parseParams(eCon))
83
						.setConcepts(parseConcepts(eCon)))
84
				.collect(Collectors.toList());
85
	}
86

    
87
	private static Boolean getClaim(final Element eCon) {
88
		final String claim = eCon.attributeValue("claim");
89
		return BooleanUtils.toBooleanObject(StringUtils.isNotBlank(claim) ? claim : "false");
90
	}
91

    
92
	private static Map<String, List<Param>> parseParams(final Element e) {
93
		final List<Element> params = (List<Element>) e.selectNodes("./param");
94
		return params.stream()
95
				.map(p -> new Param()
96
						.setName(p.attributeValue("name"))
97
						.setValue(p.getTextTrim()))
98
				.collect(Collectors.toMap(
99
						Param::getName,
100
						Lists::newArrayList,
101
						(p1, p2) -> {
102
							List<Param> p = new ArrayList<>(p1);
103
							p.addAll(p2);
104
							return p;
105
						}));
106
	}
107

    
108
	public static FunderDetails asFunderDetails(final Context c) {
109
		return new FunderDetails()
110
				.setId(c.getId())
111
				.setName(c.getLabel())
112
				.setShortname(c.getId());
113
	}
114

    
115
}
(9-9/12)