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.amber.manager.AmberPersistenceUnit;
034: import com.caucho.amber.type.Type;
035: import com.caucho.bytecode.JClassWrapper;
036: import com.caucho.config.ConfigException;
037: import com.caucho.ejb.gen21.AbstractQueryMethod;
038: import com.caucho.ejb.gen21.AmberSelectCollectionMethod;
039: import com.caucho.ejb.gen21.AmberSelectMethod;
040: import com.caucho.ejb.gen21.BeanAssembler;
041: import com.caucho.ejb.manager.EjbContainer;
042: import com.caucho.ejb.ql.EjbSelectQuery;
043: import com.caucho.ejb.ql.QLParser;
044: import com.caucho.java.gen.BaseMethod;
045: import com.caucho.util.L10N;
046:
047: import javax.ejb.EJBLocalObject;
048: import java.util.Collection;
049:
050: /**
051: * Configuration for find method.
052: */
053: public class EjbAmberSelectMethod extends EjbBaseMethod {
054: private static final L10N L = new L10N(EjbAmberSelectMethod.class);
055:
056: private String _query;
057: private String _location;
058:
059: private boolean _queryLoadsBean;
060:
061: /**
062: * Creates a new method.
063: *
064: * @param view the owning view
065: * @param apiMethod the method from the view
066: * @param implMethod the method from the implementation
067: */
068: public EjbAmberSelectMethod(EjbEntityBean bean, ApiMethod method,
069: String query, String location) throws ConfigException {
070: super (bean, method);
071:
072: _query = query;
073: _location = location;
074: }
075:
076: /**
077: * Set true if the query should load the bean.
078: */
079: public void setQueryLoadsBean(boolean queryLoadsBean) {
080: _queryLoadsBean = queryLoadsBean;
081: }
082:
083: /**
084: * Assembles the method.
085: */
086: public BaseMethod assemble(BeanAssembler assembler,
087: String fullClassName) throws ConfigException {
088: ApiMethod method = getMethod();
089:
090: QLParser parser = new QLParser((EjbEntityBean) getBean(),
091: method.getName(), method, method.getReturnType());
092:
093: parser.setLocation(_location);
094:
095: EjbSelectQuery query = (EjbSelectQuery) parser
096: .parseQuery(_query);
097:
098: String returnEJB = parser.getReturnEJB();
099: Class retType = method.getReturnType();
100:
101: EjbConfig ejbConfig = getBean().getConfig();
102: EjbContainer ejbManager = ejbConfig.getEjbContainer();
103: EjbEntityBean retBean = null;
104:
105: if (returnEJB != null)
106: retBean = (EjbEntityBean) ejbConfig
107: .getBeanConfig(returnEJB);
108:
109: AmberPersistenceUnit amberPersistenceUnit = ejbManager
110: .createEjbPersistenceUnit();
111:
112: Type amberType = null;
113:
114: if (returnEJB != null) {
115: amberType = amberPersistenceUnit.getEntityType(retBean
116: .getAbstractSchemaName());
117:
118: if (amberType == null)
119: throw new NullPointerException("No amber entity for "
120: + returnEJB);
121: } else if (EJBLocalObject.class.isAssignableFrom(retType)) {
122: EjbEntityBean targetBean = getBean().getConfig()
123: .findEntityByLocal(retType);
124:
125: Class queryRetType = parser.getSelectExpr().getJavaType();
126:
127: if (queryRetType != null
128: && !Object.class.equals(queryRetType)
129: && !EJBLocalObject.class.equals(queryRetType)) {
130: throw new ConfigException(L.l(
131: "Mismatched return type '{0}' in\n{1}", retType
132: .getName(), _query));
133: }
134:
135: amberType = amberPersistenceUnit.getEntityType(targetBean
136: .getAbstractSchemaName());
137: } else if (!Collection.class.isAssignableFrom(retType))
138: amberType = amberPersistenceUnit.createType(JClassWrapper
139: .create(retType));
140:
141: AbstractQueryMethod queryMethod;
142:
143: if (Collection.class.isAssignableFrom(retType))
144: queryMethod = new AmberSelectCollectionMethod(
145: (EjbEntityBean) getBean(), getMethod(),
146: fullClassName, query);
147: else
148: queryMethod = new AmberSelectMethod(
149: (EjbEntityBean) getBean(), method, fullClassName,
150: query, amberType);
151:
152: queryMethod.setQueryLoadsBean(_queryLoadsBean);
153:
154: return queryMethod;
155: }
156: }
|