]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/execution/JcrProcessThread.java
Use values from flows
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.jcr / src / main / java / org / argeo / slc / jcr / execution / JcrProcessThread.java
index 34685fff0973a9c01e6c33048e86bebe3ce24ac4..26e3737b0be824d3cd0f257a7595ab1629024c5f 100644 (file)
@@ -1,18 +1,28 @@
 package org.argeo.slc.jcr.execution;
 
+import java.util.HashMap;
+import java.util.Map;
+
 import javax.jcr.Node;
 import javax.jcr.NodeIterator;
 import javax.jcr.Property;
 import javax.jcr.RepositoryException;
 
 import org.argeo.ArgeoException;
+import org.argeo.slc.SlcException;
+import org.argeo.slc.core.execution.DefaultExecutionSpec;
+import org.argeo.slc.core.execution.PrimitiveSpecAttribute;
+import org.argeo.slc.core.execution.PrimitiveUtils;
 import org.argeo.slc.core.execution.ProcessThread;
 import org.argeo.slc.execution.ExecutionFlowDescriptor;
 import org.argeo.slc.execution.ExecutionModulesManager;
+import org.argeo.slc.execution.ExecutionSpecAttribute;
 import org.argeo.slc.jcr.SlcJcrUtils;
 import org.argeo.slc.jcr.SlcNames;
+import org.argeo.slc.jcr.SlcTypes;
 import org.argeo.slc.process.RealizedFlow;
 
+/** Where the actual execution takes place */
 public class JcrProcessThread extends ProcessThread implements SlcNames {
 
        public JcrProcessThread(ExecutionModulesManager executionModulesManager,
@@ -34,6 +44,7 @@ public class JcrProcessThread extends ProcessThread implements SlcNames {
                }
        }
 
+       /** Configure the realized flows */
        protected void process(Node realizedFlowNode) throws RepositoryException {
                if (realizedFlowNode.hasNode(SLC_ADDRESS)) {
                        String flowPath = realizedFlowNode.getNode(SLC_ADDRESS)
@@ -52,14 +63,89 @@ public class JcrProcessThread extends ProcessThread implements SlcNames {
                        realizedFlow.setModuleName(executionModuleName);
                        realizedFlow.setModuleVersion(executionModuleVersion);
 
+                       // retrieve execution spec
+                       DefaultExecutionSpec executionSpec = null;
+                       if (flowNode.hasProperty(SlcNames.SLC_SPEC)) {
+                               Node executionSpecNode = flowNode.getProperty(SLC_SPEC)
+                                               .getNode();
+                               executionSpec = new DefaultExecutionSpec();
+                               executionSpec.setBeanName(executionSpecNode.getProperty(
+                                               SLC_NAME).getString());
+                               executionSpec
+                                               .setAttributes(readExecutionSpecAttributes(executionSpecNode));
+                       }
+                       // TODO: will with original attr
+                       Map<String, ExecutionSpecAttribute> attrs = readExecutionSpecAttributes(realizedFlowNode);
+                       Map<String, Object> values = new HashMap<String, Object>();
+                       for (String attrName : attrs.keySet()) {
+                               if (flowNode.hasNode(attrName)) {
+                                       // we assume this is a primitive
+                                       // since ref are not yet implemented
+                                       Node valueNode = flowNode.getNode(attrName);
+                                       String type = valueNode.getProperty(SLC_TYPE).getString();
+                                       String valueStr = valueNode.getProperty(SLC_VALUE)
+                                                       .getString();
+                                       Object value = PrimitiveUtils.convert(type, valueStr);
+                                       values.put(attrName, value);
+                                       // Property prop = flowNode.getNode(attrName).getProperty(
+                                       // SLC_VALUE);
+                                       // // yes, this could be a switch... (patches welcome)
+                                       // if (prop.getType() == PropertyType.STRING)
+                                       // values.put(attrName, prop.getString());
+                                       // else if (prop.getType() == PropertyType.LONG)
+                                       // values.put(attrName, prop.getLong());
+                                       // else if (prop.getType() == PropertyType.DOUBLE)
+                                       // values.put(attrName, prop.getDouble());
+                                       // else if (prop.getType() == PropertyType.BOOLEAN)
+                                       // values.put(attrName, prop.getBoolean());
+                                       // else
+                                       // throw new SlcException("Unsupported value type "
+                                       // + PropertyType.nameFromValue(prop.getType()));
+                               } else {
+                                       ExecutionSpecAttribute attr = attrs.get(attrName);
+                                       Object value = attr.getValue();
+                                       values.put(attrName, value);
+                               }
+                       }
+
+                       // if(executionSpec!=null)
+                       // executionSpec.setAttributes(attrs);
                        ExecutionFlowDescriptor efd = new ExecutionFlowDescriptor(flowName,
-                                       null, null);
+                                       values, executionSpec);
                        realizedFlow.setFlowDescriptor(efd);
 
                        execute(realizedFlow, true);
                }
        }
 
+       protected Map<String, ExecutionSpecAttribute> readExecutionSpecAttributes(
+                       Node node) {
+               try {
+                       Map<String, ExecutionSpecAttribute> attrs = new HashMap<String, ExecutionSpecAttribute>();
+                       for (NodeIterator nit = node.getNodes(); nit.hasNext();) {
+                               Node specAttrNode = nit.nextNode();
+                               if (specAttrNode
+                                               .isNodeType(SlcTypes.SLC_PRIMITIVE_SPEC_ATTRIBUTE)) {
+                                       String type = specAttrNode.getProperty(SLC_TYPE)
+                                                       .getString();
+                                       if (specAttrNode.hasProperty(SLC_VALUE)) {
+                                               String valueStr = specAttrNode.getProperty(SLC_VALUE)
+                                                               .getString();
+                                               Object value = PrimitiveUtils.convert(type, valueStr);
+                                               PrimitiveSpecAttribute specAttr = new PrimitiveSpecAttribute(
+                                                               type, value);
+                                               attrs.put(specAttrNode.getName(), specAttr);
+                                       }
+                               }
+
+                       }
+                       return attrs;
+               } catch (RepositoryException e) {
+                       throw new SlcException("Cannot read spec attributes from " + node,
+                                       e);
+               }
+       }
+
        protected Node getNode() {
                return ((JcrExecutionProcess) getProcess()).getNode();
        }