001: package com.avaje.util.codegen;
002:
003: import com.avaje.lib.sql.ColumnInfo;
004: import com.avaje.lib.sql.TableInfo;
005: import com.avaje.lib.util.Dnode;
006:
007: /**
008: * Builds EmbeddedId information.
009: *
010: */
011: public class BuildEmbeddedId implements Contants {
012:
013: String classNameSuffix = "Id";
014:
015: String embeddedIdPropertyName = "id";
016:
017: BuildProperty buildProperty;
018:
019: public BuildEmbeddedId(GenerateConfiguration context) {
020: buildProperty = new BuildProperty(context);
021: }
022:
023: public void build(GenerateInfo parentInfo) {
024:
025: // add Id to the class name of the parent
026: String beanPkg = parentInfo.getPackage();
027: String embClassName = parentInfo.getClassName();
028: embClassName += classNameSuffix;
029:
030: // create a GenerateInfo for the embedded bean
031: GenerateInfo embInfo = parentInfo.createEmbeddedId();
032: embInfo.setPackageClass(beanPkg, embClassName);
033:
034: embInfo.addImportAnnotation(EMBEDDABLE);
035:
036: Dnode beanTree = embInfo.getBeanTree();
037: beanTree.setAttribute("embeddedIdBean", "true");
038: beanTree.setAttribute("embeddableAnnotation", EMBEDDABLE);
039:
040: String classComment = buildProperty
041: .getEmbededIdClassComment(parentInfo.getClassName());
042: beanTree.setAttribute("classcomment", classComment);
043:
044: // add the primary key properties
045: addProperties(embInfo, parentInfo);
046:
047: createEmbeddedId(embInfo, parentInfo);
048: }
049:
050: private void addProperties(GenerateInfo embInfo,
051: GenerateInfo parentInfo) {
052:
053: Dnode embTree = embInfo.getBeanTree();
054:
055: TableInfo tableInfo = parentInfo.getTableInfo();
056: ColumnInfo[] keyColumns = tableInfo.getKeyColumns();
057:
058: String toStringContent = "";
059:
060: for (int i = 0; i < keyColumns.length; i++) {
061: ColumnInfo columnInfo = keyColumns[i];
062:
063: // create the property
064: Dnode prop = buildProperty.createColumnNode(embInfo,
065: tableInfo, columnInfo);
066: buildProperty.addDerivedInfo(embInfo, prop);
067:
068: embTree.addChild(prop);
069:
070: String propName = (String) prop.getAttribute("name");
071: if (i > 0) {
072: toStringContent += "+\",\"+";
073: }
074: toStringContent += "\"" + propName + "=\"+" + propName;
075: }
076:
077: embTree.setAttribute("toStringContent", toStringContent);
078: }
079:
080: private void createEmbeddedId(GenerateInfo emb, GenerateInfo parent) {
081:
082: Dnode property = new Dnode();
083: property.setNodeName("property");
084:
085: String propName = embeddedIdPropertyName;
086:
087: property.setAttribute("name", propName);
088: property.setAttribute("notnull", "true");
089: property.setAttribute("type", emb.getClassName());
090:
091: property.setAttribute("idAnnotation", EMBEDDEDID);
092: property.setAttribute("id", "true");
093:
094: // add getter setter methods etc
095: buildProperty.addDerivedInfo(parent, property);
096:
097: // add it to the parent
098: parent.addImportAnnotation(EMBEDDEDID);
099: parent.getBeanTree().addChild(property);
100:
101: }
102:
103: }
|