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.gen.*;
034: import com.caucho.ejb.cfg.*;
035: import com.caucho.java.JavaWriter;
036: import com.caucho.java.gen.CallChain;
037: import com.caucho.java.gen.MethodCallChain;
038: import com.caucho.util.L10N;
039:
040: import java.io.IOException;
041:
042: /**
043: * Generates the skeleton for the create method.
044: */
045: public class EntityCreateCall extends CallChain {
046: private static L10N L = new L10N(EntityCreateMethod.class);
047:
048: private EjbEntityBean _bean;
049:
050: private ApiMethod _createMethod;
051: private ApiMethod _postCreateMethod;
052:
053: private CallChain _createCall;
054: private CallChain _postCreateCall;
055:
056: private ApiClass _primKeyClass;
057: private String _contextClassName;
058:
059: private boolean _isCMP;
060:
061: public EntityCreateCall(EjbEntityBean bean, ApiMethod createMethod,
062: ApiMethod postCreateMethod, String contextClassName) {
063: _bean = bean;
064:
065: _createMethod = createMethod;
066: _postCreateMethod = postCreateMethod;
067:
068: _createCall = new MethodCallChain(_createMethod.getMethod());
069:
070: if (_postCreateMethod != null)
071: _postCreateCall = new MethodCallChain(_postCreateMethod
072: .getMethod());
073:
074: _contextClassName = contextClassName;
075: }
076:
077: public void setCMP(boolean isCMP) {
078: _isCMP = isCMP;
079: }
080:
081: /**
082: * Prints the create method
083: *
084: * @param method the create method
085: */
086: public void generateCall(JavaWriter out, String retVar, String var,
087: String[] args) throws IOException {
088: if (_isCMP) {
089: out.println("try {");
090: out.pushDepth();
091: }
092:
093: // out.println("Bean home = _ejb_begin(trans, true, false);");
094: out.println();
095: out.println("Bean bean = new Bean(cxt);");
096: out.println("bean._ejb_trans = trans;");
097: out.println("bean._ejb_flags = 1;");
098:
099: out.printClass(_createCall.getReturnType());
100: out.println(" key;");
101:
102: _createCall.generateCall(out, "key", "bean", args);
103:
104: if (_isCMP) {
105: String name = _bean.getEntityType().getName();
106: out.println("trans.getAmberConnection().create(\"" + name
107: + "\", bean);");
108: out
109: .println("cxt.__amber_cacheItem = bean.__caucho_cacheItem;");
110: out.println("Object okey = bean.__caucho_getPrimaryKey();");
111: out.println("cxt.postCreate(okey);");
112: }
113:
114: out.println("trans.addObject(bean);");
115:
116: Class retType = _createCall.getReturnType();
117: if (_isCMP) {
118: } else if (!retType.isPrimitive())
119: out.println("cxt.postCreate(key);");
120: else {
121: out.print("Object okey = ");
122: out.printJavaTypeToObject("key", retType);
123: out.println(";");
124:
125: out.println("cxt.postCreate(okey);");
126: }
127:
128: // if (! _gen.isEntityCMP()) {
129: out
130: .println("if (__caucho_log.isLoggable(java.util.logging.Level.FINE))");
131: out
132: .println(" __caucho_log.fine(bean.__caucho_id + \":create(\" + key + \")\");");
133: out.println();
134:
135: out.println("bean._ejb_state = QEntity._CAUCHO_IS_LOADED;");
136: //println("bean._ejb_load_time = com.caucho.util.Alarm.getCurrentTime();");
137:
138: if (_postCreateCall != null)
139: _postCreateCall.generateCall(out, null, "bean", args);
140:
141: if (_isCMP) {
142: out.popDepth();
143: out.println("} catch (java.sql.SQLException e) {");
144: out
145: .println(" throw new com.caucho.ejb.CreateExceptionWrapper(e);");
146: out.println("}");
147: }
148: }
149: }
|