protected void setupColumnComment( Map<String, DfColumnMeta> metaMap, String columnName, Column column) { final DfColumnMeta columnMeta = metaMap.get(columnName); final String sql2EntityRelatedTableName = columnMeta.getSql2EntityRelatedTableName(); final Table relatedTable = getRelatedTable(sql2EntityRelatedTableName); String relatedComment = null; if (relatedTable != null) { final String relatedColumnName = columnMeta.getSql2EntityRelatedColumnName(); final Column relatedColumn = relatedTable.getColumn(relatedColumnName); if (relatedColumn != null) { relatedComment = relatedColumn.getPlainComment(); } } // the meta has its select column comment final String selectColumnComment = columnMeta.getColumnComment(); final String commentMark = "// "; final String delimiter = getAliasDelimiterInDbComment(); final StringBuilder sb = new StringBuilder(); if (Srl.is_NotNull_and_NotTrimmedEmpty(relatedComment)) { sb.append(relatedComment); if (Srl.is_NotNull_and_NotTrimmedEmpty(selectColumnComment)) { // both exist if (Srl.is_NotNull_and_NotTrimmedEmpty(delimiter)) { // use alias option if (relatedComment.contains(delimiter)) { // resolved in related comment sb.append(ln()).append(commentMark).append(selectColumnComment); } else { // unresolved yet if (isDbCommentOnAliasBasis()) { // related comment is alias sb.append(delimiter); } else { // related comment is description sb.append(ln()); } sb.append(commentMark).append(selectColumnComment); } } else { // no alias option sb.append(ln()).append(commentMark).append(selectColumnComment); } } } else { // not found related comment if (Srl.is_NotNull_and_NotTrimmedEmpty(selectColumnComment)) { if (Srl.is_NotNull_and_NotTrimmedEmpty(delimiter)) { // use alias option if (isDbCommentOnAliasBasis()) { // select column comment is treated as description sb.append(delimiter); } } sb.append(commentMark).append(selectColumnComment); } } column.setPlainComment(sb.toString()); }
protected void buildCustomizeEntityColumnInfo( StringBuilder logSb, String columnName, Column column, Table relatedTable, Column relatedColumn, String forcedJavaNatice) { final StringBuilder sb = new StringBuilder(); sb.append(" ").append(column.isPrimaryKey() ? "*" : " "); sb.append(columnName); sb.append(" "); sb.append(column.getDbTypeExpression()); final String columnSize = column.getColumnSize(); if (Srl.is_NotNull_and_NotTrimmedEmpty(columnSize)) { sb.append("(").append(columnSize).append(")"); } if (relatedColumn != null) { sb.append(" related to ").append(relatedTable.getTableDbName()); sb.append(".").append(relatedColumn.getName()); } if (Srl.is_NotNull_and_NotTrimmedEmpty(forcedJavaNatice)) { sb.append(" forced to ").append(forcedJavaNatice); } logSb.append(sb).append(ln()); }
// ----------------------------------------------------- // Check Definition // ---------------- public void checkDefinition(DfTableListProvider provider) { if (!hasCommonColumn()) { return; } final List<Table> tableList = provider.provideTableList(); boolean exists = false; for (Table table : tableList) { if (table.hasAllCommonColumn()) { exists = true; break; } } if (!exists) { throwCommonColumnMapRelatedTableNotFoundException(); } }
protected Column setupSql2EntityRelatedColumn( String entityName, DfCustomizeEntityInfo entityInfo, Table relatedTable, Map<String, DfColumnMeta> metaMap, String columnName, Column column) { if (relatedTable == null) { return null; } final DfColumnMeta metaInfo = metaMap.get(columnName); final String sql2EntityRelatedColumnName = metaInfo.getSql2EntityRelatedColumnName(); final Column relatedColumn = relatedTable.getColumn(sql2EntityRelatedColumnName); if (relatedColumn == null) { return null; } column.setSql2EntityRelatedColumn(relatedColumn); return column; }
public void setOption(Attributes attributes) { // Look thru supported model elements in reverse order to // find one that this option statement applies to. String key = attributes.getValue("key"); String value = attributes.getValue("value"); if (currUnique != null) { currUnique.addOption(key, value); } else if (currIndex != null) { currIndex.addOption(key, value); } else if (currFK != null) { currFK.addOption(key, value); } else if (currColumn != null) { currColumn.addOption(key, value); } else if (currTable != null) { currTable.addOption(key, value); } else { // Must be a db level option. database.addOption(key, value); } }
protected void processScalarHandling(DfCustomizeEntityInfo entityInfo, Table tbl) { final DfPmbMetaData pmbMetaData = entityInfo.getPmbMetaData(); // for check only if (pmbMetaData == null || !pmbMetaData.isTypedSelectPmb()) { // not pinpoint (but enough) final ExceptionMessageBuilder br = new ExceptionMessageBuilder(); br.addNotice("The 'scalar' option was not related to a typed parameter-bean."); br.addItem("Advice"); br.addElement("A 'scalar' option should be defined with a typed parameter-bean"); br.addElement("that is typed to things returning an scalar value."); br.addElement("For example:"); br.addElement(" (x): selectMemberName.sql"); br.addElement(" (o): MemberBhv_selectMemberName.sql"); br.addElement(" (x):"); br.addElement(" -- #df:entity#"); br.addElement(" -- +scalar+"); br.addElement(""); br.addElement(" select MEMBER_NAME from MEMBER"); br.addElement(" (o):"); br.addElement(" -- #df:entity#"); br.addElement(" -- +scalar+"); br.addElement(""); br.addElement(" -- !df:pmb!"); br.addElement(""); br.addElement(" select MEMBER_NAME from MEMBER"); br.addItem("SQL File"); br.addElement(entityInfo.getSqlFile()); final String msg = br.buildExceptionMessage(); throw new DfCustomizeEntityMarkInvalidException(msg); } final List<Column> columnList = tbl.getColumnList(); if (columnList.size() != 1) { final ExceptionMessageBuilder br = new ExceptionMessageBuilder(); br.addNotice("The 'scalar' option was related to non-only-one-column SQL."); br.addItem("Advice"); br.addElement("A 'scalar' option should be defined on only-one-column SQL."); br.addElement("For example:"); br.addElement(" (x):"); br.addElement(" -- #df:entity#"); br.addElement(" -- +scalar+"); br.addElement(" "); br.addElement(" -- !df:pmb!"); br.addElement(" "); br.addElement(" select MEMBER_NAME, BIRTHDATE from MEMBER"); br.addElement(" (o):"); br.addElement(" -- #df:entity#"); br.addElement(" -- +scalar+"); br.addElement(""); br.addElement(" -- !df:pmb!"); br.addElement(""); br.addElement(" select BIRTHDATE from MEMBER"); br.addItem("SQL File"); br.addElement(entityInfo.getSqlFile()); br.addItem("Selected Column"); if (!columnList.isEmpty()) { for (Column column : columnList) { br.addElement(column.getName()); } } else { br.addElement("(empty)"); } final String msg = br.buildExceptionMessage(); throw new DfCustomizeEntityMarkInvalidException(msg); } final Column column = columnList.get(0); entityInfo.setScalarJavaNative(column.getJavaNative()); entityInfo.setScalarColumnDisp(column.getColumnDefinitionLineDisp()); }
// ----------------------------------------------------- // Result Handling // --------------- protected void processDomainHandling(DfCustomizeEntityInfo entityInfo, Table tbl) { final DfPmbMetaData pmbMetaData = entityInfo.getPmbMetaData(); if (pmbMetaData == null || !pmbMetaData.isTypedReturnEntityPmb()) { final ExceptionMessageBuilder br = new ExceptionMessageBuilder(); br.addNotice("The 'domain' option was not related to a typed parameter-bean."); br.addItem("Advice"); br.addElement("A 'domain' option should be defined with a typed parameter-bean"); br.addElement("that is typed to things returning an entity."); br.addElement("For example:"); br.addElement(" (x): selectDomainMember.sql"); br.addElement(" (o): MemberBhv_selectDomainMember.sql"); br.addElement(" (x):"); br.addElement(" -- #df:entity#"); br.addElement(" -- +domain+"); br.addElement(""); br.addElement(" select MEMBER_ID, MEMBER_NAME, ... from MEMBER"); br.addElement(" (o):"); br.addElement(" -- #df:entity#"); br.addElement(" -- +domain+"); br.addElement(""); br.addElement(" -- !df:pmb!"); br.addElement(""); br.addElement(" select MEMBER_ID, MEMBER_NAME, ... from MEMBER"); br.addItem("SQL File"); br.addElement(entityInfo.getSqlFile()); final String msg = br.buildExceptionMessage(); throw new DfCustomizeEntityMarkInvalidException(msg); } final String entityClassName = pmbMetaData.getEntityClassName(); if (Srl.is_Null_or_TrimmedEmpty(entityClassName)) { String msg = "The entity class name should not be null: " + entityInfo.getSqlFile(); throw new IllegalStateException(msg); // no way } final Database database = _schemaData.getDatabase(); Table domainTable = database.getTable(entityClassName); if (domainTable == null) { // retry without project-prefix for a class name final String projectPrefix = getBasicProperties().getProjectPrefix(); domainTable = database.getTable(Srl.substringFirstFront(entityClassName, projectPrefix)); } if (domainTable == null) { final ExceptionMessageBuilder br = new ExceptionMessageBuilder(); br.addNotice("The table of the behavior query was not found."); br.addItem("Advice"); br.addElement("A 'domain' option should be defined under behavior query."); br.addElement("And behavior query should have an existing table."); br.addElement("For example:"); br.addElement(" (x): MembooBhv_selectDomainMember.sql"); br.addElement(" (o): MemberBhv_selectDomainMember.sql"); br.addItem("SQL File"); br.addElement(entityInfo.getSqlFile()); final String msg = br.buildExceptionMessage(); throw new DfCustomizeEntityMarkInvalidException(msg); // basically no way } final List<Column> columnList = tbl.getColumnList(); for (Column column : columnList) { final Column found = domainTable.getColumn(column.getName()); if (found == null) { final ExceptionMessageBuilder br = new ExceptionMessageBuilder(); br.addNotice("The selected column was not a column of domain table."); br.addItem("Advice"); br.addElement("A selected column with a 'domain' option"); br.addElement("should be one of domain table."); br.addElement("For example:"); br.addElement(" (x):"); br.addElement(" select MEMBER_ID, 'noexist' as NO_EXIST from MEMBER"); br.addElement(" (o):"); br.addElement(" select MEMBER_ID, MEMBER_NAME from MEMBER"); br.addElement(" (o):"); br.addElement(" select member.* from MEMBER member"); br.addItem("SQL File"); br.addElement(entityInfo.getSqlFile()); br.addItem("Unknown Column"); br.addElement(column.getName()); br.addItem("Domain Table"); br.addElement(domainTable.getTableDbName()); final String msg = br.buildExceptionMessage(); throw new DfCustomizeEntityMarkInvalidException(msg); } } entityInfo.setEntityClassName(domainTable.getExtendedEntityClassName()); entityInfo.setEntityClassName(domainTable.getImmutableExtendedEntityClassName()); }
// =================================================================================== // Prepare Generation // ================== @Override public Context initControlContext() throws Exception { _log.info(""); _log.info("...Preparing generation of customize-entities and parameter-beans"); _log.info("* * * * * * * * * *"); _log.info("* CustomizeEntity *"); _log.info("* * * * * * * * * *"); final StringBuilder logSb = new StringBuilder(); final Database database = _database; database.setSql2EntitySchemaData(_schemaData); database.setPmbMetaDataMap(_sql2entityMeta.getPmbMetaDataMap()); database.setSkipDeleteOldClass(isSkipDeleteOldClass()); final Map<String, DfCustomizeEntityInfo> entityInfoMap = _sql2entityMeta.getEntityInfoMap(); final Set<String> entityNameSet = entityInfoMap.keySet(); for (String entityName : entityNameSet) { final DfCustomizeEntityInfo entityInfo = entityInfoMap.get(entityName); final Map<String, DfColumnMeta> metaMap = entityInfo.getColumnMap(); final DfOutsideSqlFile outsideSqlFile = entityInfo.getOutsideSqlFile(); final Table tbl = new Table(); tbl.setSql2EntityCustomize(true); if (outsideSqlFile != null) { // basically true but checked just in case tbl.setSql2EntitySqlFile(outsideSqlFile); } tbl.setName(entityInfo.getTableDbName()); if (!entityInfo.needsJavaNameConvert()) { tbl.suppressJavaNameConvert(); // basically here (except STRUCT type) } if (entityInfo.hasNestedCustomizeEntity()) { tbl.setSql2EntityCustomizeHasNested(true); // basically when STRUCT type } if (entityInfo.isAdditionalSchema()) { tbl.setUnifiedSchema(entityInfo.getAdditionalSchema()); // basically when STRUCT type } tbl.setSql2EntityTypeSafeCursor(entityInfo.isCursorHandling()); buildCustomizeEntityTitle(logSb, entityName, entityInfo); final StringKeyMap<String> pkMap = getPrimaryKeyMap(entityInfo); final boolean allCommonColumn = hasAllCommonColumn(metaMap); final Set<String> columnNameSet = metaMap.keySet(); for (String columnName : columnNameSet) { final Column column = new Column(); setupColumnName(columnName, column); // an element removed from pkMap if true // and a table name related to primary key is returned final String pkRelatedTableName = setupPrimaryKey(pkMap, entityName, columnName, column); setupTorqueType(metaMap, columnName, column, allCommonColumn); setupDbType(metaMap, columnName, column); setupNotNull(metaMap, columnName, column); setupColumnSizeContainsDigit(metaMap, columnName, column); setupColumnComment(metaMap, columnName, column); setupSql2EntityElement( entityName, entityInfo, metaMap, columnName, column, pkRelatedTableName, logSb); tbl.addColumn(column); } if (!pkMap.isEmpty()) { // if not-removed columns exist throwPrimaryKeyNotFoundException(entityName, pkMap, columnNameSet); } if (entityInfo.isScalarHandling()) { // it does not generate an only-one-column entity tbl.setDatabase(database); // one-way love for utility (just in case) processScalarHandling(entityInfo, tbl); } else if (entityInfo.isDomainHandling()) { // it does not generate an customize-entity tbl.setDatabase(database); // one-way love for utility (just in case) processDomainHandling(entityInfo, tbl); } else { // initialize a class name of the entity for typed parameter-bean database.addTable(tbl); // should be before getting names entityInfo.setEntityClassName(tbl.getExtendedEntityClassName()); entityInfo.setImmutableClassName(tbl.getImmutableExtendedEntityClassName()); } logSb.append(ln()); } final String databaseType = getDatabaseTypeFacadeProp().getTargetDatabase(); final AppData appData = new AppData(databaseType); appData.addDatabase(database); showCustomizeEntity(logSb); showParameterBean(); final VelocityContext context = createVelocityContext(appData); return context; }
/** * Handles opening elements of the xml file. * * @param uri * @param localName The local name (without prefix), or the empty string if Namespace processing * is not being performed. * @param rawName The qualified name (with prefix), or the empty string if qualified names are not * available. * @param attributes The specified or defaulted attributes */ public void startElement(String uri, String localName, String rawName, Attributes attributes) throws SAXException { try { if (rawName.equals("database")) { if (isExternalSchema) { currentPackage = attributes.getValue("package"); if (currentPackage == null) { currentPackage = defaultPackage; } } else { database.loadFromXML(attributes); if (database.getPackage() == null) { database.setPackage(defaultPackage); } } } else if (rawName.equals("external-schema")) { String xmlFile = attributes.getValue("filename"); if (xmlFile.charAt(0) != '/') { File f = new File(currentXmlFile); xmlFile = new File(f.getParent(), xmlFile).getPath(); } // put current state onto the stack ParseStackElement.pushState(this); isExternalSchema = true; parseFile(xmlFile); // get the last state from the stack ParseStackElement.popState(this); } else if (rawName.equals("domain")) { Domain domain = new Domain(); domain.loadFromXML(attributes, database.getPlatform()); database.addDomain(domain); } else if (rawName.equals("table")) { currTable = database.addTable(attributes); if (isExternalSchema) { currTable.setForReferenceOnly(true); currTable.setPackage(currentPackage); } } else if (rawName.equals("view")) { currView = database.addView(attributes); } else if (rawName.equals("sequence")) { currSequence = database.addSequence(attributes); } else if (rawName.equals("column")) { currColumn = currTable.addColumn(attributes); } else if (rawName.equals("inheritance")) { currColumn.addInheritance(attributes); } else if (rawName.equals("foreign-key")) { currFK = currTable.addForeignKey(attributes); } else if (rawName.equals("reference")) { currFK.addReference(attributes); } else if (rawName.equals("index")) { currIndex = currTable.addIndex(attributes); } else if (rawName.equals("index-column")) { currIndex.addColumn(attributes); } else if (rawName.equals("unique")) { currUnique = currTable.addUnique(attributes); } else if (rawName.equals("unique-column")) { currUnique.addColumn(attributes); } else if (rawName.equals("id-method-parameter")) { currTable.addIdMethodParameter(attributes); } else if (rawName.equals("option")) { setOption(attributes); } } catch (Exception e) { throw new SAXException(e); } }