1 package org.tigris.scarab.om; 2 3 48 49 import java.util.Comparator ; 51 import java.util.List ; 52 import java.util.ArrayList ; 53 import java.sql.Connection ; 54 55 import org.apache.torque.Torque; 57 import org.apache.torque.TorqueException; 58 import org.apache.torque.om.Persistent; 59 import org.apache.torque.util.Criteria; 60 61 import org.tigris.scarab.om.ModuleManager; 62 import org.tigris.scarab.om.Module; 63 import org.tigris.scarab.tools.localization.L10NKeySet; 64 import org.tigris.scarab.util.ScarabException; 65 import org.tigris.scarab.workflow.WorkflowFactory; 66 67 73 public class RModuleOption 74 extends BaseRModuleOption 75 implements Persistent 76 { 77 78 private int level; 79 80 private static final Comparator COMPARATOR = new Comparator () 81 { 82 public int compare(Object obj1, Object obj2) 83 { 84 int result = 1; 85 RModuleOption opt1 = (RModuleOption)obj1; 86 RModuleOption opt2 = (RModuleOption)obj2; 87 if (opt1.getOrder() < opt2.getOrder()) 88 { 89 result = -1; 90 } 91 else if (opt1.getOrder() == opt2.getOrder()) 92 { 93 result = opt1.getDisplayValue() 94 .compareTo(opt2.getDisplayValue()); 95 } 96 return result; 97 } 98 }; 99 100 101 107 public ScarabModule getScarabModule() 108 { 109 throw new UnsupportedOperationException ( 110 "Should use getModule"); } 112 113 118 public void setScarabModule(ScarabModule module) 119 { 120 throw new UnsupportedOperationException ( 121 "Should use setModule(Module). Note module cannot be new."); } 123 124 127 public void setModule(Module me) 128 throws TorqueException 129 { 130 Integer id = me.getModuleId(); 131 if (id == null) 132 { 133 throw new TorqueException("Modules must be saved prior to " + 134 "being associated with other objects."); } 136 setModuleId(id); 137 } 138 139 144 public Module getModule() 145 throws TorqueException 146 { 147 Module module = null; 148 Integer id = getModuleId(); 149 if ( id != null ) 150 { 151 module = ModuleManager.getInstance(id); 152 } 153 154 return module; 155 } 156 157 161 public static Comparator getComparator() 162 { 163 return COMPARATOR; 164 } 165 166 171 public String getDisplayValue() 172 { 173 String dispVal = super.getDisplayValue(); 174 if (dispVal == null) 175 { 176 try 177 { 178 dispVal = getAttributeOption().getName(); 179 } 180 catch (Exception e) 181 { 182 getLog().error(e); 183 dispVal = "!Error-Check Logs!"; 184 } 185 } 186 return dispVal; 187 } 188 189 193 public int getLevel() 194 { 195 return level; 196 } 197 198 202 public void setLevel(int v) 203 { 204 this.level = v; 205 } 206 207 public RModuleAttribute getRModuleAttribute(IssueType issueType) 208 throws Exception 209 { 210 Module module = ModuleManager.getInstance(getModuleId()); 211 Attribute attribute = getAttributeOption().getAttribute(); 212 return module.getRModuleAttribute(attribute, issueType); 213 } 214 215 220 public List getDescendants(IssueType issueType) 221 throws Exception 222 { 223 List descendants = new ArrayList (); 224 List attrDescendants = getAttributeOption().getDescendants(); 225 for (int i =0;i < attrDescendants.size(); i++) 226 { 227 RModuleOption rmo = null; 228 AttributeOption option = (AttributeOption)attrDescendants.get(i); 229 rmo = getModule().getRModuleOption(option, issueType); 230 if (rmo != null && rmo.getOptionId().equals(option.getOptionId())) 231 { 232 descendants.add(rmo); 233 } 234 } 235 return descendants; 236 } 237 238 public void delete() 239 throws Exception 240 { 241 Module module = getModule(); 242 243 IssueType issueType = IssueTypeManager 244 .getInstance(getIssueTypeId(), false); 245 if (issueType.getLocked()) 246 { 247 throw new ScarabException(L10NKeySet.ExceptionDeleteOptionFromLockedIssueType); 248 } 249 else 250 { 251 Criteria c = new Criteria() 252 .add(RModuleOptionPeer.MODULE_ID, getModuleId()) 253 .add(RModuleOptionPeer.ISSUE_TYPE_ID, getIssueTypeId()) 254 .add(RModuleOptionPeer.OPTION_ID, getOptionId()); 255 RModuleOptionPeer.doDelete(c); 256 WorkflowFactory.getInstance().deleteWorkflowsForOption(getAttributeOption(), 257 module, issueType); 258 ArrayList optIds = new ArrayList (); 260 List rmos = module.getRModuleOptions(getAttributeOption().getAttribute(), issueType, false); 261 for (int i=0; i<rmos.size();i++) 262 { 263 RModuleOption rmo = (RModuleOption)rmos.get(i); 264 optIds.add(rmo.getOptionId()); 265 } 266 Criteria c2 = new Criteria() 267 .add(RModuleOptionPeer.MODULE_ID, getModuleId()) 268 .add(RModuleOptionPeer.ISSUE_TYPE_ID, getIssueTypeId()) 269 .addIn(RModuleOptionPeer.OPTION_ID, optIds) 270 .add(RModuleOptionPeer.PREFERRED_ORDER, getOrder(), Criteria.GREATER_THAN); 271 List adjustRmos = RModuleOptionPeer.doSelect(c2); 272 for (int j=0; j<adjustRmos.size();j++) 273 { 274 RModuleOption rmo = (RModuleOption)adjustRmos.get(j); 275 rmo.setOrder(rmo.getOrder() -1); 277 rmo.save(); 278 } 280 } 281 ((ModuleManager)Torque.getManager(ModuleManager.MANAGED_CLASS)) 283 .refreshedObject(this); 284 } 285 286 public void save(Connection con) throws TorqueException 287 { 288 if (isModified()) 289 { 290 if (isNew()) 291 { 292 super.save(con); 293 } 294 else 295 { 296 Attribute attr = getAttributeOption().getAttribute(); 297 RIssueTypeAttribute ria = null; 298 try 299 { 300 ria = getIssueType().getRIssueTypeAttribute(attr); 301 if (ria != null && ria.getLocked()) 302 { 303 throw new TorqueException(attr.getName() + "is locked"); } 305 else 306 { 307 super.save(con); 308 } 309 } 310 catch (Exception e) 311 { 312 throw new TorqueException("An error has occurred."); } 314 } 315 } 316 } 317 } 318 | Popular Tags |