1 23 24 package org.objectweb.medor.optim.jorm; 25 26 import java.util.ArrayList ; 27 import java.util.Iterator ; 28 29 import org.objectweb.jorm.api.PClassMapping; 30 import org.objectweb.jorm.api.PException; 31 import org.objectweb.jorm.api.PMapper; 32 import org.objectweb.jorm.lib.JormPathHelper; 33 import org.objectweb.jorm.metainfo.api.Class; 34 import org.objectweb.jorm.metainfo.api.ClassMapping; 35 import org.objectweb.jorm.metainfo.api.ClassProject; 36 import org.objectweb.jorm.metainfo.api.GenClassMapping; 37 import org.objectweb.jorm.metainfo.api.GenClassRef; 38 import org.objectweb.jorm.metainfo.api.Mapping; 39 import org.objectweb.jorm.metainfo.api.MetaObject; 40 import org.objectweb.jorm.metainfo.api.ParentClassMapping; 41 import org.objectweb.jorm.metainfo.api.PrimitiveElement; 42 import org.objectweb.jorm.metainfo.api.PrimitiveElementMapping; 43 import org.objectweb.jorm.naming.api.PNameCoder; 44 import org.objectweb.medor.api.MedorException; 45 import org.objectweb.medor.expression.api.Operand; 46 import org.objectweb.medor.expression.lib.BasicOperand; 47 import org.objectweb.medor.optim.lib.BasicRule; 48 import org.objectweb.medor.query.jorm.api.JormExtent; 49 import org.objectweb.medor.query.jorm.lib.ClassExtent; 50 import org.objectweb.medor.query.jorm.lib.GenClassExtent; 51 import org.objectweb.medor.query.jorm.lib.PNameField; 52 import org.objectweb.medor.type.lib.PTypeSpaceMedor; 53 import org.objectweb.util.monolog.api.BasicLevel; 54 55 62 public abstract class JormRule extends BasicRule { 63 64 public JormRule(String suffix) { 65 super(suffix); 66 } 67 68 69 77 protected PrimitiveElement getPrimitiveElement(JormExtent extent, 78 String fieldname) 79 throws MedorException { 80 PrimitiveElement te = null; 81 if (extent.getMetaObject() instanceof Class ) { 82 Class clazz = (Class ) extent.getMetaObject(); 83 te = (PrimitiveElement) clazz.getTypedElement(fieldname); 84 if (te == null) { 85 te = clazz.getHiddenField(fieldname); 86 } 87 if (te == null) { 88 String msg = "No field " + fieldname 89 + " available on the class " + clazz.getFQName(); 90 log.log(BasicLevel.ERROR, msg); 91 throw new MedorException(msg); 92 } 93 } 94 else if (extent.getMetaObject() instanceof GenClassRef) { 95 GenClassRef gcr = (GenClassRef) extent.getMetaObject(); 96 te = gcr.getHiddenField(fieldname); 97 if (te == null) { 98 String msg = "No field " + fieldname 99 + " available on the generic class " 100 + gcr.getGenClassId(); 101 log.log(BasicLevel.ERROR, msg); 102 throw new MedorException(msg); 103 } 104 } 105 else { 106 throw new MedorException("unmanaged jorm extent: " + extent); 107 } 108 return te; 109 } 110 111 119 protected PrimitiveElementMapping getPEM(JormExtent extent, 120 String fieldName) 121 throws MedorException { 122 PrimitiveElementMapping res = null; 123 String pn = extent.getProjectName(); 124 String mn = extent.getPMapper().getMapperName(); 125 if (extent instanceof ClassExtent) { 126 Class clazz = (Class ) extent.getMetaObject(); 127 ClassMapping cm = getClassMapping(extent); 128 String jormFieldName = getJORMFieldName(fieldName, extent); 129 res = cm.getPrimitiveElementMapping(jormFieldName); 130 if (res == null) { 131 132 ArrayList pcms = new ArrayList (cm.getParentClassMappings()); 134 while (!pcms.isEmpty() && res == null) { 135 ParentClassMapping pcm = (ParentClassMapping) pcms.remove(0); 136 ClassMapping _cm = pcm.getMOClass() 137 .getClassProject(pcm.getProjectName()) 138 .getMapping(pcm.getMapperName()).getClassMapping(); 139 res = _cm.getPrimitiveElementMapping(jormFieldName); 140 if (res == null) { 141 pcms.addAll(_cm.getParentClassMappings()); 142 } 143 } 144 if (res == null) { 146 PClassMapping pcm = extent.getPMapper().lookup(clazz.getFQName()); 149 try { 150 PClassMapping[] childrenPCMs = pcm.getSubPCMs(); 152 if (childrenPCMs != null) { 153 for(int j = 0; j < childrenPCMs.length; j++){ 155 Class subclass = extent.getPMapper().getMetaInfoManager().getClass( 157 childrenPCMs[j].getClassName()); 158 if (subclass == null) { 159 throw new MedorException("Impossible to retrieve the jorm class " + childrenPCMs[j].getClassName()); 160 } 161 ClassMapping subCM = subclass.getClassMapping( 162 extent.getProjectName(),getMapperName(extent.getPMapper().getMapperName())); 163 if (subCM != null) { 164 res = subCM.getPrimitiveElementMapping(jormFieldName); 165 if (res != null) { 166 return res; 167 } 168 } 169 } 170 } 171 } catch(PException e) { 172 throw new MedorException("Problem occurred while computing list of pems for sub classes.", e); 173 } 174 } 175 } 176 if (res == null) { 177 String msg = "No mapping for field '" + getJORMFieldName(fieldName, extent) 178 + "' of the class '" + clazz.getFQName() 179 + "' in the project '" + pn 180 + "' and the mapping '" + mn; 181 log.log(BasicLevel.ERROR, msg); 182 log.log(BasicLevel.ERROR, "Existing field mappings:"); 183 for(Iterator it= cm.getAllPrimitiveElementMappings().iterator(); it.hasNext();) { 184 res = (PrimitiveElementMapping) it.next(); 185 log.log(BasicLevel.ERROR, "Field: " 186 + ((PrimitiveElement) res.getLinkedMO()).getName()); 187 } 188 throw new MedorException(msg); 189 } 190 return res; 191 } 192 else if (extent instanceof GenClassExtent) { 193 GenClassRef gcr = (GenClassRef) extent.getMetaObject(); 194 GenClassMapping gcm = getGenClassMapping(extent); 195 res = gcm.getPrimitiveElementMapping( 196 getJORMFieldName(fieldName, extent)); 197 if (res == null) { 198 String msg = "No mapping for field '" + getJORMFieldName(fieldName, extent) 199 + "' of the generic class '" + gcr.getGenClassId() 200 + "' in the project '" + pn 201 + " and the mapping '" + mn; 202 log.log(BasicLevel.ERROR, msg); 203 log.log(BasicLevel.ERROR, "Existing field mappings:"); 204 for(Iterator it= gcm.getPrimitiveElementMappings().iterator(); it.hasNext();) { 205 res = (PrimitiveElementMapping) it.next(); 206 log.log(BasicLevel.ERROR, "Field: " 207 + ((PrimitiveElement) res.getLinkedMO()).getName()); 208 } 209 throw new MedorException(msg); 210 } 211 return res; 212 } 213 else { 214 throw new MedorException("Unmanaged JormExtent: " + extent); 215 } 216 } 217 218 224 protected String getJORMFieldName(String fullName, JormExtent ext) { 225 int idx = fullName.indexOf(ext.getName() + '.'); 226 if (idx == -1) 227 return fullName; 228 else 229 return fullName.substring(ext.getName().length() + 1); 230 } 231 232 243 protected ClassMapping getClassMapping(JormExtent extent) 244 throws MedorException { 245 String pn = extent.getProjectName(); 246 PMapper mapper = extent.getPMapper(); 247 if (mapper == null) { 248 String msg = "No mapper has not been assigned to the JormExtent (jorm name: " 249 + extent.getJormName() 250 + ", node name: " + extent.getName() + ")"; 251 log.log(BasicLevel.ERROR, msg); 252 throw new MedorException(msg); 253 } 254 String mn = mapper.getMapperName(); 255 Class clazz = (Class ) extent.getMetaObject(); 256 ClassProject cp = clazz.getClassProject(pn); 257 if (cp == null) { 258 String msg = "No project " + pn + " for the class " 259 + clazz.getFQName(); 260 log.log(BasicLevel.ERROR, msg); 261 throw new MedorException(msg); 262 } 263 int idx = mn.indexOf('.'); 264 if (idx != -1) { 265 mn = mn.substring(0, idx); 266 } 267 Mapping m = cp.getMapping(mn); 268 if (m == null) { 269 String msg = "No mapping '" + mn 270 + "' in the project '" + pn 271 + "' of the class " + clazz.getFQName(); 272 log.log(BasicLevel.ERROR, msg); 273 throw new MedorException(msg); 274 } 275 return m.getClassMapping(); 276 } 277 278 289 protected GenClassMapping getGenClassMapping(JormExtent extent) 290 throws MedorException { 291 String pn = extent.getProjectName(); 292 String mn = extent.getPMapper().getMapperName(); 293 GenClassRef gcr = (GenClassRef) extent.getMetaObject(); 294 MetaObject parentclazz = gcr.getParent(); 295 while (!(parentclazz instanceof Class )) { 296 parentclazz = parentclazz.getParent(); 297 } 298 ClassProject cp = ((Class ) parentclazz).getClassProject(pn); 299 if (cp == null) { 300 String msg = "No project " + pn + " for the generic class " 301 + gcr.getGenClassId(); 302 log.log(BasicLevel.ERROR, msg); 303 throw new MedorException(msg); 304 } 305 int idx = mn.indexOf('.'); 306 if (idx != -1) { 307 mn = mn.substring(0, idx); 308 } 309 Mapping m = cp.getMapping(mn); 310 if (m == null) { 311 String msg = "No mapping '" + mn 312 + "' in the project '" + pn 313 + "' of the generic class " + gcr.getGenClassId(); 314 log.log(BasicLevel.ERROR, msg); 315 throw new MedorException(msg); 316 } 317 return m.getGenClassMapping(gcr.getGenClassId()); 318 } 319 320 329 protected Operand getPNCOperand(JormExtent extent, PNameField pnf) { 330 PNameCoder pnc = JormPathHelper.getPNameCoder( 331 pnf.getPNamingContextParameter(), extent.getPMapper()); 332 log.log(BasicLevel.DEBUG, "getPNCOperand for pnf " + pnf); 333 334 if (pnc == null) { 335 return new org.objectweb.medor.expression.lib.BasicParameterOperand( 336 PTypeSpaceMedor.NAMING_CONTEXT, 337 pnf.getPNamingContextParameter()); 338 } else { 339 return new BasicOperand(pnc, PTypeSpaceMedor.NAMING_CONTEXT); 340 } 341 } 342 343 private static String getMapperName(String mn) { 344 if (mn == null) { 345 return null; 346 } 347 int idx = mn.indexOf('.'); 348 if (idx == -1) { 349 return mn; 350 } else { 351 return mn.substring(0, idx); 352 } 353 } 354 } 355 | Popular Tags |