]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.client.ui/src/org/argeo/slc/client/ui/model/ResultParentUtils.java
Add doAs in RCP CmsView.
[gpl/argeo-slc.git] / org.argeo.slc.client.ui / src / 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.Iterator;
5 import java.util.List;
6
7 import javax.jcr.Node;
8 import javax.jcr.NodeIterator;
9 import javax.jcr.Property;
10 import javax.jcr.RepositoryException;
11 import javax.jcr.Session;
12 import javax.jcr.query.Query;
13 import javax.jcr.query.QueryManager;
14 import javax.jcr.query.QueryResult;
15
16 import org.argeo.slc.SlcException;
17 import org.argeo.slc.SlcNames;
18 import org.argeo.slc.SlcTypes;
19 import org.argeo.slc.jcr.SlcJcrResultUtils;
20
21 public class ResultParentUtils {
22 // private final static Log log =
23 // LogFactory.getLog(ResultParentUtils.class);
24
25 // public static Object[] orderChildren(Object[] children) {
26 // List<ResultFolder> folders = new ArrayList<ResultFolder>();
27 // List<SingleResultNode> results = new ArrayList<SingleResultNode>();
28 // for (Object child : children) {
29 // if (child instanceof ResultFolder)
30 // folders.add((ResultFolder) child);
31 // else if (child instanceof SingleResultNode)
32 // results.add((SingleResultNode) child);
33 // }
34 //
35 // // Comparator first = Collections.reverseOrder();
36 // Collections.sort(folders);
37 // // Comparator<SingleResultNode> second = Collections.reverseOrder();
38 // Collections.sort(results);
39 //
40 // Object[] orderedChildren = new Object[folders.size() + results.size()];
41 // int i = 0;
42 // Iterator<ResultFolder> it = folders.iterator();
43 // while (it.hasNext()) {
44 // orderedChildren[i] = it.next();
45 // i++;
46 // }
47 // Iterator<SingleResultNode> it2 = results.iterator();
48 // while (it2.hasNext()) {
49 // orderedChildren[i] = it2.next();
50 // i++;
51 // }
52 // return orderedChildren;
53 // }
54
55 public static List<Node> getResultsForDates(Session session,
56 List<String> dateRelPathes) {
57 if (dateRelPathes == null || dateRelPathes.size() == 0)
58 throw new SlcException("Specify at least one correct date as Path");
59
60 try {
61 String basePath = SlcJcrResultUtils.getSlcResultsBasePath(session);
62 Iterator<String> it = dateRelPathes.iterator();
63 StringBuffer clause = new StringBuffer();
64 clause.append("SELECT * FROM [");
65 clause.append(SlcTypes.SLC_TEST_RESULT);
66 clause.append("] as results");
67 clause.append(" WHERE ");
68 while (it.hasNext()) {
69 String absPath = basePath + "/" + it.next();
70 clause.append("ISDESCENDANTNODE(results, [");
71 clause.append(absPath);
72 clause.append("]) ");
73 clause.append(" OR ");
74 }
75 // remove last " OR "
76 clause.delete(clause.length() - 4, clause.length());
77 clause.append(" ORDER BY results.[" + Property.JCR_CREATED
78 + "] DESC");
79
80 // log.debug("request : " + clause.toString());
81 QueryManager qm = session.getWorkspace().getQueryManager();
82 Query q = qm.createQuery(clause.toString(), Query.JCR_SQL2);
83 QueryResult result = q.execute();
84
85 NodeIterator ni = result.getNodes();
86 List<Node> nodes = new ArrayList<Node>();
87 while (ni.hasNext()) {
88 nodes.add(ni.nextNode());
89 }
90 return nodes;
91 } catch (RepositoryException re) {
92 throw new SlcException(
93 "Unexpected error while getting Results for given date", re);
94 }
95 }
96
97 /**
98 * recursively update passed status of the current ResultFolder and its
99 * parent if needed
100 *
101 * @param node
102 * cannot be null
103 *
104 */
105 public static void updatePassedStatus(Node node, boolean passed) {
106 try {
107 if (!node.hasNode(SlcNames.SLC_AGGREGATED_STATUS))
108 // we have reached the root of the tree. stop the
109 // recursivity
110 return;
111 boolean pStatus = node.getNode(SlcNames.SLC_AGGREGATED_STATUS)
112 .getProperty(SlcNames.SLC_SUCCESS).getBoolean();
113 if (pStatus == passed)
114 // nothing to update
115 return;
116 else if (!passed) {
117 // New status is 'failed' : we only update status of the result
118 // folder and its
119 // parent if needed
120 node.getNode(SlcNames.SLC_AGGREGATED_STATUS).setProperty(
121 SlcNames.SLC_SUCCESS, passed);
122 updatePassedStatus(node.getParent(), passed);
123 } else {
124 // New status is 'passed': we must first check if all siblings
125 // have also
126 // successfully completed
127 boolean success = true;
128 NodeIterator ni = node.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
134 .getNode(SlcNames.SLC_AGGREGATED_STATUS)
135 .getProperty(SlcNames.SLC_SUCCESS)
136 .getBoolean()) {
137 success = false;
138 break children;
139 }
140 }
141 if (success) {
142 node.getNode(SlcNames.SLC_AGGREGATED_STATUS).setProperty(
143 SlcNames.SLC_SUCCESS, passed);
144 updatePassedStatus(node.getParent(), passed);
145 } else
146 // one of the siblings had also the failed status so
147 // above tree remains unchanged.
148 return;
149 }
150 } catch (RepositoryException e) {
151 throw new SlcException("Cannot update result passed status", e);
152 }
153 }
154 }