1
|
package eu.dnetlib.msro.openaireplus.workflows.nodes;
|
2
|
|
3
|
import java.util.List;
|
4
|
|
5
|
import com.googlecode.sarasvati.NodeToken;
|
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.miscutils.datetime.DateUtils;
|
10
|
import org.apache.commons.logging.Log;
|
11
|
import org.apache.commons.logging.LogFactory;
|
12
|
import org.springframework.beans.factory.annotation.Autowired;
|
13
|
|
14
|
public class IncrementalTransformationJobNode extends IncrementalOperationJobNode {
|
15
|
|
16
|
private static final Log log = LogFactory.getLog(IncrementalTransformationJobNode.class);
|
17
|
|
18
|
@Autowired
|
19
|
private UniqueServiceLocator locator;
|
20
|
|
21
|
private static final DateUtils dateUtils = new DateUtils();
|
22
|
|
23
|
@Override
|
24
|
protected boolean forceRefresh(final NodeToken nodeToken, final Long lastSuccessStartDate, final String currentWfProfileId) throws Exception {
|
25
|
if (lastSuccessStartDate < 0) {
|
26
|
nodeToken.getFullEnv().setAttribute("OperationTypeInfo", "Last success start date < 0, transformation forced to REFRESH");
|
27
|
nodeToken.getFullEnv().setAttribute("operationType", "REFRESH");
|
28
|
return true;
|
29
|
}
|
30
|
String trRuleId = getTransformationId(currentWfProfileId);
|
31
|
final long lastUpdateDate = getLastTransformationRuleUpdate(trRuleId);
|
32
|
log.info(String.format("Last update date of the transformation rule with id %s is %s", trRuleId, DateUtils.calculate_ISO8601(lastUpdateDate)));
|
33
|
|
34
|
if (lastUpdateDate > lastSuccessStartDate) {
|
35
|
nodeToken.getFullEnv().setAttribute("OperationTypeInfo", "Transformation Rule has been updated, transformation forced to REFRESH");
|
36
|
nodeToken.getFullEnv().setAttribute("operationType", "REFRESH");
|
37
|
return true;
|
38
|
}
|
39
|
return false;
|
40
|
}
|
41
|
|
42
|
|
43
|
|
44
|
|
45
|
|
46
|
private String getTransformationId(final String workflowId) throws ISLookUpException {
|
47
|
|
48
|
final String query="for $x in collection('/db/DRIVER/WorkflowDSResources/WorkflowDSResourceType') where $x//RESOURCE_IDENTIFIER/@value='%s' " +
|
49
|
"return $x//PARAM[./@category='TRANSFORMATION_RULE_ID']/text()";
|
50
|
final ISLookUpService lookUpService = locator.getService(ISLookUpService.class);
|
51
|
final String queryInstance = String.format(query, workflowId);
|
52
|
log.debug("Query to find the Transformation Rule");
|
53
|
List<String> transformationId = lookUpService.quickSearchProfile(queryInstance);
|
54
|
if(transformationId== null || transformationId.isEmpty())
|
55
|
throw new RuntimeException("Error unable to find the Transformation rule ID on workflow profile "+workflowId);
|
56
|
return transformationId.get(0);
|
57
|
}
|
58
|
|
59
|
private Long getLastTransformationRuleUpdate(final String trId) throws ISLookUpException {
|
60
|
final String query = "for $x in collection('/db/DRIVER/TransformationRuleDSResources/TransformationRuleDSResourceType') where $x//RESOURCE_IDENTIFIER/@value='%s' return $x//DATE_OF_CREATION/@value/string()";
|
61
|
log.debug("retrieve creation date from transformation ID "+trId);
|
62
|
final ISLookUpService lookUpService = locator.getService(ISLookUpService.class);
|
63
|
final String queryInstance = String.format(query, trId);
|
64
|
log.debug("Query to find the Transformation Rule");
|
65
|
List<String> currentDate = lookUpService.quickSearchProfile(queryInstance);
|
66
|
if(currentDate== null || currentDate.isEmpty())
|
67
|
throw new RuntimeException("Error unable to find the creation date of the Transformation rule "+trId);
|
68
|
return dateUtils.parse(currentDate.get(0)).getTime();
|
69
|
}
|
70
|
|
71
|
}
|