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.CmrManyToOne;
034: import com.caucho.amber.field.AmberField;
035: import com.caucho.amber.field.EntityMapField;
036: import com.caucho.amber.field.IdField;
037: import com.caucho.amber.type.EntityType;
038: import com.caucho.bytecode.JMethodWrapper;
039: import com.caucho.config.ConfigException;
040: import com.caucho.util.L10N;
041:
042: /**
043: * map relation
044: */
045: public class CmrMap extends CmrRelation {
046: private static final L10N L = new L10N(CmrMap.class);
047:
048: private EjbEntityBean _targetBean;
049: private CmrManyToOne _idRel;
050: private ApiMethod _mapMethod;
051:
052: /**
053: * Creates a new cmr map
054: */
055: public CmrMap(EjbEntityBean sourceBean, String fieldName,
056: EjbEntityBean targetBean, CmrManyToOne idRel)
057: throws ConfigException {
058: super (sourceBean);
059:
060: setFieldName(fieldName);
061:
062: _targetBean = targetBean;
063: _idRel = idRel;
064: }
065:
066: /**
067: * Sets the map method.
068: */
069: public void setMapMethod(ApiMethod method) {
070: _mapMethod = method;
071: }
072:
073: /**
074: * Gets the map method.
075: */
076: public ApiMethod getMapMethod() {
077: return _mapMethod;
078: }
079:
080: /**
081: * Returns the index name.
082: */
083: public String getIndexName() {
084: EntityType type = _targetBean.getEntityType();
085:
086: for (IdField key : type.getId().getKeys()) {
087: if (!key.getName().equals(_idRel.getName()))
088: return key.getName();
089: }
090:
091: throw new IllegalStateException();
092: }
093:
094: /**
095: * Returns the rel name.
096: */
097: public String getIdName() {
098: return _idRel.getName();
099: }
100:
101: /**
102: * Returns the target bean.
103: */
104: public EjbEntityBean getTargetBean() {
105: return _targetBean;
106: }
107:
108: /**
109: * Create any bean methods.
110: */
111: public EjbMethod createGetter(EjbView view, ApiMethod apiMethod,
112: ApiMethod implMethod) throws ConfigException {
113: return new EjbMapMethod(view, apiMethod, implMethod, this );
114: }
115:
116: /**
117: * Creates the amber type.
118: */
119: public AmberField assembleAmber(EntityType type)
120: throws ConfigException {
121: EntityMapField field = new EntityMapField(type);
122:
123: field.setName(getName());
124: field.setMapMethod(new JMethodWrapper(_mapMethod.getMethod()));
125:
126: field.setTargetType(_targetBean.getEntityType());
127:
128: EntityType sourceType = _targetBean.getEntityType();
129: for (IdField key : sourceType.getId().getKeys()) {
130: if (key.getName().equals(_idRel.getName())) {
131: field.setId(key);
132: } else {
133: field.setIndex(key);
134: }
135: }
136:
137: return field;
138: }
139: }
|