]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/execution/JcrRealizedFlow.java
Remove SlcExecution and process packages
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.jcr / src / main / java / org / argeo / slc / jcr / execution / JcrRealizedFlow.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.slc.SlcException;
12 import org.argeo.slc.core.execution.DefaultExecutionSpec;
13 import org.argeo.slc.core.execution.PrimitiveSpecAttribute;
14 import org.argeo.slc.core.execution.PrimitiveUtils;
15 import org.argeo.slc.core.execution.RefSpecAttribute;
16 import org.argeo.slc.execution.ExecutionFlowDescriptor;
17 import org.argeo.slc.execution.ExecutionSpecAttribute;
18 import org.argeo.slc.execution.RealizedFlow;
19 import org.argeo.slc.jcr.SlcJcrUtils;
20 import org.argeo.slc.jcr.SlcNames;
21 import org.argeo.slc.jcr.SlcTypes;
22
23 public class JcrRealizedFlow extends RealizedFlow implements SlcNames {
24 private static final long serialVersionUID = -3709453850260712001L;
25 private String path;
26
27 public JcrRealizedFlow(Node node) {
28 try {
29 this.path = node.getPath();
30 loadFromNode(node);
31 } catch (RepositoryException e) {
32 throw new SlcException("Cannot initialize from " + node, e);
33 }
34 }
35
36 protected void loadFromNode(Node realizedFlowNode)
37 throws RepositoryException {
38 if (realizedFlowNode.hasNode(SLC_ADDRESS)) {
39 String flowPath = realizedFlowNode.getNode(SLC_ADDRESS)
40 .getProperty(Property.JCR_PATH).getString();
41 // TODO: convert to local path if remote
42
43 Node flowNode = realizedFlowNode.getSession().getNode(flowPath);
44 String flowName = flowNode.getProperty(SLC_NAME).getString();
45
46 Node executionModuleNode = flowNode.getSession().getNode(
47 SlcJcrUtils.modulePath(flowPath));
48 String executionModuleName = executionModuleNode.getProperty(
49 SLC_NAME).getString();
50 String executionModuleVersion = executionModuleNode.getProperty(
51 SLC_VERSION).getString();
52
53 RealizedFlow realizedFlow = this;
54 realizedFlow.setModuleName(executionModuleName);
55 realizedFlow.setModuleVersion(executionModuleVersion);
56
57 // retrieve execution spec
58 DefaultExecutionSpec executionSpec = new DefaultExecutionSpec();
59 Map<String, ExecutionSpecAttribute> attrs = readExecutionSpecAttributes(realizedFlowNode);
60 executionSpec.setAttributes(attrs);
61
62 // set execution spec name
63 if (flowNode.hasProperty(SlcNames.SLC_SPEC)) {
64 Node executionSpecNode = flowNode.getProperty(SLC_SPEC)
65 .getNode();
66 executionSpec.setBeanName(executionSpecNode.getProperty(
67 SLC_NAME).getString());
68 }
69
70 // explicitly retrieve values
71 Map<String, Object> values = new HashMap<String, Object>();
72 for (String attrName : attrs.keySet()) {
73 ExecutionSpecAttribute attr = attrs.get(attrName);
74 Object value = attr.getValue();
75 values.put(attrName, value);
76 }
77
78 ExecutionFlowDescriptor efd = new ExecutionFlowDescriptor(flowName,
79 values, executionSpec);
80 realizedFlow.setFlowDescriptor(efd);
81 } else {
82 throw new SlcException("Unsupported realized flow "
83 + realizedFlowNode);
84 }
85 }
86
87 protected Map<String, ExecutionSpecAttribute> readExecutionSpecAttributes(
88 Node node) {
89 try {
90 Map<String, ExecutionSpecAttribute> attrs = new HashMap<String, ExecutionSpecAttribute>();
91 for (NodeIterator nit = node.getNodes(); nit.hasNext();) {
92 Node specAttrNode = nit.nextNode();
93 if (specAttrNode
94 .isNodeType(SlcTypes.SLC_PRIMITIVE_SPEC_ATTRIBUTE)) {
95 String type = specAttrNode.getProperty(SLC_TYPE)
96 .getString();
97 Object value = null;
98 if (specAttrNode.hasProperty(SLC_VALUE)) {
99 String valueStr = specAttrNode.getProperty(SLC_VALUE)
100 .getString();
101 value = PrimitiveUtils.convert(type, valueStr);
102 }
103 PrimitiveSpecAttribute specAttr = new PrimitiveSpecAttribute(
104 type, value);
105 attrs.put(specAttrNode.getName(), specAttr);
106 } else if (specAttrNode
107 .isNodeType(SlcTypes.SLC_REF_SPEC_ATTRIBUTE)) {
108 if (!specAttrNode.hasProperty(SLC_VALUE)) {
109 continue;
110 }
111 Integer value = (int) specAttrNode.getProperty(SLC_VALUE)
112 .getLong();
113 RefSpecAttribute specAttr = new RefSpecAttribute();
114 NodeIterator children = specAttrNode.getNodes();
115 int index = 0;
116 String id = null;
117 while (children.hasNext()) {
118 Node child = children.nextNode();
119 if (index == value)
120 id = child.getName();
121 index++;
122 }
123 specAttr.setValue(id);
124 attrs.put(specAttrNode.getName(), specAttr);
125 }
126 // throw new SlcException("Unsupported spec attribute "
127 // + specAttrNode);
128 }
129 return attrs;
130 } catch (RepositoryException e) {
131 throw new SlcException("Cannot read spec attributes from " + node,
132 e);
133 }
134 }
135
136 public String getPath() {
137 return path;
138 }
139 }