1
|
package eu.dnetlib.msro.workflows.procs;
|
2
|
|
3
|
import java.text.SimpleDateFormat;
|
4
|
import java.util.Date;
|
5
|
import java.util.Map;
|
6
|
|
7
|
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
|
8
|
import eu.dnetlib.msro.workflows.graph.Graph;
|
9
|
import eu.dnetlib.msro.workflows.util.ProcessCallback;
|
10
|
import eu.dnetlib.rmi.enabling.ISLookUpService;
|
11
|
import eu.dnetlib.rmi.manager.MSROException;
|
12
|
import org.apache.commons.lang3.StringUtils;
|
13
|
import org.apache.commons.logging.Log;
|
14
|
import org.apache.commons.logging.LogFactory;
|
15
|
import org.springframework.beans.factory.annotation.Autowired;
|
16
|
|
17
|
/**
|
18
|
* Created by michele on 25/11/15.
|
19
|
*/
|
20
|
public class ProcessFactory {
|
21
|
|
22
|
private static final Log log = LogFactory.getLog(ProcessFactory.class);
|
23
|
|
24
|
private String oldGeneratedId = "";
|
25
|
|
26
|
@Autowired
|
27
|
private UniqueServiceLocator serviceLocator;
|
28
|
|
29
|
public WorkflowProcess newProcess(final String name,
|
30
|
final String family,
|
31
|
final String dsId,
|
32
|
final String dsInterface,
|
33
|
final Graph graph,
|
34
|
final int priority,
|
35
|
final String profileId,
|
36
|
final boolean template,
|
37
|
final Map<String, String> globalParams,
|
38
|
final ProcessCallback processCallback) throws MSROException {
|
39
|
|
40
|
final String dsName = findDsName(dsId);
|
41
|
|
42
|
return new WorkflowProcess(generateProcessId(), name, family, dsId, dsName, dsInterface, graph, priority, profileId, template, globalParams,
|
43
|
processCallback);
|
44
|
|
45
|
}
|
46
|
|
47
|
private String findDsName(final String dsId) throws MSROException {
|
48
|
|
49
|
if (StringUtils.isBlank(dsId)) { return null; }
|
50
|
|
51
|
final String query = "collection('/db/DRIVER/RepositoryServiceResources/RepositoryServiceResourceType')/*[.//RESOURCE_IDENTIFIER/@value='" + dsId
|
52
|
+ "' or .//DATASOURCE_ORIGINAL_ID='" + dsId + "']//OFFICIAL_NAME/text()";
|
53
|
try {
|
54
|
return this.serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(query);
|
55
|
} catch (final Exception e) {
|
56
|
log.error("Error executing xquery: " + query);
|
57
|
throw new MSROException("Error executing xquery: " + query, e);
|
58
|
}
|
59
|
}
|
60
|
|
61
|
private synchronized String generateProcessId() {
|
62
|
String id = "";
|
63
|
do {
|
64
|
id = "wf_" + (new SimpleDateFormat("yyyyMMdd_HHmmss_S")).format(new Date());
|
65
|
log.info("Generated processID " + id);
|
66
|
} while (id.equals(this.oldGeneratedId));
|
67
|
|
68
|
this.oldGeneratedId = id;
|
69
|
|
70
|
return id;
|
71
|
}
|
72
|
}
|