KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > cfg > EjbManyToOneSetMethod


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.cfg;
31
32 import com.caucho.bytecode.JMethod;
33 import com.caucho.config.ConfigException;
34 import com.caucho.ejb.gen.BeanAssembler;
35 import com.caucho.java.JavaWriter;
36 import com.caucho.java.gen.BaseMethod;
37 import com.caucho.util.L10N;
38
39 import java.io.IOException JavaDoc;
40
41 /**
42  * Configuration for a many-to-one CMP method.
43  */

44 public class EjbManyToOneSetMethod extends EjbMethod {
45   private static final L10N L = new L10N(EjbManyToOneSetMethod.class);
46
47   private CmrManyToOne _manyToOne;
48   
49   /**
50    * Creates a new method.
51    *
52    * @param view the owning view
53    * @param apiMethod the method from the view
54    * @param implMethod the method from the implementation
55    */

56   public EjbManyToOneSetMethod(EjbView view,
57                    JMethod apiMethod, JMethod implMethod,
58                    CmrManyToOne manyToOne)
59   {
60     super(view, apiMethod, implMethod);
61
62     _manyToOne = manyToOne;
63   }
64
65   /**
66    * Assembles the bean method.
67    */

68   public void assembleBean(BeanAssembler beanAssembler, String JavaDoc fullClassName)
69     throws ConfigException
70   {
71     beanAssembler.addMethod(new BeanMethod(getImplMethod()));
72   }
73
74   class BeanMethod extends BaseMethod {
75     BeanMethod(JMethod method)
76     {
77       super(method);
78     }
79   
80     /**
81      * Generates the code for the call.
82      *
83      * @param out the writer to the output stream.
84      * @param args the arguments
85      */

86     protected void generateCall(JavaWriter out, String JavaDoc []args)
87       throws IOException JavaDoc
88     {
89       CmrRelation targetRelation = _manyToOne.getTargetRelation();
90
91       String JavaDoc value = args[0];
92
93       if (targetRelation instanceof CmrManyToOne) {
94     CmrManyToOne targetManyToOne = (CmrManyToOne) targetRelation;
95
96     JMethod srcGetter = _manyToOne.getGetter();
97     JMethod dstSetter = targetManyToOne.getSetter();
98
99     // XXX: EJBClass, i.e. Bean setter
100

101     if (dstSetter != null) {
102       EjbEntityBean targetBean = _manyToOne.getTargetBean();
103       String JavaDoc targetType = targetBean.getLocal().getName();
104
105       String JavaDoc localType = _manyToOne.getBean().getLocal().getName();
106
107       JMethod localDstSetter = targetBean.getMethod(targetBean.getLocal(),
108                                dstSetter);
109
110       out.println(targetType + " oldBean = " + srcGetter.getName() + "();");
111
112       out.println("if (" + value + " == oldBean || " +
113               value + " != null && " + value + ".equals(oldBean))");
114       out.println(" return;");
115
116       out.println("super." + getImplMethod().getName() + "(" + value + ");");
117
118       out.println("if (oldBean != null) {");
119       out.pushDepth();
120
121       if (localDstSetter != null) {
122         out.print("oldBean");
123       }
124       else {
125         out.print("((" + targetBean.getEJBClass().getName() + ") ");
126         out.print("((com.caucho.ejb.entity.EntityObject) oldBean)._caucho_getBean(_ejb_trans, false))");
127       }
128       
129       out.println("." + dstSetter.getName() + "((" + localType + ") null);");
130       
131       out.popDepth();
132       out.println("}");
133
134       out.println();
135       out.println("if (" + value + " != null) {");
136       out.pushDepth();
137
138       if (localDstSetter != null) {
139         out.print(value);
140       }
141       else {
142         out.print("((" + targetBean.getEJBClass().getName() + ") ");
143         out.print("((com.caucho.ejb.entity.EntityObject) " + value + ")._caucho_getBean(_ejb_trans, false))");
144       }
145       
146       out.println("." + dstSetter.getName() + "((" + localType + ") _ejb_context.getEJBLocalObject());");
147       
148       out.popDepth();
149       out.println("}");
150     
151       return;
152     }
153       }
154       
155       out.println("super." + getImplMethod().getName() + "(" + value + ");");
156     }
157   }
158 }
159
Popular Tags