1 package org.tigris.scarab.om; 2 3 48 49 import java.util.Iterator ; 50 import java.util.List ; 51 import java.util.ArrayList ; 52 import java.sql.Connection ; 53 54 import org.apache.torque.TorqueException; 56 import org.apache.torque.om.Persistent; 57 import org.apache.torque.util.Criteria; 58 import org.apache.fulcrum.localization.Localization; 59 60 import org.tigris.scarab.services.cache.ScarabCache; 61 import org.tigris.scarab.tools.localization.L10NKeySet; 62 import org.tigris.scarab.om.Module; 63 import org.tigris.scarab.om.ModuleManager; 64 import org.tigris.scarab.om.IssueTypePeer; 65 import org.tigris.scarab.util.ScarabException; 66 import org.tigris.scarab.workflow.WorkflowFactory; 67 68 74 public class RModuleAttribute 75 extends BaseRModuleAttribute 76 implements Persistent, Conditioned 77 { 78 private static final String R_MODULE_ATTTRIBUTE = 79 "RModuleAttribute"; 80 private static final String GET_RMAS = 81 "getRMAs"; 82 83 public void save(Connection con) throws TorqueException 84 { 85 if (isModified()) 86 { 87 if (isNew()) 88 { 89 super.save(con); 90 } 91 else 92 { 93 RIssueTypeAttribute ria = null; 94 try 95 { 96 ria = getIssueType().getRIssueTypeAttribute(getAttribute()); 97 if ((ria != null && ria.getLocked())) 98 { 99 throw new TorqueException(getAttribute().getName() + " is locked"); } 101 else 102 { 103 super.save(con); 104 } 105 } 106 catch (Exception e) 107 { 108 throw new TorqueException("An error has occurred.", e); } 110 } 111 } 112 } 113 114 120 public ScarabModule getScarabModule() 121 { 122 throw new UnsupportedOperationException ( 123 "Should use getModule"); } 125 126 131 public void setScarabModule(ScarabModule module) 132 { 133 throw new UnsupportedOperationException ( 134 "Should use setModule(Module). Note module cannot be new."); } 136 137 140 public void setModule(Module me) 141 throws TorqueException 142 { 143 Integer id = me.getModuleId(); 144 if (id == null) 145 { 146 throw new TorqueException("Modules must be saved prior to " + 147 "being associated with other objects."); } 149 setModuleId(id); 150 } 151 152 157 public Module getModule() 158 throws TorqueException 159 { 160 Module module = null; 161 Integer id = getModuleId(); 162 if ( id != null ) 163 { 164 module = ModuleManager.getInstance(id); 165 } 166 167 return module; 168 } 169 170 175 public String getDisplayValue() 176 { 177 String dispVal = super.getDisplayValue(); 178 if (dispVal == null) 179 { 180 try 181 { 182 dispVal = getAttribute().getName(); 183 } 184 catch (Exception e) 185 { 186 getLog().error(e); 187 dispVal = "!Error-Check Logs!"; 188 } 189 } 190 return dispVal; 191 } 192 193 public void delete() 194 throws Exception 195 { 196 delete(false); 197 } 198 199 protected void delete(boolean overrideLock) 200 throws Exception 201 { 202 Module module = getModule(); 203 204 IssueType issueType = IssueTypeManager 205 .getInstance(getIssueTypeId(), false); 206 if (issueType.getLocked() && !overrideLock) 207 { 208 throw new ScarabException(L10NKeySet.CannotDeleteAttributeFromLockedIssueType); 209 } 210 else 211 { 212 Criteria c = new Criteria() 213 .add(RModuleAttributePeer.MODULE_ID, getModuleId()) 214 .add(RModuleAttributePeer.ISSUE_TYPE_ID, getIssueTypeId()) 215 .add(RModuleAttributePeer.ATTRIBUTE_ID, getAttributeId()); 216 RModuleAttributePeer.doDelete(c); 217 Attribute attr = getAttribute(); 218 String attributeType = null; 219 attributeType = (attr.isUserAttribute() ? Module.USER : Module.NON_USER); 220 module.getRModuleAttributes(getIssueType(), false, attributeType) 221 .remove(this); 222 WorkflowFactory.getInstance().deleteWorkflowsForAttribute( 223 attr, module, getIssueType()); 224 225 Criteria crit = new Criteria() 227 .add(RModuleUserAttributePeer.ATTRIBUTE_ID, 228 attr.getAttributeId()) 229 .add(RModuleUserAttributePeer.MODULE_ID, 230 getModuleId()) 231 .add(RModuleUserAttributePeer.ISSUE_TYPE_ID, 232 getIssueTypeId()); 233 RModuleUserAttributePeer.doDelete(crit); 234 235 if (attr.isOptionAttribute()) 237 { 238 List optionList = module.getRModuleOptions(attr, 239 IssueTypePeer.retrieveByPK(getIssueTypeId()), 240 false); 241 if (optionList != null && !optionList.isEmpty()) 242 { 243 ArrayList optionIdList = 244 new ArrayList (optionList.size()); 245 for (int i = 0; i < optionList.size(); i++) 246 { 247 optionIdList.add(((RModuleOption) 248 optionList.get(i)) 249 .getOptionId()); 250 } 251 Criteria c2 = new Criteria() 252 .add(RModuleOptionPeer.MODULE_ID, getModuleId()) 253 .add(RModuleOptionPeer.ISSUE_TYPE_ID, 254 getIssueTypeId()) 255 .addIn(RModuleOptionPeer.OPTION_ID, optionIdList); 256 RModuleOptionPeer.doDelete(c2); 257 } 258 } 259 } 260 261 RModuleAttributeManager.removeInstanceFromCache(this); 262 } 263 264 265 private static List getRMAs(Integer moduleId, Integer issueTypeId) 266 throws Exception 267 { 268 List result = null; 269 Object obj = ScarabCache.get(R_MODULE_ATTTRIBUTE, GET_RMAS, 270 moduleId, issueTypeId); 271 if (obj == null) 272 { 273 Criteria crit = new Criteria() 274 .add(RModuleAttributePeer.MODULE_ID, moduleId) 275 .add(RModuleAttributePeer.ISSUE_TYPE_ID, issueTypeId); 276 crit.addAscendingOrderByColumn( 277 RModuleAttributePeer.PREFERRED_ORDER); 278 result = RModuleAttributePeer.doSelect(crit); 279 ScarabCache.put(result, R_MODULE_ATTTRIBUTE, GET_RMAS, 280 moduleId, issueTypeId); 281 } 282 else 283 { 284 result = (List )obj; 285 } 286 return result; 287 } 288 289 297 public boolean getIsDefaultText() 298 throws Exception 299 { 300 boolean isDefault = getDefaultTextFlag(); 301 if (!isDefault && getAttribute().isTextAttribute()) 302 { 303 List rmas = getRMAs(getModuleId(), getIssueTypeId()); 305 306 boolean anotherIsDefault = false; 308 for (int i=0; i<rmas.size(); i++) 309 { 310 RModuleAttribute rma = (RModuleAttribute)rmas.get(i); 311 if (rma.getDefaultTextFlag()) 312 { 313 anotherIsDefault = true; 314 break; 315 } 316 } 317 318 if (!anotherIsDefault) 319 { 320 for (int i=0; i<rmas.size(); i++) 322 { 323 RModuleAttribute rma = (RModuleAttribute)rmas.get(i); 324 if (rma.getAttribute().isTextAttribute()) 325 { 326 if (rma.getAttributeId().equals(getAttributeId())) 327 { 328 isDefault = true; 329 } 330 else 331 { 332 anotherIsDefault = true; 333 } 334 335 break; 336 } 337 } 338 } 339 } 340 return isDefault; 341 } 342 343 350 public void setIsDefaultText(boolean b) 351 throws Exception 352 { 353 if (b && !getDefaultTextFlag()) 354 { 355 List rmas = getRMAs(getModuleId(), getIssueTypeId()); 357 358 for (int i=0; i<rmas.size(); i++) 360 { 361 RModuleAttribute rma = (RModuleAttribute)rmas.get(i); 362 if (rma.getDefaultTextFlag()) 363 { 364 rma.setDefaultTextFlag(false); 365 rma.save(); 366 break; 367 } 368 } 369 } 370 setDefaultTextFlag(b); 371 } 372 373 public List getConditions() throws TorqueException 374 { 375 if (collConditions == null) 376 { 377 Criteria crit = new Criteria(); 378 crit.add(ConditionPeer.ATTRIBUTE_ID, this.getAttributeId()); 379 crit.add(ConditionPeer.MODULE_ID, this.getModuleId()); 380 crit.add(ConditionPeer.ISSUE_TYPE_ID, this.getIssueTypeId()); 381 crit.add(ConditionPeer.TRANSITION_ID, null); 382 collConditions = getConditions(crit); 383 } 384 return collConditions; 385 } 386 387 392 public Integer [] getConditionsArray() 393 { 394 List conditions = new ArrayList (); 395 Integer [] aIDs = null; 396 try 397 { 398 conditions = this.getConditions(); 399 aIDs = new Integer [conditions.size()]; 400 int i=0; 401 for (Iterator iter = conditions.iterator(); iter.hasNext(); i++) 402 { 403 aIDs[i] = ((Condition)iter.next()).getOptionId(); 404 } 405 } 406 catch (TorqueException e) 407 { 408 this.getLog().error("getConditionsArray: " + e); 409 } 410 return aIDs; 411 } 412 417 public void setConditionsArray(Integer aOptionId[]) throws Exception 418 { 419 Criteria crit = new Criteria(); 420 crit.add(ConditionPeer.ATTRIBUTE_ID, this.getAttributeId()); 421 crit.add(ConditionPeer.MODULE_ID, this.getModuleId()); 422 crit.add(ConditionPeer.ISSUE_TYPE_ID, this.getIssueTypeId()); 423 crit.add(ConditionPeer.TRANSITION_ID, null); 424 ConditionPeer.doDelete(crit); 425 this.getConditions().clear(); 426 ConditionManager.clear(); 427 if (aOptionId != null) 428 { 429 for (int i=0; i<aOptionId.length; i++) 430 { 431 if (aOptionId[i].intValue() != 0) 432 { 433 Condition cond = new Condition(); 434 cond.setAttribute(this.getAttribute()); 435 cond.setOptionId(aOptionId[i]); 436 cond.setTransitionId(null); 437 cond.setIssueTypeId(this.getIssueTypeId()); 438 cond.setModuleId(this.getModuleId()); 439 this.addCondition(cond); 440 cond.save(); 441 } 442 } 443 } 444 } 445 452 public boolean isRequiredIf(Integer optionID) throws TorqueException 453 { 454 Condition condition = new Condition(); 455 condition.setAttribute(this.getAttribute()); 456 condition.setModuleId(this.getModuleId()); 457 condition.setIssueTypeId(this.getIssueTypeId()); 458 condition.setTransitionId(new Integer (0)); 459 condition.setOptionId(optionID); 460 return this.getConditions().contains(condition); 461 } 462 463 467 public boolean isConditioned() 468 { 469 boolean bRdo = false; 470 try { 471 bRdo = this.getConditions().size()>0; 472 } catch (TorqueException te) 473 { 474 } 476 return bRdo; 477 } 478 } 479 | Popular Tags |