001: package com.avaje.util.codegen;
002:
003: import java.sql.Types;
004:
005: import com.avaje.ebean.server.naming.NamingConvention;
006: import com.avaje.lib.sql.ColumnInfo;
007: import com.avaje.lib.sql.TableInfo;
008: import com.avaje.lib.util.Dnode;
009:
010: public class BuildProperty implements Contants {
011:
012: TypeDetermine typeDetermine = new TypeDetermine();
013:
014: CommentGenerator commentGenerator = new CommentGenerator();
015:
016: NamingConvention namingConvention;
017:
018: ReservedWords reservedWords;
019:
020: GenerateConfiguration config;
021:
022: public BuildProperty(GenerateConfiguration config) {
023: this .config = config;
024:
025: namingConvention = config.getNamingConvention();
026: reservedWords = config.getReservedWords();
027: }
028:
029: /**
030: * Generate the class comment for an EmbeddedId.
031: */
032: public String getEmbededIdClassComment(String parentClassName) {
033: return commentGenerator
034: .generateEmbeddedIdClass(parentClassName);
035: }
036:
037: /**
038: * Generate the class comment for a entity bean.
039: */
040: public String getClassComment(String className) {
041: return commentGenerator.generateClass(className);
042: }
043:
044: /**
045: * Add useful derived information to make the code generation easier.
046: */
047: public void addDerivedInfo(GenerateInfo info, Dnode beanProp) {
048:
049: // Dnode beanProp = new Dnode();
050: String name = (String) beanProp.getAttribute("name");
051:
052: // determine the initCapName
053: String initLetter = name.substring(0, 1);
054: String initCapName = initLetter.toUpperCase()
055: + name.substring(1);
056: beanProp.setAttribute("initcapname", initCapName);
057:
058: // determine the type without java package
059: String type = (String) beanProp.getAttribute("type");
060: String shortTypeName = type;
061: int lastDot = shortTypeName.lastIndexOf('.');
062: if (lastDot > -1) {
063: shortTypeName = shortTypeName.substring(lastDot + 1);
064: }
065: beanProp.setAttribute("shorttype", shortTypeName);
066:
067: boolean isPrimitive = (type.indexOf(".") == -1);
068: if (!isPrimitive && !type.startsWith("java.lang")) {
069: info.addImport(type);
070: }
071:
072: // determine the getter and setter methods
073: String setter = "set" + initCapName;
074: String getter = "get" + initCapName;
075: if (type.equalsIgnoreCase("boolean")) {
076: getter = "is" + initCapName;
077: }
078: beanProp.setAttribute("getter", getter);
079: beanProp.setAttribute("setter", setter);
080:
081: // generate some comments for methods
082: String setterComment = commentGenerator.generateMethod(setter);
083: String getterComment = commentGenerator.generateMethod(getter);
084: beanProp.setAttribute("gettercomment", getterComment);
085: beanProp.setAttribute("settercomment", setterComment);
086:
087: // register these methods so we can identify additional
088: // methods in the source code
089: info.addDeployedMethod(setter);
090: info.addDeployedMethod(getter);
091:
092: }
093:
094: public Dnode createForeignKey(TableInfo tableInfo, String columnName) {
095:
096: Dnode property = new Dnode();
097: property.setNodeName("property");
098:
099: String propName = namingConvention
100: .getForeignKeyProperty(columnName);
101: property.setAttribute("name", propName);
102:
103: return property;
104: }
105:
106: public Dnode createColumnNode(GenerateInfo info,
107: TableInfo tableInfo, ColumnInfo columnInfo) {
108:
109: Dnode property = new Dnode();
110: property.setNodeName("property");
111:
112: String propName = namingConvention
113: .propertyFromColumn(columnInfo.getName());
114:
115: String translated = reservedWords.getTranslation(propName);
116: if (translated != null) {
117:
118: config.print(info.getClassName());
119: String m = " property [" + propName
120: + "] uses java reserved word. Set to ["
121: + translated + "]";
122: config.println(m);
123:
124: property.setAttribute("name", translated);
125: String ann = COLUMN + "(name=\"" + columnInfo.getName()
126: + "\")";
127: property.setAttribute("columnAnnotation", ann);
128: info.addImportAnnotation(COLUMN);
129:
130: } else {
131: property.setAttribute("name", propName);
132:
133: }
134:
135: property.setAttribute("dbcolumn", columnInfo.getName());
136:
137: if (!columnInfo.isNullable()) {
138: property.setAttribute("notnull", "true");
139: }
140:
141: String className = typeDetermine.determineClass(property,
142: columnInfo);
143:
144: property.setAttribute("type", className);
145:
146: if (setLobTypes(property, columnInfo)) {
147: String lobType = (String) property.getAttribute("dbtype");
148: property.setAttribute("lob", lobType);
149: property.setAttribute("lobAnnotation", LOB);
150: info.addImportAnnotation(LOB);
151: }
152:
153: return property;
154: }
155:
156: /**
157: * Set explicit dbtype for Clob, Blob, LongVarchar and LongVarbinary types.
158: */
159: private boolean setLobTypes(Dnode property, ColumnInfo columnInfo) {
160: int dt = columnInfo.getDataType();
161: switch (dt) {
162: case Types.CLOB:
163: property.setAttribute("dbtype", "clob");
164: return true;
165:
166: case Types.LONGVARCHAR:
167: property.setAttribute("dbtype", "longvarchar");
168: return true;
169:
170: case Types.BLOB:
171: property.setAttribute("dbtype", "blob");
172: return true;
173:
174: case Types.LONGVARBINARY:
175: property.setAttribute("dbtype", "longvarbinary");
176: return true;
177:
178: default:
179: return false;
180: }
181: }
182: }
|