1 package org
.argeo
.slc
.jcr
;
3 import java
.util
.Calendar
;
4 import java
.util
.GregorianCalendar
;
7 import javax
.jcr
.RepositoryException
;
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
;
16 * Utilities around the SLC JCR model. Note that it relies on fixed base paths
17 * (convention over configuration) for optimization purposes.
19 public class SlcJcrUtils
{
20 public final static Integer AGENT_FACTORY_DEPTH
= 3;
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
]);
29 return buf
.toString();
32 /** Module node name based on module name and version */
33 public static String
getModuleNodeName(ModuleDescriptor moduleDescriptor
) {
34 return moduleDescriptor
.getName() + "_" + moduleDescriptor
.getVersion();
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('_'));
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);
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
]);
59 return buf
.toString();
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
;
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
;
77 * Set the value of the primitive accessor as a JCR property. Does nothing
78 * if the value is null.
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
);
87 /** Map a primitive value to JCR property value. */
88 public static void setPrimitiveAsProperty(Node node
, String propertyName
,
89 String type
, Object value
) {
92 if (value
instanceof CharSequence
)
93 value
= PrimitiveUtils
.convert(type
,
94 ((CharSequence
) value
).toString());
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
));
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
);
117 /** Prevents instantiation */
118 private SlcJcrUtils() {
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
));