]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/SlcJcrUtils.java
introduce a new view to display JcrResults has a tree.
[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 Mathieu Baudier
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 /** Module node name based on module name and version */
63 public static String getModuleNodeName(ModuleDescriptor moduleDescriptor) {
64 return moduleDescriptor.getName() + "_" + moduleDescriptor.getVersion();
65 }
66
67 /** Extracts the agent factory of a flow */
68 public static String flowAgentFactoryPath(String fullFlowPath) {
69 String[] tokens = fullFlowPath.split("/");
70 StringBuffer buf = new StringBuffer(fullFlowPath.length());
71 // first token is always empty
72 for (int i = 1; i < AGENT_FACTORY_DEPTH + 1; i++) {
73 buf.append('/').append(tokens[i]);
74 }
75 return buf.toString();
76 }
77
78 /** Create a new execution process path based on the current time */
79 public static String createExecutionProcessPath(String uuid) {
80 Calendar now = new GregorianCalendar();
81 return SlcJcrConstants.PROCESSES_BASE_PATH + '/'
82 + JcrUtils.dateAsPath(now, true) + uuid;
83 }
84
85 /**
86 * Create a new execution result path in the user home based on the current
87 * time
88 */
89 public static String createResultPath(Session session, String uuid)
90 throws RepositoryException {
91 Calendar now = new GregorianCalendar();
92 return UserJcrUtils.getUserHome(session).getPath() + '/'
93 + SlcNames.SLC_RESULTS + '/' + JcrUtils.dateAsPath(now, true)
94 + uuid;
95 }
96
97 /**
98 * Set the value of the primitive accessor as a JCR property. Does nothing
99 * if the value is null.
100 */
101 public static void setPrimitiveAsProperty(Node node, String propertyName,
102 PrimitiveAccessor primitiveAccessor) {
103 String type = primitiveAccessor.getType();
104 Object value = primitiveAccessor.getValue();
105 setPrimitiveAsProperty(node, propertyName, type, value);
106 }
107
108 /** Map a primitive value to JCR property value. */
109 public static void setPrimitiveAsProperty(Node node, String propertyName,
110 String type, Object value) {
111 if (value == null)
112 return;
113 if (value instanceof CharSequence)
114 value = PrimitiveUtils.convert(type,
115 ((CharSequence) value).toString());
116
117 try {
118 if (type.equals(PrimitiveAccessor.TYPE_STRING))
119 node.setProperty(propertyName, value.toString());
120 else if (type.equals(PrimitiveAccessor.TYPE_INTEGER))
121 node.setProperty(propertyName, (long) ((Integer) value));
122 else if (type.equals(PrimitiveAccessor.TYPE_LONG))
123 node.setProperty(propertyName, ((Long) value));
124 else if (type.equals(PrimitiveAccessor.TYPE_FLOAT))
125 node.setProperty(propertyName, (double) ((Float) value));
126 else if (type.equals(PrimitiveAccessor.TYPE_DOUBLE))
127 node.setProperty(propertyName, ((Double) value));
128 else if (type.equals(PrimitiveAccessor.TYPE_BOOLEAN))
129 node.setProperty(propertyName, ((Boolean) value));
130 else
131 throw new SlcException("Unsupported type " + type);
132 } catch (RepositoryException e) {
133 throw new SlcException("Cannot set primitive of " + type
134 + " as property " + propertyName + " on " + node, e);
135 }
136 }
137
138 /** Aggregates the {@link TestStatus} of this sub-tree. */
139 public static Integer aggregateTestStatus(Node node) {
140 try {
141 Integer status = TestStatus.PASSED;
142 if (node.isNodeType(SlcTypes.SLC_CHECK))
143 if (node.getProperty(SLC_SUCCESS).getBoolean())
144 status = TestStatus.PASSED;
145 else if (node.hasProperty(SLC_ERROR_MESSAGE))
146 status = TestStatus.ERROR;
147 else
148 status = TestStatus.FAILED;
149
150 NodeIterator it = node.getNodes();
151 while (it.hasNext()) {
152 Integer childStatus = aggregateTestStatus(it.nextNode());
153 if (childStatus > status)
154 status = childStatus;
155 }
156 return status;
157 } catch (Exception e) {
158 throw new SlcException("Could not aggregate test status from "
159 + node, e);
160 }
161 }
162
163 /**
164 * Aggregates the {@link TestStatus} of this sub-tree.
165 *
166 * @return the same {@link StringBuffer}, for convenience (typically calling
167 * toString() on it)
168 */
169 public static StringBuffer aggregateTestMessages(Node node,
170 StringBuffer messages) {
171 try {
172 if (node.isNodeType(SlcTypes.SLC_CHECK)) {
173 if (node.hasProperty(SLC_MESSAGE)) {
174 if (messages.length() > 0)
175 messages.append('\n');
176 messages.append(node.getProperty(SLC_MESSAGE).getString());
177 }
178 if (node.hasProperty(SLC_ERROR_MESSAGE)) {
179 if (messages.length() > 0)
180 messages.append('\n');
181 messages.append(node.getProperty(SLC_ERROR_MESSAGE)
182 .getString());
183 }
184 }
185 NodeIterator it = node.getNodes();
186 while (it.hasNext()) {
187 Node child = it.nextNode();
188 aggregateTestMessages(child, messages);
189 }
190 return messages;
191 } catch (Exception e) {
192 throw new SlcException("Could not aggregate test messages from "
193 + node, e);
194 }
195 }
196
197 /** Prevents instantiation */
198 private SlcJcrUtils() {
199
200 }
201 }