Project

General

Profile

1
package eu.dnetlib.msro.workflows.procs;
2

    
3
import java.text.SimpleDateFormat;
4
import java.util.Date;
5

    
6
import org.apache.commons.lang3.StringUtils;
7
import org.apache.commons.logging.Log;
8
import org.apache.commons.logging.LogFactory;
9
import org.springframework.beans.factory.annotation.Autowired;
10
import org.springframework.stereotype.Component;
11

    
12
import eu.dnetlib.clients.is.InformationServiceClient;
13
import eu.dnetlib.msro.exceptions.MSROException;
14
import eu.dnetlib.msro.workflows.WorkflowInstance;
15
import eu.dnetlib.services.async.AsyncServerCallback;
16

    
17
/**
18
 * Created by michele on 25/11/15.
19
 */
20
@Component
21
public class ProcessFactory {
22

    
23
	private static final Log log = LogFactory.getLog(ProcessFactory.class);
24

    
25
	private String oldGeneratedId = "";
26

    
27
	@Autowired
28
	private InformationServiceClient isLookup;
29

    
30
	public WorkflowProcess newProcess(final WorkflowInstance wf, final AsyncServerCallback callback) throws MSROException {
31
		return new WorkflowProcess(generateProcessId(),
32
				wf.getName(),
33
				wf.getFamily(),
34
				wf.getDsId(),
35
				findDsName(wf.getDsId()),
36
				wf.getDsInterface(),
37
				wf.getGraph(),
38
				wf.getPriority(),
39
				wf.getProfileId(),
40
				wf.isTemplate(),
41
				wf.getGlobalParams(),
42
				callback, wf.getParent());
43
	}
44

    
45
	private String findDsName(final String dsId) throws MSROException {
46

    
47
		if (StringUtils.isBlank(dsId)) { return null; }
48

    
49
		final String query = "collection('/db/DRIVER/RepositoryServiceResources/RepositoryServiceResourceType')/*[.//RESOURCE_IDENTIFIER/@value='" + dsId
50
				+ "' or .//DATASOURCE_ORIGINAL_ID='" + dsId + "']//OFFICIAL_NAME/text()";
51
		try {
52
			return isLookup.findOne(query);
53
		} catch (final Exception e) {
54
			log.error("Error executing xquery: " + query);
55
			throw new MSROException("Error executing xquery: " + query, e);
56
		}
57
	}
58

    
59
	private synchronized String generateProcessId() {
60
		String id = "";
61
		do {
62
			id = "wf_" + (new SimpleDateFormat("yyyyMMdd_HHmmss_S")).format(new Date());
63
			log.info("Generated processID " + id);
64
		} while (id.equals(oldGeneratedId));
65

    
66
		oldGeneratedId = id;
67

    
68
		return id;
69
	}
70
}
(4-4/8)