]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/SlcJcrUtils.java
Fix execution spec node added twice
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.jcr / src / main / java / org / argeo / slc / jcr / SlcJcrUtils.java
index df69c93f7752d5c4672b411ff98d9b6b3f448223..693389d119fe02bd1ce462c93061a988b7ce18c7 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2007-2012 Mathieu Baudier
+ * Copyright (C) 2007-2012 Argeo GmbH
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -17,13 +17,17 @@ package org.argeo.slc.jcr;
 
 import java.util.Calendar;
 import java.util.GregorianCalendar;
+import java.util.List;
 
 import javax.jcr.Node;
 import javax.jcr.NodeIterator;
+import javax.jcr.Property;
 import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+import javax.jcr.nodetype.NodeType;
 
-import org.argeo.jcr.ArgeoJcrUtils;
 import org.argeo.jcr.JcrUtils;
+import org.argeo.jcr.UserJcrUtils;
 import org.argeo.slc.SlcException;
 import org.argeo.slc.core.execution.PrimitiveAccessor;
 import org.argeo.slc.core.execution.PrimitiveUtils;
@@ -58,6 +62,14 @@ public class SlcJcrUtils implements SlcNames {
                return buf.toString();
        }
 
+       /** Extracts the module name from a flow path */
+       public static String moduleName(String fullFlowPath) {
+               String[] tokens = fullFlowPath.split("/");
+               String moduleName = tokens[AGENT_FACTORY_DEPTH + 2];
+               moduleName = moduleName.substring(0, moduleName.indexOf('_'));
+               return moduleName;
+       }
+
        /** Module node name based on module name and version */
        public static String getModuleNodeName(ModuleDescriptor moduleDescriptor) {
                return moduleDescriptor.getName() + "_" + moduleDescriptor.getVersion();
@@ -75,17 +87,54 @@ public class SlcJcrUtils implements SlcNames {
        }
 
        /** Create a new execution process path based on the current time */
-       public static String createExecutionProcessPath(String uuid) {
+       public static String createExecutionProcessPath(Session session, String uuid) {
                Calendar now = new GregorianCalendar();
-               return SlcJcrConstants.PROCESSES_BASE_PATH + '/'
+               return getSlcProcessesBasePath(session) + '/'
                                + JcrUtils.dateAsPath(now, true) + uuid;
        }
 
-       /** Create a new execution result path based on the current time */
-       public static String createResultPath(String username, String uuid) {
+       /** Get the base for the user processi. */
+       public static String getSlcProcessesBasePath(Session session) {
+               try {
+                       Node userHome = UserJcrUtils.getUserHome(session);
+                       if (userHome == null)
+                               throw new SlcException("No user home available for "
+                                               + session.getUserID());
+                       return userHome.getPath() + '/' + SlcNames.SLC_SYSTEM + '/'
+                                       + SlcNames.SLC_PROCESSES;
+               } catch (RepositoryException re) {
+                       throw new SlcException(
+                                       "Unexpected error while getting Slc Results Base Path.", re);
+               }
+       }
+
+       /**
+        * Create a new execution result path in the user home based on the current
+        * time
+        */
+       public static String createResultPath(Session session, String uuid)
+                       throws RepositoryException {
                Calendar now = new GregorianCalendar();
-               return ArgeoJcrUtils.getUserHomePath(username) + '/' + SlcNames.SLC_RESULTS
-                               + '/' + JcrUtils.dateAsPath(now, true) + uuid;
+               StringBuffer absPath = new StringBuffer(
+                               SlcJcrResultUtils.getSlcResultsBasePath(session) + '/');
+               // Remove hours and add title property to the result process path on
+               // request of O. Capillon
+               // return getSlcProcessesBasePath(session) + '/'
+               // + JcrUtils.dateAsPath(now, true) + uuid;
+               String relPath = JcrUtils.dateAsPath(now, false);
+               List<String> names = JcrUtils.tokenize(relPath);
+               for (String name : names) {
+                       absPath.append(name + "/");
+                       Node node = JcrUtils.mkdirs(session, absPath.toString());
+                       try {
+                               node.addMixin(NodeType.MIX_TITLE);
+                               node.setProperty(Property.JCR_TITLE, name.substring(1));
+                       } catch (RepositoryException e) {
+                               throw new SlcException(
+                                               "unable to create execution process path", e);
+                       }
+               }
+               return absPath.toString() + uuid;
        }
 
        /**
@@ -107,10 +156,14 @@ public class SlcJcrUtils implements SlcNames {
                if (value instanceof CharSequence)
                        value = PrimitiveUtils.convert(type,
                                        ((CharSequence) value).toString());
+               if (value instanceof char[])
+                       value = new String((char[]) value);
 
                try {
                        if (type.equals(PrimitiveAccessor.TYPE_STRING))
                                node.setProperty(propertyName, value.toString());
+                       else if (type.equals(PrimitiveAccessor.TYPE_PASSWORD))
+                               node.setProperty(propertyName, value.toString());
                        else if (type.equals(PrimitiveAccessor.TYPE_INTEGER))
                                node.setProperty(propertyName, (long) ((Integer) value));
                        else if (type.equals(PrimitiveAccessor.TYPE_LONG))
@@ -143,9 +196,14 @@ public class SlcJcrUtils implements SlcNames {
 
                        NodeIterator it = node.getNodes();
                        while (it.hasNext()) {
-                               Integer childStatus = aggregateTestStatus(it.nextNode());
-                               if (childStatus > status)
-                                       status = childStatus;
+                               Node curr = it.nextNode();
+
+                               // Manually skip aggregated status
+                               if (!SlcNames.SLC_AGGREGATED_STATUS.equals(curr.getName())) {
+                                       Integer childStatus = aggregateTestStatus(curr);
+                                       if (childStatus > status)
+                                               status = childStatus;
+                               }
                        }
                        return status;
                } catch (Exception e) {
@@ -179,7 +237,10 @@ public class SlcJcrUtils implements SlcNames {
                        NodeIterator it = node.getNodes();
                        while (it.hasNext()) {
                                Node child = it.nextNode();
-                               aggregateTestMessages(child, messages);
+                               // Manually skip aggregated status
+                               if (!SlcNames.SLC_AGGREGATED_STATUS.equals(child.getName())) {
+                                       aggregateTestMessages(child, messages);
+                               }
                        }
                        return messages;
                } catch (Exception e) {
@@ -190,6 +251,5 @@ public class SlcJcrUtils implements SlcNames {
 
        /** Prevents instantiation */
        private SlcJcrUtils() {
-
        }
-}
+}
\ No newline at end of file