Project

General

Profile

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 com.google.gson.GsonBuilder;
7
import eu.dnetlib.data.bulktag.selectioncriteria.InterfaceAdapter;
8
import eu.dnetlib.data.bulktag.selectioncriteria.Selection;
9
import eu.dnetlib.data.bulktag.selectioncriteria.VerbResolver;
10
import eu.dnetlib.data.bulktag.selectioncriteria.VerbResolverFactory;
11
import org.apache.commons.lang3.StringUtils;
12
import org.apache.commons.logging.Log;
13
import org.apache.commons.logging.LogFactory;
14
import org.dom4j.Document;
15
import org.dom4j.DocumentException;
16
import org.dom4j.Node;
17
import org.dom4j.io.SAXReader;
18

    
19
import java.io.StringReader;
20
import java.util.ArrayList;
21
import java.util.List;
22
import java.util.Map;
23

    
24
/**
25
 * Created by miriam on 03/08/2018.
26
 */
27
public class CommunityConfigurationFactory {
28

    
29
    private static final Log log = LogFactory.getLog(CommunityConfigurationFactory.class);
30

    
31
    private static VerbResolver resolver = VerbResolverFactory.newInstance();
32

    
33
    public static CommunityConfiguration newInstance(final String xml) throws DocumentException {
34

    
35
        log.debug(String.format("parsing community configuration from:\n%s", xml));
36

    
37
        final Document doc = new SAXReader().read(new StringReader(xml));
38

    
39
        final Map<String,Community> communities = Maps.newHashMap();
40

    
41
        for(final Object o : doc.selectNodes("//community")) {
42

    
43
            final Node node = (Node) o;
44

    
45
            final Community community = parseCommunity(node);
46

    
47
            if (community.isValid()) {
48
                communities.put(community.getId(), community);
49
            }
50
        }
51

    
52
        log.info(String.format("loaded %s community configuration profiles", communities.size()));
53
        log.debug(String.format("loaded community configuration:\n%s", communities.toString()));
54

    
55

    
56
            return new CommunityConfiguration(communities);
57
    }
58

    
59
    public static CommunityConfiguration fromJson(final String json) {
60
        GsonBuilder builder = new GsonBuilder();
61
        builder.registerTypeAdapter(Selection.class, new InterfaceAdapter());
62
        Gson gson = builder.create();
63
        final CommunityConfiguration conf = gson.fromJson(json, CommunityConfiguration.class);
64
        log.info(String.format("loaded %s community configuration profiles", conf.size()));
65
        conf.init();
66
        log.info("created inverse maps");
67

    
68
        return conf;
69
    }
70

    
71
    private static Community parseCommunity(final Node node) {
72

    
73
        final Community c = new Community();
74

    
75
        c.setId(node.valueOf("./@id"));
76

    
77
        log.info(String.format("community id: %s", c.getId()));
78

    
79
        c.setSubjects(parseSubjects(node));
80
        c.setDatasources(parseDatasources(node));
81
        c.setZenodoCommunities(parseZenodoCommunities(node));
82
        c.setOrganizationCommunity(parseOrganizationCommunity(node));
83
        return c;
84
    }
85

    
86
    private static List<String> parseSubjects(final Node node) {
87

    
88
        final List<String> subjects = Lists.newArrayList();
89

    
90
        final List <Node> list = node.selectNodes("./subjects/subject");
91

    
92
        for(Node n : list){
93
            log.debug("text of the node " + n.getText());
94
            subjects.add(StringUtils.trim(n.getText()));
95
        }
96
        log.info("size of the subject list " + subjects.size());
97
        return subjects;
98
    }
99

    
100
    private static List<Organization> parseOrganizationCommunity(final Node node){
101
        final List <Node> list = node.selectNodes("./organizations/organization");
102
        final List<Organization> organizationList = new ArrayList<>();
103
        for(Node n : list){
104
            Organization o = new Organization();
105
            o.setOrganizationId(n.selectSingleNode("./organizationId").getText());
106
            o.setSelCriteria(n.selectSingleNode("./selcriteria"));
107
            organizationList.add(o);
108
        }
109
        log.info("size of the datasource list " + organizationList.size());
110
        return organizationList;
111
    }
112

    
113
    private static List<Datasource> parseDatasources(final Node node) {
114
        final List <Node> list = node.selectNodes("./datasources/datasource");
115
        final List<Datasource> datasourceList = new ArrayList<>();
116
        for(Node n : list){
117
            Datasource d = new Datasource();
118
            d.setOpenaireId(n.selectSingleNode("./openaireId").getText());
119
            d.setSelCriteria(n.selectSingleNode("./selcriteria"),resolver);
120
            datasourceList.add(d);
121
        }
122
        log.info("size of the datasource list " + datasourceList.size());
123
        return datasourceList;
124
    }
125

    
126
    private static List<ZenodoCommunity> parseZenodoCommunities(final Node node) {
127
        final Node oacommunitynode = node.selectSingleNode("./oacommunity");
128
        String oacommunity = null;
129
        if (oacommunitynode != null){
130
            String tmp = oacommunitynode.getText();
131
            if(StringUtils.isNotBlank(tmp))
132
                oacommunity = tmp;
133
        }
134

    
135

    
136
        final List<Node> list = node.selectNodes("./zenodocommunities/zenodocommunity");
137
        final List<ZenodoCommunity> zenodoCommunityList = new ArrayList<>();
138
        for(Node n : list){
139
            ZenodoCommunity zc = new ZenodoCommunity();
140
            zc.setZenodoCommunityId(n.selectSingleNode("./zenodoid").getText());
141
            zc.setSelCriteria(n.selectSingleNode("./selcriteria"));
142

    
143
            zenodoCommunityList.add(zc);
144
        }
145
        if(oacommunity != null){
146
            ZenodoCommunity zc = new ZenodoCommunity();
147
            zc.setZenodoCommunityId(oacommunity);
148
            zenodoCommunityList.add(zc);
149
        }
150
        log.info("size of the zenodo community list " + zenodoCommunityList.size());
151
        return zenodoCommunityList;
152
    }
153

    
154

    
155

    
156

    
157
}
(3-3/10)