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.ejb.cfg21.CmpProperty;
034: import com.caucho.ejb.cfg21.CmpGetter;
035: import com.caucho.amber.field.AmberField;
036: import com.caucho.amber.type.EntityType;
037: import com.caucho.config.ConfigException;
038: import com.caucho.ejb.gen21.EntityBean;
039: import com.caucho.java.JavaWriter;
040: import com.caucho.util.L10N;
041:
042: import javax.ejb.EJBLocalObject;
043: import java.io.IOException;
044: import java.util.Collection;
045: import java.util.Map;
046:
047: /**
048: * Abstract relation.
049: */
050: public class CmrRelation extends CmpProperty {
051: private static final L10N L = new L10N(CmrRelation.class);
052:
053: private String _relationName;
054:
055: private String _fieldName;
056: private boolean _hasGetter;
057:
058: private CmrRelation _targetRelation;
059:
060: /**
061: * Creates a new cmp-relation
062: */
063: public CmrRelation(EjbEntityBean entityBean) throws ConfigException {
064: super (entityBean);
065: }
066:
067: /**
068: * Creates a new cmp-relation
069: */
070: public CmrRelation(EjbEntityBean entityBean, String fieldName)
071: throws ConfigException {
072: super (entityBean);
073:
074: setFieldName(fieldName);
075:
076: ApiMethod getter = getGetter();
077:
078: if (!getter.isAbstract() && !entityBean.isAllowPOJO())
079: throw new ConfigException(
080: L
081: .l(
082: "{0}: '{1}' must have an abstract getter method. cmr-relations must have abstract getter methods returning a local interface.",
083: entityBean.getEJBClass().getName(),
084: getter.getFullName()));
085:
086: Class retType = getter.getReturnType();
087:
088: if (!EJBLocalObject.class.isAssignableFrom(retType)
089: && !Collection.class.isAssignableFrom(retType)
090: && !Map.class.isAssignableFrom(retType)) {
091: throw new ConfigException(
092: L
093: .l(
094: "{0}: '{1}' must return an EJBLocalObject or a Collection. cmr-relations must have abstract getter methods returning a local interface.",
095: entityBean.getEJBClass().getName(),
096: getter.getFullName()));
097: }
098:
099: ApiMethod setter = getSetter();
100:
101: if (setter == null)
102: return;
103:
104: Class[] paramTypes = setter.getParameterTypes();
105:
106: if (!retType.equals(paramTypes[0]))
107: throw new ConfigException(
108: L
109: .l(
110: "{0}: '{1}' must return an '{2}'. Persistent setters must match the getter types .",
111: entityBean.getEJBClass().getName(),
112: setter.getFullName(), retType
113: .getName()));
114: }
115:
116: /**
117: * Returns the relation name.
118: */
119: public String getRelationName() {
120: return _relationName;
121: }
122:
123: /**
124: * Sets the relation name.
125: */
126: public void setRelationName(String name) {
127: _relationName = name;
128: }
129:
130: /**
131: * Returns the target bean
132: */
133: public EjbEntityBean getTargetBean() {
134: return null;
135: }
136:
137: /**
138: * Returns the target type.
139: */
140: public ApiClass getTargetType() {
141: throw new UnsupportedOperationException(getClass().getName());
142: }
143:
144: /**
145: * Sets a target relation.
146: */
147: public void setTarget(CmrRelation target) {
148: throw new UnsupportedOperationException(getClass().getName());
149: }
150:
151: /**
152: * Returns true for a collection.
153: */
154: public boolean isCollection() {
155: return false;
156: }
157:
158: /**
159: * Sets the paired target relation.
160: */
161: public void setTargetRelation(CmrRelation target) {
162: _targetRelation = target;
163: }
164:
165: /**
166: * Gets the paired target relation.
167: */
168: public CmrRelation getTargetRelation() {
169: return _targetRelation;
170: }
171:
172: /**
173: * Link amber.
174: */
175: public void linkAmber() throws ConfigException {
176: }
177:
178: /**
179: * Assemble any bean methods.
180: */
181: public void assemble(EntityBean bean) {
182:
183: }
184:
185: /**
186: * Set true for having a getter.
187: */
188: public void setHasGetter(boolean hasGetter) {
189: _hasGetter = true;
190: }
191:
192: /**
193: * Set true for having a getter.
194: */
195: public boolean getHasGetter() {
196: return _hasGetter;
197: }
198:
199: /**
200: * Create any bean methods.
201: */
202: public EjbMethod createGetter(EjbView view, ApiMethod apiMethod,
203: ApiMethod implMethod) throws ConfigException {
204: return new CmpGetter(view, apiMethod, implMethod);
205: }
206:
207: /**
208: * Creates the amber type.
209: */
210: public AmberField assembleAmber(EntityType type)
211: throws ConfigException {
212: throw new UnsupportedOperationException(getClass().getName());
213: }
214:
215: /**
216: * Generates the after commit code.
217: */
218: public void generateAfterCommit(JavaWriter out) throws IOException {
219: }
220:
221: /**
222: * Generates the destroy method.
223: */
224: public void generateDestroy(JavaWriter out) throws IOException {
225: }
226:
227: public String toString() {
228: return "CmrRelation[" + getName() + "]";
229: }
230: }
|