KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > persistence > entitygenerator > EntityMember


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.j2ee.persistence.entitygenerator;
20
21 import org.netbeans.modules.dbschema.ColumnElement;
22 import org.netbeans.modules.j2ee.persistence.dd.JavaPersistenceQLKeywords;
23 import org.openide.util.*;
24
25 //TODO move static methods into a separate util class or into code generator
26

27 /**
28  * This class represents an instance of an member in an entity bean class. Each
29  * entity member has certain properties that are independant of the backing.
30  * These properties are represented by abstract methods in this class. This
31  * class also serves as the abstract factory for creation of concrete backed
32  * instances of this class.
33  * @author Christopher Webster
34  */

35 public abstract class EntityMember {
36     
37     /**
38      * Name to use for member in bean and primary key class
39      */

40     private String JavaDoc memberName;
41     
42     /**
43      * Class to use for member in bean and primary key class
44      */

45     private String JavaDoc memberClass;
46     
47     /**
48      * Provide a mapping algorithm from an arbitrary string into a java
49      * recommend field name (of the form lllUlll, where l = lower case U =
50      * uppercase). The algorithm adds capitalization where a non alpha numeric
51      * character appears. If the resultant field name would be a reserved java
52      * identifier an integer will be appended to cause the result not to be a
53      * reserved word.
54      * @param fieldName name of field to transform
55      */

56     public static String JavaDoc makeFieldName(String JavaDoc fieldName) {
57         StringBuilder JavaDoc retName = makeName(fieldName);
58         retName.setCharAt(0, Character.toLowerCase(retName.charAt(0)));
59         String JavaDoc returnS = retName.toString();
60         if (!Utilities.isJavaIdentifier(returnS)) {
61             returnS += '1';
62         }
63         return returnS;
64     }
65
66     /**
67      * Provide a mapping algorithm from an arbitrary string into a java
68      * recommend relationship field name (of the form lllUlll, where l =
69      * lower case U = uppercase). The algorithm adds a suffix of "Collection"
70      * if the second parameter is true. In any case, it adds capitalization
71      * where a non alpha numeric character appears. If the resultant field
72      * name would be a reserved java identifier an integer will be appended
73      * to cause the result not to be a reserved word.
74      * @param fieldName name of field to transform
75      * @param isCollection <code>true</code> if the relationship is a
76      * a collection, <code>false</code> otherwise
77      */

78     public static String JavaDoc makeRelationshipFieldName(String JavaDoc fieldName,
79             boolean isCollection) {
80         if (isCollection){
81             fieldName += "Collection"; // NOI18N
82
}
83         return makeFieldName(fieldName);
84     }
85
86     private static StringBuilder JavaDoc makeName(String JavaDoc fieldName) {
87         if (fieldName == null || fieldName.length() == 0) {
88             fieldName = "a"; //NOI18N
89
}
90         
91         if (!Character.isLetter(fieldName.charAt(0))) {
92             StringBuilder JavaDoc removed = new StringBuilder JavaDoc(fieldName);
93             while (removed.length() > 0 &&
94                     !Character.isLetter(removed.charAt(0))) {
95                 removed.deleteCharAt(0);
96             }
97             return makeName(removed.toString());
98         }
99         
100         String JavaDoc lower = fieldName.toLowerCase();
101         String JavaDoc upper = fieldName.toUpperCase();
102         boolean mixedCase = !(fieldName.equals(lower) ||
103                 fieldName.equals(upper));
104         
105         return mapName(new StringBuilder JavaDoc(mixedCase?fieldName:lower),
106                 !mixedCase);
107     }
108     
109     /**
110      * Provide a mapping algorithm from an arbitrary string into a java
111      * recommend class name (of the form UllUlll, where l = lower case U =
112      * uppercase). The algorithm adds capitalization where a non alpha numeric
113      * character appears.
114      * @param className name of class to transform
115      */

116     public static String JavaDoc makeClassName(String JavaDoc className) {
117         StringBuilder JavaDoc fieldName = makeName(className);
118         if (JavaPersistenceQLKeywords.isKeyword(fieldName.toString())) {
119             fieldName.append('1');
120         }
121         fieldName.setCharAt(0, Character.toUpperCase(fieldName.charAt(0)));
122         return fieldName.toString();
123     }
124     
125     private static StringBuilder JavaDoc mapName(StringBuilder JavaDoc mappedName,
126             boolean convertUpper) {
127         int i = 0;
128         while (i < mappedName.length()) {
129             if (!Character.isLetterOrDigit(mappedName.charAt(i))) {
130                 if (convertUpper && ((i+1) < mappedName.length())) {
131                     mappedName.setCharAt(i+1,
132                             Character.toUpperCase(mappedName.charAt(i+1)));
133                 }
134                 mappedName.deleteCharAt(i);
135             } else {
136                 i++;
137             }
138         }
139         
140         return mappedName;
141     }
142     
143     /**
144      * Create new instance of Entity Member given a column element
145      * @param columnElement column element to populate Entity Member using
146      * @throws IllegalArgumentException if columnElement == null
147      */

148     public static EntityMember create(ColumnElement columnElement) {
149         if (columnElement == null) {
150             throw new IllegalArgumentException JavaDoc("columnElement == null");//NOI18N
151
}
152         return new DbSchemaEntityMember(columnElement);
153     }
154     
155     /**
156      * Return name to use for member variable in EJB classes.
157      * @return name to use for member variable in EJB classes
158      */

159     public String JavaDoc getMemberName() {
160         return memberName;
161     }
162     
163     /**
164      * set name to use for member variable in EJB classes
165      * @param name to use for member variable
166      * @throws IllegalArgumentException if name is not valid java identifier
167      */

168     public void setMemberName(String JavaDoc name) {
169         if (!Utilities.isJavaIdentifier(name)) {
170             throw new IllegalArgumentException JavaDoc("isJavaIdentifier()==false"); //NOI18N
171
}
172         
173         memberName = name;
174     }
175     
176     /**
177      * Return full qualified type name to use for member declaration
178      * @return type to use for member declaration
179      */

180     public String JavaDoc getMemberType() {
181         return memberClass;
182     }
183     
184     /**
185      * Set class to use for member declaration.
186      * @param aType to use for member declaration
187      * @throws IllegalArgumentException if aClass == null
188      */

189     public void setMemberType(String JavaDoc aType) {
190         if (aType == null) {
191             throw new IllegalArgumentException JavaDoc("aType == null"); //NOI18N
192
}
193         memberClass = aType;
194     }
195
196     /**
197      * Determine if this member is a large object type.
198      * @return true if member is a large object type
199      */

200     public abstract boolean isLobType();
201
202     /**
203      * Determine if this member is part of the primary key.
204      * @return true if member is part of the primary key
205      */

206     public abstract boolean isPrimaryKey();
207     
208     public abstract void setPrimaryKey(boolean isPk, boolean isPkField);
209     
210     /**
211      * @return true if underlying type supports finder equal queries
212      */

213     public abstract boolean supportsFinder();
214     
215     /**
216      * override java.lang.Object#equals based on member name.
217      */

218     public boolean equals(Object JavaDoc other) {
219         if (other == null || !(other.getClass().isInstance(getClass()))) {
220             return false;
221         }
222         return ((EntityMember) other).getMemberName().equals(getMemberName());
223     }
224     
225     /**
226      * override java.lang.Object#hashCode
227      */

228     public int hashCode() {
229         return getMemberName().hashCode();
230     }
231     
232     
233     public abstract boolean isNullable();
234     public abstract String JavaDoc getColumnName();
235     public abstract String JavaDoc getTableName();
236 }
237
238
239
240
Popular Tags