GenerateView.java 4.65 KB
package com.taover.business;

import java.io.File;
import java.io.StringWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Vector;

import com.taover.base.template.model.bussiness.ViewModel;
import com.taover.db.TableColumn;
import com.taover.db.Tools;
import com.taover.tools.StringUtil;

import freemarker.template.Configuration;
import freemarker.template.Template;

public class GenerateView {
	public static void main(String args[]) throws SQLException{
		//读取配置文件
		Properties properties = Tools.getProperties();
		//取数据库连接
		Connection conn = Tools.getConnection(properties);
		//初始化要生成的表列表
		List<String> tableNameList = new ArrayList<String>();

		String outputPath   = properties.getProperty("OutputPath");
		String schameName   = properties.getProperty("SchameName");
		String createSchame = properties.getProperty("CreateSchame");
		String dbLikeStr    = properties.getProperty("DBLikeStr");
		String dbType       = properties.getProperty("dbtype");   
		String dbPackagePath    = properties.getProperty("packageName");
		String tableName    = properties.getProperty("TableName");	
		
		//WebRootPath读取
		String webRootPath = properties.getProperty("WebRootPath");
		
		if("mysql".equalsIgnoreCase(dbType)){
			//schameName = schameName.toUpperCase();			
		}

		if ("ALL".equalsIgnoreCase(tableName) ) {
			
			String sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '"+schameName+"'";
			ResultSet rs = Tools.executeQuery(Tools.createStmt(conn), sql);
		        if (null == rs)
		              return ;
		        List<String> result = new ArrayList<String>();
		        try {
		            while (rs.next()) {
		            	tableNameList.add(rs.getString("Table_name"));
		            }
		        } catch (SQLException e) {
		            e.printStackTrace();
		            result = null;
		        }
		        Tools.close(rs);
		} else {
			if("mysql".equalsIgnoreCase(dbType)){
				tableName = tableName.toUpperCase();
			}
			String[] TableNameArr = tableName.split(",");
			for (int i = 0; i < TableNameArr.length; i++)
				tableNameList.add(TableNameArr[i]);

		}
		
		for (int i = 0; i < tableNameList.size(); i++) {			
			String tableNameCurr = (String) tableNameList.get(i);			
			
			// 取得表中所有列
			Vector<TableColumn> columnsInTable = TableColumn.GetTableInfo(conn, schameName, tableNameCurr, dbLikeStr, dbType);
			if (columnsInTable == null || columnsInTable.size() == 0) {
				System.out.println(tableNameCurr + " : 生成失败,得不到表中列明");
				continue;
			}
			TableColumn pkcolum = TableColumn.getPKColum(columnsInTable);	
			if (pkcolum == null) {
				System.out.println(tableNameCurr + " : 生成失败,未设置主键");
				continue;
			}
			
			//生成view
			generateView(webRootPath, tableNameCurr, columnsInTable, false);
			
			System.out.println(tableNameCurr + " : 生成结束");
		}
		conn.close();
		System.out.println("全部结束");
	}
	
	public static void generateView(String webRootPath, String tableName, Vector<TableColumn> columnsInTable, boolean coverWhenFileExists) {
		try{
			String controllerMapping = StringUtil.formatBeanNameFirstLow(tableName).toLowerCase();
			ViewModel model = ViewModel.createViewModel(controllerMapping, webRootPath, columnsInTable);
			
			File f = new File(Tools.getPath() + Constants.TEMPATE_PATH);
			Configuration cfg = new Configuration();
			cfg.setDirectoryForTemplateLoading(f);
			
			StringWriter htmlOut = new StringWriter();
			Template htmlTemplate = cfg.getTemplate(Constants.TEMPATE_HTMLNAME, "UTF-8");		
			htmlTemplate.process(model, htmlOut);
			File destFile = new File(
					webRootPath+File.separator+"html"+File.separator+controllerMapping+".html");
			if(coverWhenFileExists || !destFile.exists()){
				Tools.createFile(webRootPath+File.separator+"html", controllerMapping+".html", htmlOut.toString());
			}else{
				System.out.println("文件生成:"+destFile.getName()+"文件已经存在,未覆盖!");
			}
			
			StringWriter jsOut = new StringWriter();
			Template jsTemplate = cfg.getTemplate(Constants.TEMPATE_JSNAME, "UTF-8");		
			jsTemplate.process(model, jsOut);
			File destFile2 = new File(
					webRootPath+File.separator+"js"+File.separator+controllerMapping+".js");
			if(coverWhenFileExists || !destFile2.exists()){
				Tools.createFile(webRootPath+File.separator+"js", controllerMapping+".js", jsOut.toString());	
			}else{
				System.out.println("文件生成:"+destFile2.getName()+"文件已经存在,未覆盖!");
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}	
}