KickJava   Java API By Example, From Geeks To Geeks.

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


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.field.AmberField;
33 import com.caucho.amber.field.EntityMapField;
34 import com.caucho.amber.field.IdField;
35 import com.caucho.amber.type.EntityType;
36 import com.caucho.bytecode.JMethod;
37 import com.caucho.config.ConfigException;
38 import com.caucho.util.L10N;
39
40 /**
41  * map relation
42  */

43 public class CmrMap extends CmrRelation {
44   private static final L10N L = new L10N(CmrMap.class);
45
46   private EjbEntityBean _targetBean;
47   private CmrManyToOne _idRel;
48   private JMethod _mapMethod;
49
50   /**
51    * Creates a new cmr map
52    */

53   public CmrMap(EjbEntityBean sourceBean,
54         String JavaDoc fieldName,
55         EjbEntityBean targetBean,
56         CmrManyToOne idRel)
57     throws ConfigException
58   {
59     super(sourceBean);
60
61     setFieldName(fieldName);
62
63     _targetBean = targetBean;
64     _idRel = idRel;
65   }
66
67   /**
68    * Sets the map method.
69    */

70   public void setMapMethod(JMethod method)
71   {
72     _mapMethod = method;
73   }
74
75   /**
76    * Gets the map method.
77    */

78   public JMethod getMapMethod()
79   {
80     return _mapMethod;
81   }
82
83   /**
84    * Returns the index name.
85    */

86   public String JavaDoc getIndexName()
87   {
88     EntityType type = _targetBean.getEntityType();
89
90     for (IdField key : type.getId().getKeys()) {
91       if (! key.getName().equals(_idRel.getName()))
92     return key.getName();
93     }
94     
95     throw new IllegalStateException JavaDoc();
96   }
97
98   /**
99    * Returns the rel name.
100    */

101   public String JavaDoc getIdName()
102   {
103     return _idRel.getName();
104   }
105
106   /**
107    * Returns the target bean.
108    */

109   public EjbEntityBean getTargetBean()
110   {
111     return _targetBean;
112   }
113
114   /**
115    * Create any bean methods.
116    */

117   public EjbMethod createGetter(EjbView view,
118                 JMethod apiMethod,
119                 JMethod implMethod)
120     throws ConfigException
121   {
122     return new EjbMapMethod(view, apiMethod, implMethod, this);
123   }
124
125   /**
126    * Creates the amber type.
127    */

128   public AmberField assembleAmber(EntityType type)
129     throws ConfigException
130   {
131     EntityMapField field = new EntityMapField(type);
132
133     field.setName(getName());
134     field.setMapMethod(_mapMethod);
135
136     field.setTargetType(_targetBean.getEntityType());
137
138     EntityType sourceType = _targetBean.getEntityType();
139     for (IdField key : sourceType.getId().getKeys()) {
140       if (key.getName().equals(_idRel.getName())) {
141     field.setId(key);
142       }
143       else {
144     field.setIndex(key);
145       }
146     }
147     
148
149     return field;
150   }
151 }
152
Popular Tags