KickJava   Java API By Example, From Geeks To Geeks.

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


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.EjbEntityBean;
35 import com.caucho.java.JavaWriter;
36 import com.caucho.java.gen.CallChain;
37 import com.caucho.java.gen.MethodCallChain;
38 import com.caucho.util.L10N;
39
40 import java.io.IOException JavaDoc;
41
42 /**
43  * Generates the skeleton for the create method.
44  */

45 public class EntityCreateCall extends CallChain {
46   private static L10N L = new L10N(EntityCreateMethod.class);
47
48   private EjbEntityBean _bean;
49   
50   private JMethod _createMethod;
51   private JMethod _postCreateMethod;
52
53   private CallChain _createCall;
54   private CallChain _postCreateCall;
55   
56   private JClass _primKeyClass;
57   private String JavaDoc _contextClassName;
58
59   private boolean _isCMP;
60
61   public EntityCreateCall(EjbEntityBean bean,
62               JMethod createMethod,
63               JMethod postCreateMethod,
64               String JavaDoc contextClassName)
65   {
66     _bean = bean;
67     
68     _createMethod = createMethod;
69     _postCreateMethod = postCreateMethod;
70     
71     _createCall = new MethodCallChain(_createMethod);
72     
73     if (_postCreateMethod != null)
74       _postCreateCall = new MethodCallChain(_postCreateMethod);
75     
76     _contextClassName = contextClassName;
77   }
78   
79   public void setCMP(boolean isCMP)
80   {
81     _isCMP = isCMP;
82   }
83
84   /**
85    * Prints the create method
86    *
87    * @param method the create method
88    */

89   public void generateCall(JavaWriter out, String JavaDoc retVar,
90                String JavaDoc var, String JavaDoc []args)
91     throws IOException JavaDoc
92   {
93     if (_isCMP) {
94       out.println("try {");
95       out.pushDepth();
96     }
97     
98     // out.println("Bean home = _ejb_begin(trans, true, false);");
99
out.println();
100     out.println("Bean bean = new Bean(cxt);");
101     out.println("bean._ejb_trans = trans;");
102     out.println("bean._ejb_flags = 1;");
103     
104     out.print(_createCall.getReturnType().getPrintName());
105     out.println(" key;");
106
107     _createCall.generateCall(out, "key", "bean", args);
108
109     if (_isCMP) {
110       String JavaDoc name = _bean.getEntityType().getName();
111       out.println("trans.getAmberConnection().create(\"" + name + "\", bean);");
112       out.println("cxt._amber = bean.__caucho_item;");
113       out.println("Object okey = bean.__caucho_getPrimaryKey();");
114       out.println("cxt.postCreate(okey);");
115     }
116
117     out.println("trans.addObject(bean);");
118
119     JClass retType = _createCall.getReturnType();
120     if (_isCMP) {
121     }
122     else if (retType.isAssignableTo(Object JavaDoc.class))
123       out.println("cxt.postCreate(key);");
124     else {
125       out.print("Object okey = ");
126       out.printJavaTypeToObject("key", retType);
127       out.println(";");
128       
129       out.println("cxt.postCreate(okey);");
130     }
131
132     // if (! _gen.isEntityCMP()) {
133
out.println("if (__caucho_log.isLoggable(java.util.logging.Level.FINE))");
134     out.println(" __caucho_log.fine(bean.__caucho_id + \":create(\" + key + \")\");");
135     out.println();
136     
137     out.println("bean._ejb_state = QEntity._CAUCHO_IS_LOADED;");
138     //println("bean._ejb_load_time = com.caucho.util.Alarm.getCurrentTime();");
139

140     if (_postCreateCall != null)
141       _postCreateCall.generateCall(out, null, "bean", args);
142     
143     if (_isCMP) {
144       out.popDepth();
145       out.println("} catch (java.sql.SQLException e) {");
146       out.println(" throw new com.caucho.ejb.CreateExceptionWrapper(e);");
147       out.println("}");
148     }
149   }
150 }
151
Popular Tags