1 23 24 package org.objectweb.medor.query.jorm.lib; 25 26 import org.objectweb.jorm.metainfo.api.Class; 27 import org.objectweb.jorm.metainfo.api.Manager; 28 import org.objectweb.jorm.metainfo.api.MetaObject; 29 import org.objectweb.jorm.metainfo.api.Reference; 30 import org.objectweb.jorm.metainfo.api.TypedElement; 31 import org.objectweb.jorm.metainfo.api.GenClassRef; 32 import org.objectweb.jorm.metainfo.api.NameDef; 33 import org.objectweb.jorm.metainfo.api.ClassRef; 34 import org.objectweb.jorm.metainfo.api.Mapping; 35 import org.objectweb.jorm.metainfo.api.ClassProject; 36 import org.objectweb.jorm.metainfo.api.ReferenceMapping; 37 import org.objectweb.jorm.lib.JormPathHelper; 38 import org.objectweb.medor.query.api.QueryTree; 39 import org.objectweb.medor.query.jorm.api.JormExtent; 40 import org.objectweb.medor.api.MedorException; 41 import org.objectweb.util.monolog.api.BasicLevel; 42 43 import java.util.Iterator ; 44 import java.util.Map ; 45 46 63 public class PNameField extends BasicJormField { 64 65 69 protected Class clazz; 70 71 77 protected boolean isClassPName; 78 79 84 protected boolean isInGenClass; 85 86 public PNameField() { 87 } 88 89 99 public PNameField(String fieldName, 100 GenClassRef gcr, 101 boolean isGenClassPName, 102 QueryTree qt) throws MedorException { 103 super(fieldName, qt, gcr); 104 isInGenClass = true; 105 isClassPName = isGenClassPName; 106 } 107 108 114 public PNameField(String fieldName, ClassRef cr, QueryTree qt) throws MedorException { 115 super(fieldName, qt, cr); 116 isInGenClass = cr.getParent() instanceof GenClassRef; 117 isClassPName = false; 118 } 119 120 129 public PNameField(String fn, Class cl, QueryTree qt) throws MedorException { 130 super(fn, qt); 131 isInGenClass = false; 132 TypedElement te = getTypedElement(cl, fn); 133 if (te != null && te instanceof Reference) { 134 type = te.getType(); 135 isClassPName = false; 136 this.te = te; 137 } 138 else { 139 clazz = cl; 140 isClassPName = true; 141 type = clazz.getPType(); 142 } 143 } 144 145 public Object clone(Object clone, 146 Map obj2clone) throws CloneNotSupportedException { 147 clone = super.clone(clone, obj2clone); 148 ((PNameField) clone).clazz = clazz; 149 ((PNameField) clone).isClassPName = isClassPName; 150 ((PNameField) clone).isInGenClass = isInGenClass; 151 return clone; 152 } 153 154 158 public boolean isClassPName() { 159 return isClassPName; 160 } 161 162 166 public Reference getReference() { 167 return (Reference) te; 168 } 169 170 174 public Class getMetaObjectClass() { 175 return clazz; 176 } 177 178 182 public GenClassRef getGenClassRef() { 183 return (GenClassRef)te; 184 } 185 186 191 public boolean isInGenClass() { 192 return isInGenClass; 193 } 194 195 private TypedElement getTypedElement(Class clazz, String fn) { 196 int idx = fn.length(); 197 TypedElement te = null; 198 te = clazz.getTypedElement(fn); 199 while ((idx = fn.lastIndexOf(".", idx)) != -1 && te == null) { 200 te = clazz.getTypedElement(fn.substring(idx + 1, fn.length())); 201 idx--; 202 } 203 return te; 204 } 205 206 212 public NameDef getNameDef(JormExtent extent) throws MedorException { 213 if (isClassPName) { 214 if (isInGenClass) { 215 return getRefNameDef((GenClassRef) extent.getMetaObject(), extent); 217 } else { 218 return getClassNameDef(extent); 220 } 221 } else { 222 if (isInGenClass) { 223 return getElemNameDef((Reference) te, extent); 224 } else { 225 return getRefNameDef((Reference) te, extent); 227 } 228 } 229 } 230 231 234 public String getPNamingContextParameter() { 235 if (isClassPName) { 236 if (isInGenClass) { 237 return JormPathHelper.getPath((GenClassRef) te, false); 239 } else { 240 return JormPathHelper.getPath(clazz); 242 } 243 } else { 244 if (te instanceof GenClassRef) { 245 return JormPathHelper.getPath((GenClassRef) te, 247 !isInGenClass); 248 } 249 else 250 return JormPathHelper.getPath((ClassRef) te); 252 } 253 } 254 255 262 public MetaObject getTargetedMetaObject() { 263 if (isInGenClass) { 264 if (isClassPName) { 265 return te; 266 } else { 267 GenClassRef gcr = (GenClassRef) te; 268 if (gcr.isClassRef()) { 269 return gcr.getClassRef().getMOClass(); 270 } else if (gcr.isGenClassRef()) { 271 return gcr.getGenClassRef(); 272 } else { 273 return null; 274 } 275 } 276 } else { 277 if (isClassPName) { 278 return clazz; 279 } else { 280 return te; 281 } 282 } 283 } 284 291 private NameDef getRefNameDef(GenClassRef gcr, 292 JormExtent extent) throws MedorException { 293 return (NameDef) getMapping(extent) 294 .getGenClassMapping(gcr.getGenClassId()) 295 .getIdentifierMapping().getLinkedMO(); 296 } 297 298 299 307 private NameDef getElemNameDef(Reference r, 308 JormExtent extent) throws MedorException { 309 return (NameDef) getMapping(extent) 310 .getGenClassMapping(((GenClassRef) r.getParent()).getGenClassId()) 311 .getReferenceMapping().getLinkedMO(); 312 } 313 314 322 private NameDef getRefNameDef(Reference r, 323 JormExtent extent) throws MedorException { 324 if (logger != null && logger.isLoggable(BasicLevel.DEBUG)) { 325 logger.log(BasicLevel.DEBUG, "Extent class mapping " + getMapping(extent).getClassMapping()); 326 Iterator it = getMapping(extent).getClassMapping().getReferenceMappings().iterator(); 327 while (it.hasNext()) { 328 logger.log(BasicLevel.DEBUG, "Reference rule name " + ((ReferenceMapping)it.next()).getRuleName()); 329 } 330 logger.log(BasicLevel.DEBUG, "+ reference mapping " + getMapping(extent).getClassMapping().getReferenceMapping(r.getName())); 331 logger.log(BasicLevel.DEBUG, "+ rule name " + getMapping(extent).getClassMapping().getReferenceMapping(r.getName()).getRuleName()); 332 logger.log(BasicLevel.DEBUG, "+ linkedMO " + getMapping(extent).getClassMapping().getReferenceMapping(r.getName()).getLinkedMO()); 333 } 334 return (NameDef) getMapping(extent).getClassMapping() 335 .getReferenceMapping(r.getName()).getLinkedMO(); 336 } 337 338 344 private Class getClass(MetaObject mo) { 345 if (mo instanceof Class ) { 346 return (Class ) mo; 347 } else if (mo instanceof Package || mo instanceof Manager) { 348 return null; 349 } else { 350 return getClass(mo.getParent()); 351 } 352 } 353 354 360 private Mapping getMapping(JormExtent extent) throws MedorException { 361 Class c = getClass(extent.getMetaObject()); 362 ClassProject cp = c.getClassProject(extent.getProjectName()); 363 if (cp == null) { 364 throw new MedorException("No classproject found for the class " 365 + c.getFQName() + " and the project " 366 + extent.getProjectName()); 367 } 368 String mapperName = extent.getPMapper().getMapperName(); 369 int idx = mapperName.indexOf('.'); 370 Mapping m = cp.getMapping(idx == -1 371 ? mapperName 372 : mapperName.substring(0, idx)); 373 if (m == null) { 374 throw new MedorException("No mapping found for the class " 375 + c.getFQName() + ", the project " + extent.getProjectName() 376 + " and the mapper " + mapperName); 377 } 378 return m; 379 } 380 381 387 private NameDef getClassNameDef(JormExtent extent) throws MedorException { 388 return (NameDef) getMapping(extent).getClassMapping() 389 .getIdentifierMapping().getLinkedMO(); 390 } 391 392 } 393 | Popular Tags |