1 package org.tigris.scarab.om; 2 3 48 49 import java.util.List ; 50 import java.util.ArrayList ; 51 import java.io.Serializable ; 52 import org.apache.torque.util.Criteria; 53 54 import org.tigris.scarab.om.Module; 56 57 63 public class QueryPeer 64 extends org.tigris.scarab.om.BaseQueryPeer 65 { 66 67 static final String GET_QUERIES = 68 "getQueries"; 69 static final String GET_USER_QUERIES = 70 "getUserQueries"; 71 static final String GET_MODULE_QUERIES = 72 "getModuleQueries"; 73 static final String QUERY_PEER = 74 "QueryPeer"; 75 76 public static final String TYPE_ALL_USER = "allPrivate"; 78 public static final String TYPE_PRIVATE = "private"; 79 public static final String TYPE_GLOBAL = "global"; 80 public static final String TYPE_ALL = "all"; 81 82 public static final String SORT_NAME = "name"; 84 public static final String SORT_DESCRIPTION = "desc"; 85 public static final String SORT_AVAILABILITY = "avail"; 86 public static final String SORT_USER = "user"; 87 88 94 public static List getQueries(Module module, IssueType issueType, 95 ScarabUser user, String sortColumn, 96 String sortPolarity, String type) 97 throws Exception 98 { 99 List queries = null; 100 if (module == null) 101 { 102 queries = new ArrayList (); 103 } 104 else 105 { 106 Serializable [] key = {QUERY_PEER, GET_QUERIES, module, null, 109 issueType, user, sortColumn, sortPolarity, type}; 110 Object obj = QueryManager.getMethodResult().get(key); 111 if (obj == null) 112 { 113 114 Criteria crit = new Criteria() 115 .add(QueryPeer.DELETED, 0); 116 117 Criteria.Criterion moduleCrit = crit.getNewCriterion( 118 QueryPeer.MODULE_ID, module.getModuleId(), Criteria.EQUAL); 119 Criteria.Criterion crossModule = crit.getNewCriterion( 120 QueryPeer.MODULE_ID, null, Criteria.EQUAL); 121 moduleCrit.or(crossModule); 122 123 if (issueType != null) 124 { 125 Criteria.Criterion issueTypeCrit = crit.getNewCriterion( 126 QueryPeer.ISSUE_TYPE_ID, issueType.getIssueTypeId(), 127 Criteria.EQUAL); 128 Criteria.Criterion nullIssueTypeCrit = crit.getNewCriterion( 129 QueryPeer.ISSUE_TYPE_ID, null, Criteria.EQUAL); 130 moduleCrit.and(issueTypeCrit.or(nullIssueTypeCrit)); 131 } 132 133 134 135 136 137 if (TYPE_PRIVATE.equals(type)) 138 { 139 crit.add(userPrivateQueriesCrits(user, crit, moduleCrit)); 140 } 141 else if (TYPE_GLOBAL.equals(type)) 142 { 143 crit.add(allGlobalQueriesCrit(crit, moduleCrit)); 144 } 145 else if (TYPE_ALL_USER.equals(type)) 146 { 147 Criteria.Criterion cuGlob = userUnapprovedQueriesCrits(user, crit, moduleCrit); 148 Criteria.Criterion cPriv = userPrivateQueriesCrits(user, crit, moduleCrit); 149 cuGlob.or(cPriv); 150 crit.add(cuGlob); 151 } 152 else 153 { 154 Criteria.Criterion cGlob = allGlobalQueriesCrit(crit, moduleCrit); 156 Criteria.Criterion cPriv = userPrivateQueriesCrits(user, crit, moduleCrit); 157 cGlob.or(cPriv); 158 crit.add(cGlob); 159 } 160 161 if (SORT_DESCRIPTION.equals(sortColumn)) 163 { 164 addSortOrder(crit, QueryPeer.DESCRIPTION, 165 sortPolarity); 166 } 167 else if (SORT_AVAILABILITY.equals(sortColumn)) 168 { 169 crit.addJoin(QueryPeer.SCOPE_ID, 170 ScopePeer.SCOPE_ID); 171 addSortOrder(crit, ScopePeer.SCOPE_NAME, sortPolarity); 172 } 173 else if (SORT_USER.equals(sortColumn)) 174 { 175 addSortOrder(crit, QueryPeer.USER_ID, sortPolarity); 176 } 177 else 178 { 179 addSortOrder(crit, QueryPeer.NAME, sortPolarity); 181 } 182 String tmp = crit.toString(); 183 queries = QueryPeer.doSelect(crit); 184 QueryManager.getMethodResult().put(queries, key); 185 } 186 else 187 { 188 queries = (List )obj; 189 } 190 } 191 return queries; 192 } 193 194 201 private static Criteria.Criterion userPrivateQueriesCrits(ScarabUser user, Criteria crit, Criteria.Criterion moduleCrit) 202 { 203 Criteria.Criterion cPriv = crit.getNewCriterion( 204 QueryPeer.USER_ID, user.getUserId(), Criteria.EQUAL); 205 cPriv.and(crit.getNewCriterion( 206 QueryPeer.SCOPE_ID, Scope.PERSONAL__PK, 207 Criteria.EQUAL)); 208 212 218 222 cPriv.and(moduleCrit); 223 return cPriv; 224 } 225 226 235 private static Criteria.Criterion userUnapprovedQueriesCrits(ScarabUser user, Criteria crit, Criteria.Criterion moduleCrit) 236 { 237 Criteria.Criterion cUserPendingCrit = crit.getNewCriterion( 238 QueryPeer.USER_ID, user.getUserId(), Criteria.EQUAL); 239 cUserPendingCrit.and(crit.getNewCriterion( 240 QueryPeer.SCOPE_ID, Scope.MODULE__PK, 241 Criteria.EQUAL)); 242 cUserPendingCrit.and(crit.getNewCriterion(QueryPeer.APPROVED, 243 Boolean.FALSE, Criteria.EQUAL)); 244 245 249 255 259 cUserPendingCrit.and(moduleCrit); 260 261 return cUserPendingCrit; 262 } 263 264 270 private static Criteria.Criterion allGlobalQueriesCrit(Criteria crit, Criteria.Criterion moduleCrit) 271 { 272 Criteria.Criterion cGlob = crit.getNewCriterion( 273 QueryPeer.SCOPE_ID, 274 Scope.MODULE__PK, 275 Criteria.EQUAL); 276 277 cGlob.and(crit.getNewCriterion(QueryPeer.APPROVED, 278 Boolean.TRUE, Criteria.EQUAL)); 279 cGlob.and(moduleCrit); 280 return cGlob; 281 } 282 283 284 285 public static List getUserQueries(ScarabUser user) 286 throws Exception 287 { 288 List queries = null; 289 Object obj = QueryManager.getMethodResult() 290 .get(QUERY_PEER, GET_USER_QUERIES, user); 291 if (obj == null) 292 { 293 Criteria crit = new Criteria() 294 .add(QueryPeer.DELETED, 0); 295 crit.add(QueryPeer.USER_ID, user.getUserId()); 296 queries = QueryPeer.doSelect(crit); 297 QueryManager.getMethodResult() 298 .put(queries, QUERY_PEER, GET_USER_QUERIES, user); 299 } 300 else 301 { 302 queries = (List )obj; 303 } 304 return queries; 305 } 306 307 public static List getModuleQueries(Module module) 308 throws Exception 309 { 310 List queries = null; 311 Object obj = QueryManager.getMethodResult() 312 .get(QUERY_PEER, GET_MODULE_QUERIES, module); 313 if (obj == null) 314 { 315 Criteria crit = new Criteria() 316 .add(QueryPeer.DELETED, 0); 317 crit.add(QueryPeer.MODULE_ID, module.getModuleId()); 318 crit.add(QueryPeer.SCOPE_ID, Scope.MODULE__PK); 319 queries = QueryPeer.doSelect(crit); 320 QueryManager.getMethodResult() 321 .put(queries, QUERY_PEER, GET_MODULE_QUERIES, module); 322 } 323 else 324 { 325 queries = (List )obj; 326 } 327 return queries; 328 } 329 330 public static List getQueries(Module module, IssueType issueType, 331 ScarabUser user) 332 throws Exception 333 { 334 return getQueries(module, issueType, user, SORT_AVAILABILITY, "asc", 335 TYPE_ALL); 336 } 337 338 public static List getQueries(Module module, IssueType issueType, 339 ScarabUser user, String sortColumn, 340 String sortPolarity) 341 throws Exception 342 { 343 return getQueries(module, issueType, user, sortColumn, 344 sortPolarity, TYPE_ALL); 345 } 346 347 private static Criteria addSortOrder(Criteria crit, 348 String sortColumn, String sortPolarity) 349 { 350 if (sortPolarity.equals("desc")) 351 { 352 crit.addDescendingOrderByColumn(sortColumn); 353 } 354 else 355 { 356 crit.addAscendingOrderByColumn(sortColumn); 357 } 358 return crit; 359 } 360 361 } 362 | Popular Tags |