1 23 package org.objectweb.jorm.mapper.rdb.metainfo; 24 25 import org.objectweb.jorm.naming.api.NamingFilterKeyProvider; 26 import org.objectweb.jorm.metainfo.api.*; 27 import org.objectweb.jorm.metainfo.api.Class; 28 import org.objectweb.jorm.metainfo.lib.BasicClassMapping; 29 import org.objectweb.jorm.metainfo.lib.FieldComparator; 30 import org.objectweb.jorm.metainfo.lib.BasicParentClassMapping; 31 import org.objectweb.jorm.type.api.PType; 32 import org.objectweb.medor.expression.api.Expression; 33 import org.objectweb.medor.expression.api.ExpressionException; 34 import org.objectweb.medor.expression.api.ParameterOperand; 35 import org.objectweb.medor.expression.api.Operator; 36 import org.objectweb.medor.expression.lib.BasicOperand; 37 import org.objectweb.medor.expression.lib.BasicParameterOperand; 38 import org.objectweb.medor.expression.lib.BasicVariableOperand; 39 import org.objectweb.medor.expression.lib.Equal; 40 import org.objectweb.medor.expression.lib.ExpressionPrinter; 41 import org.objectweb.util.monolog.api.BasicLevel; 42 43 import java.util.Iterator ; 44 import java.util.ArrayList ; 45 import java.util.Collection ; 46 import java.util.List ; 47 import java.util.Collections ; 48 import java.util.Set ; 49 import java.util.HashSet ; 50 import java.text.DateFormat ; 51 import java.text.ParseException ; 52 import java.math.BigDecimal ; 53 import java.math.BigInteger ; 54 55 85 public class RdbClassMapping extends BasicClassMapping { 86 99 public final static String MAP_NEW_FIELDS_TO_EXTENDED_STRUCTURES = "to-extended-structures"; 100 101 108 public final static String MAP_NEW_FIELDS_TO_ADDED_STRUCTURES = "to-added-structures"; 109 110 117 public final static String REMAP_FIELDS_TO_NEW_STRUCTURES = "to-new-structures"; 118 119 127 129 132 RdbTable table = null; 133 134 137 RdbFilter filter = null; 138 139 140 143 RdbInheritanceQuery inheritanceQuery = null; 144 145 155 public RdbClassMapping(String ruleName, MetaObject linkedMO, 156 MetaObject parent) { 157 super(ruleName, linkedMO, parent); 158 } 159 160 public List getAllPrimitiveElementMappings() { 164 List pems = super.getAllPrimitiveElementMappings(); 165 List pcms = (List ) getParentClassMappings(); 166 int i; 167 for(i = pcms.size()-1; i>=0; i--) { 168 ParentClassMapping pcm = (ParentClassMapping) pcms.get(i); 169 if ((MAP_NEW_FIELDS_TO_EXTENDED_STRUCTURES.equalsIgnoreCase(pcm.getRuleName())) || 170 (MAP_NEW_FIELDS_TO_ADDED_STRUCTURES.equalsIgnoreCase(pcm.getRuleName()))) { 171 Class superClass = pcm.getMOClass(); 173 ClassProject cp = superClass.getClassProject(getProjectName()); 174 if (cp != null) { 175 Mapping m = cp.getMapping(getMapperName()); 176 if (m != null) { 177 pems.addAll(m.getClassMapping().getAllPrimitiveElementMappings()); 179 } 180 } 181 } 182 } 183 if (i == -1) { 184 Collections.sort(pems, FieldComparator.instance); 185 } 186 return pems; 187 } 188 189 public RdbTable createRdbTable(String tableName) { 190 if (table == null) { 191 table = new RdbTable(this, getLinkedMO(), tableName); 192 table.setLogger(getLogger()); 193 table.setLoggerFactory(getLoggerFactory()); 194 } else { 195 table.setName(tableName); 196 } 197 return table; 198 } 199 200 public RdbTable getRdbTable() { 201 return table; 202 } 203 211 public void getMainRdbTableOld(ArrayList res) { 212 if ( !getParentClassMappings().isEmpty()) { 213 for (Iterator it = getParentClassMappings().iterator(); it.hasNext();) { 214 ParentClassMapping pcm = (ParentClassMapping) it.next(); 215 String ruleName = pcm.getRuleName(); 216 if (MAP_NEW_FIELDS_TO_ADDED_STRUCTURES.equals(ruleName) || 218 MAP_NEW_FIELDS_TO_EXTENDED_STRUCTURES.equals(ruleName)) { 219 Class parentClass = pcm.getMOClass(); 220 RdbClassMapping classmapping = 221 (RdbClassMapping) parentClass.getClassMapping(getProjectName(),getMapperName()); 222 classmapping.getMainRdbTableOld(res); 223 } else { 224 res.add(table); 226 } 227 } 228 } else { 229 res.add(table); 232 } 233 } 234 235 public RdbTable getMainRdbTable() { 236 if (table == null) { 239 ParentClassMapping aPcm = (ParentClassMapping) 241 parentClassMappings.get(0); 242 Class aSuperClass = aPcm.getMOClass(); 243 RdbClassMapping aSuperCm = 244 (RdbClassMapping) aSuperClass.getClassMapping(getProjectName(), 245 getMapperName()); 246 table = aSuperCm.getMainRdbTable(); 247 } 248 return table; 249 } 250 251 public RdbFilter createRdbFilter() { 252 filter = new RdbFilter(this); 253 return filter; 254 } 255 256 public RdbFilter getRdbFilter() { 257 return filter; 258 } 259 260 public RdbInheritanceQuery getRdbInheritanceQuery() { 261 if(inheritanceQuery == null) 262 createRdbInheritanceQuery(); 263 return inheritanceQuery; 264 } 265 266 public RdbInheritanceQuery createRdbInheritanceQuery() { 267 if(inheritanceQuery == null) 268 inheritanceQuery = new RdbInheritanceQuery(this); 269 return inheritanceQuery; 270 } 271 272 public Expression getMappingFilterFromNamingFilter() 273 throws ExpressionException { 274 return getMappingFilterFromNamingFilter(null); 275 } 276 public Expression getMappingFilterFromNamingFilter(NamingFilterKeyProvider nfkp) 277 throws ExpressionException { 278 if (debug) { 279 logger.log(BasicLevel.DEBUG, 280 "getMappingFilterFromNamingFilter: class " 281 + ((Class ) linkedMO).getFQName()); 282 } 283 NameDef nd = getIdentifierMapping().getNameDef(); 284 if (nd == null) { 285 if (debug) { 286 logger.log(BasicLevel.DEBUG, 287 "getMappingFilterFromNamingFilter: null NameDef"); 288 } 289 return null; 290 } 291 Class clazz = getJormClass(); 292 Expression namingFilter = null; 293 namingFilter = clazz.getInheritanceFilter(nd); 294 Object key; 295 if (nfkp != null) { 296 key = nfkp.getNamingFilterKey(); 300 } else { 301 key = clazz.getInheritanceNamingKey(nd); 303 } 304 if (debug) { 305 logger.log(BasicLevel.DEBUG, 306 "getMappingFilterFromNamingFilter: key=" + key 307 + " / namingFilter=" + namingFilter); 308 } 309 if (key != null) { if (namingFilter == null) { 311 logger.log(BasicLevel.DEBUG, "Find the naming filter on ancestors"); 312 ArrayList al = new ArrayList (clazz.getSuperClasses()); 313 while (namingFilter == null && !al.isEmpty()) { 314 Class superClass = (Class ) al.remove(0); 315 logger.log(BasicLevel.DEBUG, "Ancestor " + superClass.getFQName()); 316 namingFilter = superClass.getInheritanceFilter(nd); 317 if (namingFilter == null) { 318 al.addAll(superClass.getSuperClasses()); 319 } 320 } 321 if (namingFilter == null) { 322 logger.log(BasicLevel.DEBUG, "No the naming filter found on ancestors"); 323 return null; 325 } else { 326 logger.log(BasicLevel.DEBUG, "Naming filter found on ancestors: " + ExpressionPrinter.e2str(namingFilter)); 327 } 328 } 330 logger.log(BasicLevel.DEBUG, "Compute the key filter with key value"); 331 BasicVariableOperand keyexp = new BasicVariableOperand(namingFilter.getType()); 334 if (key instanceof String ) { 335 assignKey(keyexp, (String ) key); 336 } else { 337 keyexp.setValue(key); 338 } 339 namingFilter = new Equal(keyexp, namingFilter); 340 logger.log(BasicLevel.DEBUG, "Naming filter changed into Equal: " + ExpressionPrinter.e2str(namingFilter)); 341 342 } else { if (namingFilter == null) { 344 return null; 345 } 346 } 347 Expression res = cloneAndReplaceLeaves(namingFilter); 349 if (debug) { 350 logger.log(BasicLevel.WARN, "Filter=" + ExpressionPrinter.e2str(res)); 351 } 352 return res; 353 } 354 355 private void assignKey(BasicVariableOperand keyexp, 356 String str) throws ExpressionException { 357 switch (keyexp.getType().getTypeCode()) { 358 case PType.TYPECODE_BYTE: 359 keyexp.setValue(Byte.parseByte(str)); 360 break; 361 case PType.TYPECODE_CHAR: 362 keyexp.setValue(str.charAt(0)); 363 break; 364 case PType.TYPECODE_SHORT: 365 keyexp.setValue(Short.parseShort(str)); 366 break; 367 case PType.TYPECODE_INT: 368 keyexp.setValue(Integer.parseInt(str)); 369 break; 370 case PType.TYPECODE_LONG: 371 keyexp.setValue(Long.parseLong(str)); 372 break; 373 case PType.TYPECODE_OBJBYTE: 374 keyexp.setValue(Long.valueOf(str)); 375 break; 376 case PType.TYPECODE_OBJCHAR: 377 keyexp.setValue(new Character (str.charAt(0))); 378 break; 379 case PType.TYPECODE_OBJSHORT: 380 keyexp.setValue(Short.valueOf(str)); 381 break; 382 case PType.TYPECODE_OBJINT: 383 keyexp.setValue(Integer.valueOf(str)); 384 break; 385 case PType.TYPECODE_OBJLONG: 386 keyexp.setValue(Long.valueOf(str)); 387 break; 388 case PType.TYPECODE_DATE: 389 try { 390 keyexp.setValue(DateFormat.getInstance().parse(str)); 391 } catch (ParseException e) { 392 throw new ExpressionException("Impossible to parse the date " + str, e); 393 } 394 break; 395 case PType.TYPECODE_STRING: 396 keyexp.setValue(str); 397 break; 398 case PType.TYPECODE_BIGDECIMAL: 399 keyexp.setValue(new BigDecimal (str)); 400 break; 401 case PType.TYPECODE_BIGINTEGER: 402 keyexp.setValue(new BigInteger (str)); 403 break; 404 default: 405 throw new ExpressionException("Umanaged key type: " 406 + keyexp.getType().getJavaName()); 407 } 408 } 409 410 411 418 private Expression cloneAndReplaceLeaves(Expression exp) 419 throws ExpressionException { 420 if (exp instanceof ParameterOperand) { 421 NameDef nd = getIdentifierMapping().getNameDef(); 423 String classFieldName = null; 424 if (nd.isFieldName()) { 425 if (debug) { 426 logger.log(BasicLevel.DEBUG, "nd.getFieldName = " + nd.getFieldName() 427 + " while exp.getName = " + ((ParameterOperand) exp).getName()); 428 } 429 return exp; 430 } else if (nd.isNameRef()) { 431 classFieldName = (String ) nd.getNameRef().getProjection().get( 432 ((ParameterOperand) exp).getName()); 433 if (classFieldName == null) { 435 return exp; 436 } 437 if (debug) { 438 logger.log(BasicLevel.DEBUG, "nref.getFieldName = " + classFieldName 439 + " while exp.getName = " + ((ParameterOperand) exp).getName()); 440 } 441 } else { 442 throw new ExpressionException("Naming type not managed: " + nd); 443 } 444 return new BasicParameterOperand(exp.getType(), classFieldName); 445 } else if (exp instanceof BasicVariableOperand) { 446 return new BasicVariableOperand((BasicOperand) exp); 448 } else if (exp instanceof BasicOperand) { 449 return new BasicOperand((BasicOperand) exp); 451 452 } else if (exp instanceof Operator) { 453 Operator oldop = (Operator) exp; 454 Operator newop = null; 455 try { 456 newop = (Operator) exp.getClass().newInstance(); 457 } catch (Exception e) { 458 throw new ExpressionException( 459 "Impossible to clone the operator " 460 + exp.getClass().getName() + ": ", e); 461 } 462 for(int i=(oldop.getOperandNumber() - 1); i>=0; i--) { 463 newop.setExpression(i, cloneAndReplaceLeaves(oldop.getExpression(i))); 465 } 466 return newop; 467 } else { 468 throw new ExpressionException("UnManaged expression: " + exp); 469 } 470 } 471 472 protected Collection getChildren() { 473 Collection col = super.getChildren(); 474 if (table != null) { 475 col.add(table); 476 } 477 return col; 478 } 479 480 public ParentClassMapping createImplicitParentClassMapping(Class superClass) { 481 BasicParentClassMapping pcm = 482 new BasicParentClassMapping(MAP_NEW_FIELDS_TO_EXTENDED_STRUCTURES, 483 superClass, this); 484 addParentClassMapping(pcm); 485 return pcm; 486 } 487 488 494 public void addImplicitDependencies() { 495 496 Class clazz = (Class ) getLinkedMO(); 498 for (Iterator itPcm = getParentClassMappings().iterator(); itPcm.hasNext();) { 499 ParentClassMapping parentClassMapping = (ParentClassMapping) itPcm.next(); 500 if (inheritsStructures(parentClassMapping)) { 504 Class superClass = (Class ) parentClassMapping.getLinkedMO(); 505 if (debug) { 506 logger.log(BasicLevel.DEBUG, "Add dependency: " + 507 clazz.getName() + " -> " + superClass.getName()); 508 } 509 addDependency(superClass.getFQName()); 510 } 511 512 } 513 514 for (Iterator itClass = clazz.getSubClasses().iterator(); itClass.hasNext();) { 516 Class subClass = (Class ) itClass.next(); 517 ClassMapping subClassMapping = subClass.getClassMapping(getProjectName(), getMapperName()); 518 if (subClassMapping == null) { 519 throw new InternalError ("Cannot find class mapping of " 520 + subClass.getName() + "<" + getProjectName() + "," 521 + getMapperName() + ">" + 522 " when adding implicit dependencies to " + clazz.getName()); 523 } 524 ParentClassMapping parentClassMapping = subClassMapping.getParentClassMapping(clazz.getFQName()); 525 if (debug) { 532 logger.log(BasicLevel.DEBUG, "Add dependency: " + 533 clazz.getName() + " -> " + subClass.getName()); 534 } 535 addDependency(subClass.getFQName()); 536 } 537 } 538 539 544 public boolean hasUnmappedPrimitiveElements(Collection pes) { 545 Class clazz = (Class ) linkedMO; 546 for (Iterator itPE = pes.iterator(); itPE.hasNext();) { 548 PrimitiveElement pe = (PrimitiveElement) itPE.next(); 549 PrimitiveElementMapping pem = getPrimitiveElementMapping(pe.getName(), true); 550 if (pem == null) { 551 return true; 552 } 553 } 554 return false; 555 } 556 557 563 public Set getParentClassMapping(Set inheritanceRules) { 564 HashSet res = new HashSet (); 565 for (Iterator it = parentClassMappings.iterator(); it.hasNext();) { 566 ParentClassMapping pcm = (ParentClassMapping) it.next(); 567 if (inheritanceRules.contains(pcm.getRuleName())) { 568 res.add(pcm); 569 } 570 } 571 return res; 572 } 573 574 580 public Set getAddedOrExtendedParentClassMapping() { 581 HashSet res = new HashSet (); 582 for (Iterator it = parentClassMappings.iterator(); it.hasNext();) { 583 ParentClassMapping pcm = (ParentClassMapping) it.next(); 584 if (inheritsStructures(pcm)) { 585 res.add(pcm); 586 } 587 } 588 return res; 589 } 590 591 public boolean inheritsStructures(ParentClassMapping pcm) { 592 return (RdbClassMapping.MAP_NEW_FIELDS_TO_EXTENDED_STRUCTURES. 593 equalsIgnoreCase(pcm.getRuleName()) || 594 RdbClassMapping.MAP_NEW_FIELDS_TO_ADDED_STRUCTURES. 595 equalsIgnoreCase(pcm.getRuleName())); 596 } 597 598 public boolean inheritsStructures() { 599 for(Iterator it= getParentClassMappings().iterator(); it.hasNext();) { 600 ParentClassMapping pcm = (ParentClassMapping) it.next(); 601 if (inheritsStructures(pcm)) { 602 return true; 603 } 604 } 605 return false; 606 } 607 608 public PrimitiveElementMapping getPrimitiveElementMapping(String fieldName, 609 boolean searchInSuper) { 610 PrimitiveElementMapping pem = getPrimitiveElementMapping(fieldName); 611 if ((pem == null) && searchInSuper) { 612 Mapping mapping = (Mapping) getParent(); 613 String mapperName = mapping.getMapperName(); 614 String projectName = ((ClassProject) mapping.getParent()).getProjectName(); 615 for (Iterator itPcm = parentClassMappings.iterator(); itPcm.hasNext();) { 616 ParentClassMapping pcm = (ParentClassMapping) itPcm.next(); 617 if (RdbClassMapping.REMAP_FIELDS_TO_NEW_STRUCTURES.equals(pcm.getRuleName())) { 619 continue; 620 } 621 Class superClass = (Class ) pcm.getLinkedMO(); 622 BasicClassMapping superClassMapping = 623 (BasicClassMapping) superClass.getClassMapping(projectName, mapperName); 624 pem = superClassMapping.getPrimitiveElementMapping(fieldName, searchInSuper); 625 if (pem != null) break; 626 } 627 } 628 return pem; 629 } 630 631 } 632 | Popular Tags |