KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.ejb.cfg;
30
31 import com.caucho.amber.field.IdField;
32 import com.caucho.amber.manager.AmberPersistenceUnit;
33 import com.caucho.amber.type.EntityType;
34 import com.caucho.bytecode.JClass;
35 import com.caucho.bytecode.JMethod;
36 import com.caucho.config.ConfigException;
37 import com.caucho.util.CharBuffer;
38 import com.caucho.util.L10N;
39
40 /**
41  * Configuraton for a cmp-field.
42  */

43 public class CmpProperty {
44   private static final L10N L = new L10N(CmpProperty.class);
45
46   private EjbEntityBean _entity;
47
48   private String JavaDoc _location;
49
50   private String JavaDoc _name;
51   private String JavaDoc _description;
52
53   private boolean _isId;
54
55   /**
56    * Creates a new cmp-field
57    *
58    * @param entity the owning entity bean
59    */

60   public CmpProperty(EjbEntityBean entity)
61   {
62     _entity = entity;
63   }
64
65   /**
66    * Returns the entity.
67    */

68   public EjbEntityBean getEntity()
69   {
70     return _entity;
71   }
72
73   /**
74    * Returns the entity.
75    */

76   public EjbEntityBean getBean()
77   {
78     return _entity;
79   }
80
81   /**
82    * Sets the location.
83    */

84   public void setConfigLocation(String JavaDoc filename, int line)
85   {
86     _location = filename + ":" + line + ": ";
87   }
88
89   /**
90    * Sets the location.
91    */

92   public void setLocation(String JavaDoc location)
93   {
94     if (location != null)
95       _location = location;
96   }
97
98   /**
99    * Returns the location of the cmp-field in the config file.
100    */

101   public String JavaDoc getLocation()
102   {
103     if (_location != null)
104       return _location;
105     else
106       return _entity.getLocation();
107   }
108
109   /**
110    * Sets the cmp-field name.
111    */

112   public void setFieldName(String JavaDoc name)
113   {
114     _name = name;
115   }
116
117   /**
118    * Returns the cmp-field name.
119    */

120   public String JavaDoc getName()
121   {
122     return _name;
123   }
124
125   /**
126    * Sets true for an id.
127    */

128   public void setId(boolean id)
129   {
130     _isId = id;
131   }
132
133   /**
134    * Gets true for an id.
135    */

136   public boolean isId()
137   {
138     return _isId;
139   }
140
141   /**
142    * Sets the description.
143    */

144   public void setDescription(String JavaDoc description)
145   {
146     _description = description;
147   }
148
149   /**
150    * Returns the getter.
151    */

152   public JMethod getGetter()
153   {
154     String JavaDoc methodName = ("get" +
155              Character.toUpperCase(_name.charAt(0)) +
156              _name.substring(1));
157     
158     return _entity.getMethod(methodName, new JClass[0]);
159   }
160
161   /**
162    * Returns the setter.
163    */

164   public JMethod getSetter()
165   {
166     String JavaDoc methodName = ("set" +
167              Character.toUpperCase(_name.charAt(0)) +
168              _name.substring(1));
169
170     JMethod getter = getGetter();
171
172     if (getter != null)
173       return _entity.getMethod(methodName,
174                    new JClass[] { getter.getReturnType() });
175     else
176       return null;
177   }
178
179   /**
180    * Amber creating the id field.
181    */

182   public IdField createId(AmberPersistenceUnit amberPersistenceUnit, EntityType type)
183     throws ConfigException
184   {
185     throw new UnsupportedOperationException JavaDoc(getClass().getName());
186   }
187
188   static String JavaDoc toSqlName(String JavaDoc name)
189   {
190     CharBuffer cb = new CharBuffer();
191
192     for (int i = 0; i < name.length(); i++) {
193       char ch = name.charAt(i);
194
195       if (! Character.isUpperCase(ch))
196     cb.append(ch);
197       else if (i > 0 && ! Character.isUpperCase(name.charAt(i - 1))) {
198     cb.append("_");
199     cb.append(Character.toLowerCase(ch));
200       }
201       else if (i + 1 < name.length() &&
202            ! Character.isUpperCase(name.charAt(i + 1))) {
203     cb.append("_");
204     cb.append(Character.toLowerCase(ch));
205       }
206       else
207     cb.append(Character.toLowerCase(ch));
208     }
209
210     return cb.toString();
211   }
212
213   public String JavaDoc toString()
214   {
215     return "CmpProperty[" + _name + "]";
216   }
217 }
218
Popular Tags