1 23 24 package org.objectweb.jorm.mapper.rdb.metainfo; 25 26 import org.objectweb.jorm.api.PException; 27 import org.objectweb.jorm.metainfo.api.CommonClassMapping; 28 import org.objectweb.jorm.metainfo.api.MetaObject; 29 import org.objectweb.jorm.metainfo.api.PrimitiveElement; 30 import org.objectweb.jorm.metainfo.api.PrimitiveElementMapping; 31 32 import java.util.ArrayList ; 33 import java.util.Collection ; 34 import java.util.Collections ; 35 import java.util.Iterator ; 36 import java.util.List ; 37 38 42 public class RdbExternalTable extends RdbTable { 43 44 List joins = null; 46 47 54 55 public RdbExternalTable(MetaObject parent, MetaObject linkedMO, String name) { 56 super(parent, linkedMO, name); 57 if (!(parent instanceof RdbMappingInfos)) { 58 throw new InternalError ("parent must implements the RdbMappingInfos"); 59 } 60 joins = new ArrayList (); 61 } 62 63 public RdbJoin createRdbJoin(String jn) { 64 RdbJoin j = new RdbJoin(this, jn); 65 joins.add(j); 66 Collections.sort(joins); 68 return j; 69 } 70 71 public RdbJoin removeRdbJoin(String j) { 72 for (Iterator itJoin = joins.iterator(); itJoin.hasNext();) { 73 RdbJoin rdbJoin = (RdbJoin) itJoin.next(); 74 if (rdbJoin.getName().equals(name)) { 75 itJoin.remove(); 76 return rdbJoin; 77 } 78 } 79 return null; 80 } 81 82 public RdbJoin getRdbJoin(String name) { 83 for (Iterator itJoin = joins.iterator(); itJoin.hasNext();) { 84 RdbJoin rdbJoin = (RdbJoin) itJoin.next(); 85 if (rdbJoin.getName().equals(name)) return rdbJoin; 86 } 87 return null; 88 } 89 90 public RdbTable getMainTable() { 91 return ((RdbMappingInfos) parent).getMainTable(); 92 } 93 94 public Collection getRdbJoins() { 95 return joins; 96 } 97 98 public RdbPrimitiveElementMapping createPrimitiveElementMapping( 99 PrimitiveElement pe, 100 String columnName, 101 String sqlType, 102 boolean notNull, 103 RdbJoin join) throws PException { 104 RdbPrimitiveElementMapping pem = (RdbPrimitiveElementMapping) 105 colName2pem.get(columnName); 106 if (pem != null) { 107 PrimitiveElement p = pem.lookupPrimitiveElement(join); 108 if (p != null && p != pe) { 109 throw new PException("The column '" + columnName 110 + "' of the table '" + name + "' is already bound to" 111 + " another primitive element (" 112 + (p == null ? null : "'" + p.getName() + "'") + ")" 113 + " by the join '" + join.getName() 114 + "' than the specified (" + pe.getName() + ")"); 115 } 116 pem.bindPrimitiveElement(join, pe); 117 if (pem.getType() == null && sqlType != null) { 118 pem.setType(sqlType); 119 } 120 pem.setIsNotNull(pem.isNotNull() || notNull); 121 } 122 RdbPrimitiveElementMapping pem2 = new RdbPrimitiveElementMapping( 123 columnName, sqlType, notNull, pe, this, join); 124 ((CommonClassMapping) getParent()).addPrimitiveElementMapping(pem2); 125 pem2.setLogger(getLogger()); 126 pem2.setLoggerFactory(getLoggerFactory()); 127 if (pem == null) { 128 colName2pem.put(columnName, pem2); 129 return pem2; 130 } 131 return pem; 132 } 133 134 public PrimitiveElementMapping getPrimitiveElementMappingByField(String fieldName) { 135 for (Iterator it = colName2pem.values().iterator(); it.hasNext();) { 136 RdbPrimitiveElementMapping pem = 137 (RdbPrimitiveElementMapping) it.next(); 138 Iterator itPem = pem.getPrimitiveElementByRdbJoin().values().iterator(); 139 while (itPem.hasNext()) { 140 PrimitiveElement pe = (PrimitiveElement) itPem.next(); 141 if (pe.getName().equals(fieldName)) 142 return pem; 143 } 144 } 145 return null; 146 } 147 } 148 | Popular Tags |