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