1
|
package eu.dnetlib.msro.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 eu.dnetlib.enabling.locators.UniqueServiceLocator;
|
15
|
import eu.dnetlib.miscutils.datetime.DateUtils;
|
16
|
import eu.dnetlib.miscutils.functional.hash.Hashing;
|
17
|
import eu.dnetlib.msro.workflows.graph.Arc;
|
18
|
import eu.dnetlib.msro.workflows.nodes.SimpleJobNode;
|
19
|
import eu.dnetlib.msro.workflows.procs.Env;
|
20
|
import eu.dnetlib.rmi.enabling.ISLookUpDocumentNotFoundException;
|
21
|
import eu.dnetlib.rmi.enabling.ISLookUpException;
|
22
|
import eu.dnetlib.rmi.enabling.ISLookUpService;
|
23
|
import org.apache.commons.io.IOUtils;
|
24
|
import org.apache.commons.lang3.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
|
public static final String SEPARATOR = "_";
|
33
|
/**
|
34
|
* logger.
|
35
|
*/
|
36
|
private static final Log log = LogFactory.getLog(PrepareIndexDataJobNode.class); // NOPMD by marko on 11/24/08 5:02 PM
|
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 oafSchemaLocation;
|
52
|
|
53
|
@Override
|
54
|
protected String execute(final Env env) throws Exception {
|
55
|
|
56
|
log.info("start preparing job");
|
57
|
|
58
|
final String xslt = prepareXslt(env("format", env), env("layout", env));
|
59
|
|
60
|
env.setAttribute("index.xslt", xslt);
|
61
|
|
62
|
if (!StringUtils.isBlank(getOutputRecordsPathParam())) {
|
63
|
env.setAttribute(getOutputRecordsPathParam(), "/tmp" + getFileName(env, "indexrecords"));
|
64
|
}
|
65
|
if (!StringUtils.isBlank(getRottenRecordsPathParam())) {
|
66
|
env.setAttribute(getRottenRecordsPathParam(), "/tmp" + getFileName(env, "rottenrecords"));
|
67
|
}
|
68
|
|
69
|
env.setAttribute("index.solr.url", getIndexSolrUrlZk());
|
70
|
env.setAttribute("index.solr.collection", getCollectionName(env));
|
71
|
|
72
|
env.setAttribute("index.shutdown.wait.time", getIndexSolrShutdownWait());
|
73
|
env.setAttribute("index.buffer.flush.threshold", getIndexBufferFlushTreshold());
|
74
|
env.setAttribute("index.solr.sim.mode", isFeedingSimulationMode());
|
75
|
|
76
|
env.setAttribute("index.feed.timestamp", DateUtils.now_ISO8601());
|
77
|
|
78
|
env.setAttribute("", this.oafSchemaLocation); // TODO : inserire il nome della property
|
79
|
|
80
|
return Arc.DEFAULT_ARC;
|
81
|
}
|
82
|
|
83
|
public String getIndexSolrUrlZk() throws ISLookUpException {
|
84
|
return getServiceConfigValue(
|
85
|
"for $x in /RESOURCE_PROFILE[.//RESOURCE_TYPE/@value='IndexServiceResourceType'] return $x//PROTOCOL[./@name='solr']/@address/string()");
|
86
|
}
|
87
|
|
88
|
public String getIndexSolrShutdownWait() throws ISLookUpException {
|
89
|
return queryForServiceProperty("solr:feedingShutdownTolerance");
|
90
|
}
|
91
|
|
92
|
public String getIndexBufferFlushTreshold() throws ISLookUpException {
|
93
|
return queryForServiceProperty("solr:feedingBufferFlushThreshold");
|
94
|
}
|
95
|
|
96
|
public String isFeedingSimulationMode() throws ISLookUpException {
|
97
|
return queryForServiceProperty("solr:feedingSimulationMode");
|
98
|
}
|
99
|
|
100
|
private String queryForServiceProperty(final String key) throws ISLookUpException {
|
101
|
return getServiceConfigValue(
|
102
|
"for $x in /RESOURCE_PROFILE[.//RESOURCE_TYPE/@value='IndexServiceResourceType'] return $x//SERVICE_PROPERTIES/PROPERTY[./@ key='"
|
103
|
+ key + "']/@value/string()");
|
104
|
}
|
105
|
|
106
|
private String getServiceConfigValue(final String xquery) throws ISLookUpException {
|
107
|
log.debug("quering for service property: " + xquery);
|
108
|
final String res = this.serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(xquery);
|
109
|
if (StringUtils.isBlank(res)) { throw new IllegalStateException("unable to find unique service property, xquery: " + xquery); }
|
110
|
return res;
|
111
|
}
|
112
|
|
113
|
private String getFileName(final Env env, final String fileNamePrefix) {
|
114
|
return "/" + fileNamePrefix + "_" + getHbaseTable() + "_" + env.getAttribute("format") + ".seq";
|
115
|
}
|
116
|
|
117
|
private String getCollectionName(final Env env) {
|
118
|
return env("format", env) + SEPARATOR + env("layout", env) + SEPARATOR + env("interpretation", env);
|
119
|
}
|
120
|
|
121
|
private String env(final String s, final Env env) {
|
122
|
return env.getAttribute(s, String.class);
|
123
|
}
|
124
|
|
125
|
protected String prepareXslt(final String format, final String layout) throws ISLookUpException, IOException, TransformerException {
|
126
|
|
127
|
final TransformerFactory factory = TransformerFactory.newInstance();
|
128
|
final Transformer layoutTransformer = factory.newTransformer(new StreamSource(new StringReader(readXslt(getLayoutToRecordStylesheet()))));
|
129
|
|
130
|
final StreamResult layoutToXsltXslt = new StreamResult(new StringWriter());
|
131
|
|
132
|
layoutTransformer.setParameter("format", format);
|
133
|
layoutTransformer.transform(new StreamSource(new StringReader(getLayoutSource(format, layout))), layoutToXsltXslt);
|
134
|
|
135
|
return new String(Hashing.encodeBase64(layoutToXsltXslt.getWriter().toString()));
|
136
|
}
|
137
|
|
138
|
private String readXslt(final String s) throws IOException {
|
139
|
final ClassPathResource resource = new ClassPathResource(s);
|
140
|
final InputStream inputStream = resource.getInputStream();
|
141
|
return IOUtils.toString(inputStream);
|
142
|
}
|
143
|
|
144
|
private String getLayoutSource(final String format, final String layout) throws ISLookUpDocumentNotFoundException, ISLookUpException {
|
145
|
return this.serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(
|
146
|
"collection('')//RESOURCE_PROFILE[.//RESOURCE_TYPE/@value = 'MDFormatDSResourceType' and .//NAME='" + format + "']//LAYOUT[@name='" + layout
|
147
|
+ "']");
|
148
|
}
|
149
|
|
150
|
public String getLayoutToRecordStylesheet() {
|
151
|
return this.layoutToRecordStylesheet;
|
152
|
}
|
153
|
|
154
|
public void setLayoutToRecordStylesheet(final String layoutToRecordStylesheet) {
|
155
|
this.layoutToRecordStylesheet = layoutToRecordStylesheet;
|
156
|
}
|
157
|
|
158
|
public String getHbaseTable() {
|
159
|
return this.hbaseTable;
|
160
|
}
|
161
|
|
162
|
@Required
|
163
|
public void setHbaseTable(final String hbaseTable) {
|
164
|
this.hbaseTable = hbaseTable;
|
165
|
}
|
166
|
|
167
|
public String getOutputRecordsPathParam() {
|
168
|
return this.outputRecordsPathParam;
|
169
|
}
|
170
|
|
171
|
public void setOutputRecordsPathParam(final String outputRecordsPathParam) {
|
172
|
this.outputRecordsPathParam = outputRecordsPathParam;
|
173
|
}
|
174
|
|
175
|
public String getRottenRecordsPathParam() {
|
176
|
return this.rottenRecordsPathParam;
|
177
|
}
|
178
|
|
179
|
public void setRottenRecordsPathParam(final String rottenRecordsPathParam) {
|
180
|
this.rottenRecordsPathParam = rottenRecordsPathParam;
|
181
|
}
|
182
|
|
183
|
public void setOafSchemaLocation(final String oafSchemaLocation) {
|
184
|
this.oafSchemaLocation = oafSchemaLocation;
|
185
|
}
|
186
|
}
|