Project

General

Profile

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

    
3
import java.io.BufferedWriter;
4
import java.io.FileOutputStream;
5
import java.io.OutputStreamWriter;
6
import java.io.Writer;
7

    
8
import org.apache.log4j.Logger;
9

    
10
public class ContextExporter {
11
	private String outputPath;
12
	private String contextMap;
13
	private boolean readFromURL = false;
14
	private ContextTransformer contextTransformer;
15

    
16
	private Logger log = Logger.getLogger(this.getClass());
17

    
18
	public ContextExporter(String outputPath, String contextMap, boolean readFromURL) {
19
		this.outputPath = outputPath;
20
		this.contextMap = contextMap;
21
		this.readFromURL = readFromURL;
22
		contextTransformer = new ContextTransformer();
23
		if (!outputPath.endsWith("/")) {
24
			outputPath += "/";
25
		}
26
	}
27

    
28
	public void exportToFile() throws Exception {
29
		try {
30
			String data = contextTransformer.transformXSL(contextMap);
31
			String[] split = data.split("COPY");
32
			log.info("Importing context, concept and category...");
33
			split[0]=split[0].replaceFirst("","");
34
			writeData(split[0], "context");
35
			writeData(split[1], "category");
36
			writeData(split[2], "concept");
37
		} catch (Exception e) {
38
			String msg = " Unable to create file with context, " + "concept and category values in output path " + outputPath + ". Reason: ";
39
			log.error(msg + e);
40
			throw new Exception(msg, e);
41
		}
42

    
43
	}
44

    
45
	private void writeData(String data, String name) throws Exception {
46
		Writer fileWriter = null;
47
		try {
48
			data=data.replaceFirst("\n","");
49
			fileWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputPath + name + ".txt"), "UTF8"));
50
			fileWriter.write(data);
51

    
52
		} catch (Exception e) {
53
			log.error(e);
54

    
55
			throw new Exception("Failed  to write exported data to a file", e);
56

    
57
		} finally {
58

    
59
			fileWriter.close();
60
		}
61
	}
62

    
63
	public String getOutputPath() {
64
		return outputPath;
65
	}
66

    
67
	public void setOutputPath(String outputPath) {
68
		this.outputPath = outputPath;
69
	}
70

    
71
	public String getContextMap() {
72
		return contextMap;
73
	}
74

    
75
	public void setContextMap(String contextMap) {
76
		this.contextMap = contextMap;
77
	}
78

    
79
	public boolean isReadFromURL() {
80
		return readFromURL;
81
	}
82

    
83
	public void setReadFromURL(boolean readFromURL) {
84
		this.readFromURL = readFromURL;
85
	}
86

    
87
}
(1-1/4)