Project

General

Profile

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

    
3
import com.googlecode.sarasvati.NodeToken;
4
import eu.dnetlib.data.provision.index.rmi.IndexService;
5
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpDocumentNotFoundException;
6
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
7
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
8
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
9
import eu.dnetlib.enabling.tools.blackboard.BlackboardJob;
10
import eu.dnetlib.functionality.index.solr.feed.InputDocumentFactory;
11
import eu.dnetlib.msro.rmi.MSROException;
12
import eu.dnetlib.msro.workflows.nodes.BlackboardJobNode;
13
import org.apache.commons.lang.StringUtils;
14
import org.apache.commons.logging.Log;
15
import org.apache.commons.logging.LogFactory;
16
import org.springframework.beans.factory.annotation.Autowired;
17

    
18
import static java.lang.String.format;
19

    
20
public class FinalizeIndexJobNode extends BlackboardJobNode {
21

    
22
	private static final Log log = LogFactory.getLog(FinalizeIndexJobNode.class); // NOPMD by marko on 11/24/08 5:02 PM
23

    
24
	@Autowired
25
	private UniqueServiceLocator serviceLocator;
26

    
27
	@Override
28
	protected String obtainServiceId(final NodeToken token) {
29
		return getServiceLocator().getServiceId(IndexService.class);
30
	}
31

    
32
	@Override
33
	protected void prepareJob(final BlackboardJob job, final NodeToken token) throws Exception {
34
		final String indexDsId = getEnvParam(token, "index_id");
35

    
36
		log.info("preparing blackboard job DELETE_BY_QUERY index: " + indexDsId);
37

    
38
		final String backendId = getBackendId(indexDsId);
39
		if (StringUtils.isBlank(backendId))
40
			throw new MSROException("empty index backend Id");
41

    
42
		job.setAction("DELETE_BY_QUERY");
43
		job.getParameters().put("id", indexDsId);
44
		job.getParameters().put("backend_Id", backendId);
45
		job.getParameters().put("query", buildQuery(getEnvParam(token, "index.feed.timestamp")));
46
	}
47

    
48
	private String buildQuery(final String version) {
49
		final String query =
50
				String.format("__dsversion:{* TO %s}", InputDocumentFactory.getParsedDateField(version));
51

    
52
		log.info("delete by query: " + query);
53

    
54
		return query;
55
	}
56

    
57
	private String getEnvParam(final NodeToken token, final String name) throws MSROException {
58
		final String value = token.getEnv().getAttribute(name);
59

    
60
		if (StringUtils.isBlank(value))
61
			throw new MSROException(format("unable to finalize index feeding, cannot find property '%s' in the workflow env.", name));
62

    
63
		return value;
64
	}
65

    
66
	public String getBackendId(final String indexDsId) throws ISLookUpDocumentNotFoundException, ISLookUpException {
67
		return getServiceLocator().getService(ISLookUpService.class).getResourceProfileByQuery(
68
				"//RESOURCE_PROFILE[.//RESOURCE_IDENTIFIER/@value='" + indexDsId + "']//BACKEND/text()");
69
	}
70

    
71
}
(4-4/10)