1
|
package eu.dnetlib.data.bulktag;
|
2
|
|
3
|
import com.google.common.collect.Lists;
|
4
|
import com.google.common.collect.Maps;
|
5
|
import com.google.gson.Gson;
|
6
|
import org.apache.commons.lang3.StringUtils;
|
7
|
import org.apache.commons.logging.Log;
|
8
|
import org.apache.commons.logging.LogFactory;
|
9
|
import org.dom4j.Document;
|
10
|
import org.dom4j.DocumentException;
|
11
|
import org.dom4j.Node;
|
12
|
import org.dom4j.io.SAXReader;
|
13
|
|
14
|
import java.io.StringReader;
|
15
|
import java.util.ArrayList;
|
16
|
import java.util.List;
|
17
|
import java.util.Map;
|
18
|
|
19
|
/**
|
20
|
* Created by miriam on 03/08/2018.
|
21
|
*/
|
22
|
public class CommunityConfigurationFactory {
|
23
|
|
24
|
private static final Log log = LogFactory.getLog(CommunityConfigurationFactory.class);
|
25
|
|
26
|
public static CommunityConfiguration newInstance(final String xml) throws DocumentException {
|
27
|
|
28
|
log.debug(String.format("parsing community configuration from:\n%s", xml));
|
29
|
|
30
|
final Document doc = new SAXReader().read(new StringReader(xml));
|
31
|
|
32
|
final Map<String,Community> communities = Maps.newHashMap();
|
33
|
|
34
|
for(final Object o : doc.selectNodes("//community")) {
|
35
|
|
36
|
final Node node = (Node) o;
|
37
|
|
38
|
final Community community = parseCommunity(node);
|
39
|
|
40
|
if (community.isValid()) {
|
41
|
communities.put(community.getId(), community);
|
42
|
}
|
43
|
}
|
44
|
|
45
|
log.info(String.format("loaded %s community configuration profiles", communities.size()));
|
46
|
log.debug(String.format("loaded community configuration:\n%s", communities.toString()));
|
47
|
|
48
|
return new CommunityConfiguration(communities);
|
49
|
}
|
50
|
|
51
|
public static CommunityConfiguration fromJson(final String json) {
|
52
|
return new Gson().fromJson(json, CommunityConfiguration.class);
|
53
|
}
|
54
|
|
55
|
private static Community parseCommunity(final Node node) {
|
56
|
|
57
|
final Community c = new Community();
|
58
|
|
59
|
c.setId(node.valueOf("./@id"));
|
60
|
|
61
|
log.info(String.format("community id: %s", c.getId()));
|
62
|
|
63
|
c.setSubjects(parseSubjects(node));
|
64
|
c.setDatasources(parseDatasources(node));
|
65
|
c.setZenodoCommunities(parseZenodoCommunities(node));
|
66
|
|
67
|
return c;
|
68
|
}
|
69
|
|
70
|
private static List<String> parseSubjects(final Node node) {
|
71
|
|
72
|
final List<String> subjects = Lists.newArrayList();
|
73
|
|
74
|
final List <Node> list = node.selectNodes("./subjects/subject");
|
75
|
|
76
|
for(Node n : list){
|
77
|
log.debug("text of the node " + n.getText());
|
78
|
subjects.add(StringUtils.trim(n.getText()));
|
79
|
}
|
80
|
log.info("size of the subject list " + subjects.size());
|
81
|
return subjects;
|
82
|
}
|
83
|
|
84
|
private static List<Datasource> parseDatasources(final Node node) {
|
85
|
final List <Node> list = node.selectNodes("./datasources/datasource");
|
86
|
final List<Datasource> datasourceList = new ArrayList<>();
|
87
|
for(Node n : list){
|
88
|
Datasource d = new Datasource();
|
89
|
d.setOpenaireId(n.selectSingleNode("./openaireId").getText());
|
90
|
d.setSelCriteria(new SelectionCriteria(n.selectSingleNode("./selcriteria")));
|
91
|
datasourceList.add(d);
|
92
|
}
|
93
|
log.info("size of the datasource list " + datasourceList.size());
|
94
|
return datasourceList;
|
95
|
}
|
96
|
|
97
|
private static List<ZenodoCommunity> parseZenodoCommunities(final Node node) {
|
98
|
|
99
|
final List<Node> list = node.selectNodes("./zenodocommunities/zenodocommunity");
|
100
|
final List<ZenodoCommunity> zenodoCommunityList = new ArrayList<>();
|
101
|
for(Node n : list){
|
102
|
ZenodoCommunity zc = new ZenodoCommunity();
|
103
|
zc.setZenodoCommunityId(n.selectSingleNode("./zenodoid").getText());
|
104
|
zc.setSelCriteria(new SelectionCriteria(n.selectSingleNode("./selcriteria")));
|
105
|
|
106
|
zenodoCommunityList.add(zc);
|
107
|
}
|
108
|
log.info("size of the zenodo community list " + zenodoCommunityList.size());
|
109
|
return zenodoCommunityList;
|
110
|
}
|
111
|
|
112
|
|
113
|
|
114
|
|
115
|
}
|