KickJava   Java API By Example, From Geeks To Geeks.

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


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.amber.manager.AmberPersistenceUnit;
33 import com.caucho.amber.type.Type;
34 import com.caucho.bytecode.JClass;
35 import com.caucho.bytecode.JMethod;
36 import com.caucho.config.ConfigException;
37 import com.caucho.ejb.EjbServerManager;
38 import com.caucho.ejb.gen.AbstractQueryMethod;
39 import com.caucho.ejb.gen.AmberSelectCollectionMethod;
40 import com.caucho.ejb.gen.AmberSelectMethod;
41 import com.caucho.ejb.gen.BeanAssembler;
42 import com.caucho.ejb.ql.EjbSelectQuery;
43 import com.caucho.ejb.ql.QLParser;
44 import com.caucho.java.gen.BaseMethod;
45 import com.caucho.util.L10N;
46
47 import javax.ejb.EJBLocalObject JavaDoc;
48 import java.util.Collection JavaDoc;
49
50 /**
51  * Configuration for find method.
52  */

53 public class EjbAmberSelectMethod extends EjbBaseMethod {
54   private static final L10N L = new L10N(EjbAmberSelectMethod.class);
55
56   private String JavaDoc _query;
57   private String JavaDoc _location;
58
59   private boolean _queryLoadsBean;
60   
61   /**
62    * Creates a new method.
63    *
64    * @param view the owning view
65    * @param apiMethod the method from the view
66    * @param implMethod the method from the implementation
67    */

68   public EjbAmberSelectMethod(EjbEntityBean bean, JMethod method,
69                   String JavaDoc query, String JavaDoc location)
70     throws ConfigException
71   {
72     super(bean, method);
73
74     _query = query;
75     _location = location;
76   }
77
78   /**
79    * Set true if the query should load the bean.
80    */

81   public void setQueryLoadsBean(boolean queryLoadsBean)
82   {
83     _queryLoadsBean = queryLoadsBean;
84   }
85
86   /**
87    * Assembles the method.
88    */

89   public BaseMethod assemble(BeanAssembler assembler, String JavaDoc fullClassName)
90     throws ConfigException
91   {
92     JMethod method = getMethod();
93
94     QLParser parser = new QLParser((EjbEntityBean) getBean(),
95                     method.getName(), method,
96                     method.getReturnType());
97
98     parser.setLocation(_location);
99
100     EjbSelectQuery query = (EjbSelectQuery) parser.parseQuery(_query);
101
102     String JavaDoc returnEJB = parser.getReturnEJB();
103     JClass retType = method.getReturnType();
104
105     EjbConfig ejbConfig = getBean().getConfig();
106     EjbServerManager ejbManager = ejbConfig.getEJBManager();
107     EjbEntityBean retBean = (EjbEntityBean) ejbConfig.getBeanConfig(returnEJB);
108     
109     AmberPersistenceUnit amberPersistenceUnit = ejbManager.getAmberManager();
110
111     Type amberType = null;
112
113     if (returnEJB != null) {
114       amberType = amberPersistenceUnit.getEntity(retBean.getAbstractSchemaName());
115
116       if (amberType == null)
117     throw new NullPointerException JavaDoc("No amber entity for " + returnEJB);
118     }
119     else if (retType.isAssignableTo(EJBLocalObject JavaDoc.class)) {
120       EjbEntityBean targetBean = getBean().getConfig().findEntityByLocal(retType);
121
122       JClass queryRetType = parser.getSelectExpr().getJavaType();
123       
124       if (queryRetType != null &&
125       ! queryRetType.getName().equals("java.lang.Object") &&
126       ! queryRetType.isAssignableTo(EJBLocalObject JavaDoc.class)) {
127     throw new ConfigException(L.l("Mismatched return type '{0}' in\n{1}",
128                       retType.getName(), _query));
129       }
130
131       amberType = amberPersistenceUnit.getEntity(targetBean.getAbstractSchemaName());
132     }
133     else if (! retType.isAssignableTo(Collection JavaDoc.class))
134       amberType = amberPersistenceUnit.createType(retType);
135
136     AbstractQueryMethod queryMethod;
137     
138     if (retType.isAssignableTo(Collection JavaDoc.class))
139       queryMethod = new AmberSelectCollectionMethod((EjbEntityBean) getBean(),
140                             getMethod(),
141                             fullClassName,
142                             query);
143     else
144       queryMethod = new AmberSelectMethod((EjbEntityBean) getBean(),
145                        method,
146                        fullClassName,
147                        query,
148                        amberType);
149
150     queryMethod.setQueryLoadsBean(_queryLoadsBean);
151
152     return queryMethod;
153   }
154 }
155
Popular Tags