1 29 30 package com.caucho.ejb.cfg; 31 32 import com.caucho.amber.field.AmberField; 33 import com.caucho.amber.field.EntityManyToManyField; 34 import com.caucho.amber.field.Id; 35 import com.caucho.amber.manager.AmberPersistenceUnit; 36 import com.caucho.amber.table.Column; 37 import com.caucho.amber.table.ForeignColumn; 38 import com.caucho.amber.table.LinkColumns; 39 import com.caucho.amber.table.Table; 40 import com.caucho.amber.type.EntityType; 41 import com.caucho.bytecode.JClass; 42 import com.caucho.bytecode.JMethod; 43 import com.caucho.config.ConfigException; 44 import com.caucho.ejb.ql.QLParser; 45 import com.caucho.util.L10N; 46 47 import java.util.ArrayList ; 48 49 52 public class CmrManyToMany extends CmrRelation { 53 private static final L10N L = new L10N(CmrManyToMany.class); 54 55 private String _sqlTable; 56 57 private EjbEntityBean _targetBean; 58 private String _targetField; 59 60 private boolean _isTargetUnique; 62 63 private ArrayList <String > _orderByFields; 64 private ArrayList <Boolean > _orderByAscending; 65 66 private SqlRelation []_keySQLColumns; 67 private SqlRelation []_dstSQLColumns; 68 69 private EntityManyToManyField _amberManyToMany; 70 71 74 public CmrManyToMany(EjbEntityBean entityBean, 75 String fieldName, 76 EjbEntityBean targetBean, 77 String targetField) 78 throws ConfigException 79 { 80 super(entityBean, fieldName); 81 82 _targetBean = targetBean; 83 _targetField = targetField; 84 } 85 86 89 public EjbEntityBean getTargetBean() 90 { 91 return _targetBean; 92 } 93 94 97 public void setSQLTable(String sqlTable) 98 { 99 _sqlTable = sqlTable; 100 } 101 102 105 public String getSQLTable() 106 { 107 if (_sqlTable != null) 108 return _sqlTable; 109 else 110 return getRelationName(); 111 } 112 113 116 public void setTargetUnique(boolean isUnique) 117 { 118 _isTargetUnique = isUnique; 119 } 120 121 124 public boolean isTargetUnique() 125 { 126 return _isTargetUnique; 127 } 128 129 132 public JClass getTargetType() 133 { 134 return _targetBean.getLocal(); 135 } 136 137 140 public void setKeySQLColumns(SqlRelation []columns) 141 { 142 _keySQLColumns = columns; 143 } 144 145 148 public SqlRelation []getKeySQLColumns() 149 { 150 return _keySQLColumns; 151 } 152 153 156 public void setDstSQLColumns(SqlRelation []columns) 157 { 158 _dstSQLColumns = columns; 159 } 160 161 164 public SqlRelation []getDstSQLColumns() 165 { 166 return _dstSQLColumns; 167 } 168 169 172 public void setOrderBy(String orderBySQL) 173 throws ConfigException 174 { 175 if (orderBySQL != null) { 176 ArrayList <String > fields = new ArrayList <String >(); 177 ArrayList <Boolean > asc = new ArrayList <Boolean >(); 178 179 QLParser.parseOrderBy(_targetBean, orderBySQL, fields, asc); 180 181 _orderByFields = fields; 182 _orderByAscending = asc; 183 } 184 } 185 186 189 public boolean isCollection() 190 { 191 return true; 192 } 193 194 197 public EntityManyToManyField getAmberField() 198 { 199 return _amberManyToMany; 200 } 201 202 205 public EjbMethod createGetter(EjbView view, 206 JMethod apiMethod, 207 JMethod implMethod) 208 throws ConfigException 209 { 210 return new EjbManyToManyMethod(view, apiMethod, implMethod, this); 211 212 } 213 214 217 public AmberField assembleAmber(EntityType type) 218 throws ConfigException 219 { 220 AmberPersistenceUnit persistenceUnit = type.getPersistenceUnit(); 221 222 Table map = persistenceUnit.createTable(getSQLTable()); 223 224 map.setConfigLocation(getLocation()); 225 226 EntityManyToManyField manyToMany; 227 228 manyToMany = new EntityManyToManyField(type, getName()); 229 _amberManyToMany = manyToMany; 230 231 manyToMany.setAssociationTable(map); 232 233 EntityType targetType = _targetBean.getEntityType(); 234 manyToMany.setType(targetType); 235 236 ArrayList <ForeignColumn> targetColumns = 237 calculateColumns(map, targetType, _targetField, _dstSQLColumns); 238 239 manyToMany.setTargetLink(new LinkColumns(map, 240 targetType.getTable(), 241 targetColumns)); 242 243 EntityType sourceType = getBean().getEntityType(); 244 246 247 ArrayList <ForeignColumn> sourceColumns = 248 calculateColumns(map, sourceType, getName(), _keySQLColumns); 249 250 manyToMany.setSourceLink(new LinkColumns(map, 251 sourceType.getTable(), 252 sourceColumns)); 253 254 manyToMany.setOrderBy(_orderByFields, _orderByAscending); 255 256 manyToMany.init(); 257 258 return manyToMany; 259 } 260 261 private ArrayList <ForeignColumn> 262 calculateColumns(Table mapTable, EntityType type, String fieldName, 263 SqlRelation []sqlColumns) 264 { 265 ArrayList <ForeignColumn> columns = new ArrayList <ForeignColumn>(); 266 267 Id id = type.getId(); 268 ArrayList <Column> keys = id.getColumns(); 269 270 if (sqlColumns != null && sqlColumns.length == keys.size()) { 271 for (int i = 0; i < sqlColumns.length; i++) { 272 ForeignColumn column = 273 mapTable.createForeignColumn(sqlColumns[i].getSQLColumn(), 274 keys.get(i)); 275 columns.add(column); 276 } 277 } 278 else if (keys.size() == 1) { 279 Column key = keys.get(0); 280 281 String sqlColumn; 282 283 289 if (type.getTable().getName() != null) 290 sqlColumn = type.getTable().getName(); 291 else 292 sqlColumn = CmpField.toSqlName(type.getName()); 293 294 columns.add(mapTable.createForeignColumn(sqlColumn, key)); 295 } 296 else { 297 String baseSqlColumn; 298 299 if (sqlColumns != null && sqlColumns.length == 1) 300 baseSqlColumn = sqlColumns[0].getSQLColumn(); 301 else 302 baseSqlColumn = type.getTable().getName(); 303 304 if (baseSqlColumn == null) 305 baseSqlColumn = CmpField.toSqlName(type.getName()); 306 307 for (int i = 0; i < keys.size(); i++) { 308 Column key = keys.get(i); 309 310 String sqlColumn = baseSqlColumn + "_" + key.getName(); 311 312 ForeignColumn foreignColumn = 313 mapTable.createForeignColumn(sqlColumn, key); 314 315 columns.add(foreignColumn); 316 } 317 } 318 319 return columns; 320 } 321 } 322 | Popular Tags |