All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mycomm.itest.CodeBuilder Maven / Gradle / Ivy

The newest version!
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycomm.itest;

import com.mycomm.itool.SystemUtil;
import java.util.Set;
import com.mycomm.itool.WebAppModule.annotation.MyActionURI;
import java.io.File;

/**
 *
 * @author jw362j
 */
public class CodeBuilder {

    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(CodeBuilder.class);
//    private static final String webSourceCodeBase = "C:\\Users\\jw362j\\a\\z_temp\\hadoopz\\hadoopz\\SerenaTVWeb\\src\\main\\java\\com\\mycomm\\web\\iweb\\";
//    private static final String appPackageName = "com.mycomm.web.iweb";
//    private static final String tmpFilePath = "C:\\Users\\jw362j\\a\\z_temp\\hadoopz\\hadoopz\\ITest\\src\\main\\resources\\conf\\autoCodeTemplate\\XController.tmp";

    private static String webSourceCodeBase;
    private static String appPackageName;
    private static String tmpFilePath;

    public static void GeneratePkgCode(String sourceCodeBase, String packageName, String tmpFile) {
        webSourceCodeBase = sourceCodeBase;
        appPackageName = packageName;
        tmpFilePath = tmpFile;
        Set> set = SystemUtil.getClasses(appPackageName + ".beans");
        for (Class clz : set) {
            if (clz == null || !clz.isAnnotationPresent(MyActionURI.class)) {
                continue;
            }
            MyActionURI mauri = (MyActionURI) clz.getAnnotation(MyActionURI.class);
            if (mauri == null) {
                continue;
            }
            log.debug(clz.getName());
            String ActionAddUpdate = mauri.actionAddUpdate();
            String ActionLoadById = mauri.actionLoadById();
            String ActionLoadByList = mauri.actionLoadByList();
            String ActionDelById = mauri.actionDelById();
            String ActionDelByList = mauri.actionDelByList();
            GenerateWebActionCode(webSourceCodeBase, clz, ActionAddUpdate, ActionLoadById, ActionLoadByList, ActionDelById, ActionDelByList);
        }
    }

    private static void GenerateWebActionCode(String webSourceCodeBase, Class clz, String ActionAddUpdate, String ActionLoadById, String ActionLoadByList, String ActionDelById, String ActionDelByList) {
        if (webSourceCodeBase == null || webSourceCodeBase.isEmpty() || clz == null) {
            return;
        }
        String actionCodeBase = webSourceCodeBase + "action" + File.separator;
        String className = clz.getSimpleName();

        String actionClassFileName = className + "Controller.java";
        File fileName = new File(actionCodeBase + "autocode" + File.separator + className);
        if (!fileName.exists()) {
            fileName.mkdir();
        }
        String actionClassFileFullPath = actionCodeBase + "autocode" + File.separator + className + File.separator + actionClassFileName;
        File destnation = new File(actionClassFileFullPath);
        if (destnation.exists()) {
            return;
        }
        File f_tmp = new File(tmpFilePath);
        if (!f_tmp.exists()) {
            log.error("the template file:" + tmpFilePath + "TemplateAction.tmp,is null,give up!");
            return;
        }
        String fcontent = SystemUtil.ReadFromFile(f_tmp.getAbsolutePath(), null);
        fcontent = fcontent.replace("{packageName}", appPackageName);
        fcontent = fcontent.replace("{ClassName}", className);
        fcontent = fcontent.replace("{ControllerAddUpdate}", ActionAddUpdate);
        fcontent = fcontent.replace("{ControllerLoadById}", ActionLoadById);
        fcontent = fcontent.replace("{ControllerLoadByList}", ActionLoadByList);
        fcontent = fcontent.replace("{ControllerDelById}", ActionDelById);
        fcontent = fcontent.replace("{ControllerDelByList}", ActionDelByList);
        SystemUtil.WriteIntoFile(actionClassFileFullPath, fcontent, null);

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy