Search in sources :

Example 6 with JavaCodeGenContext

use of com.ctrip.platform.dal.daogen.generator.java.JavaCodeGenContext in project dal by ctripcorp.

the class JavaCodeGeneratorOfSpProcessor method generateSpDao.

private List<Callable<ExecuteResult>> generateSpDao(CodeGenContext codeGenCtx, final File mavenLikeDir) {
    JavaCodeGenContext ctx = (JavaCodeGenContext) codeGenCtx;
    final Progress progress = ctx.getProgress();
    Map<String, SpDbHost> _spHostMaps = ctx.getSpHostMaps();
    List<Callable<ExecuteResult>> results = new ArrayList<Callable<ExecuteResult>>();
    for (final SpDbHost host : _spHostMaps.values()) {
        Callable<ExecuteResult> worker = new Callable<ExecuteResult>() {

            @Override
            public ExecuteResult call() throws Exception {
                ExecuteResult result = new ExecuteResult("Generate SP[" + host.getDbSetName() + "] Dao, Pojo, Test");
                progress.setOtherMessage(result.getTaskName());
                try {
                    VelocityContext context = GenUtils.buildDefaultVelocityContext();
                    context.put("host", host);
                    GenUtils.mergeVelocityContext(context, String.format("%s/Dao/%sSpDao.java", mavenLikeDir.getAbsolutePath(), host.getDbSetName()), "templates/java/DAOBySp.java.tpl");
                    GenUtils.mergeVelocityContext(context, String.format("%s/Test/%sSpDaoUnitTest.java", mavenLikeDir.getAbsolutePath(), host.getDbSetName()), "templates/java/test/DAOBySpUnitTest.java.tpl");
                    for (SpHost sp : host.getSpHosts()) {
                        sp.setDbSetName(host.getDbSetName());
                        context.put("host", sp);
                        GenUtils.mergeVelocityContext(context, String.format("%s/Entity/%s.java", mavenLikeDir.getAbsolutePath(), sp.getPojoClassName()), "templates/java/Pojo.java.tpl");
                    }
                    result.setSuccessal(true);
                } catch (Exception e) {
                    log.error(result.getTaskName() + " exception", e);
                }
                return result;
            }
        };
        results.add(worker);
    }
    return results;
}
Also used : Progress(com.ctrip.platform.dal.daogen.entity.Progress) VelocityContext(org.apache.velocity.VelocityContext) ArrayList(java.util.ArrayList) SpHost(com.ctrip.platform.dal.daogen.host.java.SpHost) Callable(java.util.concurrent.Callable) SpDbHost(com.ctrip.platform.dal.daogen.host.java.SpDbHost) ExecuteResult(com.ctrip.platform.dal.daogen.entity.ExecuteResult) JavaCodeGenContext(com.ctrip.platform.dal.daogen.generator.java.JavaCodeGenContext)

Example 7 with JavaCodeGenContext

use of com.ctrip.platform.dal.daogen.generator.java.JavaCodeGenContext in project dal by ctripcorp.

the class JavaCodeGeneratorOfTableProcessor method process.

@Override
public void process(CodeGenContext context) throws Exception {
    JavaCodeGenContext ctx = (JavaCodeGenContext) context;
    int projectId = ctx.getProjectId();
    File dir = new File(String.format("%s/%s/java", ctx.getGeneratePath(), projectId));
    List<Callable<ExecuteResult>> tableCallables = generateTableDao(ctx, dir);
    TaskUtils.invokeBatch(log, tableCallables);
}
Also used : File(java.io.File) Callable(java.util.concurrent.Callable) JavaCodeGenContext(com.ctrip.platform.dal.daogen.generator.java.JavaCodeGenContext)

Example 8 with JavaCodeGenContext

use of com.ctrip.platform.dal.daogen.generator.java.JavaCodeGenContext in project dal by ctripcorp.

the class JavaCodeGeneratorOfTableProcessor method generateTableDao.

private List<Callable<ExecuteResult>> generateTableDao(CodeGenContext codeGenCtx, final File mavenLikeDir) {
    JavaCodeGenContext ctx = (JavaCodeGenContext) codeGenCtx;
    final Progress progress = ctx.getProgress();
    Queue<JavaTableHost> _tableHosts = ctx.getTableHosts();
    List<Callable<ExecuteResult>> results = new ArrayList<>();
    for (final JavaTableHost host : _tableHosts) {
        Callable<ExecuteResult> worker = new Callable<ExecuteResult>() {

            @Override
            public ExecuteResult call() throws Exception {
                ExecuteResult result = new ExecuteResult("Generate Table[" + host.getDbSetName() + "." + host.getTableName() + "] Dao, Pojo, Test");
                progress.setOtherMessage(result.getTaskName());
                try {
                    VelocityContext context = GenUtils.buildDefaultVelocityContext();
                    context.put("host", host);
                    GenUtils.mergeVelocityContext(context, String.format("%s/Dao/%sDao.java", mavenLikeDir.getAbsolutePath(), host.getPojoClassName()), "templates/java/dao/standard/DAO.java.tpl");
                    GenUtils.mergeVelocityContext(context, String.format("%s/Entity/%s.java", mavenLikeDir.getAbsolutePath(), host.getPojoClassName()), "templates/java/Pojo.java.tpl");
                    GenUtils.mergeVelocityContext(context, String.format("%s/Test/%sDaoUnitTest.java", mavenLikeDir.getAbsolutePath(), host.getPojoClassName()), "templates/java/test/DaoUnitTests.java.tpl");
                    result.setSuccessal(true);
                } catch (Exception e) {
                    log.error(result.getTaskName() + " exception", e);
                }
                return result;
            }
        };
        results.add(worker);
    }
    return results;
}
Also used : Progress(com.ctrip.platform.dal.daogen.entity.Progress) VelocityContext(org.apache.velocity.VelocityContext) JavaTableHost(com.ctrip.platform.dal.daogen.host.java.JavaTableHost) ArrayList(java.util.ArrayList) ExecuteResult(com.ctrip.platform.dal.daogen.entity.ExecuteResult) Callable(java.util.concurrent.Callable) JavaCodeGenContext(com.ctrip.platform.dal.daogen.generator.java.JavaCodeGenContext)

Example 9 with JavaCodeGenContext

use of com.ctrip.platform.dal.daogen.generator.java.JavaCodeGenContext in project dal by ctripcorp.

the class AbstractJavaDataPreparer method addDatabaseSet.

protected void addDatabaseSet(CodeGenContext codeGenCtx, String databaseSetName) {
    DaoOfDatabaseSet daoOfDatabaseSet = SpringBeanGetter.getDaoOfDatabaseSet();
    List<DatabaseSet> sets = daoOfDatabaseSet.getAllDatabaseSetByName(databaseSetName);
    if (null == sets || sets.isEmpty()) {
        // databaseSetName));
        return;
    }
    JavaCodeGenContext ctx = (JavaCodeGenContext) codeGenCtx;
    ContextHost contextHost = ctx.getContextHost();
    DalConfigHost dalConfigHost = ctx.getDalConfigHost();
    dalConfigHost.addDatabaseSet(sets);
    for (DatabaseSet databaseSet : sets) {
        List<DatabaseSetEntry> entries = daoOfDatabaseSet.getAllDatabaseSetEntryByDbsetid(databaseSet.getId());
        if (entries == null || entries.isEmpty()) {
            continue;
        }
        dalConfigHost.addDatabaseSetEntry(entries);
        Map<String, DatabaseSetEntry> map = dalConfigHost.getDatabaseSetEntryMap();
        for (DatabaseSetEntry entry : entries) {
            String key = entry.getConnectionString();
            if (map.containsKey(key)) {
                DatabaseSetEntry value = map.get(key);
                Resource resource = new Resource(value.getConnectionString(), value.getUserName(), value.getPassword(), value.getDbAddress(), value.getDbPort(), value.getDbCatalog(), value.getProviderName());
                contextHost.addResource(resource);
            }
        }
    }
}
Also used : DaoOfDatabaseSet(com.ctrip.platform.dal.daogen.dao.DaoOfDatabaseSet) DaoOfDatabaseSet(com.ctrip.platform.dal.daogen.dao.DaoOfDatabaseSet) DalConfigHost(com.ctrip.platform.dal.daogen.host.DalConfigHost) JavaCodeGenContext(com.ctrip.platform.dal.daogen.generator.java.JavaCodeGenContext)

Example 10 with JavaCodeGenContext

use of com.ctrip.platform.dal.daogen.generator.java.JavaCodeGenContext in project dal by ctripcorp.

the class AbstractJavaDataPreparer method buildTableHost.

protected JavaTableHost buildTableHost(CodeGenContext codeGenCtx, GenTaskByTableViewSp tableViewSp, String tableName, DatabaseCategory dbCategory) throws Exception {
    JavaCodeGenContext ctx = (JavaCodeGenContext) codeGenCtx;
    if (!DbUtils.tableExists(tableViewSp.getAllInOneName(), tableName)) {
        throw new Exception(String.format("The table[%s,%s] doesn't exist, pls check", tableViewSp.getAllInOneName(), tableName));
    }
    JavaTableHost tableHost = new JavaTableHost();
    tableHost.setPackageName(ctx.getNamespace());
    tableHost.setDatabaseCategory(getDatabaseCategory(tableViewSp.getAllInOneName()));
    tableHost.setDbSetName(tableViewSp.getDatabaseSetName());
    tableHost.setTableName(tableName);
    tableHost.setPojoClassName(getPojoClassName(tableViewSp.getPrefix(), tableViewSp.getSuffix(), tableName));
    tableHost.setSp(tableViewSp.isCud_by_sp());
    tableHost.setApi_list(tableViewSp.getApi_list());
    // 主键及所有列
    List<String> primaryKeyNames = DbUtils.getPrimaryKeyNames(tableViewSp.getAllInOneName(), tableName);
    List<AbstractParameterHost> allColumnsAbstract = DbUtils.getAllColumnNames(tableViewSp.getAllInOneName(), tableName, new JavaColumnNameResultSetExtractor(tableViewSp.getAllInOneName(), tableName, dbCategory));
    if (null == allColumnsAbstract) {
        throw new Exception(String.format("The column names of tabel[%s, %s] is null", tableViewSp.getAllInOneName(), tableName));
    }
    List<JavaParameterHost> allColumns = new ArrayList<>();
    for (AbstractParameterHost h : allColumnsAbstract) {
        allColumns.add((JavaParameterHost) h);
    }
    List<JavaParameterHost> primaryKeys = new ArrayList<>();
    boolean hasIdentity = false;
    String identityColumnName = null;
    for (JavaParameterHost h : allColumns) {
        if (!hasIdentity && h.isIdentity()) {
            hasIdentity = true;
            identityColumnName = h.getName();
        }
        if (primaryKeyNames.contains(h.getName())) {
            h.setPrimary(true);
            primaryKeys.add(h);
        }
    }
    List<GenTaskBySqlBuilder> currentTableBuilders = filterExtraMethods(ctx, tableViewSp.getAllInOneName(), tableName);
    List<JavaMethodHost> methods = buildSqlBuilderMethodHost(allColumns, currentTableBuilders);
    tableHost.setFields(allColumns);
    tableHost.setPrimaryKeys(primaryKeys);
    tableHost.setHasIdentity(hasIdentity);
    tableHost.setIdentityColumnName(identityColumnName);
    tableHost.setMethods(methods);
    if (tableHost.isSp()) {
        tableHost.setSpInsert(getSpaOperation(tableViewSp.getAllInOneName(), tableName, "i"));
        tableHost.setSpUpdate(getSpaOperation(tableViewSp.getAllInOneName(), tableName, "u"));
        tableHost.setSpDelete(getSpaOperation(tableViewSp.getAllInOneName(), tableName, "d"));
    }
    return tableHost;
}
Also used : AbstractParameterHost(com.ctrip.platform.dal.daogen.host.AbstractParameterHost) JavaCodeGenContext(com.ctrip.platform.dal.daogen.generator.java.JavaCodeGenContext)

Aggregations

JavaCodeGenContext (com.ctrip.platform.dal.daogen.generator.java.JavaCodeGenContext)22 Callable (java.util.concurrent.Callable)11 Progress (com.ctrip.platform.dal.daogen.entity.Progress)7 File (java.io.File)7 ExecuteResult (com.ctrip.platform.dal.daogen.entity.ExecuteResult)6 VelocityContext (org.apache.velocity.VelocityContext)6 AbstractParameterHost (com.ctrip.platform.dal.daogen.host.AbstractParameterHost)4 JavaMethodHost (com.ctrip.platform.dal.daogen.host.java.JavaMethodHost)4 ArrayList (java.util.ArrayList)4 FreeSqlHost (com.ctrip.platform.dal.daogen.host.java.FreeSqlHost)3 JavaTableHost (com.ctrip.platform.dal.daogen.host.java.JavaTableHost)3 CodeGenContext (com.ctrip.platform.dal.daogen.CodeGenContext)2 DalGenerator (com.ctrip.platform.dal.daogen.DalGenerator)2 GenTaskBySqlBuilder (com.ctrip.platform.dal.daogen.entity.GenTaskBySqlBuilder)2 CSharpCodeGenContext (com.ctrip.platform.dal.daogen.generator.csharp.CSharpCodeGenContext)2 CSharpDalGenerator (com.ctrip.platform.dal.daogen.generator.csharp.CSharpDalGenerator)2 JavaDalGenerator (com.ctrip.platform.dal.daogen.generator.java.JavaDalGenerator)2 DalConfigHost (com.ctrip.platform.dal.daogen.host.DalConfigHost)2 CSharpMethodHost (com.ctrip.platform.dal.daogen.host.csharp.CSharpMethodHost)2 EmailException (org.apache.commons.mail.EmailException)2