1 18 package org.objectweb.speedo.naming.lib; 19 20 import java.util.ArrayList ; 21 import java.util.Collection ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 import java.util.Properties ; 25 26 import javax.jdo.datastore.Sequence; 27 28 import org.objectweb.jorm.api.PException; 29 import org.objectweb.jorm.facility.naming.generator.LongGen; 30 import org.objectweb.jorm.facility.naming.rdbsequence.RdbSequenceBinder; 31 import org.objectweb.jorm.facility.naming.rdbsequence.RdbSequencePName; 32 import org.objectweb.jorm.metainfo.api.CommonClassMapping; 33 import org.objectweb.jorm.metainfo.api.GenClassRef; 34 import org.objectweb.jorm.metainfo.api.Manager; 35 import org.objectweb.jorm.metainfo.api.MetaObject; 36 import org.objectweb.jorm.metainfo.api.NameDef; 37 import org.objectweb.jorm.metainfo.api.PrimitiveElement; 38 import org.objectweb.jorm.metainfo.api.Reference; 39 import org.objectweb.jorm.naming.api.PBinder; 40 import org.objectweb.jorm.naming.api.PName; 41 import org.objectweb.jorm.naming.api.PNameCoder; 42 import org.objectweb.jorm.type.api.PType; 43 import org.objectweb.jorm.type.api.PTypeSpace; 44 import org.objectweb.speedo.api.SpeedoException; 45 import org.objectweb.speedo.generation.jorm.JormMIMappingBuilder; 46 import org.objectweb.speedo.generation.lib.NamingRules; 47 import org.objectweb.speedo.mapper.api.JormFactory; 48 import org.objectweb.speedo.metadata.SpeedoClass; 49 import org.objectweb.speedo.metadata.SpeedoField; 50 import org.objectweb.speedo.naming.api.MIBuilderHelper; 51 import org.objectweb.speedo.naming.api.NamingManager; 52 import org.objectweb.speedo.sequence.lib.SpeedoSequence; 53 import org.objectweb.speedo.sequence.lib.SpeedoSequenceBinder; 54 import org.objectweb.speedo.tools.StringReplace; 55 import org.objectweb.util.monolog.api.BasicLevel; 56 57 61 public class RdbSequenceNamingManager 62 extends NamingManagerHelper 63 implements NamingManager { 64 65 private final static String SEQUENCE_ID ="sid"; 66 private final static String BINDER_CLASS_NAME 67 = RdbSequenceBinder.class.getName(); 68 private final static String SPEEDO_BINDER_CLASS_NAME 69 = SpeedoSequenceBinder.class.getName(); 70 public final static int SEQ_NAME_IDX = 4; 71 public final static int SEQ_INCREMENT_IDX = 5; 72 public final static int SEQ_STARTID_IDX = 6; 73 public final static int SEQ_CACHE_IDX = 7; 74 public final static int SEQ_ALLOCATOR_IDX = 8; 75 76 77 78 public boolean canManage(SpeedoClass sc) { 79 if (sc.datastoreSequence != null) { 80 return true; 81 } 82 return false; 83 } 84 85 88 public PName decode(PNameCoder pnc, 89 Object oid, 90 Class clazz, 91 JormFactory jf) throws PException { 92 if (oid instanceof String ) { 93 String stroid = (String ) oid; 94 if (pnc != null) { 95 if (pnc instanceof RdbSequenceBinder) { 96 int idx = stroid.indexOf(SEP); 97 if (idx != -1) { 98 return pnc.decodeString(stroid.substring(idx + SEP.length())); 100 } else { 101 return pnc.decodeString(stroid); 103 } 104 105 } else { 106 return null; 108 } 109 } else { 110 int idx = stroid.indexOf(SEP); 112 ClassLoader cl = null; 113 String fqcn; 114 if (idx != -1) { 115 fqcn = stroid.substring(0, idx); 117 } else if (clazz != null) { 118 fqcn = clazz.getName(); 119 } else { 120 return null; 122 } 123 cl = jf.getClass().getClassLoader(); 124 if (cl == null) { 125 cl = ClassLoader.getSystemClassLoader(); 126 } 127 try { 128 pnc = jf.getPBinder(fqcn, cl); 129 } catch (Exception e) { 130 return null; 131 } 132 return (pnc instanceof RdbSequenceBinder 133 ? pnc.decodeString(stroid.substring(idx + SEP.length())) 134 : null); 135 } 136 } else if (pnc instanceof RdbSequenceBinder){ 137 if (oid instanceof Integer ) { 138 pnc.decodeLong(((Integer ) oid).intValue()); 139 } else if (oid instanceof Long ) { 140 pnc.decodeLong(((Long ) oid).longValue()); 141 } 142 } 143 return null; 144 } 145 146 public Object encode(PName pn) throws PException { 147 if (pn instanceof RdbSequencePName) { 148 return pn.getPNameManager().getPType().getJormName() 149 + SEP + pn.encodeString(); 150 } 151 return null; 152 } 153 154 protected String getName() { 155 return SEQUENCE_ID; 156 } 157 158 public PBinder getPBinder(String className, 159 String hints, 160 ClassLoader cl, 161 byte mappingStructureRule, 162 Map cn2binder, 163 Map cn2pnc) throws PException { 164 SpeedoSequenceBinder binder = (SpeedoSequenceBinder) super.getPBinder( 165 className, hints, cl, mappingStructureRule, cn2binder, cn2pnc); 166 String [] tokens = getTokens(hints); 167 if (tokens[SEQ_NAME_IDX].length() > 0) { 168 if (logger != null && logger.isLoggable(BasicLevel.DEBUG)) { 169 logger.log(BasicLevel.DEBUG, "Sequence name: " + tokens[SEQ_NAME_IDX]); 170 } 171 String sequencePrefix = (className.lastIndexOf('.') != -1) ? className.substring(0, className.lastIndexOf('.')+1):""; 172 sequencePrefix = StringReplace.replaceString("/", "", sequencePrefix); 173 Sequence sequence = pmf.getSequenceManager().getSequence(sequencePrefix + tokens[SEQ_NAME_IDX]); 174 if (sequence == null) { 175 throw new PException("Wrong sequence name: " + sequencePrefix + tokens[SEQ_NAME_IDX]); 176 } 177 LongGen longGen = ((SpeedoSequence) sequence).getLongGen(); 178 182 binder.setSequence(sequence); 183 } 184 return binder; 185 } 186 187 public void getJormNamingConfig(NameDef nd, 188 SpeedoClass targetClass, 189 MetaObject sourceMO, 190 String key, 191 Properties result) throws SpeedoException{ 192 StringBuffer sb = new StringBuffer (); 193 sb.append(SEQUENCE_ID); 194 195 sb.append(HINTS_SEP); 196 sb.append(SPEEDO_BINDER_CLASS_NAME); 197 198 SpeedoClass ancestor = targetClass.getAncestor(); 199 String ancestorClassName; 200 String pncClassName; 201 if (!(sourceMO instanceof GenClassRef) && (ancestor != null || !targetClass.jormclass.getSubClasses().isEmpty()) ) { 205 if (ancestor == null) { 206 ancestor = targetClass; 207 } 208 ancestorClassName = ancestor.getFQName(); 209 try { 210 if (targetClass.jormclass.getSubClasses().isEmpty()) { 211 pncClassName = SPEEDO_BINDER_CLASS_NAME; 212 } else if (targetClass.jormclass.detectFilterElementNotInPK(targetClass.jormclass.getInheritanceFilter(nd), nd)) { 213 pncClassName = POLYMORPHIC_PNC; 217 } else { 218 pncClassName = NamingRules.kfpncName(ancestor.getFQName()); 220 } 221 } catch (Exception e) { 222 logger.log(BasicLevel.ERROR, "Error while retrieving the inheritance filter of the namedef:" + nd.toString()); 223 throw new SpeedoException("Error while retrieving the inheritance filter of the namedef:" + nd.toString(), e); 224 } 225 } else { 226 ancestorClassName = targetClass.getFQName(); 227 pncClassName = SPEEDO_BINDER_CLASS_NAME; 228 } 229 sb.append(HINTS_SEP); 231 sb.append(pncClassName); 232 233 sb.append(HINTS_SEP); 235 sb.append(ancestorClassName); 236 237 238 sb.append(HINTS_SEP); 240 if (targetClass.datastoreSequence != null) { 241 sb.append(targetClass.datastoreSequence); 242 } 243 result.setProperty(key, sb.toString()); 244 } 245 246 public String getPNameHints(SpeedoClass sc, NameDef nd) { 247 return "null"; 248 } 249 250 public String getGCPNameHints(SpeedoClass sc, NameDef nd) { 251 return "proxy.getPName()"; 252 } 253 254 258 public void fillNameDef(MIBuilderHelper mibh, 259 Manager manager, 260 NameDef nd, 261 SpeedoClass tsc, 262 SpeedoClass ssc, 263 MetaObject mo, 264 Reference ref, 265 CommonClassMapping hcm, 266 JormMIMappingBuilder mb, 267 boolean isIdentifier, 268 boolean isInGenClass, 269 boolean createField, 270 Collection createdMOs) throws SpeedoException, PException { 271 Iterator it = tsc.jdoField.values().iterator(); 272 SpeedoField sf = null; 273 ArrayList al = null; 274 while(it.hasNext()) { 275 SpeedoField _sf = (SpeedoField) it.next(); 276 if (_sf.primaryKey) { 277 if (sf != null) { 278 if (al == null) { 279 al = new ArrayList (); 280 } 281 al.add(_sf.name); 282 } else { 283 sf = _sf; 284 } 285 } 286 } 287 if (al != null) { 288 al.add(sf); 289 throw new SpeedoException( 290 "Impossible to use an auto incremented identifier if " + 291 "several fields have been marked as primary-key " + al 292 + " in the class '" + tsc.getFQName() + "'."); 293 } 294 String fieldName = null; 295 fieldName = mibh.getNameDefFieldPrefix(ref, isIdentifier, isInGenClass); 296 PType newLongFieldType = PTypeSpace.OBJLONG; 297 if (sf == null) { 298 fieldName += "id_"; 299 } else { 300 if ("java.lang.Long".equals(sf.type()) || "Long".equals(sf.type())) { 301 newLongFieldType = PTypeSpace.OBJLONG; 302 } else if ("long".equals(sf.type())) { 303 newLongFieldType = PTypeSpace.LONG; 304 } else { 305 throw new SpeedoException( 306 "Impossible to use an auto incremented identifier: " + 307 "the field type of '" + sf.name + "' is '" + sf.type() 308 + "' and 'java.lang.Long' or long is expected (class '" 309 + tsc.getFQName() + "'."); 310 } 311 if (!isInGenClass && fieldName.length() == 0) { 312 createField = false; 315 } 316 fieldName += sf.name; 317 } 318 PrimitiveElement id = null; 319 if (createField) { 320 id = mibh.createNameDefField(mo, fieldName, newLongFieldType); 321 } else { 322 id = mibh.getPrimitiveField(mo, fieldName); 323 if (id == null) { 324 throw new SpeedoException( 325 mibh.getErrorMessage(tsc, mo, ref) 326 + " Impossible to get the field '" + fieldName + "'"); 327 } 328 } 329 nd.setFieldName(fieldName); 330 if (createField && (mb != null)) { 331 mb.createNameDefMapping(hcm, nd, ssc, isIdentifier, isInGenClass); 332 } 333 } 334 335 public NamingField[] getNamingfields(SpeedoClass sc) throws PException { 336 Iterator it = sc.jdoField.values().iterator(); 337 while(it.hasNext()) { 338 SpeedoField sf = (SpeedoField) it.next(); 339 if (sf.primaryKey) { 340 Class fieldType; 341 PType fieldPType; 342 if ("java.lang.Long".equals(sf.type()) || "Long".equals(sf.type())) { 343 fieldType = Long .class; 344 fieldPType = PTypeSpace.OBJLONG; 345 } else if ("long".equals(sf.type())) { 346 fieldType = Long.TYPE; 347 fieldPType = PTypeSpace.LONG; 348 } else { 349 throw new PException( 350 "Impossible to use an auto incremented identifier: " + 351 "the field type of '" + sf.name + "' is '" + sf.type() 352 + "' and 'java.lang.Long' or 'long' is expected (class '" 353 + sc.getFQName() + "'."); 354 } 355 return new NamingField[] { 356 new NamingField(sf.name, fieldType, "lid", fieldPType)}; 357 } 358 } 359 return null; 360 } 361 } 362 | Popular Tags |