]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/SlcJcrUtils.java
Improve diff issue
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.jcr / src / main / java / org / argeo / slc / jcr / SlcJcrUtils.java
index 11a5ca28977e65fb171ae5aa2e12a83ec0b6d795..793670e7c1b1b235db6c5fc703cd09072250fa8b 100644 (file)
@@ -4,6 +4,7 @@ import java.util.Calendar;
 import java.util.GregorianCalendar;
 
 import javax.jcr.Node;
+import javax.jcr.NodeIterator;
 import javax.jcr.RepositoryException;
 
 import org.argeo.jcr.JcrUtils;
@@ -11,12 +12,13 @@ import org.argeo.slc.SlcException;
 import org.argeo.slc.core.execution.PrimitiveAccessor;
 import org.argeo.slc.core.execution.PrimitiveUtils;
 import org.argeo.slc.deploy.ModuleDescriptor;
+import org.argeo.slc.test.TestStatus;
 
 /**
  * Utilities around the SLC JCR model. Note that it relies on fixed base paths
  * (convention over configuration) for optimization purposes.
  */
-public class SlcJcrUtils {
+public class SlcJcrUtils implements SlcNames {
        public final static Integer AGENT_FACTORY_DEPTH = 3;
 
        /** Extracts the path of a flow relative to its execution module */
@@ -64,7 +66,13 @@ public class SlcJcrUtils {
                Calendar now = new GregorianCalendar();
                return SlcJcrConstants.PROCESSES_BASE_PATH + '/'
                                + JcrUtils.dateAsPath(now, true) + uuid;
+       }
 
+       /** Create a new execution result path based on the current time */
+       public static String createResultPath(String uuid) {
+               Calendar now = new GregorianCalendar();
+               return SlcJcrConstants.RESULTS_BASE_PATH + '/'
+                               + JcrUtils.dateAsPath(now, true) + uuid;
        }
 
        /**
@@ -78,7 +86,7 @@ public class SlcJcrUtils {
                setPrimitiveAsProperty(node, propertyName, type, value);
        }
 
-       /** Map a primitive value to JCR ptoperty value. */
+       /** Map a primitive value to JCR property value. */
        public static void setPrimitiveAsProperty(Node node, String propertyName,
                        String type, Object value) {
                if (value == null)
@@ -88,17 +96,17 @@ public class SlcJcrUtils {
                                        ((CharSequence) value).toString());
 
                try {
-                       if (type.equals(PrimitiveUtils.TYPE_STRING))
+                       if (type.equals(PrimitiveAccessor.TYPE_STRING))
                                node.setProperty(propertyName, value.toString());
-                       else if (type.equals(PrimitiveUtils.TYPE_INTEGER))
+                       else if (type.equals(PrimitiveAccessor.TYPE_INTEGER))
                                node.setProperty(propertyName, (long) ((Integer) value));
-                       else if (type.equals(PrimitiveUtils.TYPE_LONG))
+                       else if (type.equals(PrimitiveAccessor.TYPE_LONG))
                                node.setProperty(propertyName, ((Long) value));
-                       else if (type.equals(PrimitiveUtils.TYPE_FLOAT))
+                       else if (type.equals(PrimitiveAccessor.TYPE_FLOAT))
                                node.setProperty(propertyName, (double) ((Float) value));
-                       else if (type.equals(PrimitiveUtils.TYPE_DOUBLE))
+                       else if (type.equals(PrimitiveAccessor.TYPE_DOUBLE))
                                node.setProperty(propertyName, ((Double) value));
-                       else if (type.equals(PrimitiveUtils.TYPE_BOOLEAN))
+                       else if (type.equals(PrimitiveAccessor.TYPE_BOOLEAN))
                                node.setProperty(propertyName, ((Boolean) value));
                        else
                                throw new SlcException("Unsupported type " + type);
@@ -108,6 +116,65 @@ public class SlcJcrUtils {
                }
        }
 
+       /** Aggregates the {@link TestStatus} of this sub-tree. */
+       public static Integer aggregateTestStatus(Node node) {
+               try {
+                       Integer status = TestStatus.PASSED;
+                       if (node.isNodeType(SlcTypes.SLC_CHECK))
+                               if (node.getProperty(SLC_SUCCESS).getBoolean())
+                                       status = TestStatus.PASSED;
+                               else if (node.hasProperty(SLC_ERROR_MESSAGE))
+                                       status = TestStatus.ERROR;
+                               else
+                                       status = TestStatus.FAILED;
+
+                       NodeIterator it = node.getNodes();
+                       while (it.hasNext()) {
+                               Integer childStatus = aggregateTestStatus(it.nextNode());
+                               if (childStatus > status)
+                                       status = childStatus;
+                       }
+                       return status;
+               } catch (Exception e) {
+                       throw new SlcException("Could not aggregate test status from "
+                                       + node, e);
+               }
+       }
+
+       /**
+        * Aggregates the {@link TestStatus} of this sub-tree.
+        * 
+        * @return the same {@link StringBuffer}, for convenience (typically calling
+        *         toString() on it)
+        */
+       public static StringBuffer aggregateTestMessages(Node node,
+                       StringBuffer messages) {
+               try {
+                       if (node.isNodeType(SlcTypes.SLC_CHECK)) {
+                               if (node.hasProperty(SLC_MESSAGE)) {
+                                       if (messages.length() > 0)
+                                               messages.append('\n');
+                                       messages.append(node.getProperty(SLC_MESSAGE).getString());
+                               }
+                               if (node.hasProperty(SLC_ERROR_MESSAGE)) {
+                                       if (messages.length() > 0)
+                                               messages.append('\n');
+                                       messages.append(node.getProperty(SLC_ERROR_MESSAGE)
+                                                       .getString());
+                               }
+                       }
+                       NodeIterator it = node.getNodes();
+                       while (it.hasNext()) {
+                               Node child = it.nextNode();
+                               aggregateTestMessages(child, messages);
+                       }
+                       return messages;
+               } catch (Exception e) {
+                       throw new SlcException("Could not aggregate test messages from "
+                                       + node, e);
+               }
+       }
+
        /** Prevents instantiation */
        private SlcJcrUtils() {