]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/SlcJcrUtils.java
ab2f1540521673273d2ec2e97281392b181f64ec
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.jcr / src / main / java / org / argeo / slc / jcr / SlcJcrUtils.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.slc.jcr;
17
18 import java.util.Calendar;
19 import java.util.GregorianCalendar;
20
21 import javax.jcr.Node;
22 import javax.jcr.NodeIterator;
23 import javax.jcr.RepositoryException;
24 import javax.jcr.Session;
25
26 import org.argeo.jcr.JcrUtils;
27 import org.argeo.jcr.UserJcrUtils;
28 import org.argeo.slc.SlcException;
29 import org.argeo.slc.core.execution.PrimitiveAccessor;
30 import org.argeo.slc.core.execution.PrimitiveUtils;
31 import org.argeo.slc.deploy.ModuleDescriptor;
32 import org.argeo.slc.test.TestStatus;
33
34 /**
35 * Utilities around the SLC JCR model. Note that it relies on fixed base paths
36 * (convention over configuration) for optimization purposes.
37 */
38 public class SlcJcrUtils implements SlcNames {
39 public final static Integer AGENT_FACTORY_DEPTH = 3;
40
41 /** Extracts the path of a flow relative to its execution module */
42 public static String flowRelativePath(String fullFlowPath) {
43 String[] tokens = fullFlowPath.split("/");
44 StringBuffer buf = new StringBuffer(fullFlowPath.length());
45 for (int i = AGENT_FACTORY_DEPTH + 3; i < tokens.length; i++) {
46 buf.append('/').append(tokens[i]);
47 }
48 return buf.toString();
49 }
50
51 /** Extracts the path to the related execution module */
52 public static String modulePath(String fullFlowPath) {
53 String[] tokens = fullFlowPath.split("/");
54 StringBuffer buf = new StringBuffer(fullFlowPath.length());
55 for (int i = 0; i < AGENT_FACTORY_DEPTH + 3; i++) {
56 if (!tokens[i].equals(""))
57 buf.append('/').append(tokens[i]);
58 }
59 return buf.toString();
60 }
61
62 /** Extracts the module name from a flow path */
63 public static String moduleName(String fullFlowPath) {
64 String[] tokens = fullFlowPath.split("/");
65 String moduleName = tokens[AGENT_FACTORY_DEPTH + 2];
66 moduleName = moduleName.substring(0, moduleName.indexOf('_'));
67 return moduleName;
68 }
69
70 /** Module node name based on module name and version */
71 public static String getModuleNodeName(ModuleDescriptor moduleDescriptor) {
72 return moduleDescriptor.getName() + "_" + moduleDescriptor.getVersion();
73 }
74
75 /** Extracts the agent factory of a flow */
76 public static String flowAgentFactoryPath(String fullFlowPath) {
77 String[] tokens = fullFlowPath.split("/");
78 StringBuffer buf = new StringBuffer(fullFlowPath.length());
79 // first token is always empty
80 for (int i = 1; i < AGENT_FACTORY_DEPTH + 1; i++) {
81 buf.append('/').append(tokens[i]);
82 }
83 return buf.toString();
84 }
85
86 /** Create a new execution process path based on the current time */
87 public static String createExecutionProcessPath(Session session, String uuid) {
88 Calendar now = new GregorianCalendar();
89 return getSlcProcessesBasePath(session) + '/'
90 + JcrUtils.dateAsPath(now, true) + uuid;
91 }
92
93 /** Get the base for the user processi. */
94 public static String getSlcProcessesBasePath(Session session) {
95 try {
96 Node userHome = UserJcrUtils.getUserHome(session);
97 if (userHome == null)
98 throw new SlcException("No user home available for "
99 + session.getUserID());
100 return userHome.getPath() + '/' + SlcNames.SLC_SYSTEM + '/'
101 + SlcNames.SLC_PROCESSES;
102 } catch (RepositoryException re) {
103 throw new SlcException(
104 "Unexpected error while getting Slc Results Base Path.", re);
105 }
106 }
107
108 /**
109 * Create a new execution result path in the user home based on the current
110 * time
111 */
112 public static String createResultPath(Session session, String uuid)
113 throws RepositoryException {
114 Calendar now = new GregorianCalendar();
115 return SlcJcrResultUtils.getSlcResultsBasePath(session) + '/'
116 + JcrUtils.dateAsPath(now, true) + uuid;
117 }
118
119 /**
120 * Set the value of the primitive accessor as a JCR property. Does nothing
121 * if the value is null.
122 */
123 public static void setPrimitiveAsProperty(Node node, String propertyName,
124 PrimitiveAccessor primitiveAccessor) {
125 String type = primitiveAccessor.getType();
126 Object value = primitiveAccessor.getValue();
127 setPrimitiveAsProperty(node, propertyName, type, value);
128 }
129
130 /** Map a primitive value to JCR property value. */
131 public static void setPrimitiveAsProperty(Node node, String propertyName,
132 String type, Object value) {
133 if (value == null)
134 return;
135 if (value instanceof CharSequence)
136 value = PrimitiveUtils.convert(type,
137 ((CharSequence) value).toString());
138 if (value instanceof char[])
139 value = new String((char[]) value);
140
141 try {
142 if (type.equals(PrimitiveAccessor.TYPE_STRING))
143 node.setProperty(propertyName, value.toString());
144 else if (type.equals(PrimitiveAccessor.TYPE_PASSWORD))
145 node.setProperty(propertyName, value.toString());
146 else if (type.equals(PrimitiveAccessor.TYPE_INTEGER))
147 node.setProperty(propertyName, (long) ((Integer) value));
148 else if (type.equals(PrimitiveAccessor.TYPE_LONG))
149 node.setProperty(propertyName, ((Long) value));
150 else if (type.equals(PrimitiveAccessor.TYPE_FLOAT))
151 node.setProperty(propertyName, (double) ((Float) value));
152 else if (type.equals(PrimitiveAccessor.TYPE_DOUBLE))
153 node.setProperty(propertyName, ((Double) value));
154 else if (type.equals(PrimitiveAccessor.TYPE_BOOLEAN))
155 node.setProperty(propertyName, ((Boolean) value));
156 else
157 throw new SlcException("Unsupported type " + type);
158 } catch (RepositoryException e) {
159 throw new SlcException("Cannot set primitive of " + type
160 + " as property " + propertyName + " on " + node, e);
161 }
162 }
163
164 /** Aggregates the {@link TestStatus} of this sub-tree. */
165 public static Integer aggregateTestStatus(Node node) {
166 try {
167 Integer status = TestStatus.PASSED;
168 if (node.isNodeType(SlcTypes.SLC_CHECK))
169 if (node.getProperty(SLC_SUCCESS).getBoolean())
170 status = TestStatus.PASSED;
171 else if (node.hasProperty(SLC_ERROR_MESSAGE))
172 status = TestStatus.ERROR;
173 else
174 status = TestStatus.FAILED;
175
176 NodeIterator it = node.getNodes();
177 while (it.hasNext()) {
178 Integer childStatus = aggregateTestStatus(it.nextNode());
179 if (childStatus > status)
180 status = childStatus;
181 }
182 return status;
183 } catch (Exception e) {
184 throw new SlcException("Could not aggregate test status from "
185 + node, e);
186 }
187 }
188
189 /**
190 * Aggregates the {@link TestStatus} of this sub-tree.
191 *
192 * @return the same {@link StringBuffer}, for convenience (typically calling
193 * toString() on it)
194 */
195 public static StringBuffer aggregateTestMessages(Node node,
196 StringBuffer messages) {
197 try {
198 if (node.isNodeType(SlcTypes.SLC_CHECK)) {
199 if (node.hasProperty(SLC_MESSAGE)) {
200 if (messages.length() > 0)
201 messages.append('\n');
202 messages.append(node.getProperty(SLC_MESSAGE).getString());
203 }
204 if (node.hasProperty(SLC_ERROR_MESSAGE)) {
205 if (messages.length() > 0)
206 messages.append('\n');
207 messages.append(node.getProperty(SLC_ERROR_MESSAGE)
208 .getString());
209 }
210 }
211 NodeIterator it = node.getNodes();
212 while (it.hasNext()) {
213 Node child = it.nextNode();
214 aggregateTestMessages(child, messages);
215 }
216 return messages;
217 } catch (Exception e) {
218 throw new SlcException("Could not aggregate test messages from "
219 + node, e);
220 }
221 }
222
223 /** Prevents instantiation */
224 private SlcJcrUtils() {
225
226 }
227 }