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