1 package org.tigris.scarab.om; 2 3 48 49 import java.io.Serializable ; 50 import java.util.List ; 51 import java.util.ArrayList ; 52 import java.util.Comparator ; 53 import java.util.Collections ; 54 55 import org.apache.torque.util.Criteria; 56 import org.tigris.scarab.services.cache.ScarabCache; 57 58 64 public class AttributePeer 65 extends org.tigris.scarab.om.BaseAttributePeer 66 { 67 public static final Integer ASSIGNED_TO__PK = new Integer (2); 68 public static final Integer STATUS__PK = new Integer (3); 69 public static final Integer RESOLUTION__PK = new Integer (4); 70 public static final Integer TOTAL_VOTES__PK = new Integer (13); 71 public static final String EMAIL_TO = "to"; 72 public static final String CC_TO = "cc"; 73 public static final String USER = "user"; 74 public static final String NON_USER = "non-user"; 75 76 private static final String ATTRIBUTE_PEER = 77 "AttributePeer"; 78 79 82 public static List getAttributes(String attributeType, boolean includeDeleted, 83 String sortColumn, String sortPolarity) 84 throws Exception 85 { 86 List result = null; 87 Boolean deletedBool = (includeDeleted ? Boolean.TRUE : Boolean.FALSE); 88 Serializable [] keys = {ATTRIBUTE_PEER, attributeType, deletedBool, 91 sortColumn, null, sortPolarity}; 92 Object obj = ScarabCache.get(keys); 93 if (obj == null) 94 { 95 Criteria crit = new Criteria(); 96 crit.add(AttributePeer.ATTRIBUTE_ID, 0, Criteria.NOT_EQUAL); 97 if (!includeDeleted) 98 { 99 crit.add(AttributePeer.DELETED, 0); 100 } 101 if (attributeType.equals("user")) 103 { 104 crit.add(AttributePeer.ATTRIBUTE_TYPE_ID, 105 AttributeTypePeer.USER_TYPE_KEY); 106 } 107 else 108 { 109 crit.add(AttributePeer.ATTRIBUTE_TYPE_ID, 110 AttributeTypePeer.USER_TYPE_KEY, Criteria.NOT_EQUAL); 111 } 112 if (sortColumn.equals("desc")) 114 { 115 addSortOrder(crit, AttributePeer.DESCRIPTION, 116 sortPolarity); 117 } 118 else if (sortColumn.equals("date")) 119 { 120 addSortOrder(crit, AttributePeer.CREATED_DATE, 121 sortPolarity); 122 } 123 else if (sortColumn.equals("type")) 124 { 125 crit.addJoin(AttributePeer.ATTRIBUTE_TYPE_ID, 126 AttributeTypePeer.ATTRIBUTE_TYPE_ID); 127 addSortOrder(crit, AttributeTypePeer .ATTRIBUTE_TYPE_NAME, 128 sortPolarity); 129 } 130 else if (!sortColumn.equals("user")) 131 { 132 addSortOrder(crit, AttributePeer.ATTRIBUTE_NAME, 134 sortPolarity); 135 } 136 result = doSelect(crit); 137 } 138 else 139 { 140 result = (List )obj; 141 } 142 if (sortColumn.equals("user")) 143 { 144 result = sortAttributesByCreatingUser(result, sortPolarity); 145 } 146 147 ScarabCache.put(result, keys); 148 return result; 149 } 150 151 155 public static List getFilteredAttributes(String name, String description, 156 String searchField) 157 throws Exception 158 { 159 List result = null; 160 List allAttributes = getAttributes(); 161 if (allAttributes == null || allAttributes.size() == 0) 162 { 163 result = Collections.EMPTY_LIST; 164 } 165 else 166 { 167 List attrIds = new ArrayList (); 168 for (int i = 0; i < allAttributes.size(); i++) 169 { 170 attrIds.add(((Attribute)allAttributes.get(i)).getAttributeId()); 171 } 172 Criteria crit = new Criteria(); 173 crit.addIn(AttributePeer.ATTRIBUTE_ID, attrIds); 174 Criteria.Criterion c = null; 175 Criteria.Criterion c1 = null; 176 Criteria.Criterion c2 = null; 177 178 if (name != null) 179 { 180 c1 = crit.getNewCriterion(AttributePeer.ATTRIBUTE_NAME, 181 addWildcards(name), Criteria.LIKE); 182 } 183 if (description != null) 184 { 185 c2 = crit.getNewCriterion(AttributePeer.DESCRIPTION, 186 addWildcards(description), Criteria.LIKE); 187 } 188 if (searchField.equals("Name")) 189 { 190 c = c1; 191 } 192 else if (searchField.equals("Description")) 193 { 194 c = c2; 195 } 196 else if (searchField.equals("Any")) 197 { 198 c = c1.or(c2); 199 } 200 crit.and(c); 201 result = AttributePeer.doSelect(crit); 202 } 203 return result; 204 } 205 206 private static Object addWildcards(String s) 207 { 208 return new StringBuffer (s.length() + 2) 209 .append('%').append(s).append('%').toString(); 210 } 211 212 216 public static List getAttributes() 217 throws Exception 218 { 219 return getAttributes(NON_USER, false, AttributePeer.ATTRIBUTE_NAME, "asc"); 220 } 221 222 225 public static List getAttributes(String attributeType) 226 throws Exception 227 { 228 return getAttributes(attributeType, false, AttributePeer.ATTRIBUTE_NAME, 229 "asc"); 230 } 231 232 235 public static List getAttributes(String attributeType, boolean includeDeleted) 236 throws Exception 237 { 238 return getAttributes(attributeType, includeDeleted, AttributePeer.ATTRIBUTE_NAME, 239 "asc"); 240 } 241 242 246 public static List getAttributes(String sortColumn, 247 String sortPolarity) 248 throws Exception 249 { 250 return getAttributes(NON_USER, false, sortColumn, sortPolarity); 251 } 252 253 private static List sortAttributesByCreatingUser(List result, 254 String sortPolarity) 255 throws Exception 256 { 257 final int polarity = ("asc".equals(sortPolarity)) ? 1 : -1; 258 Comparator c = new Comparator () 259 { 260 public int compare(Object o1, Object o2) 261 { 262 int i = 0; 263 try 264 { 265 i = polarity * 266 ((Attribute)o1).getCreatedUserName() 267 .compareTo(((Attribute)o2).getCreatedUserName()); 268 } 269 catch (Exception e) 270 { 271 } 273 return i; 274 } 275 }; 276 Collections.sort(result, c); 277 return result; 278 } 279 280 private static Criteria addSortOrder(Criteria crit, 281 String sortColumn, String sortPolarity) 282 { 283 if (sortPolarity.equals("desc")) 284 { 285 crit.addDescendingOrderByColumn(sortColumn); 286 } 287 else 288 { 289 crit.addAscendingOrderByColumn(sortColumn); 290 } 291 return crit; 292 } 293 294 } 295 | Popular Tags |