package aos.framework.builder;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
public class PageEditToolKit {
private static String driver = "com.mysql.jdbc.Driver";
private static String url = "jdbc:mysql://***.***.*.***/";
private static String db = "prj_carebay";
private static String user = "root";
private static String pass = "12345";
static Connection conn = null;
static Statement statement = null;
static PreparedStatement ps = null;
static ResultSet rs = null;
static List<Map<String, String>> columns = new ArrayList<Map<String, String>>();
/**
* 连接数据库
*/
public static void startMySQLConn() {
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url + db, user, pass);
if (!conn.isClosed()) {
// System.out.println("Succeeded connecting to MySQL!");
}
statement = conn.createStatement();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 关闭数据库
*/
public static void closeMySQLConn() {
if (conn != null) {
try {
conn.close();
// System.out.println("Database connection terminated!");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 获取表中的所有字段
* @param 表名
*/
public static void getColums(String tableName) {
String sql = "select * from information_schema.columns " + "where table_schema ='" + db + "' and table_name = '" + tableName
+ "' ;";
try {
// 查询表结构
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
int beginNum = 0;
while (rs.next()) {
Map<String, String> columnElemMap = new HashMap<String, String>();
columnElemMap.put("name", rs.getString("column_name"));// 字段名称
columnElemMap.put("comment", rs.getString("column_comment"));// 注释
String date_type = rs.getString("data_type");
columnElemMap.put("type", date_type);// 类型
String length = "";
if (!date_type.equals("date") && !date_type.equals("datetime")) {
beginNum = rs.getString("column_type").indexOf("(");
length = rs.getString("column_type").substring(beginNum + 1, rs.getString("column_type").length() - 1);
}
columnElemMap.put("length", length);// 长度
columnElemMap.put("is_nullable", rs.getString("is_nullable"));// 是否可为空
columns.add(columnElemMap);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 导出编辑页面
* @param columnWidth 字段宽度
*/
public static void printColums4Edit(String columnWidth) {
System.out.println("=====导出编辑页面=====");
for (Map<String, String> columnElem : columns) {
// 字段名称
String name = "\"" + columnElem.get("name") + "\"";
// 备注
String comment = StringUtils.isEmpty(columnElem.get("comment")) ? "" : ("\"" + columnElem.get("comment") + "\"");
// 是否可为空
String null_desc = "YES".equals(columnElem.get("is_nullable")) ? "" : " allowBlank=\"false\"";
// 长度
String length = columnElem.get("length") == "" ? "" : (" maxLength=\"" + columnElem.get("length") + "\"");
// 字段类型
String type = columnElem.get("type");
if (type.equals("date")) {
System.out.println("<aos:datefield name=" + name + " fieldLabel=" + comment + null_desc + " columnWidth=\""+columnWidth+"\" />");
} else {
System.out.println("<aos:textfield name=" + name + " fieldLabel=" + comment + null_desc + length + " columnWidth=\""+columnWidth+"\" />");
}
}
}
/**
* 导出列表页面
* @param path 后台路径
*/
public static void printColums4Mantain(String path) {
System.out.println("=====导出列表页面=====");
System.out.println("<aos:gridpanel id=\"_g_moudle\" url=\""+path+"\" region=\"center\" bodyBorder=\"1 0 1 0\" onrender=\"g_query\" onitemdblclick=\"_window_show_edit\" >");
System.out.println(" <aos:docked forceBoder=\"0 0 1 0\">");
System.out.println(" <aos:triggerfield id=\"keyword\" emptyText=\"客户编号|姓名|建议内容\" width=\"200\" onenterkey=\"g_query\" trigger1Cls=\"x-form-search-trigger\" onTrigger1Click=\"g_query\" margin=\"0 5 0 5\"/>");
System.out.println(" <aos:dockeditem xtype=\"tbseparator\" />");
System.out.println(" <aos:dockeditem text=\"刷新\" onclick=\"#_g_moudle_store.reload()\" icon=\"refresh.png\" />");
System.out.println(" </aos:docked>");
System.out.println(" <aos:menu>");
System.out.println(" <aos:menuitem text=\"编辑\" onclick=\"_window_show_edit\" icon=\"edit.png\" />");
System.out.println(" <aos:menuitem xtype=\"menuseparator\" />");
System.out.println(" <aos:menuitem text=\"刷新\" onclick=\"#_g_moudle_store.reload()\" icon=\"refresh.png\" />");
System.out.println(" </aos:menu>");
System.out.println(" <aos:column type=\"rowno\" header=\"#\" align=\"center\" />");
for (Map<String, String> columnElem : columns) {
// 字段名称
String name = "\"" + columnElem.get("name") + "\"";
// 备注
String comment = StringUtils.isEmpty(columnElem.get("comment")) ? "" : ("\"" + columnElem.get("comment") + "\"");
// 字段类型
String type = columnElem.get("type");
if (columnElem.get("name").contains("create") || columnElem.get("name").contains("update")) {
System.out.println(" <aos:column header=" + name + " dataIndex=" + comment
+ " minWidth=\"100\" celltip=\"true\" hidden=\"true\" />");
continue;
}
if (type.equals("date")) {
System.out.println(" <aos:column header=" + name + " dataIndex=" + comment
+ " type=\"date\" format=\"Y-m-d\" minWidth=\"100\" align=\"center\" />");
continue;
}
if (type.equals("decimal")) {
System.out.println(" <aos:column header=" + name + " dataIndex=" + comment
+ " type=\"number\" format=\"0,000.00\" minWidth=\"100\" />");
continue;
}
System.out.println(" <aos:column header=" + name + " dataIndex=" + comment + " minWidth=\"100\" celltip=\"true\" />");
}
System.out.println(" <aos:column header=\"操作\" rendererFn=\"fn_button_render\" align=\"center\" width=\"120\" />");
System.out.println("</aos:gridpanel>");
}
/**
* 导出popup页面
* @param path 后台路径
*/
public static void printColums4Popup(String path) {
System.out.println("=====导出popup页面=====");
System.out.println("<aos:viewport layout=\"fit\">");
System.out.println("<aos:gridpanel id=\"_g_moudle\" url=\""+path+"\" region=\"center\" bodyBorder=\"1 0 1 0\" onrender=\"g_query\" >");
System.out.println(" <aos:docked forceBoder=\"0 0 1 0\">");
System.out.println(" <aos:triggerfield id=\"keyword\" emptyText=\"客户编号|姓名\" width=\"200\" onenterkey=\"g_query\" trigger1Cls=\"x-form-search-trigger\" onTrigger1Click=\"g_query\" margin=\"0 5 0 5\"/>");
System.out.println(" <aos:dockeditem xtype=\"tbseparator\" />");
System.out.println(" <aos:dockeditem text=\"刷新\" onclick=\"#_g_moudle_store.reload()\" icon=\"refresh.png\" />");
System.out.println(" </aos:docked>");
System.out.println(" <aos:column type=\"rowno\" header=\"#\" align=\"center\" />");
for (Map<String, String> columnElem : columns) {
// 字段名称
String name = "\"" + columnElem.get("name") + "\"";
// 备注
String comment = StringUtils.isEmpty(columnElem.get("comment")) ? "" : ("\"" + columnElem.get("comment") + "\"");
// 字段类型
String type = columnElem.get("type");
if (columnElem.get("name").contains("create") || columnElem.get("name").contains("update")) {
System.out.println(" <aos:column header=" + name + " dataIndex=" + comment
+ " minWidth=\"100\" celltip=\"true\" hidden=\"true\" />");
continue;
}
if (type.equals("date")) {
System.out.println(" <aos:column header=" + name + " dataIndex=" + comment
+ " type=\"date\" format=\"Y-m-d\" minWidth=\"100\" align=\"center\" />");
continue;
}
if (type.equals("decimal")) {
System.out.println(" <aos:column header=" + name + " dataIndex=" + comment
+ " type=\"number\" format=\"0,000.00\" minWidth=\"100\" />");
continue;
}
System.out.println(" <aos:column header=" + name + " dataIndex=" + comment + " minWidth=\"100\" celltip=\"true\" />");
}
System.out.println(" <aos:column header=\"选择\" type=\"action\" fixedWidth=\"60\" align=\"center\">");
System.out.println(" <aos:action handler=\"fn_do_cust_picker\" icon=\"ok1.png\" tooltip=\"选择\"/>");
System.out.println(" </aos:column>");
System.out.println("</aos:gridpanel>");
System.out.println("</aos:viewport>");
System.out.println();
System.out.println("<script type=\"text/javascript\">");
System.out.println(" //模块查询");
System.out.println(" function g_query() {");
System.out.println(" var params = {");
System.out.println(" keyword : AOS.getValue('keyword')");
System.out.println(" };");
System.out.println(" g_module_store.getProxy().extraParams = params;");
System.out.println(" g_module_store.loadPage(1);");
System.out.println(" }");
System.out.println();
System.out.println(" //模块选择事件");
System.out.println(" function fn_do_cust_picker(grid, rowIndex, colIndex) {");
System.out.println(" var record = grid.getStore().getAt(rowIndex);");
System.out.println(" //传选中值到父页面");
System.out.println(" parent.contract_picker_callback(record.raw);");
System.out.println(" }");
System.out.println("</script>");
}
public static void main(String[] args) {
startMySQLConn(); // 开始连接
getColums("biz_contract");// 根据数据库和表名获取字段,字段如"company_name"
// // 导出编辑页面
printColums4Edit("0.33");
// // 导出列表页面
printColums4Mantain("custAdviceService.list");
//导出popup页面
printColums4Popup("custAdviceService.list");
closeMySQLConn();
}
}
aos 页面快速生成工具
最新推荐文章于 2022-09-19 00:00:45 发布