1 package org.tigris.scarab.om; 2 3 48 49 import java.util.List ; 51 import java.util.ArrayList ; 52 import java.util.Comparator ; 53 import java.util.Collections ; 54 import java.util.Iterator ; 55 56 import org.apache.torque.TorqueException; 58 import org.apache.torque.om.Persistent; 59 import org.apache.torque.om.ObjectKey; 60 import org.apache.torque.util.Criteria; 61 62 import org.tigris.scarab.services.cache.ScarabCache; 63 64 81 public class AttributeOption 82 extends BaseAttributeOption 83 implements Persistent 84 { 85 private static final Integer STATUS__CLOSED__PK = new Integer (7); 86 87 88 private static final String CLASS_NAME = "AttributeOption"; 89 90 91 private Attribute aAttribute; 92 93 96 private List sortedParents = null; 97 98 101 private List sortedChildren = null; 102 103 106 private String parentIds = null; 107 108 111 private List orderedTree = null; 112 113 116 protected AttributeOption() 117 { 118 } 119 120 public static Integer getStatusClosedPK() 121 { 122 return STATUS__CLOSED__PK; 123 } 124 125 128 static String getCacheKey(ObjectKey key) 129 { 130 String keyString = key.getValue().toString(); 131 return new StringBuffer (CLASS_NAME.length() + keyString.length()) 132 .append(CLASS_NAME).append(keyString).toString(); 133 } 134 135 138 private static final Comparator COMPARATOR = new Comparator () 139 { 140 public int compare(Object obj1, Object obj2) 141 { 142 int result = 1; 143 AttributeOption opt1 = (AttributeOption)obj1; 144 AttributeOption opt2 = (AttributeOption)obj2; 145 if (opt1.getName().equals(opt2.getName())) 146 { 147 result = 0; 148 } 149 else 150 { 151 result = -1; 152 } 153 return result; 154 } 155 }; 156 157 161 public static Comparator getComparator() 162 { 163 return COMPARATOR; 164 } 165 166 169 public Attribute getAttribute() throws TorqueException 170 { 171 if (aAttribute==null && (getAttributeId() != null)) 172 { 173 aAttribute = AttributeManager.getInstance(getAttributeId()); 174 175 super.setAttribute(aAttribute); 177 } 178 return aAttribute; 179 } 180 181 184 public void setAttribute(Attribute v) throws TorqueException 185 { 186 aAttribute = v; 187 super.setAttribute(v); 188 } 189 190 193 public static AttributeOption getInstance() 194 { 195 return new AttributeOption(); 196 } 197 198 201 public static AttributeOption getInstance(Attribute attribute, String name) 202 throws Exception 203 { 204 return getInstance(attribute, name, (Module) null, (IssueType) null); 205 } 206 207 221 public static AttributeOption getInstance(Attribute attribute, String name, 222 Module module, 223 IssueType issueType) 224 throws Exception 225 { 226 AttributeOption ao = null; 227 Criteria crit; 228 if (module != null && issueType != null) 232 { 233 crit = new Criteria(4); 235 crit.add(AttributeOptionPeer.ATTRIBUTE_ID, 236 attribute.getAttributeId()); 237 crit.addJoin(AttributeOptionPeer.OPTION_ID, 238 RModuleOptionPeer.OPTION_ID); 239 crit.add(RModuleOptionPeer.MODULE_ID, module.getModuleId()); 240 crit.add(RModuleOptionPeer.ISSUE_TYPE_ID, 241 issueType.getIssueTypeId()); 242 crit.add(RModuleOptionPeer.DISPLAY_VALUE, name); 243 List rmos = RModuleOptionPeer.doSelect(crit); 244 if (rmos.size() == 1) 245 { 246 RModuleOption rmo = (RModuleOption) rmos.get(0); 247 ao = rmo.getAttributeOption(); 248 } 249 } 250 251 if (ao == null) 252 { 253 257 crit = new Criteria(2); 258 crit.add(AttributeOptionPeer.OPTION_NAME, name); 259 crit.add(AttributeOptionPeer.ATTRIBUTE_ID, 260 attribute.getAttributeId()); 261 crit.setIgnoreCase(true); 262 List options = AttributeOptionPeer.doSelect(crit); 263 if (options.size() == 1) 264 { 265 ao = (AttributeOption) options.get(0); 266 } 267 } 268 return ao; 269 } 270 271 276 public List getAncestors() 277 throws Exception 278 { 279 List options = new ArrayList (); 280 addAncestors(options); 281 return options; 282 } 283 284 287 private void addAncestors(List ancestors) 288 throws Exception 289 { 290 List parents = getParents(); 291 for (int i=parents.size()-1; i>=0; i--) 292 { 293 AttributeOption parent = (AttributeOption) 294 parents.get(i); 295 if (!ancestors.contains(parent)) 296 { 297 ancestors.add(parent); 298 parent.addAncestors(ancestors); 299 } 300 } 301 } 302 303 308 public List getDescendants() 309 throws Exception 310 { 311 List options = new ArrayList (); 312 addDescendants(options); 313 return options; 314 } 315 316 319 private void addDescendants(List descendants) 320 throws Exception 321 { 322 List children = getChildren(); 323 for (int i=children.size()-1; i>=0; i--) 324 { 325 AttributeOption child = (AttributeOption) 326 children.get(i); 327 descendants.add(child); 328 child.addDescendants(descendants); 329 } 330 } 331 332 336 public List getChildren() 337 throws TorqueException 338 { 339 if (sortedChildren == null) 340 { 341 buildChildren(); 342 } 343 return sortedChildren; 344 } 345 346 350 public List getParents() 351 throws Exception 352 { 353 buildParents(); 354 return sortedParents; 355 } 356 357 361 private synchronized void buildChildren() 362 throws TorqueException 363 { 364 Criteria crit = new Criteria() 365 .add(ROptionOptionPeer.RELATIONSHIP_ID, 366 OptionRelationship.PARENT_CHILD) 367 .add(ROptionOptionPeer.OPTION1_ID, 368 super.getOptionId()); 369 370 List relations = ROptionOptionPeer.doSelect(crit); 371 sortedChildren = new ArrayList (relations.size()); 372 for (int i=0; i < relations.size(); i++) 373 { 374 ROptionOption relation = (ROptionOption)relations.get(i); 375 Integer key = relation.getOption2Id(); 376 if (key != null) 377 { 378 sortedChildren.add(relation.getOption2Option()); 379 } 380 } 381 sortChildren(); 382 } 383 384 388 private synchronized void buildParents() 389 throws Exception 390 { 391 Criteria crit = new Criteria() 392 .add(ROptionOptionPeer.RELATIONSHIP_ID, 393 OptionRelationship.PARENT_CHILD) 394 .add(ROptionOptionPeer.OPTION2_ID, 395 super.getOptionId()); 396 397 List relations = ROptionOptionPeer.doSelect(crit); 398 sortedParents = new ArrayList (relations.size()); 399 for (int i=0; i < relations.size(); i++) 400 { 401 ROptionOption relation = (ROptionOption)relations.get(i); 402 Integer key = relation.getOption1Id(); 403 if (key != null) 404 { 405 sortedParents.add(relation.getOption1Option()); 406 } 407 } 408 sortParents(); 409 } 410 411 414 public void sortParents() 415 { 416 synchronized (this) 417 { 418 Collections.sort(sortedParents, getComparator()); 419 } 420 } 421 422 425 public void sortChildren() 426 { 427 synchronized (this) 428 { 429 Collections.sort(sortedChildren, getComparator()); 430 } 431 } 432 433 437 public boolean isChildOf(AttributeOption parent) 438 throws Exception 439 { 440 return getParents().contains(parent); 441 } 442 443 447 public boolean isParentOf(AttributeOption child) 448 throws Exception 449 { 450 return getChildren().contains(child); 451 } 452 453 456 public boolean hasChildren() 457 throws Exception 458 { 459 return getChildren().size() > 0 ? true : false; 460 } 461 462 465 public boolean hasParents() 466 throws Exception 467 { 468 return getParents().size() > 0 ? true : false; 469 } 470 471 474 public AttributeOption getParent() 475 throws Exception 476 { 477 AttributeOption parent = null; 478 Criteria crit = new Criteria() 479 .add(ROptionOptionPeer.RELATIONSHIP_ID, 480 OptionRelationship.PARENT_CHILD) 481 .add(ROptionOptionPeer.OPTION2_ID, 482 super.getOptionId()); 483 484 List results = ROptionOptionPeer.doSelect(crit); 485 if (results.size() == 1) 486 { 487 ROptionOption roo = (ROptionOption)results.get(0); 488 parent = roo.getOption1Option(); 489 } 490 return parent; 491 } 492 493 496 public void deleteModuleMappings() 497 throws Exception 498 { 499 Criteria crit = new Criteria(); 500 crit.add(RModuleOptionPeer.OPTION_ID, getOptionId()); 501 RModuleOptionPeer.doDelete(crit); 502 ScarabCache.clear(); 503 } 504 505 508 public void deleteIssueTypeMappings() 509 throws Exception 510 { 511 Criteria crit = new Criteria(); 512 crit.add(RIssueTypeOptionPeer.OPTION_ID, getOptionId()); 513 RIssueTypeOptionPeer.doDelete(crit); 514 ScarabCache.clear(); 515 } 516 517 541 542 577 578 603 604 614 650 651 669 670 698 699 714 715 742 743 780 781 820 821 824 private void clearParentIds() 825 { 826 parentIds = null; 827 } 828 829 832 public String toString() 833 { 834 try 835 { 836 return "Id: " + getOptionId() + " Name: " + getName(); } 838 catch (Exception e) 839 { 840 e.printStackTrace(); 841 } 842 return null; 843 } 844 890 891 892 895 private List getIssueTypesWithMappings() 896 throws Exception 897 { 898 Criteria crit = new Criteria(); 899 crit.add(RIssueTypeOptionPeer.OPTION_ID, getOptionId()); 900 crit.addJoin(RIssueTypeOptionPeer.ISSUE_TYPE_ID, 901 IssueTypePeer.ISSUE_TYPE_ID); 902 return IssueTypePeer.doSelect(crit); 903 } 904 905 906 913 914 public boolean isSystemDefined() 915 throws Exception 916 { 917 boolean systemDefined = false; 918 List issueTypeList = getIssueTypesWithMappings(); 919 for (Iterator i = issueTypeList.iterator(); 920 i.hasNext() && !systemDefined;) 921 { 922 systemDefined = ((IssueType)i.next()).isSystemDefined(); 923 } 924 return systemDefined; 925 } 926 } 927 | Popular Tags |