]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/execution/JcrExecutionModulesListener.java
Document and improve execution model
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.jcr / src / main / java / org / argeo / slc / jcr / execution / JcrExecutionModulesListener.java
index b019ff970a072d575263d56447ab70ec567d2c45..27d7b6b3b3e374ccc0209981105855551a4d43d6 100644 (file)
@@ -8,14 +8,20 @@ import javax.jcr.NodeIterator;
 import javax.jcr.Property;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
+import javax.jcr.nodetype.NodeType;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.argeo.jcr.JcrUtils;
 import org.argeo.slc.SlcException;
+import org.argeo.slc.core.execution.PrimitiveSpecAttribute;
+import org.argeo.slc.core.execution.RefSpecAttribute;
+import org.argeo.slc.core.execution.RefValueChoice;
 import org.argeo.slc.deploy.ModuleDescriptor;
 import org.argeo.slc.execution.ExecutionFlowDescriptor;
 import org.argeo.slc.execution.ExecutionModulesListener;
+import org.argeo.slc.execution.ExecutionSpec;
+import org.argeo.slc.execution.ExecutionSpecAttribute;
 import org.argeo.slc.jcr.SlcJcrUtils;
 import org.argeo.slc.jcr.SlcNames;
 import org.argeo.slc.jcr.SlcTypes;
@@ -24,7 +30,8 @@ import org.argeo.slc.jcr.SlcTypes;
  * Synchronizes the local execution runtime with a JCR repository. For the time
  * being the state is completely reset from one start to another.
  */
-public class JcrExecutionModulesListener implements ExecutionModulesListener {
+public class JcrExecutionModulesListener implements ExecutionModulesListener,
+               SlcNames {
        private final static Log log = LogFactory
                        .getLog(JcrExecutionModulesListener.class);
        private JcrAgent agent;
@@ -73,10 +80,8 @@ public class JcrExecutionModulesListener implements ExecutionModulesListener {
                                        .getNode(moduleNodeName) : agentNode
                                        .addNode(moduleNodeName);
                        moduleNode.addMixin(SlcTypes.SLC_EXECUTION_MODULE);
-                       moduleNode.setProperty(SlcNames.SLC_NAME,
-                                       moduleDescriptor.getName());
-                       moduleNode.setProperty(SlcNames.SLC_VERSION,
-                                       moduleDescriptor.getVersion());
+                       moduleNode.setProperty(SLC_NAME, moduleDescriptor.getName());
+                       moduleNode.setProperty(SLC_VERSION, moduleDescriptor.getVersion());
                        moduleNode.setProperty(Property.JCR_TITLE,
                                        moduleDescriptor.getTitle());
                        moduleNode.setProperty(Property.JCR_DESCRIPTION,
@@ -104,44 +109,119 @@ public class JcrExecutionModulesListener implements ExecutionModulesListener {
        }
 
        public synchronized void executionFlowAdded(ModuleDescriptor module,
-                       ExecutionFlowDescriptor executionFlow) {
+                       ExecutionFlowDescriptor efd) {
                try {
                        Node agentNode = session.getNode(agent.getNodePath());
                        Node moduleNode = agentNode.getNode(SlcJcrUtils
                                        .getModuleNodeName(module));
-                       String relativePath = getExecutionFlowRelativePath(executionFlow);
+                       String relativePath = getExecutionFlowRelativePath(efd);
+                       @SuppressWarnings("unused")
                        Node flowNode = null;
                        if (!moduleNode.hasNode(relativePath)) {
-                               Iterator<String> names = Arrays.asList(relativePath.split("/"))
-                                               .iterator();
-                               // create intermediary paths
-                               Node currNode = moduleNode;
-                               while (names.hasNext()) {
-                                       String name = names.next();
-                                       if (currNode.hasNode(name))
-                                               currNode = currNode.getNode(name);
-                                       else {
-                                               if (names.hasNext())
-                                                       currNode = currNode.addNode(name);
-                                               else
-                                                       flowNode = currNode.addNode(name,
-                                                                       SlcTypes.SLC_EXECUTION_FLOW);
-                                       }
-                               }
-                               flowNode.setProperty(SlcNames.SLC_NAME, executionFlow.getName());
+                               flowNode = createExecutionFlowNode(moduleNode, relativePath,
+                                               efd);
                                session.save();
                        } else {
                                flowNode = moduleNode.getNode(relativePath);
                        }
 
                        if (log.isTraceEnabled())
-                               log.trace("Flow " + executionFlow + " added to JCR");
+                               log.trace("Flow " + efd + " added to JCR");
                } catch (RepositoryException e) {
                        JcrUtils.discardQuietly(session);
-                       throw new SlcException("Cannot add flow " + executionFlow
-                                       + " from module " + module, e);
+                       throw new SlcException("Cannot add flow " + efd + " from module "
+                                       + module, e);
+               }
+
+       }
+
+       protected Node createExecutionFlowNode(Node moduleNode,
+                       String relativePath, ExecutionFlowDescriptor efd)
+                       throws RepositoryException {
+               Node flowNode = null;
+               Iterator<String> names = Arrays.asList(relativePath.split("/"))
+                               .iterator();
+               // create intermediary paths
+               Node currNode = moduleNode;
+               while (names.hasNext()) {
+                       String name = names.next();
+                       if (currNode.hasNode(name))
+                               currNode = currNode.getNode(name);
+                       else {
+                               if (names.hasNext())
+                                       currNode = currNode.addNode(name);
+                               else
+                                       flowNode = currNode.addNode(name,
+                                                       SlcTypes.SLC_EXECUTION_FLOW);
+                       }
                }
+               flowNode.setProperty(SLC_NAME, efd.getName());
+               String[] tokens = relativePath.split("/");
+               flowNode.setProperty(Property.JCR_TITLE, tokens[tokens.length - 1]);
+               if (efd.getDescription() != null
+                               && efd.getDescription().trim().equals(""))
+                       flowNode.setProperty(Property.JCR_DESCRIPTION, efd.getDescription());
 
+               // execution spec
+               ExecutionSpec executionSpec = efd.getExecutionSpec();
+               String esName = executionSpec.getName();
+               if (esName != null && !esName.equals(ExecutionSpec.INTERNAL_NAME)) {
+                       Node executionSpecsNode = moduleNode.hasNode(SLC_EXECUTION_SPECS) ? moduleNode
+                                       .getNode(SLC_EXECUTION_SPECS) : moduleNode
+                                       .addNode(SLC_EXECUTION_SPECS);
+                       Node executionSpecNode = executionSpecsNode.addNode(esName,
+                                       SlcTypes.SLC_EXECUTION_SPEC);
+                       executionSpecNode.setProperty(Property.JCR_TITLE, esName);
+                       if (executionSpec.getDescription() != null
+                                       && !executionSpec.getDescription().trim().equals(""))
+                               executionSpecNode.setProperty(Property.JCR_DESCRIPTION,
+                                               executionSpec.getDescription());
+                       mapExecutionSpec(executionSpecNode, executionSpec);
+               } else {
+                       mapExecutionSpec(flowNode, executionSpec);
+               }
+               return flowNode;
+       }
+
+       /**
+        * Base can be either an execution spec node, or an execution flow node (in
+        * case the execution spec is internal)
+        */
+       protected void mapExecutionSpec(Node baseNode, ExecutionSpec executionSpec)
+                       throws RepositoryException {
+               for (String attrName : executionSpec.getAttributes().keySet()) {
+                       ExecutionSpecAttribute esa = executionSpec.getAttributes().get(
+                                       attrName);
+                       Node attrNode = baseNode.addNode(attrName);
+                       // booleans
+                       attrNode.addMixin(SlcTypes.SLC_EXECUTION_SPEC_ATTRIBUTE);
+                       attrNode.setProperty(SLC_IS_IMMUTABLE, esa.getIsImmutable());
+                       attrNode.setProperty(SLC_IS_CONSTANT, esa.getIsConstant());
+                       attrNode.setProperty(SLC_IS_HIDDEN, esa.getIsHidden());
+
+                       if (esa instanceof PrimitiveSpecAttribute) {
+                               attrNode.addMixin(SlcTypes.SLC_PRIMITIVE_SPEC_ATTRIBUTE);
+                               PrimitiveSpecAttribute psa = (PrimitiveSpecAttribute) esa;
+                               SlcJcrUtils.setPrimitiveAsProperty(attrNode, SLC_VALUE, psa);
+                               attrNode.setProperty(SLC_TYPE, psa.getType());
+                       } else if (esa instanceof RefSpecAttribute) {
+                               attrNode.addMixin(SlcTypes.SLC_REF_SPEC_ATTRIBUTE);
+                               RefSpecAttribute rsa = (RefSpecAttribute) esa;
+                               attrNode.setProperty(SLC_TYPE, rsa.getTargetClassName());
+                               if (rsa.getChoices() != null) {
+                                       for (RefValueChoice choice : rsa.getChoices()) {
+                                               Node choiceNode = attrNode.addNode(choice.getName());
+                                               choiceNode.addMixin(NodeType.MIX_TITLE);
+                                               choiceNode.setProperty(Property.JCR_TITLE,
+                                                               choice.getName());
+                                               if (choice.getDescription() != null
+                                                               && !choice.getDescription().trim().equals(""))
+                                                       choiceNode.setProperty(Property.JCR_DESCRIPTION,
+                                                                       choice.getDescription());
+                                       }
+                               }
+                       }
+               }
        }
 
        public synchronized void executionFlowRemoved(ModuleDescriptor module,