KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > amber > expr > KeyManyToOneExpr


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

28
29 package com.caucho.amber.expr;
30
31 import com.caucho.amber.field.AmberField;
32 import com.caucho.amber.field.IdField;
33 import com.caucho.amber.field.KeyManyToOneField;
34 import com.caucho.amber.field.KeyPropertyField;
35 import com.caucho.amber.query.FromItem;
36 import com.caucho.amber.query.QueryParser;
37 import com.caucho.amber.table.Column;
38 import com.caucho.amber.type.EntityType;
39 import com.caucho.util.CharBuffer;
40
41 /**
42  * Bound identifier expression.
43  */

44 public class KeyManyToOneExpr extends AbstractPathExpr
45   implements IdFieldExpr {
46   private PathExpr _parent;
47   // identifier name value
48
private KeyManyToOneField _manyToOne;
49
50   private FromItem _fromItem;
51   private FromItem _childFromItem;
52
53   /**
54    * Creates a new unbound id expression.
55    */

56   public KeyManyToOneExpr(PathExpr parent, KeyManyToOneField manyToOne)
57   {
58     _parent = parent;
59     _manyToOne = manyToOne;
60   }
61
62   /**
63    * Returns the parent expression.
64    */

65   public PathExpr getParent()
66   {
67     return _parent;
68   }
69
70   /**
71    * Returns the name.
72    */

73   public IdField getField()
74   {
75     return _manyToOne;
76   }
77
78   /**
79    * Returns the entity class.
80    */

81   public EntityType getTargetType()
82   {
83     return _manyToOne.getEntityType();
84   }
85
86   /**
87    * Returns the column.
88    */

89   public Column getColumn()
90   {
91     throw new UnsupportedOperationException JavaDoc();
92   }
93   
94   /**
95    * Creates the expr from the path.
96    */

97   public AmberExpr createField(QueryParser parser, String JavaDoc name)
98   {
99     AmberField field = getTargetType().getField(name);
100
101     if (field == null)
102       return null;
103     else if (field instanceof IdField) {
104       KeyPropertyField idField = _manyToOne.getIdField((IdField) field);
105
106       return idField.createExpr(parser, _parent);
107     }
108     else
109       return field.createExpr(parser, this);
110   }
111
112   /**
113    * Binds the expression as a select item.
114    */

115   public AmberExpr bindSelect(QueryParser parser)
116   {
117     _fromItem = _parent.bindSubPath(parser);
118
119     return this;
120   }
121
122   /**
123    * Return the child from item.
124    */

125   public FromItem getChildFromItem()
126   {
127     return _childFromItem;
128   }
129
130   /**
131    * Binds the expression as a subpath.
132    */

133   public FromItem bindSubPath(QueryParser parser)
134   {
135     if (_childFromItem != null)
136       return _childFromItem;
137
138     KeyManyToOneExpr pathExpr = (KeyManyToOneExpr) parser.addPath(this);
139
140     if (pathExpr != this) {
141       _fromItem = pathExpr._fromItem;
142       _childFromItem = pathExpr._childFromItem;
143
144       return _childFromItem;
145     }
146     
147     _fromItem = _parent.bindSubPath(parser);
148
149     if (_fromItem != null)
150       _childFromItem = _fromItem.getQuery().createFromItem(getTargetType().getTable(),
151                                parser.createTableName());
152     else
153       _childFromItem = parser.getSelectQuery().createFromItem(getTargetType().getTable(),
154                             parser.createTableName());
155
156     JoinExpr link = new ManyToOneJoinExpr(_manyToOne.getLinkColumns(),
157                       _fromItem,
158                       _childFromItem);
159
160     _childFromItem.setJoinExpr(link);
161     
162     return _childFromItem;
163   }
164
165   /**
166    * Returns true if the expression uses the from item.
167    */

168   public boolean usesFrom(FromItem from, int type, boolean isNot)
169   {
170     return from == _childFromItem || _parent.usesFrom(from, type);
171   }
172
173   /**
174    * Replaces linked join to eliminate a table.
175    */

176   /*
177   public AmberExpr replaceJoin(JoinExpr join)
178   {
179     if (_parent instanceof IdExpr) {
180       return join.replace(this);
181     }
182     else
183       return super.replaceJoin(join);
184   }
185   */

186
187   /**
188    * Returns the table.
189    */

190   /*
191   public String getTable()
192   {
193     if (_childFromItem != null)
194       return _childFromItem.getName();
195     else if (_fromItem != null)
196       return _fromItem.getName();
197     else
198       return _parent.getChildFromItem().getName();
199   }
200   */

201   
202   /**
203    * Generates the where expression.
204    */

205   public void generateMatchArgWhere(CharBuffer cb)
206   {
207     String JavaDoc table;
208     
209     if (_fromItem != null)
210       table = _fromItem.getName();
211     else
212       table = _parent.getChildFromItem().getName();
213     
214     cb.append(_manyToOne.getLinkColumns().generateMatchArgSQL(table));
215   }
216
217   public String JavaDoc toString()
218   {
219     return _parent + "." + getField();
220   }
221
222   public int hashCode()
223   {
224     return 65521 * _parent.hashCode() + getField().hashCode();
225   }
226
227   public boolean equals(Object JavaDoc o)
228   {
229     if (o == null || ! getClass().equals(o.getClass()))
230       return false;
231
232     KeyManyToOneExpr manyToOne = (KeyManyToOneExpr) o;
233
234     return (_parent.equals(manyToOne._parent) &&
235         getField().equals(manyToOne.getField()));
236   }
237 }
238
Popular Tags