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