]> git.argeo.org Git - gpl/argeo-slc.git/blob - plugins/org.argeo.slc.client.ui/src/main/java/org/argeo/slc/client/ui/model/ResultParentUtils.java
Make execution editor more robust
[gpl/argeo-slc.git] / plugins / org.argeo.slc.client.ui / src / main / java / org / argeo / slc / client / ui / model / ResultParentUtils.java
1 package org.argeo.slc.client.ui.model;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.Iterator;
6 import java.util.List;
7
8 import javax.jcr.Node;
9 import javax.jcr.NodeIterator;
10 import javax.jcr.Property;
11 import javax.jcr.RepositoryException;
12 import javax.jcr.Session;
13 import javax.jcr.query.Query;
14 import javax.jcr.query.QueryManager;
15 import javax.jcr.query.QueryResult;
16
17 import org.apache.commons.logging.Log;
18 import org.apache.commons.logging.LogFactory;
19 import org.argeo.slc.SlcException;
20 import org.argeo.slc.jcr.SlcJcrResultUtils;
21 import org.argeo.slc.jcr.SlcNames;
22 import org.argeo.slc.jcr.SlcTypes;
23
24 public class ResultParentUtils {
25 private final static Log log = LogFactory.getLog(ResultParentUtils.class);
26
27 public static Object[] orderChildren(Object[] children) {
28 List<ResultFolder> folders = new ArrayList<ResultFolder>();
29 List<SingleResultNode> results = new ArrayList<SingleResultNode>();
30 for (Object child : children) {
31 if (child instanceof ResultFolder)
32 folders.add((ResultFolder) child);
33 else if (child instanceof SingleResultNode)
34 results.add((SingleResultNode) child);
35 }
36
37 // Comparator first = Collections.reverseOrder();
38 Collections.sort(folders);
39 // Comparator<SingleResultNode> second = Collections.reverseOrder();
40 Collections.sort(results);
41
42 Object[] orderedChildren = new Object[children.length];
43 int i = 0;
44 Iterator<ResultFolder> it = folders.iterator();
45 while (it.hasNext()) {
46 orderedChildren[i] = it.next();
47 i++;
48 }
49 Iterator<SingleResultNode> it2 = results.iterator();
50 while (it2.hasNext()) {
51 orderedChildren[i] = it2.next();
52 i++;
53 }
54 return orderedChildren;
55 }
56
57 public static ResultParent[] getResultsForDates(Session session,
58 List<String> dateRelPathes) {
59 if (dateRelPathes == null || dateRelPathes.size() == 0)
60 throw new SlcException("Specify at least one correct date as Path");
61
62 try {
63 String basePath = SlcJcrResultUtils.getSlcResultsBasePath(session);
64 Iterator<String> it = dateRelPathes.iterator();
65 StringBuffer clause = new StringBuffer();
66 clause.append("SELECT * FROM [");
67 clause.append(SlcTypes.SLC_DIFF_RESULT);
68 clause.append("] as results");
69 clause.append(" WHERE ");
70 while (it.hasNext()) {
71 String absPath = basePath + "/" + it.next();
72 clause.append("ISDESCENDANTNODE(results, [");
73 clause.append(absPath);
74 clause.append("]) ");
75 clause.append(" OR ");
76 }
77 // remove last " OR "
78 clause.delete(clause.length() - 4, clause.length());
79 clause.append(" ORDER BY results.[" + Property.JCR_CREATED
80 + "] DESC");
81
82 // log.debug("request : " + clause.toString());
83 QueryManager qm = session.getWorkspace().getQueryManager();
84 Query q = qm.createQuery(clause.toString(), Query.JCR_SQL2);
85 QueryResult result = q.execute();
86
87 NodeIterator ni = result.getNodes();
88 ResultParent[] results = new ResultParent[(int) ni.getSize()];
89 int i = 0;
90 while (ni.hasNext()) {
91 Node currNode = ni.nextNode();
92 SingleResultNode srn = new SingleResultNode(null, currNode,
93 currNode.getProperty(SlcNames.SLC_TEST_CASE)
94 .getString());
95
96 results[i] = srn;
97 i++;
98 }
99 return results;
100 } catch (RepositoryException re) {
101 throw new SlcException(
102 "Unexpected error while getting Results for given date", re);
103 }
104 }
105
106 /**
107 * recursively update passed status of the parent ResultFolder and its
108 * parent if needed
109 *
110 * @param node
111 * cannot be null
112 *
113 */
114 public static void updatePassedStatus(Node node, boolean passed) {
115 try {
116 Node pNode = node.getParent();
117 if (!pNode.hasNode(SlcNames.SLC_STATUS))
118 // we have reached the root of the tree. stop the
119 // recursivity
120 return;
121 boolean pStatus = pNode.getNode(SlcNames.SLC_STATUS)
122 .getProperty(SlcNames.SLC_SUCCESS).getBoolean();
123 if (pStatus == passed)
124 // nothing to update
125 return;
126 else if (!passed) {
127 // error we only update status of the result folder and its
128 // parent if needed
129 pNode.getNode(SlcNames.SLC_STATUS).setProperty(
130 SlcNames.SLC_SUCCESS, passed);
131 updatePassedStatus(pNode, passed);
132 } else {
133 // success we must first check if all siblings have also
134 // successfully completed
135 boolean success = true;
136 NodeIterator ni = pNode.getNodes();
137 children: while (ni.hasNext()) {
138 Node currNode = ni.nextNode();
139 if ((currNode.isNodeType(SlcTypes.SLC_DIFF_RESULT) || currNode
140 .isNodeType(SlcTypes.SLC_RESULT_FOLDER))
141 && !currNode.getNode(SlcNames.SLC_STATUS)
142 .getProperty(SlcNames.SLC_SUCCESS)
143 .getBoolean()) {
144 success = false;
145 break children;
146 }
147 }
148 if (success) {
149 pNode.getNode(SlcNames.SLC_STATUS).setProperty(
150 SlcNames.SLC_SUCCESS, passed);
151 updatePassedStatus(pNode, passed);
152 } else
153 // one of the siblings had also the failed status so
154 // above tree remains unchanged.
155 return;
156 }
157 } catch (RepositoryException e) {
158 throw new SlcException("Cannot register listeners", e);
159 }
160 }
161
162 public static void updateStatusOnRemoval(Node node) {
163 try {
164 if (!node.hasNode(SlcNames.SLC_STATUS))
165 // nothing to do
166 return;
167 boolean pStatus = node.getNode(SlcNames.SLC_STATUS)
168 .getProperty(SlcNames.SLC_SUCCESS).getBoolean();
169 if (pStatus == true)
170 // nothing to update
171 return;
172 else {
173 // success we must first check if all siblings have also
174 // successfully completed
175 boolean success = true;
176 NodeIterator ni = node.getNodes();
177 children: while (ni.hasNext()) {
178 Node currNode = ni.nextNode();
179 if ((currNode.isNodeType(SlcTypes.SLC_DIFF_RESULT) || currNode
180 .isNodeType(SlcTypes.SLC_RESULT_FOLDER))
181 && !currNode.getNode(SlcNames.SLC_STATUS)
182 .getProperty(SlcNames.SLC_SUCCESS)
183 .getBoolean()) {
184 success = false;
185 break children;
186 }
187 }
188 if (success) {
189 node.getNode(SlcNames.SLC_STATUS).setProperty(
190 SlcNames.SLC_SUCCESS, true);
191 updatePassedStatus(node, true);
192 } else
193 // one of the siblings had also the failed status so
194 // above tree remains unchanged.
195 return;
196 }
197 } catch (RepositoryException e) {
198 throw new SlcException("Unexpected error while updating status on removal", e);
199 }
200 }
201
202 }