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.cfg21;
031:
032: import com.caucho.ejb.cfg.*;
033: import com.caucho.ejb.cfg21.EjbEntityBean;
034: import com.caucho.ejb.cfg21.CmrRelation;
035: import com.caucho.ejb.cfg21.CmrManyToOne;
036: import com.caucho.config.ConfigException;
037: import com.caucho.ejb.gen21.BeanAssembler;
038: import com.caucho.java.JavaWriter;
039: import com.caucho.java.gen.BaseMethod;
040: import com.caucho.util.L10N;
041:
042: import java.io.IOException;
043:
044: /**
045: * Configuration for a many-to-one CMP method.
046: */
047: public class EjbManyToOneSetMethod extends EjbMethod {
048: private static final L10N L = new L10N(EjbManyToOneSetMethod.class);
049:
050: private CmrManyToOne _manyToOne;
051:
052: /**
053: * Creates a new method.
054: *
055: * @param view the owning view
056: * @param apiMethod the method from the view
057: * @param implMethod the method from the implementation
058: */
059: public EjbManyToOneSetMethod(EjbView view, ApiMethod apiMethod,
060: ApiMethod implMethod, CmrManyToOne manyToOne) {
061: super (view, apiMethod, implMethod);
062:
063: _manyToOne = manyToOne;
064: }
065:
066: /**
067: * Assembles the bean method.
068: */
069: public void assembleBean(BeanAssembler beanAssembler,
070: String fullClassName) throws ConfigException {
071: beanAssembler.addMethod(new BeanMethod(getImplMethod()));
072: }
073:
074: class BeanMethod extends BaseMethod {
075: BeanMethod(ApiMethod method) {
076: super (method.getMethod());
077: }
078:
079: /**
080: * Generates the code for the call.
081: *
082: * @param out the writer to the output stream.
083: * @param args the arguments
084: */
085: protected void generateCall(JavaWriter out, String[] args)
086: throws IOException {
087: CmrRelation targetRelation = _manyToOne.getTargetRelation();
088:
089: String value = args[0];
090:
091: if (targetRelation instanceof CmrManyToOne) {
092: CmrManyToOne targetManyToOne = (CmrManyToOne) targetRelation;
093:
094: ApiMethod srcGetter = _manyToOne.getGetter();
095: ApiMethod dstSetter = targetManyToOne.getSetter();
096:
097: // XXX: EJBClass, i.e. Bean setter
098:
099: if (dstSetter != null) {
100: EjbEntityBean targetBean = _manyToOne
101: .getTargetBean();
102: String targetType = targetBean.getLocal().getName();
103:
104: String localType = _manyToOne.getBean().getLocal()
105: .getName();
106:
107: ApiMethod localDstSetter = targetBean.getMethod(
108: targetBean.getLocal(), dstSetter);
109:
110: out.println(targetType + " oldBean = "
111: + srcGetter.getName() + "();");
112:
113: out.println("if (" + value + " == oldBean || "
114: + value + " != null && " + value
115: + ".equals(oldBean))");
116: out.println(" return;");
117:
118: out.println("super." + getImplMethod().getName()
119: + "(" + value + ");");
120:
121: out.println("if (oldBean != null) {");
122: out.pushDepth();
123:
124: if (localDstSetter != null) {
125: out.print("oldBean");
126: } else {
127: out.print("(("
128: + targetBean.getEJBClass().getName()
129: + ") ");
130: out
131: .print("((com.caucho.ejb.entity.EntityObject) oldBean)._caucho_getBean(_ejb_trans, false))");
132: }
133:
134: out.println("." + dstSetter.getName() + "(("
135: + localType + ") null);");
136:
137: out.popDepth();
138: out.println("}");
139:
140: out.println();
141: out.println("if (" + value + " != null) {");
142: out.pushDepth();
143:
144: if (localDstSetter != null) {
145: out.print(value);
146: } else {
147: out.print("(("
148: + targetBean.getEJBClass().getName()
149: + ") ");
150: out
151: .print("((com.caucho.ejb.entity.EntityObject) "
152: + value
153: + ")._caucho_getBean(_ejb_trans, false))");
154: }
155:
156: out.println("." + dstSetter.getName() + "(("
157: + localType
158: + ") _ejb_context.getEJBLocalObject());");
159:
160: out.popDepth();
161: out.println("}");
162:
163: return;
164: }
165: }
166:
167: out.println("super." + getImplMethod().getName() + "("
168: + value + ");");
169: }
170: }
171: }
|