]> 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
Work in progress - work on modular distributions.
[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 // FIXME start related module
43 Node flowNode = realizedFlowNode.getSession().getNode(flowPath);
44 String flowName = flowNode.getProperty(SLC_NAME).getString();
45 String description = null;
46 if (flowNode.hasProperty(Property.JCR_DESCRIPTION))
47 description = flowNode.getProperty(Property.JCR_DESCRIPTION)
48 .getString();
49
50 Node executionModuleNode = flowNode.getSession().getNode(
51 SlcJcrUtils.modulePath(flowPath));
52 String executionModuleName = executionModuleNode.getProperty(
53 SLC_NAME).getString();
54 String executionModuleVersion = executionModuleNode.getProperty(
55 SLC_VERSION).getString();
56
57 RealizedFlow realizedFlow = this;
58 realizedFlow.setModuleName(executionModuleName);
59 realizedFlow.setModuleVersion(executionModuleVersion);
60
61 // retrieve execution spec
62 DefaultExecutionSpec executionSpec = new DefaultExecutionSpec();
63 Map<String, ExecutionSpecAttribute> attrs = readExecutionSpecAttributes(realizedFlowNode);
64 executionSpec.setAttributes(attrs);
65
66 // set execution spec name
67 if (flowNode.hasProperty(SlcNames.SLC_SPEC)) {
68 Node executionSpecNode = flowNode.getProperty(SLC_SPEC)
69 .getNode();
70 executionSpec.setBeanName(executionSpecNode.getProperty(
71 SLC_NAME).getString());
72 }
73
74 // explicitly retrieve values
75 Map<String, Object> values = new HashMap<String, Object>();
76 for (String attrName : attrs.keySet()) {
77 ExecutionSpecAttribute attr = attrs.get(attrName);
78 Object value = attr.getValue();
79 values.put(attrName, value);
80 }
81
82 ExecutionFlowDescriptor efd = new ExecutionFlowDescriptor(flowName,
83 description, values, executionSpec);
84 realizedFlow.setFlowDescriptor(efd);
85 } else {
86 throw new SlcException("Unsupported realized flow "
87 + realizedFlowNode);
88 }
89 }
90
91 protected Map<String, ExecutionSpecAttribute> readExecutionSpecAttributes(
92 Node node) {
93 try {
94 Map<String, ExecutionSpecAttribute> attrs = new HashMap<String, ExecutionSpecAttribute>();
95 for (NodeIterator nit = node.getNodes(); nit.hasNext();) {
96 Node specAttrNode = nit.nextNode();
97 if (specAttrNode
98 .isNodeType(SlcTypes.SLC_PRIMITIVE_SPEC_ATTRIBUTE)) {
99 String type = specAttrNode.getProperty(SLC_TYPE)
100 .getString();
101 Object value = null;
102 if (specAttrNode.hasProperty(SLC_VALUE)) {
103 String valueStr = specAttrNode.getProperty(SLC_VALUE)
104 .getString();
105 value = PrimitiveUtils.convert(type, valueStr);
106 }
107 PrimitiveSpecAttribute specAttr = new PrimitiveSpecAttribute(
108 type, value);
109 attrs.put(specAttrNode.getName(), specAttr);
110 } else if (specAttrNode
111 .isNodeType(SlcTypes.SLC_REF_SPEC_ATTRIBUTE)) {
112 if (!specAttrNode.hasProperty(SLC_VALUE)) {
113 continue;
114 }
115 Integer value = (int) specAttrNode.getProperty(SLC_VALUE)
116 .getLong();
117 RefSpecAttribute specAttr = new RefSpecAttribute();
118 NodeIterator children = specAttrNode.getNodes();
119 int index = 0;
120 String id = null;
121 while (children.hasNext()) {
122 Node child = children.nextNode();
123 if (index == value)
124 id = child.getName();
125 index++;
126 }
127 specAttr.setValue(id);
128 attrs.put(specAttrNode.getName(), specAttr);
129 }
130 // throw new SlcException("Unsupported spec attribute "
131 // + specAttrNode);
132 }
133 return attrs;
134 } catch (RepositoryException e) {
135 throw new SlcException("Cannot read spec attributes from " + node,
136 e);
137 }
138 }
139
140 public String getPath() {
141 return path;
142 }
143 }