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: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.ejb.cfg21;
030:
031: import com.caucho.ejb.cfg.*;
032: import com.caucho.amber.field.IdField;
033: import com.caucho.amber.manager.AmberPersistenceUnit;
034: import com.caucho.amber.type.EntityType;
035: import com.caucho.config.ConfigException;
036: import com.caucho.util.CharBuffer;
037: import com.caucho.util.L10N;
038:
039: /**
040: * Configuraton for a cmp-field.
041: */
042: public class CmpProperty {
043: private static final L10N L = new L10N(CmpProperty.class);
044:
045: private EjbEntityBean _entity;
046:
047: private String _location;
048:
049: private String _name;
050: private String _description;
051:
052: private boolean _isId;
053:
054: /**
055: * Creates a new cmp-field
056: *
057: * @param entity the owning entity bean
058: */
059: public CmpProperty(EjbEntityBean entity) {
060: _entity = entity;
061: }
062:
063: /**
064: * Returns the entity.
065: */
066: public EjbEntityBean getEntity() {
067: return _entity;
068: }
069:
070: /**
071: * Returns the entity.
072: */
073: public EjbEntityBean getBean() {
074: return _entity;
075: }
076:
077: /**
078: * Sets the location.
079: */
080: public void setConfigLocation(String filename, int line) {
081: _location = filename + ":" + line + ": ";
082: }
083:
084: /**
085: * Sets the location.
086: */
087: public void setLocation(String location) {
088: if (location != null)
089: _location = location;
090: }
091:
092: /**
093: * Returns the location of the cmp-field in the config file.
094: */
095: public String getLocation() {
096: if (_location != null)
097: return _location;
098: else
099: return _entity.getLocation();
100: }
101:
102: /**
103: * Sets the cmp-field name.
104: */
105: public void setFieldName(String name) {
106: _name = name;
107: }
108:
109: /**
110: * Returns the cmp-field name.
111: */
112: public String getName() {
113: return _name;
114: }
115:
116: /**
117: * Sets true for an id.
118: */
119: public void setId(boolean id) {
120: _isId = id;
121: }
122:
123: /**
124: * Gets true for an id.
125: */
126: public boolean isId() {
127: return _isId;
128: }
129:
130: /**
131: * Sets the description.
132: */
133: public void setDescription(String description) {
134: _description = description;
135: }
136:
137: /**
138: * Returns the getter.
139: */
140: public ApiMethod getGetter() {
141: String methodName = ("get"
142: + Character.toUpperCase(_name.charAt(0)) + _name
143: .substring(1));
144:
145: return _entity.getMethod(methodName, new Class[0]);
146: }
147:
148: /**
149: * Returns the setter.
150: */
151: public ApiMethod getSetter() {
152: String methodName = ("set"
153: + Character.toUpperCase(_name.charAt(0)) + _name
154: .substring(1));
155:
156: ApiMethod getter = getGetter();
157:
158: if (getter != null)
159: return _entity.getMethod(methodName, new Class[] { getter
160: .getReturnType() });
161: else
162: return null;
163: }
164:
165: /**
166: * Amber creating the id field.
167: */
168: public IdField createId(AmberPersistenceUnit amberPersistenceUnit,
169: EntityType type) throws ConfigException {
170: throw new UnsupportedOperationException(getClass().getName());
171: }
172:
173: static String toSqlName(String name) {
174: CharBuffer cb = new CharBuffer();
175:
176: for (int i = 0; i < name.length(); i++) {
177: char ch = name.charAt(i);
178:
179: if (!Character.isUpperCase(ch))
180: cb.append(ch);
181: else if (i > 0
182: && !Character.isUpperCase(name.charAt(i - 1))) {
183: cb.append("_");
184: cb.append(Character.toLowerCase(ch));
185: } else if (i + 1 < name.length()
186: && !Character.isUpperCase(name.charAt(i + 1))) {
187: cb.append("_");
188: cb.append(Character.toLowerCase(ch));
189: } else
190: cb.append(Character.toLowerCase(ch));
191: }
192:
193: return cb.toString();
194: }
195:
196: public String toString() {
197: return "CmpProperty[" + _name + "]";
198: }
199: }
|