1 package org.tigris.scarab.om; 2 3 48 49 import org.apache.fulcrum.TurbineServices; 51 import org.apache.fulcrum.cache.CachedObject; 52 import org.apache.fulcrum.cache.GlobalCacheService; 53 import org.apache.fulcrum.cache.ObjectExpiredException; 54 import org.apache.torque.TorqueException; 55 import org.apache.torque.om.Persistent; 56 import org.apache.torque.util.Criteria; 57 import org.apache.turbine.services.yaaficomponent.YaafiComponentService; 58 import org.tigris.scarab.tools.localization.L10NKeySet; 59 import org.tigris.scarab.util.ScarabException; 60 import org.tigris.scarab.util.ScarabRuntimeException; 61 62 68 public class ROptionOption 69 extends org.tigris.scarab.om.BaseROptionOption 70 implements Persistent 71 { 72 private int level; 73 74 75 private static final String CLASS_NAME = "ROptionOption"; 76 77 80 protected ROptionOption() 81 { 82 } 83 84 87 static String getCacheKey(Integer option1, Integer option2) 88 { 89 String keyStringA = option1.toString(); 90 String keyStringB = option2.toString(); 91 return new StringBuffer (CLASS_NAME.length() + keyStringA.length() + keyStringB.length()) 92 .append(CLASS_NAME).append(keyStringA).append(keyStringB).toString(); 93 } 94 95 98 public static ROptionOption getInstance() 99 { 100 return new ROptionOption(); 101 } 102 103 104 107 public static ROptionOption getInstance(Integer parent, Integer child) 108 throws Exception 109 { 110 GlobalCacheService tgcs = getGlobalCacheService(); 111 112 String key = getCacheKey(parent, child); 113 ROptionOption option = null; 114 try 115 { 116 option = (ROptionOption)tgcs.getObject(key).getContents(); 117 } 118 catch (ObjectExpiredException oee) 119 { 120 try 121 { 122 Criteria crit = new Criteria(); 123 crit.add (ROptionOptionPeer.OPTION1_ID, parent); 124 crit.add (ROptionOptionPeer.OPTION2_ID, child); 125 option = (ROptionOption) (ROptionOptionPeer.doSelect(crit)).get(0); 126 } 127 catch (Exception e) 128 { 129 throw new ScarabException(L10NKeySet.ExceptionOptionNotFound, 130 parent.toString(), 131 child.toString()); 132 } 133 tgcs.addObject(key, new CachedObject(option)); 134 } 135 return option; 136 } 137 138 142 public static void doRemove(ROptionOption roo) 143 throws Exception 144 { 145 Criteria crit = new Criteria(); 149 crit.add (ROptionOptionPeer.OPTION1_ID, roo.getOption1Id()); 150 crit.add (ROptionOptionPeer.OPTION2_ID, roo.getOption2Id()); 151 152 ROptionOptionPeer.doDelete(crit); 153 154 GlobalCacheService tgcs = getGlobalCacheService(); 155 156 String key = getCacheKey(roo.getOption1Id(), roo.getOption2Id()); 157 tgcs.removeObject(key); 158 } 159 160 164 public static void doRemove(Integer parent, Integer child) 165 throws Exception 166 { 167 ROptionOption roo = getInstance(); 168 roo.setOption1Id(parent); 169 roo.setOption2Id(child); 170 ROptionOption.doRemove(roo); 171 } 172 173 176 public AttributeOption getOption1Option() 177 throws TorqueException 178 { 179 return AttributeOptionManager.getInstance(getOption1Id()); 180 } 181 182 185 public AttributeOption getOption2Option() 186 throws TorqueException 187 { 188 return AttributeOptionManager.getInstance(getOption2Id()); 189 } 190 191 196 public int getLevel() 197 { 198 return level; 199 } 200 201 206 public void setLevel(int v) 207 { 208 this.level = v; 209 } 210 211 220 221 230 231 234 public String toString() 235 { 236 return "Parent: " + getOption1Id() + " Child: " + 237 getOption2Id() + " : Order: " + getPreferredOrder(); 238 } 239 240 245 protected static final GlobalCacheService getGlobalCacheService() 246 { 247 try{ 248 YaafiComponentService yaafi = (YaafiComponentService) TurbineServices.getInstance().getService( 249 YaafiComponentService.SERVICE_NAME); 250 return (GlobalCacheService) yaafi.lookup(GlobalCacheService.class.getName()); 251 } 252 catch (Exception e) { 253 throw new ScarabRuntimeException(L10NKeySet.ExceptionLookupGlobalCache, e); 254 } 255 } 256 } 257 258 259 260 | Popular Tags |