1 24 package org.ofbiz.entity.model; 25 26 import java.io.*; 27 28 import org.ofbiz.base.util.*; 29 30 37 public class ModelUtil { 38 39 public static final String module = ModelUtil.class.getName(); 40 41 46 public static String upperFirstChar(String string) { 47 if (string == null) return null; 48 if (string.length() <= 1) return string.toLowerCase(); 49 StringBuffer sb = new StringBuffer (string); 50 51 sb.setCharAt(0, Character.toUpperCase(sb.charAt(0))); 52 return sb.toString(); 53 } 54 55 61 public static String lowerFirstChar(String string) { 62 if (string == null) return null; 63 if (string.length() <= 1) return string.toLowerCase(); 64 StringBuffer sb = new StringBuffer (string); 65 66 sb.setCharAt(0, Character.toLowerCase(sb.charAt(0))); 67 return sb.toString(); 68 } 69 70 81 public static String dbNameToClassName(String columnName) { 82 return upperFirstChar(dbNameToVarName(columnName)); 83 } 84 85 96 public static String dbNameToVarName(String columnName) { 97 if (columnName == null) return null; 98 99 StringBuffer fieldName = new StringBuffer (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 129 public static String javaNameToDbName(String javaName) { 130 if (javaName == null) return null; 131 if (javaName.length() <= 0) return ""; 132 StringBuffer dbName = new StringBuffer (); 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 vowelBag = "aeiouyAEIOUY"; 149 155 public static String shortenDbName(String dbName, int desiredLength) { 156 StringBuffer dbBuf = new StringBuffer (dbName); 157 if (dbBuf.length() > desiredLength) { 158 for (int i = dbBuf.length() - 1; i > 0; i--) { 160 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 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 int prevUsIndex = dbBuf.lastIndexOf("_", usIndex - 1); 184 if (prevUsIndex < 0 && usIndex < 4) { 185 break; 186 } 187 188 if (prevUsIndex >= 0 && (usIndex - prevUsIndex) <= 4) { 190 usIndex = prevUsIndex; 191 continue; 192 } 193 194 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 if (dbBuf.length() > desiredLength) { 206 int removeIndex = dbBuf.length() - 1; 207 int prevRemoveIndex = dbBuf.lastIndexOf("_", removeIndex - 1); 208 if (prevRemoveIndex < 0 || (removeIndex - prevRemoveIndex) >= 3) { 210 dbBuf.deleteCharAt(removeIndex - 1); 212 removedChars = true; 213 } 214 } 215 216 while (dbBuf.indexOf("__") > 0) { 218 dbBuf.deleteCharAt(dbBuf.indexOf("__")); 219 removedChars = true; 220 } 221 222 if (!removedChars) { 224 break; 225 } 226 } 227 228 while (dbBuf.indexOf("__") > 0) { 230 dbBuf.deleteCharAt(dbBuf.indexOf("__")); 231 } 232 233 while (dbBuf.length() > desiredLength) { 234 int firstUs = dbBuf.indexOf("_"); 237 if (firstUs > 0) { 238 int nextUs = dbBuf.indexOf("_", firstUs + 1); 239 if (nextUs > 0) { 240 dbBuf.delete(firstUs, nextUs); 242 } 243 } 244 } 245 246 return dbBuf.toString(); 248 } 249 250 256 public static String packageToPath(String packageName) { 257 return packageName.replace('.', File.separatorChar); 259 } 260 261 268 public static String replaceString(String mainString, String oldString, String newString) { 269 return StringUtil.replaceString(mainString, oldString, newString); 270 } 271 272 public static String induceFieldType(String 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 |