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