]> 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
+ Fix some bugs on ResultTree Observer
[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[folders.size() + results.size()];
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 List<Node> 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 List<Node> nodes = new ArrayList<Node>();
88 while (ni.hasNext()) {
89 nodes.add(ni.nextNode());
90 }
91 return nodes;
92 } catch (RepositoryException re) {
93 throw new SlcException(
94 "Unexpected error while getting Results for given date", re);
95 }
96 }
97
98 /**
99 * recursively update passed status of the parent ResultFolder and its
100 * parent if needed
101 *
102 * @param node
103 * cannot be null
104 *
105 */
106 public static void updatePassedStatus(Node node, boolean passed) {
107 try {
108 Node pNode = node.getParent();
109 if (!pNode.hasNode(SlcNames.SLC_STATUS))
110 // we have reached the root of the tree. stop the
111 // recursivity
112 return;
113 boolean pStatus = pNode.getNode(SlcNames.SLC_STATUS)
114 .getProperty(SlcNames.SLC_SUCCESS).getBoolean();
115 if (pStatus == passed)
116 // nothing to update
117 return;
118 else if (!passed) {
119 // error we only update status of the result folder and its
120 // parent if needed
121 pNode.getNode(SlcNames.SLC_STATUS).setProperty(
122 SlcNames.SLC_SUCCESS, passed);
123 updatePassedStatus(pNode, passed);
124 } else {
125 // success we must first check if all siblings have also
126 // successfully completed
127 boolean success = true;
128 NodeIterator ni = pNode.getNodes();
129 children: while (ni.hasNext()) {
130 Node currNode = ni.nextNode();
131 if ((currNode.isNodeType(SlcTypes.SLC_DIFF_RESULT) || currNode
132 .isNodeType(SlcTypes.SLC_RESULT_FOLDER))
133 && !currNode.getNode(SlcNames.SLC_STATUS)
134 .getProperty(SlcNames.SLC_SUCCESS)
135 .getBoolean()) {
136 success = false;
137 break children;
138 }
139 }
140 if (success) {
141 pNode.getNode(SlcNames.SLC_STATUS).setProperty(
142 SlcNames.SLC_SUCCESS, passed);
143 updatePassedStatus(pNode, passed);
144 } else
145 // one of the siblings had also the failed status so
146 // above tree remains unchanged.
147 return;
148 }
149 } catch (RepositoryException e) {
150 throw new SlcException("Cannot register listeners", e);
151 }
152 }
153
154 public static void updateStatusOnRemoval(Node node) {
155 try {
156 if (!node.hasNode(SlcNames.SLC_STATUS))
157 // nothing to do
158 return;
159 boolean pStatus = node.getNode(SlcNames.SLC_STATUS)
160 .getProperty(SlcNames.SLC_SUCCESS).getBoolean();
161 if (pStatus == true)
162 // nothing to update
163 return;
164 else {
165 // success we must first check if all siblings have also
166 // successfully completed
167 boolean success = true;
168 NodeIterator ni = node.getNodes();
169 children: while (ni.hasNext()) {
170 Node currNode = ni.nextNode();
171 if ((currNode.isNodeType(SlcTypes.SLC_DIFF_RESULT) || currNode
172 .isNodeType(SlcTypes.SLC_RESULT_FOLDER))
173 && !currNode.getNode(SlcNames.SLC_STATUS)
174 .getProperty(SlcNames.SLC_SUCCESS)
175 .getBoolean()) {
176 success = false;
177 break children;
178 }
179 }
180 if (success) {
181 node.getNode(SlcNames.SLC_STATUS).setProperty(
182 SlcNames.SLC_SUCCESS, true);
183 updatePassedStatus(node, true);
184 } else
185 // one of the siblings had also the failed status so
186 // above tree remains unchanged.
187 return;
188 }
189 } catch (RepositoryException e) {
190 throw new SlcException(
191 "Unexpected error while updating status on removal", e);
192 }
193 }
194
195 }