1 27 28 package org.objectweb.speedo.metadata; 29 30 import org.objectweb.jorm.metainfo.api.Class; 31 import org.objectweb.speedo.api.SpeedoException; 32 import org.objectweb.util.monolog.api.BasicLevel; 33 import org.objectweb.util.monolog.api.Logger; 34 35 import java.io.File ; 36 import java.util.HashMap ; 37 import java.util.Iterator ; 38 import java.util.Map ; 39 40 import javax.jdo.JDOUserException; 41 42 46 public class SpeedoClass extends SpeedoElement { 47 50 public String name; 51 52 55 public byte identityType = SpeedoIdentity.CONTAINER_ID; 56 57 60 public SpeedoVersion version; 61 62 65 public SpeedoDatastoreId datastoreId; 66 67 70 public String objectidClass; 71 72 75 public boolean requiresExtent = true; 76 77 80 public boolean isDetachable = false; 81 82 85 public String superClassName = ""; 86 87 91 public Map jdoField = new HashMap (); 92 93 97 public Map jdoFetchGroup = new HashMap (); 98 99 102 public ClassCode classCode = new ClassCode(); 103 104 107 public SpeedoPackage jdoPackage; 108 109 112 public boolean isAbstract = false; 113 114 117 public boolean isInstanceCallbacks = false; 118 119 122 public boolean isSerializable = false; 123 124 127 public String signature; 128 129 132 public long VersionUID = 0; 133 134 137 public boolean failed = false; 138 139 142 public Class jormclass; 143 144 147 public String datastoreSequence; 148 149 152 private int fieldsCount = -1; 153 154 private int pkFieldsCount = -1; 155 156 public boolean classAlreadyEnhanced = false; 157 public boolean requireEnhancement = true; 158 159 public Map name2query = new HashMap (); 160 161 public final static byte NO_NO_ARG_CONSTRUCTOR = 1; 162 public final static byte NON_PUBLIC_NO_ARG_CONSTRUCTOR = 2; 163 public final static byte PUBLIC_NO_ARG_CONSTRUCTOR = 3; 164 165 public byte noArgConstructorStatus = NO_NO_ARG_CONSTRUCTOR; 166 167 public final static byte SUBCLASS_TABLE = 1; 168 public final static byte NEW_TABLE = 2; 169 public final static byte SUPERCLASS_TABLE = 3; 170 171 public byte inheritanceStrategy = 0; 172 173 public String getFQName() { 174 if (jdoPackage.name == null || jdoPackage.name.length() == 0) { 175 return name; 176 } else { 177 return jdoPackage.name + "." + name; 178 } 179 } 180 181 185 public String toString() { 186 String s = ("\n class name : " + name + 187 ", identityType : " + identityType + 188 ", objectidClass : " + objectidClass + 189 ", requiresExtent : " + requiresExtent + 190 ",version : " + version + 191 ", persistenceCapableSuperClass : " + superClassName); 192 s += ", \t fields:["; 193 Iterator it = jdoField.values().iterator(); 194 while (it.hasNext()) { 195 s = s + "\t" + it.next().toString(); 196 } 197 s += "], \t fetchGroups:["; 198 Iterator it2 = jdoFetchGroup.values().iterator(); 199 while (it2.hasNext()) { 200 s += "\t" + it2.next().toString(); 201 } 202 s += "]"; 203 return s; 204 } 205 206 211 public void add(Object field) { 212 SpeedoField f = (SpeedoField) field; 213 f.jdoClass = this; 214 f.number = fieldsCount++; 215 jdoField.put(f.name, field); 216 } 217 218 225 public void add(Object field, boolean failsOnError, Logger logger) throws SpeedoException { 226 SpeedoField f = (SpeedoField) field; 227 if (jdoField.containsKey(f.name)) { 228 if (failsOnError) 229 throw new SpeedoException("The field " + f.name + " of class " 230 + name + " is defined twice."); 231 else 232 logger.log(BasicLevel.ERROR, "The field " + f.name + " of class " 233 + name + " is defined twice."); 234 } else { 235 logger.log(BasicLevel.DEBUG, "Add the field '" + f.name 236 + "' in class '" + name + "'."); 237 f.jdoClass = this; 238 jdoField.put(f.name, field); 239 } 240 } 241 242 246 public void addVersion(Object version) { 247 SpeedoVersion v = (SpeedoVersion) version; 248 v.jdoClass = this; 249 this.version = v; 250 } 251 252 public void addDatastoreId(Object datastoreId) { 253 SpeedoDatastoreId d = (SpeedoDatastoreId) datastoreId; 254 d.jdoClass = this; 255 this.datastoreId = d; 256 this.datastoreSequence = d.sequence; 258 } 259 260 265 public void addFetchGroup(Object fetchGroup) { 266 SpeedoFetchGroup fg = (SpeedoFetchGroup) fetchGroup; 267 jdoFetchGroup.put(fg.name, fetchGroup); 268 } 269 270 277 public void addFetchGroup(Object fetchGroup, boolean failsOnError, Logger logger) throws SpeedoException { 278 SpeedoFetchGroup fg = (SpeedoFetchGroup) fetchGroup; 279 if (jdoFetchGroup.containsKey(fg.name)) { 280 if (failsOnError) 281 throw new SpeedoException("The fetchgroup " + fg.name + " of class " 282 + name + " is defined twice."); 283 else 284 logger.log(BasicLevel.ERROR, "The fetchgroup " + fg.name + " of class " 285 + name + " is defined twice."); 286 } else { 287 logger.log(BasicLevel.DEBUG, "Add the fetchgroup '" + fg.name 288 + "' in class '" + name + "'."); 289 jdoFetchGroup.put(fg.name, fetchGroup); 290 } 291 } 292 293 294 299 public int computeFieldNumbers() { 300 if (fieldsCount != -1) { 301 return fieldsCount; 302 } 303 if (superClassName.length() == 0) { 304 fieldsCount = 0; 305 } else { 306 SpeedoClass jdoSuperClass = jdoPackage.jdoXMLDescriptor.smi 307 .getSpeedoClass(superClassName, jdoPackage); 308 if (jdoSuperClass == null) { 309 throw new JDOUserException( 310 "The persistence-capable-superclass field has a bad value: '" 311 + superClassName 312 + "'. See the description of the class '" + getFQName() 313 + "' from the file '" + getJDOFileName() + "'."); 314 } 315 fieldsCount = jdoSuperClass.computeFieldNumbers(); 316 } 317 Iterator i = jdoField.values().iterator(); 318 while (i.hasNext()) { 319 SpeedoField jf = (SpeedoField) i.next(); 320 jf.number = fieldsCount++; 321 } 322 return fieldsCount; 323 } 324 325 public String getJormFileName() { 326 return getFQName().replace('.', File.separatorChar) + ".pd"; 327 } 328 329 public boolean generateObjectId() { 330 return getPkFieldCount() > 1; 331 } 332 333 public int getPkFieldCount() { 334 if (pkFieldsCount == -1) { 335 pkFieldsCount = 0; 336 if (identityType == SpeedoIdentity.USER_ID && 337 (objectidClass == null || objectidClass.length() == 0)) { 338 Iterator it = jdoField.values().iterator(); 339 while(it.hasNext()) { 340 if (((SpeedoField) it.next()).primaryKey) { 341 pkFieldsCount++; 342 } 343 } 344 } 345 } 346 return pkFieldsCount; 347 } 348 public SpeedoClass getSpeedoClassFromContext(String className) { 349 return jdoPackage.jdoXMLDescriptor.smi.getSpeedoClass(className, jdoPackage); 350 } 351 352 public String getJDOFileName() { 353 return jdoPackage.jdoXMLDescriptor.xmlFile; 354 } 355 356 public SpeedoClass getSuper() { 357 if (superClassName == null) { 358 return null; 359 } else { 360 return getSpeedoClassFromContext(superClassName); 361 } 362 } 363 364 public SpeedoClass getAncestor() { 365 SpeedoClass tmp = getSuper(); 366 SpeedoClass ancestor = null; 367 while(tmp != null && ancestor != tmp) { 368 ancestor = tmp; 369 tmp = tmp.getSuper(); 370 } 371 return ancestor; 372 } 373 } 374 | Popular Tags |