Project

General

Profile

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

    
3
import java.util.Iterator;
4
import java.util.Map;
5

    
6
import com.googlecode.sarasvati.Arc;
7
import com.googlecode.sarasvati.NodeToken;
8
import eu.dnetlib.common.logging.DnetLogger;
9
import eu.dnetlib.miscutils.datetime.DateUtils;
10
import eu.dnetlib.msro.rmi.MSROException;
11
import eu.dnetlib.msro.workflows.nodes.SimpleJobNode;
12
import eu.dnetlib.msro.workflows.util.WorkflowsConstants;
13
import org.apache.commons.lang.math.NumberUtils;
14
import org.apache.commons.logging.Log;
15
import org.apache.commons.logging.LogFactory;
16
import org.springframework.beans.factory.annotation.Autowired;
17

    
18
public class IncrementalOperationJobNode extends SimpleJobNode {
19

    
20
    private static final Log log = LogFactory.getLog(IncrementalOperationJobNode.class);
21

    
22
    //incremental or refresh
23
    private String operationType;
24

    
25
    @Autowired
26
    private DnetLogger dnetLogger;
27

    
28
    @Override
29
    protected String execute(NodeToken nodeToken) throws Exception {
30

    
31
        if ("incremental".equalsIgnoreCase(operationType)) {
32
            final String currentWfProfileId = findCurrentWfProfileId(nodeToken);
33
            final Long lastSuccessStartDate = findLastSuccessStartDate(currentWfProfileId);
34
            if(forceRefresh(nodeToken, lastSuccessStartDate, currentWfProfileId)) return Arc.DEFAULT_ARC;
35
            log.info("Last success start date "+ lastSuccessStartDate);
36

    
37
            nodeToken.getFullEnv().setAttribute("OperationTypeInfo", "Operation type set to INCREMENTAL with date "+DateUtils.calculate_ISO8601(lastSuccessStartDate));
38
            nodeToken.getFullEnv().setAttribute("operationType", "INCREMENTAL");
39
            nodeToken.getFullEnv().setAttribute("DateFromFilter", lastSuccessStartDate);
40
            return Arc.DEFAULT_ARC;
41
        }
42
        nodeToken.getFullEnv().setAttribute("operationType", "REFRESH");
43
        nodeToken.getFullEnv().setAttribute("OperationTypeInfo", "Operation type manually set to REFRESH");
44
        return Arc.DEFAULT_ARC;
45
    }
46

    
47
    protected boolean forceRefresh(final NodeToken nodeToken, final Long lastSuccessStartDate, final String currentWfProfileId) throws Exception {
48
        if (lastSuccessStartDate < 0) {
49
            nodeToken.getFullEnv().setAttribute("OperationTypeInfo", "Last success start date < 0, operation forced to REFRESH");
50
            nodeToken.getFullEnv().setAttribute("operationType", "REFRESH");
51
            return true;
52
        }
53
        return false;
54
    }
55

    
56
    private Long findLastSuccessStartDate(String profId) {
57
        long res = -1;
58

    
59
        final Iterator<Map<String, String>> iter = dnetLogger.find(WorkflowsConstants.SYSTEM_WF_PROFILE_ID, profId);
60
        while (iter.hasNext()) {
61
            final Map<String, String> map = iter.next();
62
            if ("true".equalsIgnoreCase(map.get(WorkflowsConstants.SYSTEM_COMPLETED_SUCCESSFULLY))) {
63
                final long curr = NumberUtils.toLong(map.get(WorkflowsConstants.SYSTEM_START_DATE), -1);
64
                if (curr > res) {
65
                    res = curr;
66
                }
67
            }
68
        }
69
        log.debug("D-Net logger says the last success end date was on "+res);
70
        return res;
71
    }
72

    
73
    private String findCurrentWfProfileId(NodeToken token) throws MSROException {
74
        log.debug("Start to find the current profile Id");
75
        final String p1 = token.getEnv().getAttribute(WorkflowsConstants.SYSTEM_WF_PROFILE_ID);
76
        if (p1 != null && !p1.isEmpty()) {
77
            log.debug("The profile Id found is "+p1);
78
            return p1;
79
        }
80
        final String p2 = token.getFullEnv().getAttribute(WorkflowsConstants.SYSTEM_WF_PROFILE_ID);
81
        if (p2 != null && !p2.isEmpty()) {
82
            log.debug("The profile Id found is "+p2);
83
            return p2;
84
        }
85
        final String p3 = token.getProcess().getEnv().getAttribute(WorkflowsConstants.SYSTEM_WF_PROFILE_ID);
86
        if (p3 != null && !p3.isEmpty()) {
87
            log.debug("The profile Id found is "+p3);
88
            return p3;
89
        }
90
        throw new MSROException("Missing property in env: " + WorkflowsConstants.SYSTEM_WF_PROFILE_ID);
91
    }
92

    
93
    public String getOperationType() {
94
        return operationType;
95
    }
96

    
97
    public void setOperationType(final String operationType) {
98
        this.operationType = operationType;
99
    }
100
}
(6-6/24)