001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.ejb.gen21;
031:
032: import com.caucho.ejb.cfg21.EjbEntityBean;
033: import com.caucho.ejb.cfg21.CmrRelation;
034: import com.caucho.ejb.cfg21.CmrManyToOne;
035: import com.caucho.ejb.cfg21.CmrManyToMany;
036: import com.caucho.ejb.cfg.*;
037: import com.caucho.java.JavaWriter;
038: import com.caucho.java.gen.BaseClass;
039: import com.caucho.util.L10N;
040:
041: import java.io.IOException;
042: import java.util.Set;
043:
044: /**
045: * Generates the skeleton for an Amber-based entity bean.
046: */
047: public class CollectionClass extends BaseClass {
048: private final static L10N L = new L10N(CollectionClass.class);
049:
050: private CmrRelation _oneToMany;
051:
052: public CollectionClass(CmrRelation oneToMany, String className) {
053: _oneToMany = oneToMany;
054:
055: setClassName(className);
056:
057: Class retType = oneToMany.getGetter().getReturnType();
058: if (Set.class.isAssignableFrom(retType))
059: setSuperClassName("com.caucho.ejb.entity.CmpSetImpl");
060: else
061: setSuperClassName("com.caucho.ejb.entity.CmpCollectionImpl");
062: }
063:
064: /**
065: * Generates the list's class content.
066: */
067: public void generateClassContent(JavaWriter out) throws IOException {
068: generateConstructor(out);
069:
070: generateAdd(out);
071: generateRemove(out);
072:
073: super .generateClassContent(out);
074: }
075:
076: /**
077: * Generates the list's class content.
078: */
079: public void generateConstructor(JavaWriter out) throws IOException {
080: EjbEntityBean sourceBean = _oneToMany.getBean();
081: String sourceType = sourceBean.getLocal().getName();
082:
083: out.println();
084: out.println("Bean _bean;");
085: out.println(sourceType + " _beanLocal;");
086:
087: out.println();
088: out.println("public " + getClassName()
089: + "(Bean bean, com.caucho.amber.AmberQuery query)");
090: out.println("{");
091: out.pushDepth();
092: out.println("_bean = bean;");
093: out.println("_beanLocal = bean._ejb_context._viewLocal;");
094: out.println("fill(query);");
095: out.popDepth();
096: out.println("}");
097: }
098:
099: /**
100: * Generates the list's class content.
101: */
102: public void generateAdd(JavaWriter out) throws IOException {
103: out.println();
104: out.println("public boolean addImpl(Object v)");
105: out.println("{");
106: out.pushDepth();
107:
108: EjbEntityBean targetBean = _oneToMany.getTargetBean();
109: String targetType = targetBean.getLocal().getName();
110:
111: out.println("if (v == null)");
112: out.println(" return false;");
113: out.println("else if (! (v instanceof " + targetType + "))");
114: out
115: .println(" throw new IllegalArgumentException(v.getClass().getName() + \": \" + v);");
116:
117: CmrRelation targetRelation = _oneToMany.getTargetRelation();
118:
119: if (targetRelation instanceof CmrManyToOne) {
120: CmrManyToOne manyToOne = (CmrManyToOne) targetRelation;
121:
122: ApiMethod setter = manyToOne.getSetter();
123:
124: if (setter != null) {
125: out.println(targetType + " bean = (" + targetType
126: + ") v;");
127:
128: ApiMethod localDstSetter = targetBean.getMethod(
129: targetBean.getLocal(), setter);
130:
131: if (localDstSetter != null) {
132: out.print("bean");
133: } else {
134: out.print("((" + targetBean.getEJBClass().getName()
135: + ") ");
136: out
137: .print("((com.caucho.ejb.entity.EntityObject) bean)._caucho_getBean(_ejb_trans, true))");
138: }
139:
140: out.println("." + setter.getName() + "(_beanLocal);");
141: }
142: } else if (_oneToMany instanceof CmrManyToMany) {
143: CmrManyToMany manyToMany = (CmrManyToMany) _oneToMany;
144:
145: ApiMethod getter = manyToMany.getGetter();
146:
147: if (manyToMany.isTargetUnique())
148: out.println("_bean.__amber_" + getter.getName()
149: + "_remove_target(v);");
150:
151: // ejb/069l
152: out.println("_bean.__amber_" + getter.getName()
153: + "_add(_bean.__caucho_getConnection(), v);");
154: }
155:
156: out.println("return true;");
157:
158: out.popDepth();
159: out.println("}");
160: }
161:
162: /**
163: * Generates the list's class content.
164: */
165: public void generateRemove(JavaWriter out) throws IOException {
166: out.println();
167: out.println("protected boolean removeImpl(Object v)");
168: out.println("{");
169: out.pushDepth();
170:
171: EjbEntityBean targetBean = _oneToMany.getTargetBean();
172: String targetType = targetBean.getLocal().getName();
173:
174: out.println("if (v == null)");
175: out.println(" return false;");
176: out.println("else if (! (v instanceof " + targetType + "))");
177: out
178: .println(" throw new IllegalArgumentException(v.getClass().getName() + \": \" + v);");
179:
180: CmrRelation targetRelation = _oneToMany.getTargetRelation();
181:
182: if (targetRelation instanceof CmrManyToOne) {
183: CmrManyToOne manyToOne = (CmrManyToOne) targetRelation;
184:
185: ApiMethod setter = manyToOne.getSetter();
186: ApiMethod getter = manyToOne.getGetter();
187:
188: if (setter != null) {
189: out.println("if (_bean != null) {");
190: out.pushDepth();
191:
192: out.println(targetType + " bean = (" + targetType
193: + ") v;");
194:
195: out.println("if (_beanLocal != null) {");
196: out.pushDepth();
197:
198: ApiMethod localDstSetter = targetBean.getMethod(
199: targetBean.getLocal(), setter);
200: String bean = "bean";
201:
202: if (localDstSetter == null) {
203: String beanClass = targetBean.getEJBClass()
204: .getName();
205:
206: out.print(beanClass + " bean1 = ((" + beanClass
207: + ") ");
208: out
209: .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 + "."
215: + getter.getName() + "())) {");
216: out.pushDepth();
217:
218: out.println(bean + "." + setter.getName() + "(null);");
219:
220: out.popDepth();
221: out.println("}");
222:
223: out.popDepth();
224: out.println("}");
225:
226: out.popDepth();
227: out.println("}");
228: out.println();
229: }
230: } else if (_oneToMany instanceof CmrManyToMany) {
231: CmrManyToMany manyToMany = (CmrManyToMany) _oneToMany;
232:
233: ApiMethod getter = manyToMany.getGetter();
234:
235: out.println("_bean.__amber_" + getter.getName()
236: + "_remove(v);");
237: }
238:
239: out.println("return true;");
240:
241: out.popDepth();
242: out.println("}");
243: }
244: }
|