1 package org.tigris.scarab.om; 2 3 48 49 import java.util.ArrayList ; 50 import java.util.List ; 51 52 import org.apache.fulcrum.TurbineServices; 53 import org.apache.fulcrum.cache.CachedObject; 54 import org.apache.fulcrum.cache.GlobalCacheService; 55 import org.apache.fulcrum.cache.ObjectExpiredException; 56 import org.apache.fulcrum.intake.Retrievable; 57 import org.apache.fulcrum.localization.Localization; 58 import org.apache.torque.TorqueException; 59 import org.apache.turbine.services.yaaficomponent.YaafiComponentService; 60 import org.tigris.scarab.tools.localization.L10NKeySet; 61 import org.tigris.scarab.util.ScarabException; 62 import org.tigris.scarab.util.ScarabRuntimeException; 63 64 71 public class ParentChildAttributeOption 72 implements Retrievable, java.io.Serializable 73 { 74 75 private static final String CLASS_NAME = "ParentChildAttributeOption"; 76 77 private Integer attributeId = null; 78 private Integer optionId = null; 79 private Integer parentId = null; 80 private boolean deleted = false; 81 private String name = null; 82 private int preferredOrder = 0; 83 private int weight = 0; 84 private List ancestors = null; 85 private static final Integer ROOT_ID = new Integer (0); 86 87 88 91 protected ParentChildAttributeOption() 92 { 93 } 94 95 98 static String getCacheKey(Integer option1, Integer option2) 99 { 100 String keyStringA = option1.toString(); 101 String keyStringB = option2.toString(); 102 String output = new StringBuffer (CLASS_NAME.length() + 103 keyStringA.length() + keyStringB.length()) 104 .append(CLASS_NAME).append(keyStringA) 105 .append(keyStringB).toString(); 106 return output; 107 } 108 109 112 public static ParentChildAttributeOption getInstance() 113 { 114 return new ParentChildAttributeOption(); 115 } 116 117 120 public static ParentChildAttributeOption getInstance( 121 Integer parent, Integer child) 122 { 123 GlobalCacheService tgcs =getGlobalCacheService(); 124 125 String key = getCacheKey(parent, child); 126 ParentChildAttributeOption pcao = null; 127 try 128 { 129 pcao = (ParentChildAttributeOption)tgcs.getObject(key) 130 .getContents(); 131 } 132 catch (ObjectExpiredException oee) 133 { 134 pcao = getInstance(); 135 pcao.setParentId(parent); 136 pcao.setOptionId(child); 137 tgcs.addObject(key, new CachedObject(pcao)); 138 } 139 return pcao; 140 } 141 142 146 public String getQueryKey() 147 { 148 if (parentId == null || optionId == null) 149 { 150 return ""; 151 } 152 return getParentId().toString() + ":" + getOptionId().toString(); 153 } 154 155 159 public void setQueryKey(String key) 160 throws Exception 161 { 162 int index = key.indexOf(":"); 163 String a = key.substring(0,index); 164 String b = key.substring(index,key.length()); 165 setParentId(new Integer (a)); 166 setOptionId(new Integer (b)); 167 } 168 169 public Integer getAttributeId() 170 { 171 return attributeId; 172 } 173 174 public void setAttributeId(Integer attributeId) 175 { 176 this.attributeId = attributeId; 177 } 178 179 182 public Integer getOptionId() 183 { 184 return this.optionId; 185 } 186 187 190 public void setOptionId(Integer key) 191 { 192 this.optionId = key; 193 } 194 195 198 public AttributeOption getChildOption() 199 throws TorqueException 200 { 201 return AttributeOptionManager.getInstance(getOptionId()); 202 } 203 204 public Integer getParentId() 205 { 206 if (this.parentId == null) 207 { 208 return new Integer (0); 209 } 210 return this.parentId; 211 } 212 213 public void setParentId(Integer id) 214 { 215 this.parentId = id; 216 } 217 218 public AttributeOption getParentOption() 219 throws TorqueException 220 { 221 return AttributeOptionManager.getInstance(getParentId()); 222 } 223 224 public List getAncestors() 225 throws TorqueException, Exception 226 { 227 ancestors = new ArrayList (); 228 AttributeOption parent = getParentOption(); 229 if (!ROOT_ID.equals(parent.getOptionId())) 230 { 231 addAncestors(parent); 232 } 233 return ancestors; 234 } 235 236 239 private void addAncestors(AttributeOption option) 240 throws TorqueException, Exception 241 { 242 if (!ROOT_ID.equals(option.getParent().getOptionId())) 243 { 244 if (ancestors.contains(option.getParent())) 245 { 246 throw new Exception ("Tried to add a recursive parent-child " + 247 "attribute option relationship."); } 249 else 250 { 251 addAncestors(option.getParent()); 252 } 253 } 254 ancestors.add(option.getOptionId()); 255 } 256 257 public boolean getDeleted() 258 { 259 return this.deleted; 260 } 261 262 public void setDeleted(boolean deleted) 263 { 264 this.deleted = deleted; 265 } 266 267 public String getName() 268 { 269 if (this.name == null) 270 { 271 return ""; 272 } 273 return this.name; 274 } 275 276 public void setName(String name) 277 { 278 this.name = name; 279 } 280 281 public int getPreferredOrder() 282 { 283 return this.preferredOrder; 284 } 285 286 public void setPreferredOrder(int preferredOrder) 287 { 288 this.preferredOrder = preferredOrder; 289 } 290 291 public int getWeight() 292 { 293 return this.weight; 294 } 295 296 public void setWeight(int weight) 297 { 298 this.weight = weight; 299 } 300 301 304 public static void doRemoveFromCache(Integer parent, Integer child) 305 { 306 GlobalCacheService tgcs =getGlobalCacheService(); 307 308 String key = getCacheKey(parent, child); 309 tgcs.removeObject(key); 310 } 311 312 public String toString() 313 { 314 return getParentId() + ":" + getOptionId() + " -> " + getName(); 315 } 316 317 public void save() 318 throws Exception 319 { 320 AttributeOption ao = null; 321 ROptionOption roo = null; 322 323 Attribute tmpAttr = AttributeManager.getInstance(getAttributeId()); 324 325 if (getOptionId() == null) 327 { 328 AttributeOption duplicate = 330 AttributeOption.getInstance(tmpAttr, getName().trim()); 331 AttributeOption parent = 332 AttributeOptionManager.getInstance(getParentId()); 333 if (duplicate != null) 334 { 335 throw new Exception (Localization.getString("CannotCreateDuplicateOption")); } 337 else if (parent.getDeleted()) 338 { 339 throw new Exception (Localization.getString("CannotCreateChild")); } 341 } 342 343 Integer optionId = getOptionId(); 345 if (optionId == null) 346 { 347 ao = AttributeOptionManager.getInstance(); 348 } 349 else 350 { 351 ao = AttributeOptionManager.getInstance(getOptionId()); 352 } 353 354 355 ao.setName(getName()); 356 ao.setDeleted(getDeleted()); 357 ao.setAttribute(tmpAttr); 358 ao.save(); 359 360 tmpAttr.doRemoveCaches(); 362 363 this.setOptionId(ao.getOptionId()); 365 366 try 368 { 369 roo = ROptionOption.getInstance(getParentId(), getOptionId()); 371 } 372 catch (ScarabException se) 373 { 374 roo = ROptionOption.getInstance(); 376 roo.setOption1Id(getParentId()); 377 roo.setOption2Id(getOptionId()); 378 } 379 roo.setPreferredOrder(getPreferredOrder()); 380 roo.setWeight(getWeight()); 381 roo.setRelationshipId(OptionRelationship.PARENT_CHILD); 382 roo.save(); 383 } 384 385 390 protected static final GlobalCacheService getGlobalCacheService() 391 { 392 try{ 393 YaafiComponentService yaafi = (YaafiComponentService) TurbineServices.getInstance().getService( 394 YaafiComponentService.SERVICE_NAME); 395 return (GlobalCacheService) yaafi.lookup(GlobalCacheService.class.getName()); 396 } 397 catch (Exception e) { 398 throw new ScarabRuntimeException(L10NKeySet.ExceptionLookupGlobalCache, e); 399 } 400 } 401 } 402 | Popular Tags |