1 29 30 package com.caucho.ejb.cfg; 31 32 import com.caucho.amber.field.AmberField; 33 import com.caucho.amber.field.EntityOneToManyField; 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.type.EntityType; 40 import com.caucho.bytecode.JClass; 41 import com.caucho.bytecode.JMethod; 42 import com.caucho.config.ConfigException; 43 import com.caucho.ejb.ql.QLParser; 44 import com.caucho.java.JavaWriter; 45 import com.caucho.util.L10N; 46 47 import java.io.IOException ; 48 import java.util.ArrayList ; 49 50 53 public class CmrOneToMany extends CmrRelation { 54 private static final L10N L = new L10N(CmrOneToMany.class); 55 56 private EjbEntityBean _targetBean; 57 private String _targetField; 58 59 private ArrayList <String > _orderByFields; 60 private ArrayList <Boolean > _orderByAscending; 61 62 private SqlRelation []_sqlColumns; 63 64 private EntityOneToManyField _amberOneToMany; 65 66 69 public CmrOneToMany(EjbEntityBean entityBean, 70 String fieldName, 71 EjbEntityBean targetBean, 72 String targetField) 73 throws ConfigException 74 { 75 super(entityBean, fieldName); 76 77 _targetBean = targetBean; 78 _targetField = targetField; 79 } 80 81 84 public EjbEntityBean getTargetBean() 85 { 86 return _targetBean; 87 } 88 89 92 public JClass getTargetType() 93 { 94 return _targetBean.getLocal(); 95 } 96 97 100 public void setSQLColumns(SqlRelation []columns) 101 { 102 _sqlColumns = columns; 103 } 104 105 108 public SqlRelation []getSQLColumns() 109 { 110 return _sqlColumns; 111 } 112 113 116 public void setOrderBy(String orderBySQL) 117 throws ConfigException 118 { 119 if (orderBySQL != null) { 120 ArrayList <String > fields = new ArrayList <String >(); 121 ArrayList <Boolean > asc = new ArrayList <Boolean >(); 122 123 QLParser.parseOrderBy(_targetBean, orderBySQL, fields, asc); 124 125 _orderByFields = fields; 126 _orderByAscending = asc; 127 } 128 } 129 130 133 public boolean isCollection() 134 { 135 return true; 136 } 137 138 141 public EjbMethod createGetter(EjbView view, 142 JMethod apiMethod, 143 JMethod implMethod) 144 throws ConfigException 145 { 146 return new EjbOneToManyMethod(view, apiMethod, implMethod, this); 147 } 148 149 152 public AmberField assembleAmber(EntityType type) 153 throws ConfigException 154 { 155 EntityOneToManyField oneToMany = new EntityOneToManyField(type, getName()); 156 157 AmberPersistenceUnit manager = type.getPersistenceUnit(); 158 159 EntityType targetType = _targetBean.getEntityType(); 160 oneToMany.setType(targetType); 161 162 165 oneToMany.setOrderBy(_orderByFields, _orderByAscending); 166 167 _amberOneToMany = oneToMany; 168 169 return oneToMany; 170 } 171 172 175 public void linkAmber() 176 throws ConfigException 177 { 178 CmrManyToOne manyToOne = (CmrManyToOne) getTargetRelation(); 179 180 _amberOneToMany.setSourceField(manyToOne.getAmberManyToOne()); 181 _amberOneToMany.setLinkColumns(manyToOne.getAmberManyToOne().getLinkColumns()); 182 183 _amberOneToMany.init(); 184 } 185 186 189 public void generateAfterCommit(JavaWriter out) 190 throws IOException 191 { 192 if (getHasGetter()) 193 out.println("__caucho_field_" + getName() + " = null;"); 194 } 195 196 private LinkColumns calculateColumn(EntityType parentType, 197 EntityType childType, 198 String fieldName, 199 SqlRelation []sqlColumns) 200 throws ConfigException 201 { 202 Id id = parentType.getId(); 203 ArrayList <Column> keys = new ArrayList <Column>(id.getColumns()); 204 ArrayList <ForeignColumn> columns = new ArrayList (); 205 206 219 220 if (_sqlColumns != null && _sqlColumns.length == keys.size()) { 221 for (int i = 0; i < keys.size(); i++) { 222 Column key = keys.get(i); 223 224 String sqlColumn = getColumn(_sqlColumns, key.getName()); 225 ForeignColumn column = 226 childType.getTable().createForeignColumn(sqlColumn, key); 227 228 columns.add(column); 229 } 230 } 231 else if (_sqlColumns != null && _sqlColumns.length == 1) { 232 String baseSqlColumn = _sqlColumns[0].getSQLColumn(); 233 234 for (Column key : keys) { 235 String sqlColumn; 236 237 sqlColumn = baseSqlColumn + "_" + key.getName(); 238 239 ForeignColumn column = 240 childType.getTable().createForeignColumn(sqlColumn, key); 241 columns.add(column); 242 } 243 } 244 else if (_sqlColumns != null && _sqlColumns.length > 0) { 245 throw new IllegalStateException ("Mismatched SQL columns"); 246 } 247 else if (keys.size() == 1) { 248 Column key = keys.get(0); 249 250 String sqlColumn = CmpField.toSqlName(fieldName); 251 252 ForeignColumn column = 253 childType.getTable().createForeignColumn(sqlColumn, key); 254 255 columns.add(column); 256 } 257 else { 258 String baseSqlColumn = CmpField.toSqlName(fieldName); 259 260 for (Column key : keys) { 261 String sqlColumn = baseSqlColumn + "_" + key.getName(); 262 263 ForeignColumn foreignColumn = 264 childType.getTable().createForeignColumn(sqlColumn, key); 265 266 columns.add(foreignColumn); 267 } 268 } 269 270 return new LinkColumns(childType.getTable(), 271 parentType.getTable(), 272 columns); 273 } 274 275 private String getColumn(SqlRelation []sqlColumns, String fieldName) 276 throws ConfigException 277 { 278 if (sqlColumns.length == 1) 279 return sqlColumns[0].getSQLColumn(); 280 281 for (int i = 0; i < sqlColumns.length; i++) { 282 String ref = sqlColumns[i].getReferences(); 283 284 if (ref == null) 285 throw new ConfigException(L.l("sql-column '{0}' needs a references attribute.", 286 sqlColumns[i].getSQLColumn())); 287 288 if (ref.equals(fieldName)) 289 return sqlColumns[i].getSQLColumn(); 290 } 291 292 throw new ConfigException(L.l("key '{0}' has no matching sql-column", 293 fieldName)); 294 } 295 } 296 | Popular Tags |