]> 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
Improve JCR UI
[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 // retrieve execution spec
67 DefaultExecutionSpec executionSpec = null;
68 if (flowNode.hasProperty(SlcNames.SLC_SPEC)) {
69 Node executionSpecNode = flowNode.getProperty(SLC_SPEC)
70 .getNode();
71 executionSpec = new DefaultExecutionSpec();
72 executionSpec.setBeanName(executionSpecNode.getProperty(
73 SLC_NAME).getString());
74 executionSpec
75 .setAttributes(readExecutionSpecAttributes(executionSpecNode));
76 }
77 // TODO: will with original attr
78 Map<String, ExecutionSpecAttribute> attrs = readExecutionSpecAttributes(realizedFlowNode);
79 Map<String, Object> values = new HashMap<String, Object>();
80 for (String attrName : attrs.keySet()) {
81 if (flowNode.hasNode(attrName)) {
82 // we assume this is a primitive
83 // since ref are not yet implemented
84 Node valueNode = flowNode.getNode(attrName);
85 String type = valueNode.getProperty(SLC_TYPE).getString();
86 String valueStr = valueNode.getProperty(SLC_VALUE)
87 .getString();
88 Object value = PrimitiveUtils.convert(type, valueStr);
89 values.put(attrName, value);
90 } else {
91 ExecutionSpecAttribute attr = attrs.get(attrName);
92 Object value = attr.getValue();
93 values.put(attrName, value);
94 }
95 }
96
97 // if(executionSpec!=null)
98 // executionSpec.setAttributes(attrs);
99 ExecutionFlowDescriptor efd = new ExecutionFlowDescriptor(flowName,
100 values, executionSpec);
101 realizedFlow.setFlowDescriptor(efd);
102
103 execute(realizedFlow, true);
104 }
105 }
106
107 protected Map<String, ExecutionSpecAttribute> readExecutionSpecAttributes(
108 Node node) {
109 try {
110 Map<String, ExecutionSpecAttribute> attrs = new HashMap<String, ExecutionSpecAttribute>();
111 for (NodeIterator nit = node.getNodes(); nit.hasNext();) {
112 Node specAttrNode = nit.nextNode();
113 if (specAttrNode
114 .isNodeType(SlcTypes.SLC_PRIMITIVE_SPEC_ATTRIBUTE)) {
115 String type = specAttrNode.getProperty(SLC_TYPE)
116 .getString();
117 Object value = null;
118 if (specAttrNode.hasProperty(SLC_VALUE)) {
119 String valueStr = specAttrNode.getProperty(SLC_VALUE)
120 .getString();
121 value = PrimitiveUtils.convert(type, valueStr);
122 }
123 PrimitiveSpecAttribute specAttr = new PrimitiveSpecAttribute(
124 type, value);
125 attrs.put(specAttrNode.getName(), specAttr);
126 }
127
128 }
129 return attrs;
130 } catch (RepositoryException e) {
131 throw new SlcException("Cannot read spec attributes from " + node,
132 e);
133 }
134 }
135
136 protected Node getNode() {
137 return ((JcrExecutionProcess) getProcess()).getNode();
138 }
139 }