KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > cfg > CmrManyToMany


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

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 JavaDoc;
48
49 /**
50  * many-to-many relation
51  */

52 public class CmrManyToMany extends CmrRelation {
53   private static final L10N L = new L10N(CmrManyToMany.class);
54
55   private String JavaDoc _sqlTable;
56   
57   private EjbEntityBean _targetBean;
58   private String JavaDoc _targetField;
59
60   // true if the target is only in a single instance
61
private boolean _isTargetUnique;
62
63   private ArrayList JavaDoc<String JavaDoc> _orderByFields;
64   private ArrayList JavaDoc<Boolean JavaDoc> _orderByAscending;
65   
66   private SqlRelation []_keySQLColumns;
67   private SqlRelation []_dstSQLColumns;
68
69   private EntityManyToManyField _amberManyToMany;
70
71   /**
72    * Creates a new cmp-relation
73    */

74   public CmrManyToMany(EjbEntityBean entityBean,
75                String JavaDoc fieldName,
76                EjbEntityBean targetBean,
77                String JavaDoc targetField)
78     throws ConfigException
79   {
80     super(entityBean, fieldName);
81
82     _targetBean = targetBean;
83     _targetField = targetField;
84   }
85
86   /**
87    * Returns the target bean
88    */

89   public EjbEntityBean getTargetBean()
90   {
91     return _targetBean;
92   }
93
94   /**
95    * Sets the sql table.
96    */

97   public void setSQLTable(String JavaDoc sqlTable)
98   {
99     _sqlTable = sqlTable;
100   }
101
102   /**
103    * Gets the sql table.
104    */

105   public String JavaDoc getSQLTable()
106   {
107     if (_sqlTable != null)
108       return _sqlTable;
109     else
110       return getRelationName();
111   }
112
113   /**
114    * Sets true if the target is unique.
115    */

116   public void setTargetUnique(boolean isUnique)
117   {
118     _isTargetUnique = isUnique;
119   }
120
121   /**
122    * Sets true if the target is unique.
123    */

124   public boolean isTargetUnique()
125   {
126     return _isTargetUnique;
127   }
128
129   /**
130    * Returns the target type.
131    */

132   public JClass getTargetType()
133   {
134     return _targetBean.getLocal();
135   }
136
137   /**
138    * Sets the column.
139    */

140   public void setKeySQLColumns(SqlRelation []columns)
141   {
142     _keySQLColumns = columns;
143   }
144
145   /**
146    * Gets the column.
147    */

148   public SqlRelation []getKeySQLColumns()
149   {
150     return _keySQLColumns;
151   }
152
153   /**
154    * Sets the column.
155    */

156   public void setDstSQLColumns(SqlRelation []columns)
157   {
158     _dstSQLColumns = columns;
159   }
160
161   /**
162    * Gets the column.
163    */

164   public SqlRelation []getDstSQLColumns()
165   {
166     return _dstSQLColumns;
167   }
168
169   /**
170    * Sets the order by.
171    */

172   public void setOrderBy(String JavaDoc orderBySQL)
173     throws ConfigException
174   {
175     if (orderBySQL != null) {
176       ArrayList JavaDoc<String JavaDoc> fields = new ArrayList JavaDoc<String JavaDoc>();
177       ArrayList JavaDoc<Boolean JavaDoc> asc = new ArrayList JavaDoc<Boolean JavaDoc>();
178
179       QLParser.parseOrderBy(_targetBean, orderBySQL, fields, asc);
180       
181       _orderByFields = fields;
182       _orderByAscending = asc;
183     }
184   }
185
186   /**
187    * The OneToMany is a collection.
188    */

189   public boolean isCollection()
190   {
191     return true;
192   }
193
194   /**
195    * Returns the amber field.
196    */

197   public EntityManyToManyField getAmberField()
198   {
199     return _amberManyToMany;
200   }
201
202   /**
203    * Create any bean methods.
204    */

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   /**
215    * Creates the amber type.
216    */

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 JavaDoc<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     // manyToMany.setType(targetType);
245

246
247     ArrayList JavaDoc<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 JavaDoc<ForeignColumn>
262     calculateColumns(Table mapTable, EntityType type, String JavaDoc fieldName,
263              SqlRelation []sqlColumns)
264   {
265     ArrayList JavaDoc<ForeignColumn> columns = new ArrayList JavaDoc<ForeignColumn>();
266
267     Id id = type.getId();
268     ArrayList JavaDoc<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 JavaDoc sqlColumn;
282
283       /*
284       if (fieldName != null)
285     sqlColumn = CmpField.toSqlName(type.getName());
286       else
287       sqlColumn = key.getColumn().getName();
288       */

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 JavaDoc 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 JavaDoc 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