1 23 24 package org.objectweb.jorm.mapper.rdb.generator; 25 26 import org.objectweb.jorm.api.PException; 27 import org.objectweb.jorm.generator.lib.CommonHelper; 28 import org.objectweb.jorm.mapper.rdb.adapter.RdbAdapterFactory; 29 import org.objectweb.jorm.mapper.rdb.adapter.api.RdbAdapter; 30 import org.objectweb.jorm.mapper.rdb.metainfo.RdbClassMapping; 31 import org.objectweb.jorm.mapper.rdb.metainfo.RdbClassMultiMapping; 32 import org.objectweb.jorm.mapper.rdb.metainfo.RdbExternalTable; 33 import org.objectweb.jorm.mapper.rdb.metainfo.RdbJoin; 34 import org.objectweb.jorm.mapper.rdb.metainfo.RdbMapping; 35 import org.objectweb.jorm.mapper.rdb.metainfo.RdbPrimitiveElementMapping; 36 import org.objectweb.jorm.mapper.rdb.metainfo.RdbTable; 37 import org.objectweb.jorm.metainfo.api.Class; 38 import org.objectweb.jorm.metainfo.api.ClassMapping; 39 import org.objectweb.jorm.metainfo.api.Mapping; 40 import org.objectweb.jorm.metainfo.api.NameDef; 41 import org.objectweb.jorm.metainfo.api.Package; 42 import org.objectweb.jorm.metainfo.api.ParentClassMapping; 43 import org.objectweb.jorm.metainfo.api.PrimitiveElement; 44 import org.objectweb.jorm.metainfo.api.Reference; 45 import org.objectweb.jorm.metainfo.api.ReferenceMapping; 46 import org.objectweb.jorm.metainfo.api.TypedElement; 47 import org.objectweb.jorm.type.api.PType; 48 import org.objectweb.util.monolog.api.BasicLevel; 49 import org.objectweb.util.monolog.api.Logger; 50 51 import java.util.ArrayList ; 52 import java.util.Collections ; 53 import java.util.Comparator ; 54 import java.util.HashMap ; 55 import java.util.Iterator ; 56 import java.util.List ; 57 import java.util.Map ; 58 import java.util.TreeMap ; 59 60 65 public class RdbCommonHelper extends CommonHelper { 66 67 private Map rdbAdapters = new HashMap (); 68 69 public RdbCommonHelper() { 70 } 71 public RdbCommonHelper(Logger logger) { 72 setLogger(logger); 73 } 74 75 81 protected RdbAdapter getRdbAdapter(String mappername) throws PException { 82 RdbAdapter res = (RdbAdapter) rdbAdapters.get(mappername); 83 if (res != null) { 84 return res; 85 } 86 String dbn; 87 if (mappername.indexOf(".") == -1) 88 dbn = RdbAdapterFactory.DATABASE_NAME_JDBC; 89 else 90 dbn = mappername.substring(mappername.indexOf(".") + 1); 91 try { 92 res = RdbAdapterFactory.getTypeConverter(dbn); 93 } catch (org.objectweb.jorm.mapper.rdb.adapter.api.RdbAdapterException e) { 94 throw new PException(e, ""); 95 } 96 rdbAdapters.put(mappername, res); 97 return res; 98 } 99 100 public String getSqlType(RdbPrimitiveElementMapping pem, 101 String adapter, 102 boolean usedInPk) throws PException { 103 String userSQLType = pem.getType(); 104 if (userSQLType == null || userSQLType.length() == 0) { 105 StringBuffer sb = new StringBuffer (adapter); 106 sb.append(".getSqlType("); 107 PrimitiveElement pe = (PrimitiveElement) pem.getLinkedMO(); 108 sb.append(pe.getType().getProgName()); 109 sb.append(".getTypeCode(), "); 110 sb.append(usedInPk); 111 sb.append(", "); 112 if (pe.getSize() == PType.NOSIZE) { 113 sb.append("RdbAdapter.NOSIZE"); 114 } else { 115 sb.append(pe.getSize()); 116 } 117 sb.append(", "); 118 if (pe.getScale() == PType.NOSIZE) { 119 sb.append("RdbAdapter.NOSIZE"); 120 } else { 121 sb.append(pe.getScale()); 122 } 123 sb.append(")"); 124 return sb.toString(); 125 } else { 126 return "\"" + userSQLType + "\""; 127 } 128 } 129 142 RdbGenInfos getRdbGenInfos(Class clazz, String projectName, String mapperName) 143 throws PException { 144 Mapping mapping = clazz.getClassProject(projectName).getMapping(mapperName); 145 return createRdbGenInfosContext((RdbMapping) mapping, 146 (Class ) mapping.getClassMapping().getJormClass()); 147 } 148 149 RdbGenInfos getRdbGenInfos(Mapping mapping) 150 throws PException { 151 return createRdbGenInfosContext((RdbMapping) mapping, 152 (Class ) mapping.getClassMapping().getLinkedMO()); 153 } 154 155 RdbGenInfos getRdbGenInfos(Mapping mapping, Map ctx) 156 throws PException { 157 return createRdbGenInfosContext((RdbMapping) mapping, 158 (Class ) mapping.getClassMapping().getLinkedMO(), ctx); 159 } 160 161 RdbGenInfos createRdbGenInfosContext(RdbMapping mapping, Class clazz) throws PException { 162 return createRdbGenInfosContext(mapping, clazz, new HashMap ()); 163 } 164 RdbGenInfos createRdbGenInfosContext(RdbMapping mapping, Class clazz, Map ctx) 165 throws PException { 166 debug = logger != null && logger.isLoggable(BasicLevel.DEBUG); 168 169 RdbGenInfos rgi = new RdbGenInfos(); 171 172 rgi.mapping = mapping; 174 rgi.logger = logger; 175 rgi.debug = debug; 176 177 178 179 RdbClassMultiMapping classMapping = (RdbClassMultiMapping) 181 mapping.getClassMapping(); 182 if (debug) { 183 logger.log(BasicLevel.DEBUG, "mapping.getClassMapping() " + classMapping); 184 logger.log(BasicLevel.DEBUG, "Creating the generation MI for the class = " 185 + ((Class ) classMapping.getLinkedMO()).getFQName()); 186 } 187 188 189 RdbTable mainRdbTable = classMapping.getMainRdbTable(); 191 if (mainRdbTable == null) { 192 logger.log(BasicLevel.DEBUG, "No main table defined for this " + 193 "class (implicit inheritance ?). Abort Gen infos creation"); 194 return rgi; 195 } 196 if (debug) { 197 logger.log(BasicLevel.DEBUG, "Main table: " 198 + mainRdbTable.getName()); 199 } 200 201 RdbGenTable mainGenTable = new RdbGenTable(mainRdbTable, clazz, rgi); 202 203 rgi.mainTable = mainGenTable; 204 205 206 getInheritedData(classMapping, 208 mainGenTable.inheritedColumns, 209 rgi.readableColumns, 210 rgi.genRefs, 211 rgi.tables, 212 clazz, 213 ctx); 214 if (debug) { 215 logger.log(BasicLevel.DEBUG, "Inherited columns:"); 216 printColumns(rgi.readableColumns); 217 } 218 219 221 rgi.tables.put(mainGenTable.tableName, mainGenTable); 222 addColumnUnique(mainGenTable.columns, rgi.readableColumns, false); 223 224 225 for (Iterator it = classMapping.getExternalTables().iterator(); 227 it.hasNext();) { 228 RdbExternalTable extTable = (RdbExternalTable) it.next(); 229 230 RdbGenTable genTable = new RdbGenTable(extTable, mainGenTable, 232 clazz, rgi); 233 234 rgi.tables.put(genTable.tableName, genTable); 236 237 238 if (debug) { 239 logger.log(BasicLevel.DEBUG, 240 "ext table -- name = " + genTable.getTableName()); 241 } 242 243 for (int i = 0; i < genTable.columns.size(); i++) { 246 RdbGenColumn genColumn = (RdbGenColumn) genTable.columns.get(i); 247 if (genColumn.joinCol == null) { 248 rgi.readableColumns.add(genColumn); 249 } 250 } 251 } 252 253 NameDef nd = classMapping.getIdentifierMapping().getNameDef(); 256 Integer ndIdInteger = (Integer ) ctx.get("ndid"); 257 258 int ndId = (ndIdInteger == null ? 0 : ndIdInteger.intValue()); 259 260 rgi.genId = defineGenRef(nd, null, ndId++, classMapping, rgi); 261 262 if (debug) { 263 for (Iterator iter = clazz.getAllFields().iterator(); 264 iter.hasNext();) { 265 TypedElement typedElem = (TypedElement) iter.next(); 266 if (typedElem instanceof Reference) { 267 logger.log(BasicLevel.DEBUG, "current class: " + clazz.getName() + "current reference field " + typedElem.getName()); 268 logger.log(BasicLevel.DEBUG, "classMapping.getReferenceMapping(typedElem.getName()) " + classMapping.getReferenceMapping(typedElem.getName())); 269 NameDef namedef = (NameDef) classMapping.getReferenceMapping(typedElem.getName()).getLinkedMO(); 270 logger.log(BasicLevel.DEBUG, "current reference field " + typedElem.getName() + " namedef " + namedef); 271 } 272 } 273 } 274 275 for (Iterator it = classMapping.getReferenceMappings().iterator(); it.hasNext();) { 277 nd = (NameDef) ((ReferenceMapping) it.next()).getLinkedMO(); 278 String fn = ((Reference) nd.getParent()).getName(); 279 rgi.genRefs.put(fn, defineGenRef(nd, fn, ndId++, classMapping, rgi)); 280 } 281 ctx.put("ndid", new Integer (ndId)); 282 283 for (Iterator it = rgi.tables.values().iterator(); it.hasNext();) { 285 ((RdbGenTable) it.next()).fixMustGenerateUpdate(); 286 } 287 288 Collections.sort(rgi.readableColumns, new RGCComparator()); 289 if (debug) { 290 logger.log(BasicLevel.DEBUG, "ReadableColumn: "); 291 for(int i=0; i<rgi.readableColumns.size(); i++) { 292 RdbGenColumn col = (RdbGenColumn) rgi.readableColumns.get(i); 293 if (col.joins == null) { 294 logger.log(BasicLevel.DEBUG, "\tCOL=" + col.columnName 295 + " / TABLE=" + col.table.tableName 296 + " / COL_IDX=" + rgi.getColumnPosition(col) 297 + " rgc=" + col); 298 } else { 299 logger.log(BasicLevel.DEBUG, "\tCOL=" + col.columnName 300 + " / TABLE=" + col.table.tableName 301 + " rgc=" + col); 302 for(int j=0; j<col.joins.size(); j++) { 303 RdbGenJoin join = (RdbGenJoin) col.joins.get(j); 304 logger.log(BasicLevel.DEBUG, "\t\tJOIN_IDX=" + join.getJoinIdx() 305 + " / COL_IDX=" + rgi.getColumnPosition(col, join)); 306 } 307 } 308 } 309 logger.log(BasicLevel.DEBUG, "End of createClassGenerationContext for class " + clazz.getFQName()); 310 } 311 312 rgi.filterExpr = rgi.getFilterExpression(); 314 return rgi; 315 } 316 318 private class RGCComparator implements Comparator { 319 public int compare(Object o1, Object o2) { 320 RdbGenColumn rgc1 = (RdbGenColumn) o1; 321 RdbGenColumn rgc2 = (RdbGenColumn) o2; 322 return rgc1.columnName.compareTo(rgc2.columnName); 323 } 324 } 325 326 333 private void addColumnUnique(List source, List dest, boolean addInHead) { 334 int pos = (addInHead) ? 0 : dest.size(); 335 for(int i=0; i<source.size(); i++) { 336 boolean found = false; 337 RdbGenColumn rgcToAdd = (RdbGenColumn) source.get(i); 338 for(int j=0; j<dest.size() && !found; j++) { 339 RdbGenColumn col = (RdbGenColumn) dest.get(j); 340 found = col.table.tableName.equals(rgcToAdd.table.tableName) 341 && col.columnName.equals(rgcToAdd.columnName); 342 } 343 if (!found) { 344 dest.add(pos++, rgcToAdd); 345 } 346 } 347 } 348 349 360 private RdbGenRef defineGenRef(NameDef nd, 361 String fn, 362 int cnid, 363 RdbClassMapping cm, 364 RdbGenInfos rgi) 365 throws PException { 366 367 RdbGenRef rgr = new RdbGenRef(); 368 rgr.nd = nd; 369 rgr.fieldName = fn; 370 if (nd.isFieldName()) { 371 RdbPrimitiveElementMapping pem = (RdbPrimitiveElementMapping) 373 cm.getPrimitiveElementMapping(nd.getFieldName(), true); 374 if (pem == null) { 375 throw new PException( 376 "Unable to find the mapping of the field " 377 + nd.getFieldName()); 378 } 379 RdbTable table = (RdbTable) pem.getParent(); 380 if (debug) { 381 logger.log(BasicLevel.DEBUG, "nd(" + fn 382 + ") FieldName, field=" + nd.getFieldName() 383 + " / column name=" + pem.getName() 384 + " in the table: " + table.getName()); 385 } 386 RdbGenTable rgt = (RdbGenTable) rgi.tables.get(table.getName()); 387 if (rgt == null) { 388 throw new NullPointerException ("No table " + table.getName() + " found among: " + rgi.tables); 389 } 390 rgr.refColumn = rgt.getColumn(pem.getName()); 391 if (rgt.joins != null) { 392 getJoin(rgt, pem, nd.getFieldName(), rgr); 393 } 394 } else { if (debug) { 396 logger.log(BasicLevel.DEBUG, "nd(" + fn + ") CommpositeName, cnId=" + cnid); 397 } 398 rgr.cnId = cnid; 399 rgr.cnName = nd.getNameRef().getCompositeName().getName(); 400 rgr.cnPackage = ((Package ) nd.getNameRef().getCompositeName().getParent()).getName(); 401 rgr.cnFieldColumns = new TreeMap (); 402 Iterator itp = nd.getNameRef().getProjection().entrySet().iterator(); 403 RdbPrimitiveElementMapping pem = null; 404 while (itp.hasNext()) { 405 Map.Entry me = (Map.Entry ) itp.next(); 406 String cnfield = (String ) me.getKey(); 407 String clafn = (String ) me.getValue(); 408 pem = (RdbPrimitiveElementMapping) 410 cm.getPrimitiveElementMapping(clafn, true); 411 if (pem == null) { 412 throw new PException( 413 "Unable to find the mapping of the field " 414 + nd.getFieldName()); 415 } 416 RdbTable table = (RdbTable) pem.getParent(); 417 if (debug) { 418 logger.log(BasicLevel.DEBUG, "nd(" + fn 419 + ") composite field name=" + cnfield 420 + " / class field name=" + clafn 421 + " / column name=" + pem.getName() 422 + " in the table: " + table.getName()); 423 } 424 RdbGenTable rgt = (RdbGenTable) rgi.tables.get(table.getName()); 425 if (rgt == null) { 426 throw new PException("Cannot find gen table"); 427 } 428 RdbGenColumn c = rgt.getColumn(pem.getName()); 429 rgr.cnFieldColumns.put(cnfield, c); 430 if (rgr.join == null && rgt.joins != null) { 431 getJoin(rgt, pem, clafn, rgr); 432 } 433 } 434 } 435 return rgr; 436 } 437 438 private void getJoin(RdbGenTable rgt, 439 RdbPrimitiveElementMapping pem, 440 String fn, 441 RdbGenRef rgr) { 442 if (debug) { 443 logger.log(BasicLevel.DEBUG, "getJoin: fn: " + rgr.fieldName 444 + " / col: " + pem.getName() 445 + " / rgt: " + rgt.tableName); 446 } 447 if (rgt.joins.size() == 1) { 448 rgr.join = (RdbGenJoin) rgt.joins.get(0); 449 if (debug) { 450 logger.log(BasicLevel.DEBUG, "return: fn: " + rgr.fieldName 451 + " / col: " + pem.getName() 452 + " / join: " + rgr.join.joinIdx); 453 } 454 } 455 Iterator it = pem.getPrimitiveElementByRdbJoin().entrySet().iterator(); 456 while (it.hasNext()) { 457 Map.Entry me2 = (Map.Entry ) it.next(); 458 PrimitiveElement pe = (PrimitiveElement) me2.getValue(); 459 if (pe.getName().equals(fn)) { 460 rgr.join = (RdbGenJoin) 461 rgt.jn2join.get(((RdbJoin) me2.getKey()).getName()); 462 if (debug) { 463 logger.log(BasicLevel.DEBUG, "return fn: " + rgr.fieldName 464 + " / col: " + pem.getName() 465 + " / joinName: " + ((RdbJoin) me2.getKey()).getName() 466 + " / join: " + rgr.join.joinIdx); 467 } 468 } 469 } 470 } 471 472 482 private void getInheritedData(RdbClassMapping cm, 483 List inheritedColumns, 484 List inheritedReadableColumns, 485 Map inheritedGenRefs, 486 Map inheritedExtTables, 487 Class constantHolderClass, 488 Map ctx) 489 throws PException { 490 for (Iterator pcmit = cm.getParentClassMappings().iterator(); 492 pcmit.hasNext();) { 493 ParentClassMapping pcm = (ParentClassMapping) pcmit.next(); 494 if (cm.inheritsStructures(pcm)) { 496 Class superClass = pcm.getMOClass(); 497 if (debug) { 498 logger.log(BasicLevel.DEBUG, 499 "rule-name = " + pcm.getRuleName() + "=<" + superClass.getFQName() + ">" 500 + " abstract =<" + superClass.isAbstract() + ">"); 501 } 502 503 RdbClassMapping superClassMapping = (RdbClassMapping) 504 superClass.getClassMapping( 505 cm.getProjectName(), 506 cm.getMapperName()); 507 508 if (superClassMapping.getMainRdbTable() != null) { 509 RdbGenInfos superGenInfos = 510 getRdbGenInfos((Mapping) superClassMapping.getParent(), ctx); 511 addColumnUnique(superGenInfos.getReferenceTable().columns, 512 inheritedColumns, true); 513 addColumnUnique(superGenInfos.readableColumns, 514 inheritedReadableColumns, true); 515 inheritedGenRefs.putAll(superGenInfos.genRefs); 516 inheritedExtTables.putAll(superGenInfos.tables); 517 } 518 519 getInheritedData(superClassMapping, 521 inheritedColumns, 522 inheritedReadableColumns, 523 inheritedGenRefs, 524 inheritedExtTables, 525 constantHolderClass, 526 ctx); 527 } 528 } 529 } 530 531 private void printColumns(List columns) { 532 RdbGenColumn rgc = null; 533 for (int i = 0; i < columns.size(); i++) { 534 rgc = (RdbGenColumn) columns.get(i); 535 logger.log(BasicLevel.DEBUG, "\tcolumn : " + rgc.columnName 536 + " isHiddenField: " + rgc.isHiddenField() 537 + " isConstantField: " + (rgc.constant != null)); 538 } 539 } 540 } 541 | Popular Tags |