Java tutorial
/* @(#) * * Project:NEBMis * * Modify Information: * ============================================================================= * Author Date Description * ------------ ---------- --------------------------------------------------- * PanShuang 2010-6-5 first release * * * Copyright Notice: * ============================================================================= * Copyright 2008 allinfinance, Inc. All rights reserved. * * This software is the confidential and proprietary information of * Shanghai allinfinance Co., Ltd. ("Confidential Information"). * You shall not disclose such Confidential Information and shall use it * only in accordance with the terms of the license agreement you entered * into with allinfinance. * * Warning: * ============================================================================= * */ package com.allinfinance.common.grid; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.SAXReader; import com.allinfinance.common.Constants; import com.allinfinance.common.GridConfigConstants; import com.allinfinance.commquery.dao.ICommQueryDAO; import com.allinfinance.system.util.ContextUtil; import com.allinfinance.system.util.JSONBean; /** * Title:? * * Description: * * Copyright: Copyright (c) 2010-6-5 * * Company: Shanghai allinfinance Co., Ltd. * * @author PanShuang * * @version 1.0 */ public class GridConfigUtil { private static Map<String, GridModel> gridConfigMap = new HashMap<String, GridModel>(10); /** * ?? * @param context * @throws Exception */ @SuppressWarnings("unchecked") public static void initGirdConfig(ServletContext context) throws Exception { SAXReader reader = new SAXReader(); Document document = null; document = reader.read(context.getResourceAsStream(Constants.GRID_CONFIG_CONTEXTPATH)); Element root = document.getRootElement(); List<Element> gridInfoList = root.elements(GridConfigConstants.NODE_GRID); for (Element gridNode : gridInfoList) { String gridId = gridNode.attributeValue(GridConfigConstants.GRID_ID); String gridConfigType = gridNode.attributeValue(GridConfigConstants.GRID_TYPE); //? GridModel gridModel = new GridModel(); gridModel.setId(gridId); gridModel.setType(gridConfigType); gridModel.setColumns(gridNode.elementText(GridConfigConstants.COLUMNS).trim()); if (GridConfigConstants.TYPE_SQL.equals(gridConfigType)) { SqlMode sqlMode = new SqlMode(); //SQL? Element sqlModeNode = gridNode.element(GridConfigConstants.TYPE_SQLMODE); //? Element wheresNode = sqlModeNode.element(GridConfigConstants.WHERES); List<Element> whereList = wheresNode.elements(GridConfigConstants.WHERE); sqlMode.setSql(sqlModeNode.elementText(GridConfigConstants.SQL).trim()); sqlMode.setDao(sqlModeNode.elementText(GridConfigConstants.QUERY_DAO).trim()); //? if (whereList != null && whereList.size() > 0) { WheresModel wheresModel = new WheresModel(); List<WhereModel> whereModelList = new ArrayList<WhereModel>(); for (Element whereNode : whereList) { WhereModel whereModel = new WhereModel(); whereModel.setType(whereNode.attributeValue(GridConfigConstants.WHERE_TYPE)); whereModel.setOperator(whereNode.attributeValue(GridConfigConstants.WHERE_OPERATOR)); whereModel.setLogic(whereNode.attributeValue(GridConfigConstants.WHERE_LOGIC)); whereModel.setDataBaseColumn( whereNode.elementText(GridConfigConstants.WHERE_DATABASE_COLUMN).trim()); whereModel.setQueryColumn( whereNode.elementText(GridConfigConstants.WHERE_QUERY_COLUMN).trim()); whereModelList.add(whereModel); } wheresModel.setWhereModelList(whereModelList); sqlMode.setWheresModel(wheresModel); } // ??? if (sqlModeNode.element(GridConfigConstants.ORDERS) != null) { OrdersModel ordersModel = new OrdersModel(); Element orderModels = sqlModeNode.element(GridConfigConstants.ORDERS); ordersModel.setSort(orderModels.attributeValue("sort")); List<Element> orderList = orderModels.elements(GridConfigConstants.ORDER); for (Element element : orderList) { ordersModel.getOrders().add(element.getText()); } sqlMode.setOrdersModel(ordersModel); } gridModel.setSqlMode(sqlMode); } else if (GridConfigConstants.TYPE_SYNC.equals(gridConfigType)) { SyncMode syncMode = new SyncMode(); Element syncModeNode = gridNode.element(GridConfigConstants.TYPE_SYNCMODE); Element methodNode = syncModeNode.element(GridConfigConstants.SYNC_METHOD); syncMode.setMethod(methodNode.attributeValue(GridConfigConstants.SYNC_METHOD_VALUE)); gridModel.setSyncMode(syncMode); } else { throw new Exception("???[ id:" + gridId + " ]"); } gridConfigMap.put(gridId, gridModel); } } /** * ??? * @param id * @param request * @return * @throws Exception */ @SuppressWarnings("unchecked") public static String getGridData(String id, int begin, HttpServletRequest request) throws Exception { GridModel gridModel = gridConfigMap.get(id); //? List<Object[]> dataList = null; List<Object> jsonDataList = new ArrayList<Object>(); String totalCount = ""; if (GridConfigConstants.TYPE_SQL.equals(gridModel.getType())) { String sql = gridModel.getSqlMode().getSql(); String dao = gridModel.getSqlMode().getDao(); // ? if (gridModel.getSqlMode().getWheresModel() != null) { WheresModel wheresModel = gridModel.getSqlMode().getWheresModel(); List<WhereModel> whereList = wheresModel.getWhereModelList(); for (WhereModel whereModel : whereList) { //?? String queryColumn = null; if (!"".equals(whereModel.getDataBaseColumn()) && !"".equals(whereModel.getQueryColumn())) { queryColumn = request.getParameter(whereModel.getQueryColumn()); //? if (!"".equals(queryColumn) && queryColumn != null) { sql += " " + whereModel.getLogic() + " "; if (GridConfigConstants.WHERE_TYPE_CHAR.equals(whereModel.getType())) { if (GridConfigConstants.WHERE_OPERATOR_EQ.equals(whereModel.getOperator())) { sql += whereModel.getDataBaseColumn() + " = '" + queryColumn + "'"; } else if (GridConfigConstants.WHERE_OPERATOR_NE.equals(whereModel.getOperator())) { sql += whereModel.getDataBaseColumn() + " != '" + queryColumn + "'"; } else if (GridConfigConstants.WHERE_OPERATOR_GE.equals(whereModel.getOperator())) { sql += whereModel.getDataBaseColumn() + " >= '" + queryColumn + "'"; } else if (GridConfigConstants.WHERE_OPERATOR_GT.equals(whereModel.getOperator())) { sql += whereModel.getDataBaseColumn() + " > '" + queryColumn + "'"; } else if (GridConfigConstants.WHERE_OPERATOR_IN.equals(whereModel.getOperator())) { sql += whereModel.getDataBaseColumn() + " in (" + queryColumn + ")"; } else if (GridConfigConstants.WHERE_OPERATOR_LE.equals(whereModel.getOperator())) { sql += whereModel.getDataBaseColumn() + " <= '" + queryColumn + "'"; } else if (GridConfigConstants.WHERE_OPERATOR_LT.equals(whereModel.getOperator())) { sql += whereModel.getDataBaseColumn() + " < '" + queryColumn + "'"; } else if (GridConfigConstants.WHERE_OPERATOR_LIKE .equals(whereModel.getOperator())) { sql += whereModel.getDataBaseColumn() + " like '%" + queryColumn + "%'"; } } else if (GridConfigConstants.WHERE_TYPE_NUMBER.equals(whereModel.getType())) { if (GridConfigConstants.WHERE_OPERATOR_EQ.equals(whereModel.getOperator())) { sql += whereModel.getDataBaseColumn() + " = " + queryColumn; } else if (GridConfigConstants.WHERE_OPERATOR_GE.equals(whereModel.getOperator())) { sql += whereModel.getDataBaseColumn() + " >= " + queryColumn; } else if (GridConfigConstants.WHERE_OPERATOR_GT.equals(whereModel.getOperator())) { sql += whereModel.getDataBaseColumn() + " > " + queryColumn; } else if (GridConfigConstants.WHERE_OPERATOR_LE.equals(whereModel.getOperator())) { sql += whereModel.getDataBaseColumn() + " <= " + queryColumn; } else if (GridConfigConstants.WHERE_OPERATOR_LT.equals(whereModel.getOperator())) { sql += whereModel.getDataBaseColumn() + " < " + queryColumn; } else if (GridConfigConstants.WHERE_OPERATOR_NE.equals(whereModel.getOperator())) { sql += whereModel.getDataBaseColumn() + " != " + queryColumn; } } } } } } // ?? if (gridModel.getSqlMode().getOrdersModel() != null) { OrdersModel ordersModel = gridModel.getSqlMode().getOrdersModel(); for (int i = 0; i < ordersModel.getOrders().size(); i++) { sql += " order by " + ordersModel.getOrders().get(i); if ((i + 1) < ordersModel.getOrders().size()) { sql += ","; } } sql += " " + ordersModel.getSort(); } // String countSql = "select count(*)" + sql.substring(sql.toLowerCase().indexOf(" from ")); if (countSql.toLowerCase().indexOf(" order ") != -1) { countSql = countSql.substring(0, countSql.indexOf(" order ")); } ICommQueryDAO commQueryDAO = (ICommQueryDAO) ContextUtil.getBean(dao); dataList = commQueryDAO.findBySQLQuery(sql, begin, Constants.QUERY_RECORD_COUNT); totalCount = commQueryDAO.findCountBySQLQuery(countSql); } else if (GridConfigConstants.TYPE_SYNC.equals(gridModel.getType())) { SyncMode syncMode = gridModel.getSyncMode(); Object[] result = (Object[]) GridConfigMethod.class .getMethod(syncMode.getMethod(), new Class[] { int.class, HttpServletRequest.class }) .invoke(GridConfigMethod.class, begin, request); dataList = (List<Object[]>) result[0]; totalCount = result[1].toString(); } //??? String[] columns = gridModel.getColumns().split(","); for (Object[] data : dataList) { int columnLen = data.length; Map<String, String> jsonMap = new HashMap<String, String>(); for (int i = 0; i < columnLen; i++) { jsonMap.put(columns[i], transNull(data[i])); } jsonDataList.add(jsonMap); } JSONBean bean = new JSONBean(); bean.addJSONElement(Constants.JSON_HEANDER_TOTALCOUNT, totalCount); bean.addChild(Constants.JSON_HEANDER_DATA, jsonDataList); return bean.toString(); } /** * * @param object * @return */ private static String transNull(Object object) { if (object == null) { return ""; } else { return object.toString().trim(); } } }