KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > ql > CollectionIdExpr


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  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.ejb.ql;
30
31 import com.caucho.config.ConfigException;
32 import com.caucho.ejb.cfg.EjbEntityBean;
33 import com.caucho.util.CharBuffer;
34
35 /**
36  * Identifier expression for EJB-QL.
37  */

38 class CollectionIdExpr extends PathExpr {
39   // identifier name
40
private String JavaDoc _name;
41   
42   // the persistent bean this id is a member of
43
private CollectionExpr _path;
44
45   private String JavaDoc _keyTable;
46   private String JavaDoc []_keyColumns;
47
48   private boolean _usesField;
49
50   /**
51    * Creates a new identifier expression.
52    *
53    * @param query the owning query
54    * @param name the identifier
55    * @param bean the mapped bean
56    */

57   CollectionIdExpr(Query query, String JavaDoc name, CollectionExpr path)
58     throws ConfigException
59   {
60     super(path.getItemBean());
61     
62     _query = query;
63     _name = name;
64     _path = path;
65
66     path.setId(this);
67
68     if (getBean() == null)
69       throw new NullPointerException JavaDoc("unknown bean for " + name + " " + query);
70     
71     // setJavaType(getBean().getJavaType());
72

73     /*
74     PersistentRelation relation = path.getRelation();
75
76     if (relation.hasMiddleTable()) {
77       _keyTable = "caucho" + query.getUnique();
78       PrimaryKey key = relation.getTargetBean().getPrimaryKey();
79       _keyColumns = relation.getTargetSQLColumns();
80
81       query.addFromItem(_keyTable, relation.getSQLTable());
82       _keyColumns = relation.addTargetLinks(query,
83                                             path.getBase().getTable(),
84                                             _keyTable);
85     }
86     else {
87       _usesField = true;
88       path.setUsesField();
89       
90       _keyTable = name;
91       PrimaryKey key = relation.getTargetBean().getPrimaryKey();
92       
93       query.addFromItem(_keyTable, relation.getSQLTable());
94       _keyColumns = relation.addTargetLinks(query, path.getBase().getTable(),
95                                             _keyTable);
96     }
97     */

98   }
99
100   CollectionExpr getPath()
101   {
102     return _path;
103   }
104
105   void setUsesField()
106   {
107     if (_usesField)
108       return;
109
110     _usesField = true;
111
112     /*
113     _query.addFromItem(_name, _bean.getSQLTable());
114     _keyColumns = _path.getRelation().addSourceLinks(_query, _keyTable, _name);
115     _keyTable = _name;
116     */

117   }
118
119   String JavaDoc getKeyTable()
120   {
121     return _keyTable;
122   }
123
124   String JavaDoc []getKeyFields()
125   {
126     return _keyColumns;
127   }
128
129   int getComponentCount()
130   {
131     return _path.getComponentCount();
132   }
133
134   String JavaDoc getTable()
135   {
136     return _name;
137   }
138
139   /**
140    * Returns the identifier name.
141    */

142   String JavaDoc getName()
143   {
144     return _name;
145   }
146
147   /**
148    * Returns the persistent bean this id is a member of
149    */

150   EjbEntityBean getBean()
151   {
152     return _bean;
153   }
154
155   EjbEntityBean getItemBean()
156   {
157     return _bean;
158   }
159
160   String JavaDoc getReturnEJB()
161   {
162     return getItemBean().getEJBName();
163   }
164
165   /**
166    * Prints the select SQL for this expression
167    *
168    * @param gen the java code generator
169    */

170   void printSelect(CharBuffer cb)
171     throws ConfigException
172   {
173     String JavaDoc []names = getKeyFields();
174
175     for (int i = 0; i < names.length; i++) {
176       if (i != 0)
177         cb.append(", ");
178       
179       cb.append(getKeyTable());
180       cb.append(".");
181       cb.append(names[i]);
182     }
183   }
184
185   /**
186    * Prints the select SQL for this expression
187    *
188    * @param gen the java code generator
189    */

190   String JavaDoc getSelectTable(CharBuffer cb)
191     throws ConfigException
192   {
193     return getKeyTable();
194   }
195
196   /**
197    * Prints the where SQL for this expression
198    *
199    * @param gen the java code generator
200    */

201   void printWhere(CharBuffer cb)
202     throws ConfigException
203   {
204     String JavaDoc []names = getKeyFields();
205     if (names.length != 1)
206       throw new RuntimeException JavaDoc();
207     
208     cb.append(getKeyTable());
209     cb.append(".");
210     cb.append(names[0]);
211   }
212
213   void printComponent(CharBuffer cb, int index)
214     throws ConfigException
215   {
216     cb.append(getKeyTable());
217     cb.append(".");
218     cb.append(getKeyFields()[index]);
219   }
220
221   /**
222    * Prints the where SQL for this expression
223    *
224    * @param gen the java code generator
225    */

226   void generateWhere(CharBuffer cb)
227   {
228     if (_bean == null)
229       throw new IllegalStateException JavaDoc("no bean for " + getName());
230
231     cb.append(getName());
232   }
233
234   /**
235    * Prints the where SQL for this expression
236    *
237    * @param gen the java code generator
238    */

239   void generateComponent(CharBuffer cb, int i)
240   {
241     if (_bean == null)
242       throw new IllegalStateException JavaDoc("no bean for " + getName());
243
244     cb.append(getName());
245     cb.append('.');
246     cb.append(generateKeyField(getItemBean().getEntityType(), i));
247   }
248
249   /**
250    * Prints the where SQL for this expression
251    *
252    * @param gen the java code generator
253    */

254   void generateAmber(CharBuffer cb)
255   {
256     cb.append("IN(");
257     _path.generateSelect(cb);
258     cb.append(") ");
259     cb.append(getName());
260   }
261   
262   /**
263    * Returns true if the two expressions are equal
264    */

265   public boolean equals(Object JavaDoc bObj)
266   {
267     if (! (bObj instanceof CollectionIdExpr))
268       return false;
269
270     CollectionIdExpr b = (CollectionIdExpr) bObj;
271
272     return _name.equals(b._name);
273   }
274
275   /**
276    * Returns a hash code for the expression
277    */

278   public int hashCode()
279   {
280     return _name.hashCode();
281   }
282
283   /**
284    * Printable version of the object.
285    */

286   public String JavaDoc toString()
287   {
288     return _name;
289   }
290 }
291
Popular Tags