]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/execution/JcrProcessThread.java
Deal with result parts
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.jcr / src / main / java / org / argeo / slc / jcr / execution / JcrProcessThread.java
1 package org.argeo.slc.jcr.execution;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 import javax.jcr.Node;
7 import javax.jcr.NodeIterator;
8 import javax.jcr.Property;
9 import javax.jcr.RepositoryException;
10
11 import org.argeo.ArgeoException;
12 import org.argeo.slc.SlcException;
13 import org.argeo.slc.core.execution.DefaultExecutionSpec;
14 import org.argeo.slc.core.execution.PrimitiveSpecAttribute;
15 import org.argeo.slc.core.execution.PrimitiveUtils;
16 import org.argeo.slc.core.execution.ProcessThread;
17 import org.argeo.slc.execution.ExecutionFlowDescriptor;
18 import org.argeo.slc.execution.ExecutionModulesManager;
19 import org.argeo.slc.execution.ExecutionSpecAttribute;
20 import org.argeo.slc.jcr.SlcJcrUtils;
21 import org.argeo.slc.jcr.SlcNames;
22 import org.argeo.slc.jcr.SlcTypes;
23 import org.argeo.slc.process.RealizedFlow;
24
25 /** Where the actual execution takes place */
26 public class JcrProcessThread extends ProcessThread implements SlcNames {
27
28 public JcrProcessThread(ExecutionModulesManager executionModulesManager,
29 JcrExecutionProcess process) {
30 super(executionModulesManager, process);
31 }
32
33 @Override
34 protected void process() {
35 try {
36 Node realizedFlowNode = getNode().getNode(SLC_FLOW);
37 // we just manage one level for the time being
38 NodeIterator nit = realizedFlowNode.getNodes(SLC_FLOW);
39 while (nit.hasNext()) {
40 process(nit.nextNode());
41 }
42 } catch (RepositoryException e) {
43 throw new ArgeoException("Cannot process " + getNode(), e);
44 }
45 }
46
47 /** Configure the realized flows */
48 protected void process(Node realizedFlowNode) throws RepositoryException {
49 if (realizedFlowNode.hasNode(SLC_ADDRESS)) {
50 String flowPath = realizedFlowNode.getNode(SLC_ADDRESS)
51 .getProperty(Property.JCR_PATH).getString();
52 // TODO: convert to local path if remote
53
54 Node flowNode = realizedFlowNode.getSession().getNode(flowPath);
55 String flowName = flowNode.getProperty(SLC_NAME).getString();
56
57 String executionModuleName = SlcJcrUtils
58 .flowExecutionModuleName(flowPath);
59 String executionModuleVersion = SlcJcrUtils
60 .flowExecutionModuleVersion(flowPath);
61
62 RealizedFlow realizedFlow = new RealizedFlow();
63 realizedFlow.setModuleName(executionModuleName);
64 realizedFlow.setModuleVersion(executionModuleVersion);
65
66 DefaultExecutionSpec executionSpec = null;
67 if (flowNode.hasProperty(SlcNames.SLC_SPEC)) {
68 Node executionSpecNode = flowNode.getProperty(SLC_SPEC)
69 .getNode();
70 executionSpec = new DefaultExecutionSpec();
71 executionSpec.setBeanName(executionSpecNode.getProperty(
72 SLC_NAME).getString());
73 executionSpec
74 .setAttributes(readExecutionSpecAttributes(executionSpecNode));
75 }
76 // TODO: will with original attr
77 Map<String, ExecutionSpecAttribute> attrs = readExecutionSpecAttributes(realizedFlowNode);
78 Map<String, Object> values = new HashMap<String, Object>();
79 for (String attrName : attrs.keySet()) {
80 ExecutionSpecAttribute attr = attrs.get(attrName);
81 Object value = attr.getValue();
82 values.put(attrName,value);
83 }
84
85 // if(executionSpec!=null)
86 // executionSpec.setAttributes(attrs);
87 ExecutionFlowDescriptor efd = new ExecutionFlowDescriptor(flowName,
88 values, executionSpec);
89 realizedFlow.setFlowDescriptor(efd);
90
91 execute(realizedFlow, true);
92 }
93 }
94
95 protected Map<String, ExecutionSpecAttribute> readExecutionSpecAttributes(
96 Node node) {
97 try {
98 Map<String, ExecutionSpecAttribute> attrs = new HashMap<String, ExecutionSpecAttribute>();
99 for (NodeIterator nit = node.getNodes(); nit.hasNext();) {
100 Node specAttrNode = nit.nextNode();
101 if (specAttrNode
102 .isNodeType(SlcTypes.SLC_PRIMITIVE_SPEC_ATTRIBUTE)) {
103 String type = specAttrNode.getProperty(SLC_TYPE)
104 .getString();
105 if (specAttrNode.hasProperty(SLC_VALUE)) {
106 String valueStr = specAttrNode.getProperty(SLC_VALUE)
107 .getString();
108 Object value = PrimitiveUtils.convert(type, valueStr);
109 PrimitiveSpecAttribute specAttr = new PrimitiveSpecAttribute(
110 type, value);
111 attrs.put(specAttrNode.getName(), specAttr);
112 }
113 }
114
115 }
116 return attrs;
117 } catch (RepositoryException e) {
118 throw new SlcException("Cannot read spec attributes from " + node,
119 e);
120 }
121 }
122
123 protected Node getNode() {
124 return ((JcrExecutionProcess) getProcess()).getNode();
125 }
126 }