KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > gen > CollectionClass


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.gen;
31
32 import com.caucho.bytecode.JClass;
33 import com.caucho.bytecode.JMethod;
34 import com.caucho.ejb.cfg.CmrManyToMany;
35 import com.caucho.ejb.cfg.CmrManyToOne;
36 import com.caucho.ejb.cfg.CmrRelation;
37 import com.caucho.ejb.cfg.EjbEntityBean;
38 import com.caucho.java.JavaWriter;
39 import com.caucho.java.gen.BaseClass;
40 import com.caucho.util.L10N;
41
42 import java.io.IOException JavaDoc;
43 import java.util.Set JavaDoc;
44
45 /**
46  * Generates the skeleton for an Amber-based entity bean.
47  */

48 public class CollectionClass extends BaseClass {
49   private final static L10N L = new L10N(CollectionClass.class);
50
51   private CmrRelation _oneToMany;
52   
53   public CollectionClass(CmrRelation oneToMany,
54              String JavaDoc className)
55   {
56     _oneToMany = oneToMany;
57
58     setClassName(className);
59
60     JClass retType = oneToMany.getGetter().getReturnType();
61     if (retType.isAssignableTo(Set JavaDoc.class))
62       setSuperClassName("com.caucho.ejb.entity.CmpSetImpl");
63     else
64       setSuperClassName("com.caucho.ejb.entity.CmpCollectionImpl");
65   }
66
67   /**
68    * Generates the list's class content.
69    */

70   public void generateClassContent(JavaWriter out)
71     throws IOException JavaDoc
72   {
73     generateConstructor(out);
74     
75     generateAdd(out);
76     generateRemove(out);
77     
78     super.generateClassContent(out);
79   }
80
81   /**
82    * Generates the list's class content.
83    */

84   public void generateConstructor(JavaWriter out)
85     throws IOException JavaDoc
86   {
87     EjbEntityBean sourceBean = _oneToMany.getBean();
88     String JavaDoc sourceType = sourceBean.getLocal().getName();
89
90     out.println();
91     out.println("Bean _bean;");
92     out.println(sourceType + " _beanLocal;");
93     
94     out.println();
95     out.println("public " + getClassName() + "(Bean bean, com.caucho.amber.AmberQuery query)");
96     out.println("{");
97     out.pushDepth();
98     out.println("_bean = bean;");
99     out.println("_beanLocal = bean._ejb_context._viewLocal;");
100     out.println("fill(query);");
101     out.popDepth();
102     out.println("}");
103   }
104
105   /**
106    * Generates the list's class content.
107    */

108   public void generateAdd(JavaWriter out)
109     throws IOException JavaDoc
110   {
111     out.println();
112     out.println("public boolean addImpl(Object v)");
113     out.println("{");
114     out.pushDepth();
115     
116     EjbEntityBean targetBean = _oneToMany.getTargetBean();
117     String JavaDoc targetType = targetBean.getLocal().getName();
118
119     out.println("if (v == null)");
120     out.println(" return false;");
121     out.println("else if (! (v instanceof " + targetType + "))");
122     out.println(" throw new IllegalArgumentException(v.getClass().getName() + \": \" + v);");
123
124     CmrRelation targetRelation = _oneToMany.getTargetRelation();
125
126     if (targetRelation instanceof CmrManyToOne) {
127       CmrManyToOne manyToOne = (CmrManyToOne) targetRelation;
128
129       JMethod setter = manyToOne.getSetter();
130
131       if (setter != null) {
132     out.println(targetType + " bean = (" + targetType + ") v;");
133
134     JMethod localDstSetter = targetBean.getMethod(targetBean.getLocal(),
135                               setter);
136
137     if (localDstSetter != null) {
138       out.print("bean");
139     }
140     else {
141       out.print("((" + targetBean.getEJBClass().getName() + ") ");
142       out.print("((com.caucho.ejb.entity.EntityObject) bean)._caucho_getBean(_ejb_trans, true))");
143     }
144     
145     out.println("." + setter.getName() + "(_beanLocal);");
146       }
147     }
148     else if (_oneToMany instanceof CmrManyToMany) {
149       CmrManyToMany manyToMany = (CmrManyToMany) _oneToMany;
150
151       JMethod getter = manyToMany.getGetter();
152
153       if (manyToMany.isTargetUnique())
154     out.println("_bean.__amber_" + getter.getName() + "_remove_target(v);");
155       
156       out.println("_bean.__amber_" + getter.getName() + "_add(v);");
157     }
158
159     out.println("return true;");
160     
161     out.popDepth();
162     out.println("}");
163   }
164
165   /**
166    * Generates the list's class content.
167    */

168   public void generateRemove(JavaWriter out)
169     throws IOException JavaDoc
170   {
171     out.println();
172     out.println("protected boolean removeImpl(Object v)");
173     out.println("{");
174     out.pushDepth();
175     
176     EjbEntityBean targetBean = _oneToMany.getTargetBean();
177     String JavaDoc targetType = targetBean.getLocal().getName();
178
179     out.println("if (v == null)");
180     out.println(" return false;");
181     out.println("else if (! (v instanceof " + targetType + "))");
182     out.println(" throw new IllegalArgumentException(v.getClass().getName() + \": \" + v);");
183
184     CmrRelation targetRelation = _oneToMany.getTargetRelation();
185
186     if (targetRelation instanceof CmrManyToOne) {
187       CmrManyToOne manyToOne = (CmrManyToOne) targetRelation;
188
189       JMethod setter = manyToOne.getSetter();
190       JMethod getter = manyToOne.getGetter();
191
192       if (setter != null) {
193     out.println("if (_bean != null) {");
194     out.pushDepth();
195     
196     out.println(targetType + " bean = (" + targetType + ") v;");
197     
198     out.println("if (_beanLocal != null) {");
199     out.pushDepth();
200
201     JMethod localDstSetter = targetBean.getMethod(targetBean.getLocal(),
202                               setter);
203     String JavaDoc bean = "bean";
204
205     if (localDstSetter == null) {
206       String JavaDoc beanClass = targetBean.getEJBClass().getName();
207       
208       out.print(beanClass + " bean1 = ((" + beanClass + ") ");
209       out.print("((com.caucho.ejb.entity.EntityObject) bean)._caucho_getBean(_ejb_trans, true));");
210
211       bean = "bean1";
212     }
213     
214     out.println("if (_beanLocal.equals(" + bean + "." + getter.getName() + "())) {");
215     out.pushDepth();
216     
217     out.println(bean + "." + setter.getName() + "(null);");
218     
219     out.popDepth();
220     out.println("}");
221     
222     out.popDepth();
223     out.println("}");
224     
225     out.popDepth();
226     out.println("}");
227     out.println();
228       }
229     }
230     else if (_oneToMany instanceof CmrManyToMany) {
231       CmrManyToMany manyToMany = (CmrManyToMany) _oneToMany;
232
233       JMethod getter = manyToMany.getGetter();
234
235       out.println("_bean.__amber_" + getter.getName() + "_remove(v);");
236     }
237
238     out.println("return true;");
239     
240     out.popDepth();
241     out.println("}");
242   }
243 }
244
Popular Tags