]>
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
2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.slc
.client
.ui
.model
;
18 import java
.util
.ArrayList
;
19 import java
.util
.Collections
;
20 import java
.util
.Iterator
;
21 import java
.util
.List
;
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
;
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
;
37 public class ResultParentUtils
{
38 // private final static Log log =
39 // LogFactory.getLog(ResultParentUtils.class);
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
);
51 // Comparator first = Collections.reverseOrder();
52 Collections
.sort(folders
);
53 // Comparator<SingleResultNode> second = Collections.reverseOrder();
54 Collections
.sort(results
);
56 Object
[] orderedChildren
= new Object
[folders
.size() + results
.size()];
58 Iterator
<ResultFolder
> it
= folders
.iterator();
59 while (it
.hasNext()) {
60 orderedChildren
[i
] = it
.next();
63 Iterator
<SingleResultNode
> it2
= results
.iterator();
64 while (it2
.hasNext()) {
65 orderedChildren
[i
] = it2
.next();
68 return orderedChildren
;
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");
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
);
89 clause
.append(" OR ");
92 clause
.delete(clause
.length() - 4, clause
.length());
93 clause
.append(" ORDER BY results.[" + Property
.JCR_CREATED
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();
101 NodeIterator ni
= result
.getNodes();
102 List
<Node
> nodes
= new ArrayList
<Node
>();
103 while (ni
.hasNext()) {
104 nodes
.add(ni
.nextNode());
107 } catch (RepositoryException re
) {
108 throw new SlcException(
109 "Unexpected error while getting Results for given date", re
);
114 * recursively update passed status of the parent ResultFolder and its
121 public static void updatePassedStatus(Node node
, boolean passed
) {
123 Node pNode
= node
.getParent();
124 if (!pNode
.hasNode(SlcNames
.SLC_STATUS
))
125 // we have reached the root of the tree. stop the
128 boolean pStatus
= pNode
.getNode(SlcNames
.SLC_STATUS
)
129 .getProperty(SlcNames
.SLC_SUCCESS
).getBoolean();
130 if (pStatus
== passed
)
134 // error we only update status of the result folder and its
136 pNode
.getNode(SlcNames
.SLC_STATUS
).setProperty(
137 SlcNames
.SLC_SUCCESS
, passed
);
138 updatePassedStatus(pNode
, passed
);
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
)
156 pNode
.getNode(SlcNames
.SLC_STATUS
).setProperty(
157 SlcNames
.SLC_SUCCESS
, passed
);
158 updatePassedStatus(pNode
, passed
);
160 // one of the siblings had also the failed status so
161 // above tree remains unchanged.
164 } catch (RepositoryException e
) {
165 throw new SlcException("Cannot register listeners", e
);
169 public static void updateStatusOnRemoval(Node node
) {
171 if (!node
.hasNode(SlcNames
.SLC_STATUS
))
174 boolean pStatus
= node
.getNode(SlcNames
.SLC_STATUS
)
175 .getProperty(SlcNames
.SLC_SUCCESS
).getBoolean();
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
)
196 node
.getNode(SlcNames
.SLC_STATUS
).setProperty(
197 SlcNames
.SLC_SUCCESS
, true);
198 updatePassedStatus(node
, true);
200 // one of the siblings had also the failed status so
201 // above tree remains unchanged.
204 } catch (RepositoryException e
) {
205 throw new SlcException(
206 "Unexpected error while updating status on removal", e
);