Project

General

Profile

1
package eu.dnetlib.data.mapreduce.hbase.lodExport.utils;
2

    
3
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
4
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
5
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
6
import org.apache.hadoop.conf.Configuration;
7
import org.apache.hadoop.fs.FSDataOutputStream;
8
import org.apache.hadoop.fs.FileSystem;
9
import org.apache.hadoop.fs.Path;
10
import org.apache.log4j.Logger;
11

    
12
import java.util.ArrayList;
13
import java.util.List;
14

    
15
public class ContextExporter {
16
    private ContextTransformer contextTransformer = new ContextTransformer();
17
    private String outputPath;
18
    private Logger log = Logger.getLogger(this.getClass());
19

    
20

    
21
    private ArrayList<String> context = new ArrayList<String>();
22
    private ArrayList<String> category = new ArrayList<String>();
23
    private ArrayList<String> concept = new ArrayList<String>();
24

    
25
    public ContextExporter(String outputPath, String contextMap, boolean readFromUrl) throws Exception {
26
        if (!outputPath.endsWith("/")) {
27
            outputPath += "/";
28
        }
29
        this.outputPath = outputPath;
30
        if (readFromUrl) {
31
            readFromUrl(contextMap);
32
        } else {
33
            readFromBuffer(contextMap);
34
        }
35

    
36
    }
37

    
38
    public void readFromUrl(String url) throws Exception {
39

    
40
        List<String> concepts = getContextResouces(url);
41
        log.info("Returned concept  " + concepts.size()
42
        );
43

    
44
        for (String data : concepts) {
45
            log.info("++++++++++++++ Transforming concept data ");
46
            String res = contextTransformer.transformXSL(data);
47

    
48
            processData(res);
49
        }
50

    
51
        writeData(this.context, "context");
52
        writeData(this.category, "category");
53
        writeData(this.concept, "concept");
54

    
55

    
56
    }
57

    
58
    private void readFromBuffer(String contextMap) throws Exception {
59

    
60
        if (contextMap == null || contextMap.isEmpty()) {
61
            log.error("Context Resources file is empty.");
62
            throw new Exception("Context Resources file is empty.");
63
        }
64

    
65
        String data = contextTransformer.transformXSL(contextMap);
66

    
67
        log.info(data);
68
        processData(data);
69
    }
70

    
71
    private List<String> getContextResouces(String url) throws ISLookUpException {
72
        ISLookUpService lookUpService;
73

    
74
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
75
        factory.setServiceClass(ISLookUpService.class);
76
        factory.setAddress(url);
77

    
78
        lookUpService = (ISLookUpService) factory.create();
79
//		for $x in //RESOURCE_PROFILE[.//RESOURCE_TYPE/@value='ContextDSResourceType']
80
//		[.//RESOURCE_KIND/@value='ContextDSResources'] return  $x
81
        return lookUpService.quickSearchProfile("//RESOURCE_PROFILE[.//RESOURCE_TYPE/@value='ContextDSResourceType'][.//RESOURCE_KIND/@value='ContextDSResources']");
82
    }
83

    
84

    
85
    private void writeData(ArrayList<String> dataList, String listName) throws Exception {
86
        if (dataList.isEmpty()) return;
87
        log.info(listName + "  size " + dataList.size());
88

    
89
        String data = new String();
90
        for (int i = 0; i < dataList.size(); i++) {
91

    
92
            data += dataList.get(i);
93

    
94

    
95
        }
96

    
97

    
98
        data = data.substring(0, data.lastIndexOf("\n"));
99

    
100

    
101
        flushString(data, outputPath + listName);
102

    
103

    
104
    }
105

    
106
    private void processData(String data) throws Exception {
107
        try {
108

    
109
            String[] split = data.split("COPY\n");
110

    
111
            if (split.length > 0) {
112
                context.add(split[0]);
113
            }
114

    
115

    
116
            if (split.length > 1) {
117
                category.add(split[1]);
118
            }
119

    
120
            if (split.length > 2) {
121

    
122
                concept.add(split[2]);
123
            }
124

    
125
        } catch (Exception e) {
126
            String msg = " Unable to create file with context, " + "concept and category values in output path " + outputPath + ". Reason: ";
127
            log.error(msg);
128
            throw new Exception(msg, e);
129
        }
130

    
131
    }
132

    
133
    private void flushString(String data, String destination) throws Exception {
134

    
135
        FSDataOutputStream fin = null;
136
        try {
137

    
138

    
139
            log.info("***********************Writing data:***********************\n" + data);
140
            log.info("***********************  data:***********************\n");
141
            FileSystem fs = FileSystem.get(new Configuration());
142
            fin = fs.create(new Path(destination), true);
143

    
144
            fin.write(data.getBytes());
145

    
146
        } catch (Exception e) {
147
            log.error("Failed  to write exported data to a file : ", e);
148
            throw new Exception("Failed  to write exported data to a file : " + e.toString(), e);
149

    
150
        } finally {
151

    
152
            fin.close();
153

    
154
        }
155
    }
156

    
157
    public String getOutputPath() {
158
        return outputPath;
159
    }
160

    
161
    public void setOutputPath(String outputPath) {
162
        this.outputPath = outputPath;
163
    }
164

    
165
}
(1-1/5)