1
|
package eu.dnetlib.msro.workflows.nodes.index;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.io.StringReader;
|
5
|
import java.io.StringWriter;
|
6
|
import javax.xml.transform.Transformer;
|
7
|
import javax.xml.transform.TransformerConfigurationException;
|
8
|
import javax.xml.transform.TransformerException;
|
9
|
import javax.xml.transform.TransformerFactory;
|
10
|
import javax.xml.transform.dom.DOMResult;
|
11
|
import javax.xml.transform.dom.DOMSource;
|
12
|
import javax.xml.transform.stream.StreamResult;
|
13
|
import javax.xml.transform.stream.StreamSource;
|
14
|
|
15
|
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
|
16
|
import eu.dnetlib.enabling.resultset.client.ResultSetClient;
|
17
|
import eu.dnetlib.enabling.resultset.factory.ResultSetFactory;
|
18
|
import eu.dnetlib.enabling.tools.blackboard.BlackboardJob;
|
19
|
import eu.dnetlib.msro.workflows.nodes.BlackboardJobNode;
|
20
|
import eu.dnetlib.msro.workflows.procs.Env;
|
21
|
import eu.dnetlib.msro.workflows.procs.Token;
|
22
|
import eu.dnetlib.msro.workflows.util.ResultsetProgressProvider;
|
23
|
import eu.dnetlib.rmi.common.ResultSet;
|
24
|
import eu.dnetlib.rmi.common.ResultSetException;
|
25
|
import eu.dnetlib.rmi.enabling.ISLookUpDocumentNotFoundException;
|
26
|
import eu.dnetlib.rmi.enabling.ISLookUpException;
|
27
|
import eu.dnetlib.rmi.enabling.ISLookUpService;
|
28
|
import eu.dnetlib.rmi.provision.IndexService;
|
29
|
import org.apache.commons.logging.Log;
|
30
|
import org.apache.commons.logging.LogFactory;
|
31
|
import org.springframework.beans.factory.annotation.Autowired;
|
32
|
import org.springframework.beans.factory.annotation.Required;
|
33
|
|
34
|
public class UpdateIndexJobNode extends BlackboardJobNode {
|
35
|
|
36
|
private static final Log log = LogFactory.getLog(UpdateIndexJobNode.class);
|
37
|
|
38
|
private String eprParam;
|
39
|
private String indexId;
|
40
|
private String format;
|
41
|
private String layout;
|
42
|
private String feedingType;
|
43
|
private String defaultIndexId;
|
44
|
|
45
|
@Autowired
|
46
|
private ResultSetFactory resultSetFactory;
|
47
|
|
48
|
@Autowired
|
49
|
private ResultSetClient resultSetClient;
|
50
|
|
51
|
/**
|
52
|
* Stylesheet which transforms a layout to another stylesheet which converts a input record to a index record.
|
53
|
*/
|
54
|
private org.springframework.core.io.Resource layoutToRecordStylesheet;
|
55
|
|
56
|
/**
|
57
|
* service locator.
|
58
|
*/
|
59
|
@Autowired
|
60
|
private UniqueServiceLocator serviceLocator;
|
61
|
|
62
|
@Override
|
63
|
protected String obtainServiceId(final Env env) {
|
64
|
return getServiceLocator().getServiceId(IndexService.class);
|
65
|
}
|
66
|
|
67
|
@Override
|
68
|
protected void prepareJob(final BlackboardJob job, final Token token) throws ResultSetException, ISLookUpException, IOException, TransformerException {
|
69
|
log.info("preparing blackboard job update index: " + getIndexId());
|
70
|
|
71
|
final ResultSet<?> rs = token.getEnv().getAttribute(getEprParam(), ResultSet.class);
|
72
|
|
73
|
final ResultSet<?> mappedRs = prepareForIndexing(rs, getFormat(), getLayout());
|
74
|
|
75
|
token.setProgressProvider(new ResultsetProgressProvider(mappedRs, this.resultSetClient));
|
76
|
|
77
|
job.setAction("FEED");
|
78
|
job.getParameters().put("resultset_epr", mappedRs.toJson());
|
79
|
job.getParameters().put("id", getIndexId());
|
80
|
job.getParameters().put("feeding_type", getFeedingType());
|
81
|
job.getParameters().put("backend_Id", this.defaultIndexId);
|
82
|
}
|
83
|
|
84
|
/**
|
85
|
* Transforms each mdstore record into a index record.
|
86
|
*
|
87
|
* @param resultSet
|
88
|
* mdstore resulsetset
|
89
|
* @param layout
|
90
|
* layout
|
91
|
* @param format
|
92
|
* format
|
93
|
* @return resultset with transformed records
|
94
|
* @throws ISLookUpException
|
95
|
* could happen
|
96
|
* @throws IOException
|
97
|
* could happen
|
98
|
* @throws TransformerException
|
99
|
* could happen
|
100
|
*/
|
101
|
protected ResultSet<String> prepareForIndexing(final ResultSet<?> resultSet, final String format, final String layout)
|
102
|
throws ISLookUpException, IOException, TransformerException {
|
103
|
|
104
|
final TransformerFactory factory = TransformerFactory.newInstance();
|
105
|
final Transformer layoutTransformer = factory.newTransformer(new StreamSource(getLayoutToRecordStylesheet().getInputStream()));
|
106
|
|
107
|
final DOMResult layoutToXsltXslt = new DOMResult();
|
108
|
layoutTransformer.setParameter("format", format);
|
109
|
layoutTransformer.transform(new StreamSource(new StringReader(getLayoutSource(format, layout))), layoutToXsltXslt);
|
110
|
|
111
|
dumpXslt(factory, layoutToXsltXslt);
|
112
|
|
113
|
return this.resultSetFactory.xsltMap(resultSet, new DOMSource(layoutToXsltXslt.getNode()));
|
114
|
}
|
115
|
|
116
|
private String getLayoutSource(final String format, final String layout) throws ISLookUpDocumentNotFoundException, ISLookUpException {
|
117
|
return this.serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(
|
118
|
"collection('')//RESOURCE_PROFILE[.//RESOURCE_TYPE/@value = 'MDFormatDSResourceType' and .//NAME='" + format + "']//LAYOUT[@name='" + layout
|
119
|
+ "']");
|
120
|
}
|
121
|
|
122
|
private void dumpXslt(final TransformerFactory factory, final DOMResult layoutToXsltXslt) throws TransformerConfigurationException, TransformerException {
|
123
|
if (log.isDebugEnabled()) {
|
124
|
final StringWriter buffer = new StringWriter();
|
125
|
factory.newTransformer().transform(new DOMSource(layoutToXsltXslt.getNode()), new StreamResult(buffer));
|
126
|
log.debug(buffer.toString());
|
127
|
}
|
128
|
}
|
129
|
|
130
|
// setters and getters
|
131
|
|
132
|
public String getIndexId() {
|
133
|
return this.indexId;
|
134
|
}
|
135
|
|
136
|
public void setIndexId(final String indexId) {
|
137
|
this.indexId = indexId;
|
138
|
}
|
139
|
|
140
|
public String getEprParam() {
|
141
|
return this.eprParam;
|
142
|
}
|
143
|
|
144
|
public void setEprParam(final String eprParam) {
|
145
|
this.eprParam = eprParam;
|
146
|
}
|
147
|
|
148
|
public String getFeedingType() {
|
149
|
return this.feedingType;
|
150
|
}
|
151
|
|
152
|
public void setFeedingType(final String feedingType) {
|
153
|
this.feedingType = feedingType;
|
154
|
}
|
155
|
|
156
|
public org.springframework.core.io.Resource getLayoutToRecordStylesheet() {
|
157
|
return this.layoutToRecordStylesheet;
|
158
|
}
|
159
|
|
160
|
@Required
|
161
|
public void setLayoutToRecordStylesheet(final org.springframework.core.io.Resource layoutToRecordStylesheet) {
|
162
|
this.layoutToRecordStylesheet = layoutToRecordStylesheet;
|
163
|
}
|
164
|
|
165
|
public String getFormat() {
|
166
|
return this.format;
|
167
|
}
|
168
|
|
169
|
public void setFormat(final String format) {
|
170
|
this.format = format;
|
171
|
}
|
172
|
|
173
|
public String getLayout() {
|
174
|
return this.layout;
|
175
|
}
|
176
|
|
177
|
public void setLayout(final String layout) {
|
178
|
this.layout = layout;
|
179
|
}
|
180
|
|
181
|
/**
|
182
|
* @return the defaultIndexId
|
183
|
*/
|
184
|
public String getDefaultIndexId() {
|
185
|
return this.defaultIndexId;
|
186
|
}
|
187
|
|
188
|
/**
|
189
|
* @param defaultIndexId
|
190
|
* the defaultIndexId to set
|
191
|
*/
|
192
|
@Required
|
193
|
public void setDefaultIndexId(final String defaultIndexId) {
|
194
|
this.defaultIndexId = defaultIndexId;
|
195
|
}
|
196
|
|
197
|
}
|