KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > entity > model > ModelUtil


1 /*
2  * $Id: ModelUtil.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.entity.model;
25
26 import java.io.*;
27
28 import org.ofbiz.base.util.*;
29
30 /**
31  * Generic Entity - General Utilities
32  *
33  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
34  * @version $Rev: 5462 $
35  * @since 2.0
36  */

37 public class ModelUtil {
38     
39     public static final String JavaDoc module = ModelUtil.class.getName();
40     
41     /**
42      * Changes the first letter of the passed String to upper case.
43      * @param string The passed String
44      * @return A String with an upper case first letter
45      */

46     public static String JavaDoc upperFirstChar(String JavaDoc string) {
47         if (string == null) return null;
48         if (string.length() <= 1) return string.toLowerCase();
49         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(string);
50
51         sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
52         return sb.toString();
53     }
54
55     /**
56      * Changes the first letter of the passed String to lower case.
57      *
58      * @param string The passed String
59      * @return A String with a lower case first letter
60      */

61     public static String JavaDoc lowerFirstChar(String JavaDoc string) {
62         if (string == null) return null;
63         if (string.length() <= 1) return string.toLowerCase();
64         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(string);
65
66         sb.setCharAt(0, Character.toLowerCase(sb.charAt(0)));
67         return sb.toString();
68     }
69
70     /** Converts a database name to a Java class name.
71      * The naming conventions used to allow for this are as follows: a database name (table or
72      * column) is in all capital letters, and the words are separated by an underscore
73      * (for example: NEAT_ENTITY_NAME or RANDOM_FIELD_NAME); a Java name (ejb or field) is in all
74      * lower case letters, except the letter at the beginning of each word (for example:
75      * NeatEntityName or RandomFieldName). The convention of using a capital letter at
76      * the beginning of a class name in Java, or a lower-case letter for the beginning of a
77      * variable name in Java is also used along with the Java name convention above.
78      * @param columnName The database name
79      * @return The Java class name
80      */

81     public static String JavaDoc dbNameToClassName(String JavaDoc columnName) {
82         return upperFirstChar(dbNameToVarName(columnName));
83     }
84
85     /** Converts a database name to a Java variable name.
86      * The naming conventions used to allow for this are as follows: a database name (table or
87      * column) is in all capital letters, and the words are separated by an underscore
88      * (for example: NEAT_ENTITY_NAME or RANDOM_FIELD_NAME); a Java name (ejb or field) is in all
89      * lower case letters, except the letter at the beginning of each word (for example:
90      * NeatEntityName or RandomFieldName). The convention of using a capital letter at
91      * the beginning of a class name in Java, or a lower-case letter for the beginning of a
92      * variable name in Java is also used along with the Java name convention above.
93      * @param columnName The database name
94      * @return The Java variable name
95      */

96     public static String JavaDoc dbNameToVarName(String JavaDoc columnName) {
97         if (columnName == null) return null;
98
99         StringBuffer JavaDoc fieldName = new StringBuffer JavaDoc(columnName.length());
100
101         boolean toUpper = false;
102         for (int i=0; i < columnName.length(); i++) {
103             char ch = columnName.charAt(i);
104             if (ch == '_') {
105                 toUpper = true;
106             } else if (toUpper) {
107                 fieldName.append(Character.toUpperCase(ch));
108                 toUpper = false;
109             } else {
110                 fieldName.append(Character.toLowerCase(ch));
111             }
112         }
113
114         return fieldName.toString();
115     }
116
117     /**
118      * Converts a Java variable name to a database name.
119      * The naming conventions used to allow for this are as follows: a database name (table or
120      * column) is in all capital letters, and the words are separated by an underscore
121      * (for example: NEAT_ENTITY_NAME or RANDOM_FIELD_NAME); a Java name (ejb or field) is in all
122      * lower case letters, except the letter at the beginning of each word (for example:
123      * NeatEntityName or RandomFieldName). The convention of using a capital letter at
124      * the beginning of a class name in Java, or a lower-case letter for the beginning of a
125      * variable name in Java is also used along with the Java name convention above.
126      * @param javaName The Java variable name
127      * @return The database name
128      */

129     public static String JavaDoc javaNameToDbName(String JavaDoc javaName) {
130         if (javaName == null) return null;
131         if (javaName.length() <= 0) return "";
132         StringBuffer JavaDoc dbName = new StringBuffer JavaDoc();
133
134         dbName.append(Character.toUpperCase(javaName.charAt(0)));
135         int namePos = 1;
136
137         while (namePos < javaName.length()) {
138             char curChar = javaName.charAt(namePos);
139
140             if (Character.isUpperCase(curChar)) dbName.append('_');
141             dbName.append(Character.toUpperCase(curChar));
142             namePos++;
143         }
144
145         return dbName.toString();
146     }
147
148     public static String JavaDoc vowelBag = "aeiouyAEIOUY";
149     /** Start by removing all vowels, then pull 1 letter at a time off the end of each _ separated segment, go until it is less than or equal to the desired length
150      *
151      * @param dbName
152      * @param desiredLength
153      * @return shortened String
154      */

155     public static String JavaDoc shortenDbName(String JavaDoc dbName, int desiredLength) {
156         StringBuffer JavaDoc dbBuf = new StringBuffer JavaDoc(dbName);
157         if (dbBuf.length() > desiredLength) {
158             // remove one vowel at a time, starting at beginning
159
for (int i = dbBuf.length() - 1; i > 0; i--) {
160                 // don't remove vowels that are at the beginning of the string (taken care of by the i > 0) or right after an underscore
161
if (dbBuf.charAt(i - 1) == '_') {
162                     continue;
163                 }
164                 
165                 char curChar = dbBuf.charAt(i);
166                 if (vowelBag.indexOf(curChar) > 0) {
167                     dbBuf.deleteCharAt(i);
168                 }
169             }
170         }
171         
172         // remove all double underscores
173
while (dbBuf.indexOf("__") > 0) {
174             dbBuf.deleteCharAt(dbBuf.indexOf("__"));
175         }
176         
177         while (dbBuf.length() > desiredLength) {
178             boolean removedChars = false;
179             
180             int usIndex = dbBuf.lastIndexOf("_");
181             while (usIndex > 0 && dbBuf.length() > desiredLength) {
182                 // if this is the first word in the group, don't pull letters off unless it is 4 letters or more
183
int prevUsIndex = dbBuf.lastIndexOf("_", usIndex - 1);
184                 if (prevUsIndex < 0 && usIndex < 4) {
185                     break;
186                 }
187                 
188                 // don't remove characters to reduce the size two less than three characters between underscores
189
if (prevUsIndex >= 0 && (usIndex - prevUsIndex) <= 4) {
190                     usIndex = prevUsIndex;
191                     continue;
192                 }
193                 
194                 // delete the second to last character instead of the last, better chance of being unique
195
dbBuf.deleteCharAt(usIndex - 2);
196                 removedChars = true;
197                 if (usIndex > 2) {
198                     usIndex = dbBuf.lastIndexOf("_", usIndex - 2);
199                 } else {
200                     break;
201                 }
202             }
203             
204             // now delete the char at the end of the string if necessary
205
if (dbBuf.length() > desiredLength) {
206                 int removeIndex = dbBuf.length() - 1;
207                 int prevRemoveIndex = dbBuf.lastIndexOf("_", removeIndex - 1);
208                 // don't remove characters to reduce the size two less than two characters between underscores
209
if (prevRemoveIndex < 0 || (removeIndex - prevRemoveIndex) >= 3) {
210                     // delete the second to last character instead of the last, better chance of being unique
211
dbBuf.deleteCharAt(removeIndex - 1);
212                     removedChars = true;
213                 }
214             }
215             
216             // remove all double underscores
217
while (dbBuf.indexOf("__") > 0) {
218                 dbBuf.deleteCharAt(dbBuf.indexOf("__"));
219                 removedChars = true;
220             }
221             
222             // if we didn't remove anything break out to avoid an infinite loop
223
if (!removedChars) {
224                 break;
225             }
226         }
227         
228         // remove all double underscores
229
while (dbBuf.indexOf("__") > 0) {
230             dbBuf.deleteCharAt(dbBuf.indexOf("__"));
231         }
232         
233         while (dbBuf.length() > desiredLength) {
234             // still not short enough, get more aggressive
235
// don't remove the first segment, just remove the second over and over until we are short enough
236
int firstUs = dbBuf.indexOf("_");
237             if (firstUs > 0) {
238                 int nextUs = dbBuf.indexOf("_", firstUs + 1);
239                 if (nextUs > 0) {
240                     //Debug.logInfo("couldn't shorten enough normally, removing second segment from " + dbBuf, module);
241
dbBuf.delete(firstUs, nextUs);
242                 }
243             }
244         }
245             
246         //Debug.logInfo("Shortened " + dbName + " to " + dbBuf.toString(), module);
247
return dbBuf.toString();
248     }
249
250     /**
251      * Converts a package name to a path by replacing all '.' characters with the File.separatorChar character.
252      * Is therefore platform independent.
253      * @param packageName The package name.
254      * @return The path name corresponding to the specified package name.
255      */

256     public static String JavaDoc packageToPath(String JavaDoc packageName) {
257         // just replace all of the '.' characters with the folder separater character
258
return packageName.replace('.', File.separatorChar);
259     }
260
261     /**
262      * Replaces all occurances of oldString in mainString with newString
263      * @param mainString The original string
264      * @param oldString The string to replace
265      * @param newString The string to insert in place of the old
266      * @return mainString with all occurances of oldString replaced by newString
267      */

268     public static String JavaDoc replaceString(String JavaDoc mainString, String JavaDoc oldString, String JavaDoc newString) {
269         return StringUtil.replaceString(mainString, oldString, newString);
270     }
271
272     public static String JavaDoc induceFieldType(String JavaDoc sqlTypeName, int length, int precision, ModelFieldTypeReader fieldTypeReader) {
273         if (sqlTypeName == null) return "invalid";
274
275         if (sqlTypeName.equalsIgnoreCase("VARCHAR") || sqlTypeName.equalsIgnoreCase("VARCHAR2") || (sqlTypeName.equalsIgnoreCase("CHAR") && length > 1)) {
276             if (length <= 10) return "very-short";
277             if (length <= 60) return "short-varchar";
278             if (length <= 255) return "long-varchar";
279             return "very-long";
280         } else if (sqlTypeName.equalsIgnoreCase("TEXT")) {
281             return "very-long";
282         } else if (sqlTypeName.equalsIgnoreCase("INT") || sqlTypeName.equalsIgnoreCase("SMALLINT") ||
283                 sqlTypeName.equalsIgnoreCase("DECIMAL") || sqlTypeName.equalsIgnoreCase("NUMERIC")) {
284             if (length > 18 || precision > 6) return "invalid-" + sqlTypeName + ":" + length + ":" + precision;
285             if (precision == 0) return "numeric";
286             if (precision == 2) return "currency-amount";
287             if (precision <= 6) return "floating-point";
288             return "invalid-" + sqlTypeName + ":" + length + ":" + precision;
289         } else if (sqlTypeName.equalsIgnoreCase("BLOB") || sqlTypeName.equalsIgnoreCase("OID")) {
290             return "blob";
291         } else if (sqlTypeName.equalsIgnoreCase("DATETIME") || sqlTypeName.equalsIgnoreCase("TIMESTAMP")) {
292             return "date-time";
293         } else if (sqlTypeName.equalsIgnoreCase("DATE")) {
294             return "date";
295         } else if (sqlTypeName.equalsIgnoreCase("TIME")) {
296             return "time";
297         } else if (sqlTypeName.equalsIgnoreCase("CHAR") && length == 1) {
298             return "indicator";
299         } else {
300             return "invalid-" + sqlTypeName + ":" + length + ":" + precision;
301         }
302     }
303 }
304
Popular Tags