Search in sources :

Example 41 with VelocityContext

use of org.apache.velocity.VelocityContext in project dal by ctripcorp.

the class JavaCodeGeneratorOfFreeSqlProcessor method generateFreeSqlDao.

private List<Callable<ExecuteResult>> generateFreeSqlDao(CodeGenContext codeGenCtx, final File mavenLikeDir) {
    JavaCodeGenContext ctx = (JavaCodeGenContext) codeGenCtx;
    final Progress progress = ctx.getProgress();
    List<Callable<ExecuteResult>> results = new ArrayList<>();
    Map<String, JavaMethodHost> freeSqlPojoHosts = ctx.get_freeSqlPojoHosts();
    for (final JavaMethodHost host : freeSqlPojoHosts.values()) {
        if (host.isSampleType())
            continue;
        Callable<ExecuteResult> worker = new Callable<ExecuteResult>() {

            @Override
            public ExecuteResult call() throws Exception {
                ExecuteResult result = new ExecuteResult("Generate Free SQL[" + host.getPojoClassName() + "] Pojo");
                progress.setOtherMessage(result.getTaskName());
                try {
                    VelocityContext context = GenUtils.buildDefaultVelocityContext();
                    context.put("host", host);
                    GenUtils.mergeVelocityContext(context, String.format("%s/Entity/%s.java", mavenLikeDir.getAbsolutePath(), host.getPojoClassName()), "templates/java/Pojo.java.tpl");
                    result.setSuccessal(true);
                } catch (Throwable e) {
                    throw e;
                }
                return result;
            }
        };
        results.add(worker);
    }
    Queue<FreeSqlHost> freeSqlHosts = ctx.getFreeSqlHosts();
    for (final FreeSqlHost host : freeSqlHosts) {
        Callable<ExecuteResult> worker = new Callable<ExecuteResult>() {

            @Override
            public ExecuteResult call() throws Exception {
                ExecuteResult result = new ExecuteResult("Generate Free SQL[" + host.getClassName() + "] Dap, 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.getClassName()), "templates/java/dao/freesql/FreeSqlDAO.java.tpl");
                    GenUtils.mergeVelocityContext(context, String.format("%s/Test/%sDaoUnitTest.java", mavenLikeDir.getAbsolutePath(), host.getClassName()), "templates/java/test/FreeSqlDaoUnitTest.java.tpl");
                    result.setSuccessal(true);
                } catch (Throwable e) {
                    throw 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) FreeSqlHost(com.ctrip.platform.dal.daogen.host.java.FreeSqlHost) JavaMethodHost(com.ctrip.platform.dal.daogen.host.java.JavaMethodHost) Callable(java.util.concurrent.Callable) ExecuteResult(com.ctrip.platform.dal.daogen.entity.ExecuteResult) JavaCodeGenContext(com.ctrip.platform.dal.daogen.generator.java.JavaCodeGenContext)

Example 42 with VelocityContext

use of org.apache.velocity.VelocityContext 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 (Throwable e) {
                    throw 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 43 with VelocityContext

use of org.apache.velocity.VelocityContext in project dal by ctripcorp.

the class JavaCodeGeneratorOfTableProcessor method generateTableDao.

private List<Callable<ExecuteResult>> generateTableDao(CodeGenContext context, final File mavenLikeDir) {
    JavaCodeGenContext ctx = (JavaCodeGenContext) context;
    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 (Throwable e) {
                    throw 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 44 with VelocityContext

use of org.apache.velocity.VelocityContext in project dal by ctripcorp.

the class JavaCodeGeneratorOfViewProcessor method generateViewDao.

private List<Callable<ExecuteResult>> generateViewDao(CodeGenContext codeGenCtx, final File mavenLikeDir) {
    JavaCodeGenContext ctx = (JavaCodeGenContext) codeGenCtx;
    final Progress progress = ctx.getProgress();
    Queue<ViewHost> _viewHosts = ctx.getViewHosts();
    List<Callable<ExecuteResult>> results = new ArrayList<>();
    for (final ViewHost host : _viewHosts) {
        Callable<ExecuteResult> worker = new Callable<ExecuteResult>() {

            @Override
            public ExecuteResult call() throws Exception {
                ExecuteResult result = new ExecuteResult("Generate View[" + host.getDbSetName() + "." + host.getViewName() + "] Dao");
                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/ViewDAO.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/DAOByViewUnitTest.java.tpl");
                    result.setSuccessal(true);
                } catch (Throwable e) {
                    throw e;
                }
                return result;
            }
        };
        results.add(worker);
    }
    return results;
}
Also used : ViewHost(com.ctrip.platform.dal.daogen.host.java.ViewHost) Progress(com.ctrip.platform.dal.daogen.entity.Progress) VelocityContext(org.apache.velocity.VelocityContext) 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 45 with VelocityContext

use of org.apache.velocity.VelocityContext in project dal by ctripcorp.

the class GenTaskResource method approveTask.

@POST
@Path("approveTask")
@Produces(MediaType.APPLICATION_JSON)
public Status approveTask(@Context HttpServletRequest request, @FormParam("taskId") String taskId, @FormParam("taskType") String taskType, @FormParam("userId") int userId) throws Exception {
    try {
        Status status = Status.ERROR();
        LoginUser approver = BeanGetter.getDaoOfLoginUser().getUserById(userId);
        if (approver == null) {
            return status;
        }
        int len = request.getRequestURI().length();
        String host = request.getRequestURL().toString();
        host = host.substring(0, len);
        String approveUrl = host + "rest/task/taskApproveOperationForEmail?";
        String myApprovelTaskUrl = host + "eventmanage.jsp";
        String[] taskIds = taskId.split(",");
        String[] taskTypes = taskType.split(",");
        List<GenTaskByTableViewSp> tableViewSpTasks = new ArrayList<>();
        List<GenTaskBySqlBuilder> autoTasks = new ArrayList<>();
        List<GenTaskByFreeSql> sqlTasks = new ArrayList<>();
        String userNo = RequestUtil.getUserNo(request);
        LoginUser user = BeanGetter.getDaoOfLoginUser().getUserByNo(userNo);
        ApproveTask at = new ApproveTask();
        at.setApprove_user_id(approver.getId());
        at.setCreate_user_id(user.getId());
        at.setCreate_time(new Timestamp(System.currentTimeMillis()));
        for (int i = 0; i < taskIds.length; i++) {
            int id = Integer.parseInt(taskIds[i]);
            String type = taskTypes[i].trim();
            if ("table_view_sp".equalsIgnoreCase(type)) {
                GenTaskByTableViewSp task = BeanGetter.getDaoByTableViewSp().getTasksByTaskId(id);
                tableViewSpTasks.add(task);
                at.setTask_id(task.getId());
                at.setTask_type("table_view_sp");
                BeanGetter.getApproveTaskDao().insertApproveTask(at);
            } else if ("auto".equalsIgnoreCase(type)) {
                GenTaskBySqlBuilder task = BeanGetter.getDaoBySqlBuilder().getTasksByTaskId(id);
                autoTasks.add(task);
                at.setTask_id(task.getId());
                at.setTask_type("auto");
                BeanGetter.getApproveTaskDao().insertApproveTask(at);
            } else if ("sql".equalsIgnoreCase(type)) {
                GenTaskByFreeSql task = BeanGetter.getDaoByFreeSql().getTasksByTaskId(id);
                sqlTasks.add(task);
                at.setTask_id(task.getId());
                at.setTask_type("sql");
                BeanGetter.getApproveTaskDao().insertApproveTask(at);
            }
        }
        java.util.Collections.sort(tableViewSpTasks);
        java.util.Collections.sort(autoTasks);
        java.util.Collections.sort(sqlTasks);
        VelocityContext context = GenUtils.buildDefaultVelocityContext();
        context.put("standardDao", tableViewSpTasks);
        context.put("autoDao", autoTasks);
        context.put("sqlDao", sqlTasks);
        context.put("approveUrl", approveUrl);
        context.put("myApprovelTaskUrl", myApprovelTaskUrl);
        context.put("approveUser", approver.getUserName());
        String msg = GenUtils.mergeVelocityContext(context, "templates/approval/approveDao.tpl");
        HtmlEmail email = new HtmlEmail();
        email.setHostName(Configuration.get("email_host_name"));
        email.setAuthentication(Configuration.get("email_user_name"), Configuration.get("email_password"));
        try {
            email.addTo(approver.getUserEmail());
            email.setFrom(user.getUserEmail(), user.getUserName());
            email.setSubject("Codegen DAO 审批");
            email.setHtmlMsg(msg);
            email.send();
        } catch (EmailException e) {
            e.printStackTrace();
        }
        return Status.OK();
    } catch (Throwable e) {
        LoggerManager.getInstance().error(e);
        Status status = Status.ERROR();
        status.setInfo(e.getMessage());
        return status;
    }
}
Also used : Status(com.ctrip.platform.dal.daogen.domain.Status) VelocityContext(org.apache.velocity.VelocityContext) HtmlEmail(org.apache.commons.mail.HtmlEmail) Timestamp(java.sql.Timestamp) EmailException(org.apache.commons.mail.EmailException)

Aggregations

VelocityContext (org.apache.velocity.VelocityContext)146 StringWriter (java.io.StringWriter)43 Template (org.apache.velocity.Template)41 VelocityEngine (org.apache.velocity.app.VelocityEngine)23 IOException (java.io.IOException)19 Test (org.junit.Test)15 HashMap (java.util.HashMap)13 Map (java.util.Map)12 ObjEntity (org.apache.cayenne.map.ObjEntity)12 File (java.io.File)11 ArrayList (java.util.ArrayList)10 ResourceNotFoundException (org.apache.velocity.exception.ResourceNotFoundException)10 Writer (java.io.Writer)8 ExecuteResult (com.ctrip.platform.dal.daogen.entity.ExecuteResult)7 Progress (com.ctrip.platform.dal.daogen.entity.Progress)7 Callable (java.util.concurrent.Callable)7 ParseErrorException (org.apache.velocity.exception.ParseErrorException)7 JavaCodeGenContext (com.ctrip.platform.dal.daogen.generator.java.JavaCodeGenContext)6 OutputStreamWriter (java.io.OutputStreamWriter)6 PrintWriter (java.io.PrintWriter)6