]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/SlcJcrUtils.java
Deal with result parts
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.jcr / src / main / java / org / argeo / slc / jcr / SlcJcrUtils.java
1 package org.argeo.slc.jcr;
2
3 import java.util.Calendar;
4 import java.util.GregorianCalendar;
5
6 import javax.jcr.Node;
7 import javax.jcr.RepositoryException;
8
9 import org.argeo.jcr.JcrUtils;
10 import org.argeo.slc.SlcException;
11 import org.argeo.slc.core.execution.PrimitiveAccessor;
12 import org.argeo.slc.core.execution.PrimitiveUtils;
13 import org.argeo.slc.deploy.ModuleDescriptor;
14
15 /**
16 * Utilities around the SLC JCR model. Note that it relies on fixed base paths
17 * (convention over configuration) for optimization purposes.
18 */
19 public class SlcJcrUtils {
20 public final static Integer AGENT_FACTORY_DEPTH = 3;
21
22 /** Extracts the path of a flow relative to its execution module */
23 public static String flowRelativePath(String fullFlowPath) {
24 String[] tokens = fullFlowPath.split("/");
25 StringBuffer buf = new StringBuffer(fullFlowPath.length());
26 for (int i = AGENT_FACTORY_DEPTH + 3; i < tokens.length; i++) {
27 buf.append('/').append(tokens[i]);
28 }
29 return buf.toString();
30 }
31
32 /** Module node name based on module name and version */
33 public static String getModuleNodeName(ModuleDescriptor moduleDescriptor) {
34 return moduleDescriptor.getName() + "_" + moduleDescriptor.getVersion();
35 }
36
37 /** Extracts the execution module name of a flow */
38 public static String flowExecutionModuleName(String fullFlowPath) {
39 String[] tokens = fullFlowPath.split("/");
40 String moduleNodeName = tokens[AGENT_FACTORY_DEPTH + 2];
41 return moduleNodeName.substring(0, moduleNodeName.lastIndexOf('_'));
42 }
43
44 /** Extracts the execution module version of a flow */
45 public static String flowExecutionModuleVersion(String fullFlowPath) {
46 String[] tokens = fullFlowPath.split("/");
47 String moduleNodeName = tokens[AGENT_FACTORY_DEPTH + 2];
48 return moduleNodeName.substring(moduleNodeName.lastIndexOf('_') + 1);
49 }
50
51 /** Extracts the agent factory of a flow */
52 public static String flowAgentFactoryPath(String fullFlowPath) {
53 String[] tokens = fullFlowPath.split("/");
54 StringBuffer buf = new StringBuffer(fullFlowPath.length());
55 // first token is always empty
56 for (int i = 1; i < AGENT_FACTORY_DEPTH + 1; i++) {
57 buf.append('/').append(tokens[i]);
58 }
59 return buf.toString();
60 }
61
62 /** Create a new execution process path based on the current time */
63 public static String createExecutionProcessPath(String uuid) {
64 Calendar now = new GregorianCalendar();
65 return SlcJcrConstants.PROCESSES_BASE_PATH + '/'
66 + JcrUtils.dateAsPath(now, true) + uuid;
67 }
68
69 /** Create a new execution result path based on the current time */
70 public static String createResultPath(String uuid) {
71 Calendar now = new GregorianCalendar();
72 return SlcJcrConstants.RESULTS_BASE_PATH + '/'
73 + JcrUtils.dateAsPath(now, true) + uuid;
74 }
75
76 /**
77 * Set the value of the primitive accessor as a JCR property. Does nothing
78 * if the value is null.
79 */
80 public static void setPrimitiveAsProperty(Node node, String propertyName,
81 PrimitiveAccessor primitiveAccessor) {
82 String type = primitiveAccessor.getType();
83 Object value = primitiveAccessor.getValue();
84 setPrimitiveAsProperty(node, propertyName, type, value);
85 }
86
87 /** Map a primitive value to JCR ptoperty value. */
88 public static void setPrimitiveAsProperty(Node node, String propertyName,
89 String type, Object value) {
90 if (value == null)
91 return;
92 if (value instanceof CharSequence)
93 value = PrimitiveUtils.convert(type,
94 ((CharSequence) value).toString());
95
96 try {
97 if (type.equals(PrimitiveAccessor.TYPE_STRING))
98 node.setProperty(propertyName, value.toString());
99 else if (type.equals(PrimitiveAccessor.TYPE_INTEGER))
100 node.setProperty(propertyName, (long) ((Integer) value));
101 else if (type.equals(PrimitiveAccessor.TYPE_LONG))
102 node.setProperty(propertyName, ((Long) value));
103 else if (type.equals(PrimitiveAccessor.TYPE_FLOAT))
104 node.setProperty(propertyName, (double) ((Float) value));
105 else if (type.equals(PrimitiveAccessor.TYPE_DOUBLE))
106 node.setProperty(propertyName, ((Double) value));
107 else if (type.equals(PrimitiveAccessor.TYPE_BOOLEAN))
108 node.setProperty(propertyName, ((Boolean) value));
109 else
110 throw new SlcException("Unsupported type " + type);
111 } catch (RepositoryException e) {
112 throw new SlcException("Cannot set primitive of " + type
113 + " as property " + propertyName + " on " + node, e);
114 }
115 }
116
117 /** Prevents instantiation */
118 private SlcJcrUtils() {
119
120 }
121
122 public static void main(String[] args) {
123 String path = "/slc/agents/vm/default/org.argeo_1.2.3/myPath/myFlow";
124 System.out.println("Flow relative path: " + flowRelativePath(path));
125 System.out.println("Execution Module Name: "
126 + flowExecutionModuleName(path));
127 System.out.println("Execution Module Version: "
128 + flowExecutionModuleVersion(path));
129 System.out.println("Agent Factory path: " + flowAgentFactoryPath(path));
130 }
131
132 }