Project

General

Profile

« Previous | Next » 

Revision 33185

Added by Eri Katsari almost 10 years ago

updated xslt for concept to ommit newlines that were causing an issue with FET concept entries

View differences:

modules/dnet-openaire-stats/trunk/src/test/java/eu/dnetlib/data/mapreduce/hbase/statsExport/utils/ContextExporterTest.java
1
package eu.dnetlib.data.mapreduce.hbase.statsExport.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
import org.junit.Before;
12
import org.junit.Test;
13

  
14
import java.util.ArrayList;
15
import java.util.List;
16

  
17
public class ContextExporterTest {
18
    private ContextTransformer contextTransformer = new ContextTransformer();
19
    private String outputPath;
20
    private String url;
21

  
22
    private Logger log = Logger.getLogger(this.getClass());
23

  
24
    private ArrayList<String> context = new ArrayList<String>();
25
    private ArrayList<String> category = new ArrayList<String>();
26
    private ArrayList<String> concept = new ArrayList<String>();
27

  
28
    public ContextExporterTest() throws Exception {
29

  
30
    }
31

  
32
    @Before
33
    public void init() {
34
        url = "http://beta.services.openaire.eu:8280/is/services/isLookUp";
35
    }
36

  
37
    @Test
38
    public void readFromUrl() 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
            //  log.info("++++++++++++++");
49
            // log.info(res);
50

  
51
            // log.info("++++++++++++++");
52
            processData(res);
53

  
54
        }
55

  
56
        log.info(" ++++++++++++++concept ++++++++++++++");
57

  
58
        //  log.info(concept);
59

  
60

  
61
        log.info("Context  size " + context.size() +" and data " +context);
62

  
63

  
64
        log.info("category list size " + category.size() +" and data "+ category);
65

  
66
        // writeData(context, outputPath + "context");
67

  
68

  
69
//        writeData(category, outputPath + "category");
70

  
71
        //   writeData(concept, outputPath + "concept");
72

  
73
    }
74

  
75

  
76
    private List<String> getContextResouces(String url) throws ISLookUpException {
77
        ISLookUpService lookUpService;
78

  
79
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
80
        factory.setServiceClass(ISLookUpService.class);
81
        factory.setAddress(url);
82

  
83
        lookUpService = (ISLookUpService) factory.create();
84
//		for $x in //RESOURCE_PROFILE[.//RESOURCE_TYPE/@value='ContextDSResourceType']
85
//		[.//RESOURCE_KIND/@value='ContextDSResources'] return  $x
86
        return lookUpService.quickSearchProfile("//RESOURCE_PROFILE[.//RESOURCE_TYPE/@value='ContextDSResourceType'][.//RESOURCE_KIND/@value='ContextDSResources']");
87
    }
88

  
89

  
90
    private void processData(String data) throws Exception {
91
        try {
92

  
93
            String[] split = data.split("COPY");
94

  
95
            split[0] = split[0].replaceFirst("\n", "");
96
            context.add(split[0]);
97
            split[1] = split[1].replaceFirst("\n", "");
98
            if (split[1].endsWith("\n")) {
99
                split[1] = split[1].substring(0, split[1].lastIndexOf("\n"));
100
                log.info("split 1 : " + split[1] + "+++++++");
101
            }
102
            split[1] = split[1].replaceFirst("\n", "");
103
            category.add(split[1]);
104

  
105

  
106
            split[2] = split[2].replaceFirst("\n", "");
107

  
108
            concept.add(split[2]);
109

  
110
        } catch (Exception e) {
111
            String msg = " Unable to create file with context, " + "concept and category values in output path " + outputPath + ". Reason: ";
112
            log.error(msg);
113
            throw new Exception(msg, e);
114
        }
115

  
116
    }
117

  
118
    private void writeData(String data, String destination) throws Exception {
119

  
120
        FSDataOutputStream fin = null;
121
        try {
122

  
123
            data = data.replaceAll("\n\n", "");
124
            data = data.substring(0, data.lastIndexOf("\n"));
125
            if (data.endsWith("\n")) {
126
                data = data.substring(0, data.lastIndexOf("\n"));
127
            }
128
            log.info("***********************Writing data:***********************\n" + data);
129
            log.info("***********************  data:***********************\n");
130
            FileSystem fs = FileSystem.get(new Configuration());
131
            fin = fs.create(new Path(destination), true);
132

  
133
            fin.writeUTF(data);
134

  
135
        } catch (Exception e) {
136
            log.error("Failed  to write exported data to a file : ", e);
137
            throw new Exception("Failed  to write exported data to a file : " + e.toString(), e);
138

  
139
        } finally {
140

  
141
            fin.close();
142

  
143
        }
144
    }
145

  
146
    public String getOutputPath() {
147
        return outputPath;
148
    }
149

  
150
    public void setOutputPath(String outputPath) {
151
        this.outputPath = outputPath;
152
    }
153

  
154
}

Also available in: Unified diff