package com.avaje.util.codegen;
import java.sql.Types;
import com.avaje.ebean.server.naming.NamingConvention;
import com.avaje.lib.sql.ColumnInfo;
import com.avaje.lib.sql.TableInfo;
import com.avaje.lib.util.Dnode;
public class BuildProperty implements Contants {
TypeDetermine typeDetermine = new TypeDetermine();
CommentGenerator commentGenerator = new CommentGenerator();
NamingConvention namingConvention;
ReservedWords reservedWords;
GenerateConfiguration config;
public BuildProperty(GenerateConfiguration config) {
this.config = config;
namingConvention = config.getNamingConvention();
reservedWords = config.getReservedWords();
}
/**
* Generate the class comment for an EmbeddedId.
*/
public String getEmbededIdClassComment(String parentClassName) {
return commentGenerator.generateEmbeddedIdClass(parentClassName);
}
/**
* Generate the class comment for a entity bean.
*/
public String getClassComment(String className) {
return commentGenerator.generateClass(className);
}
/**
* Add useful derived information to make the code generation easier.
*/
public void addDerivedInfo(GenerateInfo info, Dnode beanProp) {
// Dnode beanProp = new Dnode();
String name = (String) beanProp.getAttribute("name");
// determine the initCapName
String initLetter = name.substring(0, 1);
String initCapName = initLetter.toUpperCase() + name.substring(1);
beanProp.setAttribute("initcapname", initCapName);
// determine the type without java package
String type = (String) beanProp.getAttribute("type");
String shortTypeName = type;
int lastDot = shortTypeName.lastIndexOf('.');
if (lastDot > -1) {
shortTypeName = shortTypeName.substring(lastDot + 1);
}
beanProp.setAttribute("shorttype", shortTypeName);
boolean isPrimitive = (type.indexOf(".") == -1);
if (!isPrimitive && !type.startsWith("java.lang")) {
info.addImport(type);
}
// determine the getter and setter methods
String setter = "set" + initCapName;
String getter = "get" + initCapName;
if (type.equalsIgnoreCase("boolean")) {
getter = "is" + initCapName;
}
beanProp.setAttribute("getter", getter);
beanProp.setAttribute("setter", setter);
// generate some comments for methods
String setterComment = commentGenerator.generateMethod(setter);
String getterComment = commentGenerator.generateMethod(getter);
beanProp.setAttribute("gettercomment", getterComment);
beanProp.setAttribute("settercomment", setterComment);
// register these methods so we can identify additional
// methods in the source code
info.addDeployedMethod(setter);
info.addDeployedMethod(getter);
}
public Dnode createForeignKey(TableInfo tableInfo, String columnName) {
Dnode property = new Dnode();
property.setNodeName("property");
String propName = namingConvention.getForeignKeyProperty(columnName);
property.setAttribute("name", propName);
return property;
}
public Dnode createColumnNode(GenerateInfo info, TableInfo tableInfo, ColumnInfo columnInfo) {
Dnode property = new Dnode();
property.setNodeName("property");
String propName = namingConvention.propertyFromColumn(columnInfo.getName());
String translated = reservedWords.getTranslation(propName);
if (translated != null) {
config.print(info.getClassName());
String m = " property ["+propName+"] uses java reserved word. Set to ["+translated+"]";
config.println(m);
property.setAttribute("name", translated);
String ann = COLUMN+"(name=\"" + columnInfo.getName() + "\")";
property.setAttribute("columnAnnotation", ann);
info.addImportAnnotation(COLUMN);
} else {
property.setAttribute("name", propName);
}
property.setAttribute("dbcolumn", columnInfo.getName());
if (!columnInfo.isNullable()) {
property.setAttribute("notnull", "true");
}
String className = typeDetermine.determineClass(property, columnInfo);
property.setAttribute("type", className);
if (setLobTypes(property, columnInfo)) {
String lobType = (String) property.getAttribute("dbtype");
property.setAttribute("lob", lobType);
property.setAttribute("lobAnnotation", LOB);
info.addImportAnnotation(LOB);
}
return property;
}
/**
* Set explicit dbtype for Clob, Blob, LongVarchar and LongVarbinary types.
*/
private boolean setLobTypes(Dnode property, ColumnInfo columnInfo) {
int dt = columnInfo.getDataType();
switch (dt) {
case Types.CLOB:
property.setAttribute("dbtype", "clob");
return true;
case Types.LONGVARCHAR:
property.setAttribute("dbtype", "longvarchar");
return true;
case Types.BLOB:
property.setAttribute("dbtype", "blob");
return true;
case Types.LONGVARBINARY:
property.setAttribute("dbtype", "longvarbinary");
return true;
default:
return false;
}
}
}
|