Project

General

Profile

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
import org.springframework.beans.factory.annotation.Value;
34
import org.springframework.core.io.Resource;
35

    
36
public class UpdateIndexJobNode extends BlackboardJobNode {
37

    
38
	private static final Log log = LogFactory.getLog(UpdateIndexJobNode.class);
39

    
40
	private String eprParam;
41
	private String indexId;
42
	private String format;
43
	@Value("${service.index.solr.default.interpretation}")
44
	private String interpretation;
45
	private String layout;
46
	private String feedingType;
47
	private String defaultIndexId;
48

    
49
	@Autowired
50
	private ResultSetFactory resultSetFactory;
51

    
52
	@Autowired
53
	private ResultSetClient resultSetClient;
54

    
55
	@Autowired
56
	private TransformerFactory saxonTransformerFactory;
57

    
58
	/**
59
	 * Stylesheet which transforms a layout to another stylesheet which converts a input record to a index record.
60
	 */
61
	@Value("${service.index.layoutToRecordStylesheet}")
62
	private Resource layoutToRecordStylesheet;
63

    
64
	/**
65
	 * service locator.
66
	 */
67
	@Autowired
68
	private UniqueServiceLocator serviceLocator;
69

    
70
	@Override
71
	protected String obtainServiceId(final Env env) {
72
		return getServiceLocator().getServiceId(IndexService.class);
73
	}
74

    
75
	@Override
76
	protected void prepareJob(final BlackboardJob job, final Token token) throws ResultSetException, ISLookUpException, IOException, TransformerException {
77
		log.info("preparing blackboard job update index: " + getIndexId());
78

    
79
		final ResultSet<?> rs = token.getEnv().getAttribute(getEprParam(), ResultSet.class);
80

    
81
		final ResultSet<?> mappedRs = prepareForIndexing(rs, getFormat(), getInterpretation(), getLayout());
82

    
83
		token.setProgressProvider(new ResultsetProgressProvider(mappedRs, this.resultSetClient));
84

    
85
		job.setAction("FEED");
86
		job.getParameters().put("resultset_epr", mappedRs.toJson());
87
		job.getParameters().put("id", getIndexId());
88
		job.getParameters().put("feeding_type", getFeedingType());
89
		job.getParameters().put("backend_Id", this.defaultIndexId);
90
	}
91

    
92
	/**
93
	 * Transforms each mdstore record into a index record.
94
	 *
95
	 * @param resultSet
96
	 *            mdstore resulsetset
97
	 * @param layout
98
	 *            layout
99
	 * @param format
100
	 *            format
101
	 * @return resultset with transformed records
102
	 * @throws ISLookUpException
103
	 *             could happen
104
	 * @throws IOException
105
	 *             could happen
106
	 * @throws TransformerException
107
	 *             could happen
108
	 */
109
	protected ResultSet<String> prepareForIndexing(final ResultSet<?> resultSet, final String format, final String interpretation, final String layout)
110
			throws ISLookUpException, IOException, TransformerException {
111

    
112
		log.info("Using layout to record stylesheet: "+getLayoutToRecordStylesheet().getURI());
113
		final Transformer layoutTransformer = saxonTransformerFactory.newTransformer(new StreamSource(getLayoutToRecordStylesheet().getInputStream()));
114

    
115
		final DOMResult layoutToXsltXslt = new DOMResult();
116
		layoutTransformer.setParameter("format", format);
117
		layoutTransformer.transform(new StreamSource(new StringReader(getLayoutSource(format, interpretation, layout))), layoutToXsltXslt);
118

    
119
		dumpXslt(layoutToXsltXslt);
120

    
121
		return this.resultSetFactory.xsltMap(resultSet, new DOMSource(layoutToXsltXslt.getNode()));
122
	}
123

    
124
	private String getLayoutSource(final String format, final String interpretation, final String layout) throws ISLookUpDocumentNotFoundException, ISLookUpException {
125
		return this.serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(
126
				"collection('')//RESOURCE_PROFILE[.//RESOURCE_TYPE/@value = 'MDFormatDSResourceType' and .//NAME='" + format + "' and .//INTERPRETATION='"+interpretation+"']//LAYOUT[@name='" + layout
127
						+ "']");
128
	}
129

    
130
	private void dumpXslt(final DOMResult layoutToXsltXslt) throws TransformerConfigurationException, TransformerException {
131
		if (log.isDebugEnabled()) {
132
			final StringWriter buffer = new StringWriter();
133
			saxonTransformerFactory.newTransformer().transform(new DOMSource(layoutToXsltXslt.getNode()), new StreamResult(buffer));
134
			log.debug(buffer.toString());
135
		}
136
	}
137

    
138
	// setters and getters
139

    
140
	public String getIndexId() {
141
		return this.indexId;
142
	}
143

    
144
	public void setIndexId(final String indexId) {
145
		this.indexId = indexId;
146
	}
147

    
148
	public String getEprParam() {
149
		return this.eprParam;
150
	}
151

    
152
	public void setEprParam(final String eprParam) {
153
		this.eprParam = eprParam;
154
	}
155

    
156
	public String getFeedingType() {
157
		return this.feedingType;
158
	}
159

    
160
	public void setFeedingType(final String feedingType) {
161
		this.feedingType = feedingType;
162
	}
163

    
164
	public org.springframework.core.io.Resource getLayoutToRecordStylesheet() {
165
		return this.layoutToRecordStylesheet;
166
	}
167

    
168
	public void setLayoutToRecordStylesheet(final Resource layoutToRecordStylesheet) {
169
		this.layoutToRecordStylesheet = layoutToRecordStylesheet;
170
	}
171

    
172
	public String getFormat() {
173
		return this.format;
174
	}
175

    
176
	public void setFormat(final String format) {
177
		this.format = format;
178
	}
179

    
180
	public String getInterpretation() {
181
		return interpretation;
182
	}
183

    
184
	public void setInterpretation(final String interpretation) {
185
		this.interpretation = interpretation;
186
	}
187

    
188
	public String getLayout() {
189
		return this.layout;
190
	}
191

    
192
	public void setLayout(final String layout) {
193
		this.layout = layout;
194
	}
195

    
196
	/**
197
	 * @return the defaultIndexId
198
	 */
199
	public String getDefaultIndexId() {
200
		return this.defaultIndexId;
201
	}
202

    
203
	/**
204
	 * @param defaultIndexId
205
	 *            the defaultIndexId to set
206
	 */
207
	@Required
208
	public void setDefaultIndexId(final String defaultIndexId) {
209
		this.defaultIndexId = defaultIndexId;
210
	}
211

    
212
}
(5-5/6)