1 23 24 25 package com.sun.jdo.api.persistence.enhancer.meta; 26 27 import java.io.PrintWriter ; 28 29 import com.sun.jdo.api.persistence.enhancer.util.Support; 30 import com.sun.jdo.api.persistence.model.Model; 31 import com.sun.jdo.api.persistence.model.jdo.PersistenceFieldElement; 32 import com.sun.jdo.api.persistence.model.jdo.PersistenceClassElement; 33 import com.sun.jdo.api.persistence.model.jdo.RelationshipElement; 34 35 36 39 public class JDOMetaDataModelImpl extends Support 41 implements JDOMetaData 42 { 43 protected final PrintWriter out; 45 46 protected Model model; 48 49 52 public JDOMetaDataModelImpl(Model model) { 53 this(model, null); 54 } 55 56 public JDOMetaDataModelImpl(Model model, 57 PrintWriter out) 58 throws JDOMetaDataUserException, JDOMetaDataFatalError 59 { 60 if (model == null) { 62 final String msg 63 = "Initializing meta data: model == null"; throw new JDOMetaDataFatalError(msg); 65 } 66 this.model = model; 67 this.out = out; 68 } 69 70 73 public boolean isTransientClass(String classPath) 74 throws JDOMetaDataUserException, JDOMetaDataFatalError 75 { 76 if (classPath.startsWith("java/")) return true; 79 if (classPath.startsWith("javax/")) return true; 81 if (classPath.startsWith("com/sun/jdo/")) return true; 83 return false; 84 } 85 86 89 public boolean isPersistenceCapableClass(String classPath) 90 throws JDOMetaDataUserException, JDOMetaDataFatalError 91 { 92 if (isTransientClass(classPath)) 94 return false; 95 final String className = pathToName(classPath); 96 return model.isPersistent(className); 97 } 98 99 102 public boolean isPersistenceCapableRootClass(String classPath) 103 throws JDOMetaDataUserException, JDOMetaDataFatalError 104 { 105 final String className = pathToName(classPath); 107 return model.isPersistent(className); 108 } 111 112 115 public String getPersistenceCapableRootClass(String classPath) 116 throws JDOMetaDataUserException, JDOMetaDataFatalError 117 { 118 return (isPersistenceCapableClass(classPath) ? classPath : null); 120 } 121 122 128 public String getSuperClass(String classPath) 129 throws JDOMetaDataUserException, JDOMetaDataFatalError 130 { 131 return null; 133 } 134 135 138 public boolean isSecondClassObjectType(String classPath) 139 throws JDOMetaDataUserException, JDOMetaDataFatalError 140 { 141 final String className = pathToName(classPath); 142 return model.isSecondClassObject(className); 143 } 144 145 148 public boolean isMutableSecondClassObjectType(String classPath) 149 throws JDOMetaDataUserException, JDOMetaDataFatalError 150 { 151 final String className = pathToName(classPath); 152 return model.isMutableSecondClassObject(className); 153 } 154 155 158 public boolean isPersistentField(String classPath, String fieldName) 159 throws JDOMetaDataUserException, JDOMetaDataFatalError 160 { 161 final String className = pathToName(classPath); 162 return model.isPersistent(className, fieldName); 163 } 164 165 168 public boolean isTransactionalField(String classPath, String fieldName) 169 throws JDOMetaDataUserException, JDOMetaDataFatalError 170 { 171 return false; 173 } 174 175 178 public boolean isPrimaryKeyField(String classPath, String fieldName) 179 throws JDOMetaDataUserException, JDOMetaDataFatalError 180 { 181 final String className = pathToName(classPath); 182 return model.isKey(className, fieldName); 183 } 184 185 190 public boolean isDefaultFetchGroupField(String classPath, String fieldName) 191 throws JDOMetaDataUserException, JDOMetaDataFatalError 192 { 193 final String className = pathToName(classPath); 194 boolean isdfgField = model.isDefaultFetchGroup(className, fieldName); 195 if(isdfgField) { 196 final PersistenceFieldElement pfe 197 = model.getPersistenceField(className, fieldName); 198 if (pfe instanceof RelationshipElement) { 199 isdfgField = false; 209 } 210 } 211 return isdfgField; 212 } 213 214 218 public int getFieldNo(String classPath, String fieldName) 219 throws JDOMetaDataUserException, JDOMetaDataFatalError 220 { 221 final String className = pathToName(classPath); 222 final PersistenceFieldElement pfe 223 = model.getPersistenceField(className, fieldName); 224 if (pfe == null 225 || pfe.getPersistenceType() != PersistenceFieldElement.PERSISTENT) 226 return -1; 227 228 return pfe.getFieldNumber(); 229 } 230 231 235 private final String [] getPersistentFields(String classPath) 236 throws JDOMetaDataUserException, JDOMetaDataFatalError 237 { 238 final String className = pathToName(classPath); 239 final PersistenceClassElement pce 240 = model.getPersistenceClass(className); 241 if (pce == null) 242 return new String []{}; 243 244 final PersistenceFieldElement[] pfes = pce.getFields(); 246 final int nofFields = (pfes != null ? pfes.length : 0); 247 final String [] names = new String [nofFields]; 248 for (int i = 0; i < nofFields; i++) { 249 final PersistenceFieldElement pfe = pfes[i]; 250 names[i] = pfe.getName(); 251 252 if (false) { 254 if (pfe.getPersistenceType() 255 != PersistenceFieldElement.PERSISTENT) { 256 final String msg 257 = ("Getting persistent field names: " + "Encountered non-persistent field '" + names[i] + "' for class " + classPath); throw new JDOMetaDataFatalError(msg); 261 } 264 } 265 } 266 return names; 267 } 268 269 273 public String [] getManagedFields(String classPath) 274 throws JDOMetaDataUserException, JDOMetaDataFatalError 275 { 276 return getPersistentFields(classPath); 277 } 278 279 static protected String pathToName(String classPath) { 280 if (classPath != null) { 281 return classPath.replace('/', '.'); 282 } else { 283 return null; 284 } 285 } 286 287 static protected String nameToPath(String className) { 288 if (className != null) { 289 return className.replace('.', '/'); 290 } else { 291 return null; 292 } 293 } 294 } 295 | Popular Tags |