1 package org.tigris.scarab.om; 2 3 import java.util.List ; 4 import java.util.Comparator ; 5 import java.util.Collections ; 6 import java.io.Serializable ; 7 import org.apache.torque.util.Criteria; 8 import org.tigris.scarab.services.cache.ScarabCache; 9 10 import org.tigris.scarab.om.Module; 12 13 18 public class IssueTemplateInfoPeer 19 extends org.tigris.scarab.om.BaseIssueTemplateInfoPeer 20 { 21 22 private static final String GET_TEMPLATES = 23 "getTemplates"; 24 static final String GET_USER_TEMPLATES = 25 "getUserTemplates"; 26 static final String GET_MODULE_TEMPLATES = 27 "getModuleTemplates"; 28 static final String TEMPLATE_PEER = 29 "IssueTemplateInfoPeer"; 30 31 public static final String TYPE_PRIVATE = "private"; 33 public static final String TYPE_GLOBAL = "global"; 34 public static final String TYPE_ALL = "all"; 35 36 41 public static List getTemplates(Module module, IssueType issueType, 42 ScarabUser user, String sortColumn, 43 String sortPolarity, String type) 44 throws Exception 45 { 46 List templates = null; 47 Serializable [] key = {TEMPLATE_PEER, GET_TEMPLATES, module, null, 48 issueType, user, sortColumn, sortPolarity, type}; 49 Object obj = ScarabCache.get(key); 50 if (obj == null) 51 { 52 Criteria crit = new Criteria() 53 .add(IssuePeer.MODULE_ID, module.getModuleId()) 54 .add(IssuePeer.DELETED, 0) 55 .addJoin(ActivitySetPeer.TRANSACTION_ID, 56 ActivityPeer.TRANSACTION_ID) 57 .addJoin(IssuePeer.ISSUE_ID, 58 ActivityPeer.ISSUE_ID) 59 .add(IssuePeer.TYPE_ID, issueType.getTemplateId()) 60 .addJoin(IssueTemplateInfoPeer.ISSUE_ID, 61 IssuePeer.ISSUE_ID); 62 crit.setDistinct(); 63 64 Criteria.Criterion cGlob = crit.getNewCriterion( 65 IssueTemplateInfoPeer.SCOPE_ID, Scope.MODULE__PK, 66 Criteria.EQUAL); 67 cGlob.and(crit.getNewCriterion(IssueTemplateInfoPeer.APPROVED, 68 Boolean.TRUE, Criteria.EQUAL)); 69 Criteria.Criterion cPriv = crit.getNewCriterion( 70 ActivitySetPeer.CREATED_BY, user.getUserId(), 71 Criteria.EQUAL); 72 cPriv.and(crit.getNewCriterion( 73 IssueTemplateInfoPeer.SCOPE_ID, Scope.PERSONAL__PK, 74 Criteria.EQUAL)); 75 76 if (TYPE_PRIVATE.equals(type)) 77 { 78 crit.add(cPriv); 79 } 80 else if (TYPE_GLOBAL.equals(type)) 81 { 82 crit.add(cGlob); 83 } 84 else 85 { 86 cGlob.or(cPriv); 88 crit.add(cGlob); 89 } 90 91 if (sortColumn.equals("desc")) 93 { 94 addSortOrder(crit, IssueTemplateInfoPeer.DESCRIPTION, 95 sortPolarity); 96 } 97 else if (sortColumn.equals("avail")) 98 { 99 crit.addJoin(IssueTemplateInfoPeer.SCOPE_ID, 100 ScopePeer.SCOPE_ID); 101 addSortOrder(crit, ScopePeer.SCOPE_NAME, sortPolarity); 102 } 103 else if (!sortColumn.equals("user")) 104 { 105 addSortOrder(crit, IssueTemplateInfoPeer.NAME, sortPolarity); 107 } 108 templates = IssueTemplateInfoPeer.doSelect(crit); 109 ScarabCache.put(templates, key); 110 } 111 else 112 { 113 templates = (List )obj; 114 } 115 if (sortColumn.equals("user")) 116 { 117 templates = sortByCreatingUser(templates, sortPolarity); 118 } 119 return templates; 120 } 121 122 public static List getUserTemplates(ScarabUser user, Module module,IssueType issueType) 123 throws Exception 124 { 125 List templates = null; 126 Object obj = ScarabCache.get(TEMPLATE_PEER, GET_USER_TEMPLATES, user); 127 if (obj == null) 128 { 129 Criteria crit = new Criteria() 130 .addJoin(IssueTemplateInfoPeer.ISSUE_ID, 131 IssuePeer.ISSUE_ID) 132 .add(IssuePeer.DELETED, 0) 133 .add(IssuePeer.MODULE_ID, module.getModuleId()) 134 .addJoin(ActivitySetPeer.TRANSACTION_ID, 135 ActivityPeer.TRANSACTION_ID) 136 .addJoin(IssuePeer.ISSUE_ID, 137 ActivityPeer.ISSUE_ID) 138 .add(IssuePeer.TYPE_ID, issueType.getTemplateId()) 139 .add(ActivitySetPeer.CREATED_BY, user.getUserId()); 140 templates = IssueTemplateInfoPeer.doSelect(crit); 141 ScarabCache.put(templates, TEMPLATE_PEER, GET_USER_TEMPLATES, user); 142 } 143 else 144 { 145 templates = (List )obj; 146 } 147 return templates; 148 } 149 150 151 public static List getModuleTemplates(Module module) 152 throws Exception 153 { 154 List templates = null; 155 Object obj = ScarabCache.get(TEMPLATE_PEER, GET_MODULE_TEMPLATES, module); 156 if (obj == null) 157 { 158 Criteria crit = new Criteria() 159 .addJoin(IssueTemplateInfoPeer.ISSUE_ID, 160 IssuePeer.ISSUE_ID) 161 .add(IssuePeer.DELETED, 0) 162 .add(IssuePeer.MODULE_ID, module.getModuleId()) 163 .add(IssueTemplateInfoPeer.SCOPE_ID, Scope.MODULE__PK); 164 templates = IssueTemplateInfoPeer.doSelect(crit); 165 ScarabCache.put(templates, TEMPLATE_PEER, GET_USER_TEMPLATES, module); 166 } 167 else 168 { 169 templates = (List )obj; 170 } 171 return templates; 172 } 173 174 private static Criteria addSortOrder(Criteria crit, 175 String sortColumn, String sortPolarity) 176 { 177 if (sortPolarity.equals("desc")) 178 { 179 crit.addDescendingOrderByColumn(sortColumn); 180 } 181 else 182 { 183 crit.addAscendingOrderByColumn(sortColumn); 184 } 185 return crit; 186 } 187 188 private static List sortByCreatingUser(List result, 189 String sortPolarity) 190 throws Exception 191 { 192 final int polarity = ("asc".equals(sortPolarity)) ? 1 : -1; 193 Comparator c = new Comparator () 194 { 195 public int compare(Object o1, Object o2) 196 { 197 int i = 0; 198 try 199 { 200 i = polarity * 201 ((Issue)o1).getCreatedBy().getFirstName() 202 .compareTo(((Issue)o2).getCreatedBy().getFirstName()); 203 } 204 catch (Exception e) 205 { 206 } 208 return i; 209 } 210 }; 211 Collections.sort(result, c); 212 return result; 213 } 214 215 } 216 | Popular Tags |