001: package com.avaje.util.codegen;
002:
003: import java.util.HashMap;
004: import java.util.Iterator;
005: import java.util.Map;
006:
007: import com.avaje.ebean.server.plugin.Plugin;
008: import com.avaje.ebean.server.plugin.PluginProperties;
009: import com.avaje.lib.util.StringHelper;
010:
011: public class TableNaming {
012:
013: String defaultPackage;
014:
015: String trimPrefix;
016:
017: /**
018: * Array of Trimmable prefixes. The trimPrefix can be a comma delimited
019: * list.
020: */
021: String[] prefixTrimArray = null;
022:
023: String defaultBeanPrefix;
024:
025: String defaultBeanSuffix;
026:
027: boolean upperCaseFirstLetter = true;
028:
029: HashMap aliasMap = new HashMap();
030:
031: Plugin plugin;
032:
033: HashMap tableNameMap = new HashMap();
034:
035: public TableNaming(Plugin plugin) {
036: this .plugin = plugin;
037: PluginProperties props = plugin.getProperties();
038:
039: defaultBeanPrefix = props.getProperty("codegen.beanprefix",
040: null);
041: defaultBeanSuffix = props.getProperty("codegen.beansuffix",
042: null);
043: trimPrefix = props.getProperty("codegen.tabletrimprefix", null);
044: defaultPackage = props.getProperty("codegen.javapackage", null);
045:
046: String nameMap = props.getProperty("codegen.table.namemap",
047: null);
048: if (nameMap != null) {
049: // load a map of Table name to Bean class names
050: Map map = StringHelper.delimitedToMap(nameMap, ",", "=");
051: Iterator it = map.entrySet().iterator();
052: while (it.hasNext()) {
053: Map.Entry entry = (Map.Entry) it.next();
054: String tableName = (String) entry.getKey();
055: String beanClassName = (String) entry.getValue();
056: tableNameMap
057: .put(tableName.toLowerCase(), beanClassName);
058: }
059: }
060:
061: }
062:
063: public void addAlias(String from, String to) {
064: aliasMap.put(from.toLowerCase(), to);
065: }
066:
067: public void setDefaultBeanPrefix(String defaultBeanPrefix) {
068: this .defaultBeanPrefix = defaultBeanPrefix;
069: }
070:
071: public void setDefaultBeanSuffix(String defaultBeanSuffix) {
072: this .defaultBeanSuffix = defaultBeanSuffix;
073: }
074:
075: public void setTrimPrefix(String trimPrefix) {
076: this .trimPrefix = trimPrefix;
077: }
078:
079: public String toPackage(String dbTableName) {
080: return defaultPackage;
081: }
082:
083: public String toBeanClassName(String dbTableName) {
084:
085: String javaBeanName = (String) tableNameMap.get(dbTableName
086: .toLowerCase());
087: if (javaBeanName != null) {
088: return javaBeanName;
089: }
090:
091: javaBeanName = trimFirst(dbTableName);
092: javaBeanName = convertUnderscore(javaBeanName);
093:
094: if (upperCaseFirstLetter) {
095: char firstLetter = Character.toUpperCase(javaBeanName
096: .charAt(0));
097: javaBeanName = firstLetter + javaBeanName.substring(1);
098: }
099: StringBuffer sb = new StringBuffer();
100: if (defaultBeanPrefix != null) {
101: sb.append(defaultBeanPrefix);
102: }
103: sb.append(javaBeanName);
104: if (defaultBeanSuffix != null) {
105: sb.append(defaultBeanSuffix);
106: }
107:
108: javaBeanName = sb.toString();
109: // make sure the bean name starts with a capital letter
110: char firstLetter = Character
111: .toUpperCase(javaBeanName.charAt(0));
112: javaBeanName = firstLetter + javaBeanName.substring(1);
113:
114: // if (javaPackage != null) {
115: // javaBeanName = javaPackage + "." + javaBeanName;
116: // }
117:
118: return javaBeanName;
119: }
120:
121: public String convertUnderscore(String source) {
122:
123: String upperCased = source.toUpperCase();
124: boolean forceLower = upperCased.equals(source);
125:
126: StringBuffer result = new StringBuffer();
127: String[] vals = source.split("_");
128:
129: if (aliasMap.size() > 0) {
130: // convert using from to alias
131: // example prodoption to productOption
132: for (int y = 0; y < vals.length; y++) {
133: String to = (String) aliasMap
134: .get(vals[y].toLowerCase());
135: if (to != null) {
136: vals[y] = to;
137: }
138: }
139: }
140:
141: for (int i = 0; i < vals.length; i++) {
142: String section = vals[i];
143: if (forceLower) {
144: section = section.toLowerCase();
145: }
146: if (i > 0) {
147:
148: char c = Character.toUpperCase(section.charAt(0));
149: result.append(c);
150: result.append(section.substring(1));
151: } else {
152: result.append(section);
153: }
154: }
155:
156: return result.toString();
157:
158: }
159:
160: protected String trimFirst(String beanName) {
161: if (trimPrefix == null) {
162: return beanName;
163: }
164: if (prefixTrimArray == null) {
165: prefixTrimArray = trimPrefix.split("[,;]");
166: }
167: // search and trim table prefix
168: for (int i = 0; i < prefixTrimArray.length; i++) {
169: if (0 == beanName.indexOf(prefixTrimArray[i])) {
170: int newStart = prefixTrimArray[i].length();
171: return beanName.substring(newStart);
172: }
173: }
174: return beanName;
175: }
176:
177: }
|