1 23 24 package org.objectweb.jorm.mapper.rdb.mi2xml; 25 26 import org.objectweb.jorm.api.PException; 27 import org.objectweb.jorm.mapper.rdb.metainfo.RdbClassMultiMapping; 28 import org.objectweb.jorm.mapper.rdb.metainfo.RdbExternalTable; 29 import org.objectweb.jorm.mapper.rdb.metainfo.RdbFilter; 30 import org.objectweb.jorm.mapper.rdb.metainfo.RdbGenClassMapping; 31 import org.objectweb.jorm.mapper.rdb.metainfo.RdbJoin; 32 import org.objectweb.jorm.mapper.rdb.metainfo.RdbPrimitiveElementMapping; 33 import org.objectweb.jorm.mapper.rdb.metainfo.RdbTable; 34 import org.objectweb.jorm.metainfo.api.Class; 35 import org.objectweb.jorm.metainfo.api.ClassMapping; 36 import org.objectweb.jorm.metainfo.api.ClassRef; 37 import org.objectweb.jorm.metainfo.api.GenClassMapping; 38 import org.objectweb.jorm.metainfo.api.GenClassRef; 39 import org.objectweb.jorm.metainfo.api.IdentifierMapping; 40 import org.objectweb.jorm.metainfo.api.MetaObject; 41 import org.objectweb.jorm.metainfo.api.NameDef; 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.mi2xml.lib.BasicMappingDomtreeBuilder; 47 import org.objectweb.util.monolog.api.BasicLevel; 48 import org.w3c.dom.Document ; 49 import org.w3c.dom.Element ; 50 51 import java.util.Collection ; 52 import java.util.Iterator ; 53 import java.util.Map ; 54 55 61 public class RdbDomtreeBuilder extends BasicMappingDomtreeBuilder { 62 public final static String RDB_CLASS_MAPPING = "rdb-class-mapping"; 64 public final static String COLOCATED_CLASS = "colocated-class"; 65 public final static String RDB_TABLE_SPEC = "rdb-table-spec"; 66 public final static String ID_MAPPING = "id-mapping"; 67 public final static String RDB_CLASS_REF_MAPPING = "rdb-class-ref-mapping"; 68 public final static String RDB_GEN_CLASS_REF_MAPPING = "rdb-gen-class-ref-mapping"; 69 public final static String RDB_REF_MAPPING = "rdb-ref-mapping"; 70 public final static String RDB_EXTERNAL_TABLE_SPEC = "rdb-external-table-spec"; 71 public final static String RDB_JOIN = "rdb-join"; 72 public final static String RDB_FILTER_SPEC = "rdb-filter-spec"; 73 public final static String RDB_FILTER = "rdb-filter"; 74 public final static String RDB_COLUMN_SPEC = "rdb-column-spec"; 75 public final static String RDB_COLUMN_MATCH = "rdb-column-match"; 76 public final static String RDB_GEN_CLASS_MAPPING = "rdb-gen-class-mapping"; 77 public final static String RDB_EXTENSION_REF_MAPPING = "rdb-extension-ref-mapping"; 78 public final static String PARENT_CLASS_MAPPING = "parent-class-mapping"; 79 80 public final static String LINKEND_SEP = "/"; 81 82 86 public RdbDomtreeBuilder() { 87 } 89 90 96 public void processClassMapping(Document document, Element mappingElement, 97 ClassMapping classMapping) throws PException { 98 Element cmElement = document.createElement(RDB_CLASS_MAPPING); 99 mappingElement.appendChild(cmElement); 101 if (!(classMapping.getDependencies().isEmpty())) { 103 Iterator classNameIterator = classMapping.getDependencies().iterator(); 104 while (classNameIterator.hasNext()) { 105 String jormClassName = (String ) classNameIterator.next(); 106 Element ccElement = document.createElement(COLOCATED_CLASS); 107 ccElement.setAttribute("class-name", jormClassName); 108 if (logger.isLoggable(BasicLevel.DEBUG)) { 109 logger.log(BasicLevel.DEBUG, 110 "begin =<" + ccElement.getTagName() + ">"); 111 logger.log(BasicLevel.DEBUG, 112 " colocated class =<" + jormClassName + ">"); 113 } 114 cmElement.appendChild(ccElement); 115 if (logger.isLoggable(BasicLevel.DEBUG)) { 116 logger.log(BasicLevel.DEBUG, 117 "end =<" + ccElement.getTagName() + ">"); 118 } 119 } 120 } 121 RdbTable table = ((RdbClassMultiMapping) classMapping).getRdbTable(); 123 if (table != null) { 124 String colocated = (table.isColocated() ? "TRUE" : "FALSE"); 125 String colocatedMaster = (table.isColocatedMaster() ? "TRUE" : "FALSE"); 126 Element tableElement = document.createElement(RDB_TABLE_SPEC); 127 tableElement.setAttribute("table-name", table.getName()); 128 tableElement.setAttribute("colocated", colocated); 129 tableElement.setAttribute("colocated-master", colocatedMaster); 130 cmElement.appendChild(tableElement); 131 processRdbTableSpec(document, tableElement, table); 132 } 133 if (!((RdbClassMultiMapping) classMapping).getRdbExternalTables().isEmpty()) { 135 Iterator etIterator = ((RdbClassMultiMapping) classMapping).getRdbExternalTables().iterator(); 136 while (etIterator.hasNext()) { 137 RdbExternalTable externalTable = (RdbExternalTable) etIterator.next(); 138 String etcolocated = (externalTable.isColocated() ? "TRUE" : "FALSE"); 139 String etcolocatedMaster = (externalTable.isColocatedMaster() ? "TRUE" : "FALSE"); 140 Element etElement = document.createElement(RDB_EXTERNAL_TABLE_SPEC); 141 if (logger.isLoggable(BasicLevel.DEBUG)) { 142 logger.log(BasicLevel.DEBUG, 143 "begin =<" + etElement.getTagName() + ">"); 144 } 145 etElement.setAttribute("table-name", externalTable.getName()); 146 etElement.setAttribute("colocated", etcolocated); 147 etElement.setAttribute("colocated-master", etcolocatedMaster); 148 cmElement.appendChild(etElement); 150 processRdbExternalTableSpec(document, etElement, externalTable); 151 if (logger.isLoggable(BasicLevel.DEBUG)) { 152 logger.log(BasicLevel.DEBUG, 153 "end =<" + etElement.getTagName() + ">"); 154 } 155 } 156 } 157 RdbFilter filter = ((RdbClassMultiMapping) classMapping).getRdbFilter(); 159 if (filter != null) { 160 Element filterElement = document.createElement(RDB_FILTER_SPEC); 161 if (logger.isLoggable(BasicLevel.DEBUG)) { 162 logger.log(BasicLevel.DEBUG, 163 "begin =<" + filterElement.getTagName() + ">"); 164 } 165 cmElement.appendChild(filterElement); 166 Element rdbFilterElement = document.createElement(RDB_FILTER); 167 String filterStr = filter.getStringExpression(); 168 rdbFilterElement.setAttribute("value", filterStr); 169 filterElement.appendChild(rdbFilterElement); 170 if (logger.isLoggable(BasicLevel.DEBUG)) { 171 logger.log(BasicLevel.DEBUG, 172 "rdf-filter <" + filterStr + ">"); 173 } 174 175 if (logger.isLoggable(BasicLevel.DEBUG)) { 176 logger.log(BasicLevel.DEBUG, 177 "end =<" + filterElement.getTagName() + ">"); 178 } 179 } 180 181 if (logger.isLoggable(BasicLevel.DEBUG)) { 183 logger.log(BasicLevel.DEBUG, 184 "rdb-class-mapping.linkedMO = " + ((Class ) classMapping.getLinkedMO()).getFQName()); 185 } 186 Collection pcm = classMapping.getParentClassMappings(); 187 if (logger.isLoggable(BasicLevel.DEBUG)) { 188 logger.log(BasicLevel.DEBUG, 189 "pcm = " + pcm); 190 } 191 if (!pcm.isEmpty()) { 192 if (logger.isLoggable(BasicLevel.DEBUG)) { 193 logger.log(BasicLevel.DEBUG, 194 "begin =<parent-class-mapping>"); 195 } 196 Iterator pcmIterator = pcm.iterator(); 197 while (pcmIterator.hasNext()) { 198 ParentClassMapping parentClassMapping = (ParentClassMapping) pcmIterator.next(); 199 Element pcmElem = document.createElement(PARENT_CLASS_MAPPING); 200 pcmElem.setAttribute("link-end", parentClassMapping.getFQName()); 201 pcmElem.setAttribute("rule-name", parentClassMapping.getRuleName()); 202 cmElement.appendChild(pcmElem); 203 } 204 } 205 206 207 IdentifierMapping identifierMapping = classMapping.getIdentifierMapping(); 209 if ( identifierMapping != null) { 210 NameDef idNameDef = (NameDef) identifierMapping.getLinkedMO(); 212 Class parent = (Class ) idNameDef.getParent(); 214 String linkend = null; 215 if (parent == currentClass) { 217 linkend = idNameDef.getName(); 218 } else { 219 linkend = LINKEND_SEP + parent.getFQName() + LINKEND_SEP + idNameDef.getName(); 220 } 221 Element idNameDefElement = document.createElement(ID_MAPPING); 222 idNameDefElement.setAttribute("link-end", linkend); 224 cmElement.appendChild(idNameDefElement); 225 } 226 227 if (!(classMapping.getReferenceMappings().isEmpty())) { 229 Iterator rmIterator = classMapping.getReferenceMappings().iterator(); 230 while (rmIterator.hasNext()) { 231 ReferenceMapping referenceMapping = (ReferenceMapping) rmIterator.next(); 232 NameDef refNameDef = (NameDef) referenceMapping.getLinkedMO(); 233 String ruleName = referenceMapping.getRuleName(); 234 Reference reference = (Reference) refNameDef.getParent(); 236 String linkend = null; 237 Class clazz = (Class ) reference.getParent(); 238 if (clazz == currentClass) { 239 linkend = (refNameDef.getName().equals("") 240 ? reference.getName() 241 : reference.getName() + LINKEND_SEP + refNameDef.getName()); 242 } else { 243 linkend = LINKEND_SEP + clazz.getFQName() + LINKEND_SEP + reference.getName() + LINKEND_SEP + refNameDef.getName(); 244 } 245 Element refElement = null; 246 if (refNameDef.getParent() instanceof ClassRef) { 247 refElement = document.createElement(RDB_CLASS_REF_MAPPING); 248 } else if (refNameDef.getParent() instanceof GenClassRef) { 249 refElement = document.createElement(RDB_GEN_CLASS_REF_MAPPING); 250 } 251 refElement.setAttribute("link-end", linkend); 253 refElement.setAttribute("rule-name", ruleName); 254 cmElement.appendChild(refElement); 255 } 256 } 257 } 258 259 265 private void processRdbTableSpec(Document document, Element tableElement, RdbTable table) { 266 if (!(table.getPrimitiveElementMappings().isEmpty())) { 268 Iterator pemIterator = table.getPrimitiveElementMappings().iterator(); 269 while (pemIterator.hasNext()) { 270 RdbPrimitiveElementMapping pem = (RdbPrimitiveElementMapping) pemIterator.next(); 271 String notNull = (pem.isNotNull() ? "TRUE" : "FALSE"); 272 PrimitiveElement pe = (PrimitiveElement) pem.getLinkedMO(); 273 Element csElement = document.createElement(RDB_COLUMN_SPEC); 274 MetaObject parent = pe.getParent(); 279 String linkend = pe.getName(); 280 if (parent instanceof Class ) { 281 boolean isInherited = (parent != currentClass); 282 if (isInherited) { 283 linkend = LINKEND_SEP + ((Class ) parent).getFQName() + LINKEND_SEP + pe.getName(); 284 } 285 } 286 logger.log(BasicLevel.DEBUG, 287 " rdb-column-spec link-end =<" + linkend + ">"); 288 csElement.setAttribute("link-end", linkend); 289 csElement.setAttribute("column-name", pem.getName()); 290 csElement.setAttribute("sql-type", pem.getType()); 291 csElement.setAttribute("not-null", notNull); 292 csElement.setAttribute("join-name", ""); 293 tableElement.appendChild(csElement); 294 } 295 } 296 } 297 298 304 private void processRdbExternalTableSpec(Document document, Element etElement, 305 RdbExternalTable externalTable) throws PException { 306 Iterator joinIterator = externalTable.getRdbJoins().iterator(); 308 while (joinIterator.hasNext()) { 309 RdbJoin join = (RdbJoin) joinIterator.next(); 310 Element joinElement = document.createElement(RDB_JOIN); 311 joinElement.setAttribute("name", join.getName()); 312 etElement.appendChild(joinElement); 313 processRdbJoin(document, joinElement, join); 314 } 315 if (!(externalTable.getPrimitiveElementMappings().isEmpty())) { 317 Iterator peIterator = externalTable.getPrimitiveElementMappings().iterator(); 318 Element csElement = null; 319 while (peIterator.hasNext()) { 320 RdbPrimitiveElementMapping pem = (RdbPrimitiveElementMapping) peIterator.next(); 321 String notNull = (pem.isNotNull() ? "TRUE" : "FALSE"); 322 PrimitiveElement pe = null; 323 RdbJoin join = null; 324 Map join2pe = pem.getPrimitiveElementByRdbJoin(); 325 if (join2pe != null) { 326 joinIterator = join2pe.keySet().iterator(); 328 while (joinIterator.hasNext()) { 329 join = (RdbJoin) joinIterator.next(); 330 pe = (PrimitiveElement) join2pe.get(join); 331 csElement = document.createElement(RDB_COLUMN_SPEC); 332 csElement.setAttribute("link-end", pe.getName()); 333 csElement.setAttribute("column-name", pem.getName()); 334 csElement.setAttribute("sql-type", pem.getType()); 335 csElement.setAttribute("not-null", notNull); 336 csElement.setAttribute("join-name", join.getName()); 337 etElement.appendChild(csElement); 338 } 339 } else { 340 pe = (PrimitiveElement) pem.getLinkedMO(); 341 join = pem.getJoinByPrimitiveElement(pe); 342 if (join == null) { 343 throw new PException("No join defined in the external table for the field: " 344 + pe.getName()); 345 } 346 csElement = document.createElement(RDB_COLUMN_SPEC); 347 csElement.setAttribute("link-end", pe.getName()); 348 csElement.setAttribute("column-name", pem.getName()); 349 csElement.setAttribute("sql-type", pem.getType()); 350 csElement.setAttribute("not-null", notNull); 351 csElement.setAttribute("join-name", join.getName()); 352 etElement.appendChild(csElement); 353 } 354 if (logger.isLoggable(BasicLevel.DEBUG)) { 355 logger.log(BasicLevel.DEBUG, 356 "begin =<" + csElement.getTagName() + ">"); 357 } 358 if (logger.isLoggable(BasicLevel.DEBUG)) { 360 logger.log(BasicLevel.DEBUG, 361 "end =<" + csElement.getTagName() + ">"); 362 } 363 } 364 } 365 } 366 367 373 private void processRdbJoin(Document document, Element joinElement, RdbJoin join) { 374 Iterator column1Iterator = join.getPTJoinColumnNames().iterator(); 376 Iterator column2Iterator = join.getETJoinColumnNames().iterator(); 377 while (column1Iterator.hasNext()) { 378 String column1 = (String ) column1Iterator.next(); 379 String column2 = (String ) column2Iterator.next(); 380 Element cmElement = document.createElement(RDB_COLUMN_MATCH); 381 cmElement.setAttribute("column1", column1); 382 cmElement.setAttribute("column2", column2); 383 ; 384 joinElement.appendChild(cmElement); 385 } 386 } 387 388 394 public void processGenClassMapping(Document document, Element mappingElement, 395 GenClassMapping genClassMapping) throws PException { 396 GenClassRef genClassRef = (GenClassRef) genClassMapping.getLinkedMO(); 397 Element gcmElement = document.createElement(RDB_GEN_CLASS_MAPPING); 398 gcmElement.setAttribute("link-end", (String ) metaobject2idvalue.get(genClassRef)); 399 mappingElement.appendChild(gcmElement); 400 RdbTable table = ((RdbGenClassMapping) genClassMapping).getRdbTable(); 402 String colocated = (table.isColocated() ? "TRUE" : "FALSE"); 403 String colocatedMaster = (table.isColocatedMaster() ? "TRUE" : "FALSE"); 404 Element tableElement = document.createElement(RDB_TABLE_SPEC); 405 tableElement.setAttribute("table-name", table.getName()); 406 tableElement.setAttribute("colocated", colocated); 407 tableElement.setAttribute("colocated-master", colocatedMaster); 408 gcmElement.appendChild(tableElement); 409 processRdbTableSpec(document, tableElement, table); 410 if (((RdbGenClassMapping) genClassMapping).getRdbExternalTables() != null) { 412 Iterator etIterator = ((RdbClassMultiMapping) genClassMapping).getRdbExternalTables().iterator(); 413 while (etIterator.hasNext()) { 414 RdbExternalTable externalTable = (RdbExternalTable) etIterator.next(); 415 String etcolocated = (externalTable.isColocated() ? "TRUE" : "FALSE"); 416 String etcolocatedMaster = (externalTable.isColocatedMaster() ? "TRUE" : "FALSE"); 417 Element etElement = document.createElement(RDB_EXTERNAL_TABLE_SPEC); 418 if (logger.isLoggable(BasicLevel.DEBUG)) { 419 logger.log(BasicLevel.DEBUG, 420 "begin =<" + etElement.getTagName() + ">"); 421 } 422 etElement.setAttribute("table-name", externalTable.getName()); 423 etElement.setAttribute("colocated", etcolocated); 424 etElement.setAttribute("colocated-master", etcolocatedMaster); 425 gcmElement.appendChild(etElement); 427 processRdbExternalTableSpec(document, etElement, externalTable); 428 if (logger.isLoggable(BasicLevel.DEBUG)) { 429 logger.log(BasicLevel.DEBUG, 430 "end =<" + etElement.getTagName() + ">"); 431 } 432 } 433 } 434 IdentifierMapping identifierMapping = genClassMapping.getIdentifierMapping(); 436 NameDef idNameDef = (NameDef) identifierMapping.getLinkedMO(); 437 Element idNameDefElement = document.createElement(ID_MAPPING); 438 idNameDefElement.setAttribute("link-end", idNameDef.getName()); 440 gcmElement.appendChild(idNameDefElement); 441 if (genClassMapping.getReferenceMapping() != null) { 443 ReferenceMapping referenceMapping = genClassMapping.getReferenceMapping(); 444 NameDef refNameDef = (NameDef) referenceMapping.getLinkedMO(); 445 Element refElement = document.createElement(RDB_REF_MAPPING); 446 refElement.setAttribute("link-end", refNameDef.getName()); 448 refElement.setAttribute("rule-name", referenceMapping.getRuleName()); 449 gcmElement.appendChild(refElement); 450 } 451 } 452 } 453 454 | Popular Tags |