Project

General

Profile

1
package eu.dnetlib.msro.openaireplus.workflows.nodes.index;
2

    
3
import java.io.IOException;
4
import java.io.InputStream;
5
import java.io.StringReader;
6
import java.io.StringWriter;
7
import javax.annotation.Resource;
8
import javax.xml.transform.Transformer;
9
import javax.xml.transform.TransformerException;
10
import javax.xml.transform.TransformerFactory;
11
import javax.xml.transform.stream.StreamResult;
12
import javax.xml.transform.stream.StreamSource;
13

    
14
import com.googlecode.sarasvati.Arc;
15
import com.googlecode.sarasvati.NodeToken;
16
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpDocumentNotFoundException;
17
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
18
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
19
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
20
import eu.dnetlib.miscutils.datetime.DateUtils;
21
import eu.dnetlib.miscutils.functional.hash.Hashing;
22
import eu.dnetlib.msro.workflows.nodes.SimpleJobNode;
23
import org.apache.commons.io.IOUtils;
24
import org.apache.commons.lang.StringUtils;
25
import org.apache.commons.logging.Log;
26
import org.apache.commons.logging.LogFactory;
27
import org.springframework.beans.factory.annotation.Required;
28
import org.springframework.core.io.ClassPathResource;
29

    
30
public class PrepareIndexDataJobNode extends SimpleJobNode {
31

    
32
	/**
33
	 * logger.
34
	 */
35
	private static final Log log = LogFactory.getLog(PrepareIndexDataJobNode.class); // NOPMD by marko on 11/24/08 5:02 PM
36

    
37
	@Resource
38
	private UniqueServiceLocator serviceLocator;
39

    
40
	/**
41
	 * Stylesheet which transforms a layout to another stylesheet which converts a input record to a index record.
42
	 */
43
	private String layoutToRecordStylesheet;
44

    
45
	private String outputRecordsPathParam;
46

    
47
	private String rottenRecordsPathParam;
48

    
49
	private String hbaseTable;
50

    
51
	private String oafSchemaLocationProperty;
52

    
53
	@Override
54
	protected String execute(final NodeToken token) throws Exception {
55

    
56
		log.info("start preparing job");
57

    
58
		final String xslt = prepareXslt(env("format", token), env("layout", token));
59

    
60
		token.getEnv().setAttribute("index.xslt", xslt);
61

    
62
		if (!StringUtils.isBlank(getOutputRecordsPathParam())) {
63
			token.getEnv().setAttribute(getOutputRecordsPathParam(), "/tmp" + getFileName(token, "indexrecords"));
64
		}
65
		if (!StringUtils.isBlank(getRottenRecordsPathParam())) {
66
			token.getEnv().setAttribute(getRottenRecordsPathParam(), "/tmp" + getFileName(token, "rottenrecords"));
67
		}
68

    
69
		token.getEnv().setAttribute("index.solr.url", getIndexSolrUrlZk());
70
		token.getEnv().setAttribute("index.solr.collection", getCollectionName(token));
71

    
72
		token.getEnv().setAttribute("index.shutdown.wait.time", getIndexSolrShutdownWait());
73
		token.getEnv().setAttribute("index.buffer.flush.threshold", getIndexBufferFlushTreshold());
74
		token.getEnv().setAttribute("index.solr.sim.mode", isFeedingSimulationMode());
75

    
76
		token.getEnv().setAttribute("index.feed.timestamp", DateUtils.now_ISO8601());
77

    
78
		token.getEnv().setAttribute(getOafSchemaLocationProperty(), getPropertyFetcher().getProperty(getOafSchemaLocationProperty()));
79

    
80
		return Arc.DEFAULT_ARC;
81
	}
82

    
83
	protected String tableName(final NodeToken token) {
84
		if (token.getEnv().hasAttribute("hbaseTable")) {
85
			String table = token.getEnv().getAttribute("hbaseTable");
86
			log.debug("found override value in wfEnv for 'hbaseTable' param: " + table);
87
			return table;
88
		}
89
		return getHbaseTable();
90
	}
91

    
92
	public String getIndexSolrUrlZk() throws ISLookUpException {
93
		return getServiceConfigValue("for $x in /RESOURCE_PROFILE[.//RESOURCE_TYPE/@value='IndexServiceResourceType'] return $x//PROTOCOL[./@name='solr']/@address/string()");
94
	}
95

    
96
	public String getIndexSolrShutdownWait() throws ISLookUpException {
97
		return queryForServiceProperty("solr:feedingShutdownTolerance");
98
	}
99

    
100
	public String getIndexBufferFlushTreshold() throws ISLookUpException {
101
		return queryForServiceProperty("solr:feedingBufferFlushThreshold");
102
	}
103

    
104
	public String isFeedingSimulationMode() throws ISLookUpException {
105
		return queryForServiceProperty("solr:feedingSimulationMode");
106
	}
107

    
108
	private String queryForServiceProperty(final String key) throws ISLookUpException {
109
		return getServiceConfigValue("for $x in /RESOURCE_PROFILE[.//RESOURCE_TYPE/@value='IndexServiceResourceType'] return $x//SERVICE_PROPERTIES/PROPERTY[./@ key='"
110
				+ key + "']/@value/string()");
111
	}
112

    
113
	private String getServiceConfigValue(final String xquery) throws ISLookUpException {
114
		log.debug("quering for service property: " + xquery);
115
		final String res = serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(xquery);
116
		if (StringUtils.isBlank(res)) { throw new IllegalStateException("unable to find unique service property, xquery: " + xquery); }
117
		return res;
118
	}
119

    
120
	private String getFileName(final NodeToken token, final String fileNamePrefix) {
121
		return "/" + fileNamePrefix + "_" + tableName(token) + "_" + token.getEnv().getAttribute("format") + ".seq";
122
	}
123

    
124
	private String getCollectionName(final NodeToken token) {
125
		return env("format", token) + "-" + env("layout", token) + "-" + env("interpretation", token);
126
	}
127

    
128
	private String env(final String s, final NodeToken token) {
129
		return token.getEnv().getAttribute(s);
130
	}
131

    
132
	/**
133
	 * Transforms each OAF record into a index record.
134
	 * 
135
	 * @param mdStoreRsetEpr
136
	 *            mdstore resulsetset
137
	 * @param layout
138
	 *            layout
139
	 * @param format
140
	 *            format
141
	 * @return resultset with transformed records
142
	 * @throws ISLookUpException
143
	 *             could happen
144
	 * @throws IOException
145
	 *             could happen
146
	 * @throws TransformerException
147
	 *             could happen
148
	 */
149
	protected String prepareXslt(final String format, final String layout) throws ISLookUpException, IOException, TransformerException {
150

    
151
		final TransformerFactory factory = TransformerFactory.newInstance();
152
		final Transformer layoutTransformer = factory.newTransformer(new StreamSource(new StringReader(readXslt(getLayoutToRecordStylesheet()))));
153

    
154
		final StreamResult layoutToXsltXslt = new StreamResult(new StringWriter());
155

    
156
		layoutTransformer.setParameter("format", format);
157
		layoutTransformer.transform(new StreamSource(new StringReader(getLayoutSource(format, layout))), layoutToXsltXslt);
158

    
159
		return new String(Hashing.encodeBase64(layoutToXsltXslt.getWriter().toString()));
160
	}
161

    
162
	private String readXslt(final String s) throws IOException {
163
		ClassPathResource resource = new ClassPathResource(s);
164
		InputStream inputStream = resource.getInputStream();
165
		return IOUtils.toString(inputStream);
166
	}
167

    
168
	private String getLayoutSource(final String format, final String layout) throws ISLookUpDocumentNotFoundException, ISLookUpException {
169
		return serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(
170
				"collection('')//RESOURCE_PROFILE[.//RESOURCE_TYPE/@value = 'MDFormatDSResourceType' and .//NAME='" + format + "']//LAYOUT[@name='" + layout
171
						+ "']");
172
	}
173

    
174
	public String getLayoutToRecordStylesheet() {
175
		return layoutToRecordStylesheet;
176
	}
177

    
178
	public void setLayoutToRecordStylesheet(final String layoutToRecordStylesheet) {
179
		this.layoutToRecordStylesheet = layoutToRecordStylesheet;
180
	}
181

    
182
	public String getHbaseTable() {
183
		return hbaseTable;
184
	}
185

    
186
	@Required
187
	public void setHbaseTable(final String hbaseTable) {
188
		this.hbaseTable = hbaseTable;
189
	}
190

    
191
	public String getOutputRecordsPathParam() {
192
		return outputRecordsPathParam;
193
	}
194

    
195
	public void setOutputRecordsPathParam(final String outputRecordsPathParam) {
196
		this.outputRecordsPathParam = outputRecordsPathParam;
197
	}
198

    
199
	public String getRottenRecordsPathParam() {
200
		return rottenRecordsPathParam;
201
	}
202

    
203
	public void setRottenRecordsPathParam(final String rottenRecordsPathParam) {
204
		this.rottenRecordsPathParam = rottenRecordsPathParam;
205
	}
206

    
207
	public String getOafSchemaLocationProperty() {
208
		return oafSchemaLocationProperty;
209
	}
210

    
211
	public void setOafSchemaLocationProperty(final String oafSchemaLocationProperty) {
212
		this.oafSchemaLocationProperty = oafSchemaLocationProperty;
213
	}
214

    
215
}
(5-5/6)