X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=runtime%2Forg.argeo.slc.support.jcr%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fjcr%2FSlcJcrUtils.java;h=51ea14e98d0d0a24b790da8e70a7d1168fb6d665;hb=86fc845e533e99358ec3de7182ea0c01bcd1c432;hp=ed2294f30087a45066a3799f706fa5d788312401;hpb=9bab32638cc6a08ba96c3feca190733e9d1ac0ac;p=gpl%2Fargeo-slc.git diff --git a/runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/SlcJcrUtils.java b/runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/SlcJcrUtils.java index ed2294f30..51ea14e98 100644 --- a/runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/SlcJcrUtils.java +++ b/runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/SlcJcrUtils.java @@ -3,34 +3,51 @@ package org.argeo.slc.jcr; 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; +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. */ -public class SlcJcrUtils { +/** + * Utilities around the SLC JCR model. Note that it relies on fixed base paths + * (convention over configuration) for optimization purposes. + */ +public class SlcJcrUtils implements SlcNames { public final static Integer AGENT_FACTORY_DEPTH = 3; - public final static Integer EXECUTION_MODULES_DEPTH = AGENT_FACTORY_DEPTH + 2; - public final static Integer EXECUTION_FLOWS_DEPTH = EXECUTION_MODULES_DEPTH + 3; /** Extracts the path of a flow relative to its execution module */ public static String flowRelativePath(String fullFlowPath) { String[] tokens = fullFlowPath.split("/"); StringBuffer buf = new StringBuffer(fullFlowPath.length()); - for (int i = EXECUTION_FLOWS_DEPTH; i < tokens.length; i++) { + for (int i = AGENT_FACTORY_DEPTH + 3; i < tokens.length; i++) { buf.append('/').append(tokens[i]); } return buf.toString(); } + /** Module node name based on module name and version */ + public static String getModuleNodeName(ModuleDescriptor moduleDescriptor) { + return moduleDescriptor.getName() + "_" + moduleDescriptor.getVersion(); + } + /** Extracts the execution module name of a flow */ public static String flowExecutionModuleName(String fullFlowPath) { String[] tokens = fullFlowPath.split("/"); - return tokens[EXECUTION_MODULES_DEPTH + 1]; + String moduleNodeName = tokens[AGENT_FACTORY_DEPTH + 2]; + return moduleNodeName.substring(0, moduleNodeName.lastIndexOf('_')); } /** Extracts the execution module version of a flow */ public static String flowExecutionModuleVersion(String fullFlowPath) { String[] tokens = fullFlowPath.split("/"); - return tokens[EXECUTION_MODULES_DEPTH + 2]; + String moduleNodeName = tokens[AGENT_FACTORY_DEPTH + 2]; + return moduleNodeName.substring(moduleNodeName.lastIndexOf('_') + 1); } /** Extracts the agent factory of a flow */ @@ -49,19 +66,128 @@ 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; + } + + /** + * Set the value of the primitive accessor as a JCR property. Does nothing + * if the value is null. + */ + public static void setPrimitiveAsProperty(Node node, String propertyName, + PrimitiveAccessor primitiveAccessor) { + String type = primitiveAccessor.getType(); + Object value = primitiveAccessor.getValue(); + setPrimitiveAsProperty(node, propertyName, type, value); + } + /** Map a primitive value to JCR property value. */ + public static void setPrimitiveAsProperty(Node node, String propertyName, + String type, Object value) { + if (value == null) + return; + if (value instanceof CharSequence) + value = PrimitiveUtils.convert(type, + ((CharSequence) value).toString()); + + try { + if (type.equals(PrimitiveAccessor.TYPE_STRING)) + 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)) + node.setProperty(propertyName, ((Long) value)); + else if (type.equals(PrimitiveAccessor.TYPE_FLOAT)) + node.setProperty(propertyName, (double) ((Float) value)); + else if (type.equals(PrimitiveAccessor.TYPE_DOUBLE)) + node.setProperty(propertyName, ((Double) value)); + else if (type.equals(PrimitiveAccessor.TYPE_BOOLEAN)) + node.setProperty(propertyName, ((Boolean) value)); + else + throw new SlcException("Unsupported type " + type); + } catch (RepositoryException e) { + throw new SlcException("Cannot set primitive of " + type + + " as property " + propertyName + " on " + node, e); + } + } + + /** 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() { } - // public static void main(String[] args) { - // String path = - // "/slc/agents/vm/54654654654/executionModules/org.argeo/1.2.3/myFlow"; - // System.out.println(flowRelativePath(path)); - // System.out.println(flowExecutionModuleName(path)); - // System.out.println(flowAgentFactoryPath(path)); - // } + public static void main(String[] args) { + String path = "/slc/agents/vm/default/org.argeo_1.2.3/myPath/myFlow"; + System.out.println("Flow relative path: " + flowRelativePath(path)); + System.out.println("Execution Module Name: " + + flowExecutionModuleName(path)); + System.out.println("Execution Module Version: " + + flowExecutionModuleVersion(path)); + System.out.println("Agent Factory path: " + flowAgentFactoryPath(path)); + } + }