package xin.glue.ui.common; import java.sql.CallableStatement; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import xin.glue.ui.common.component.PosSiteLog; import com.posdata.glue.biz.activity.PosActivity; import com.posdata.glue.biz.constants.PosBizControlConstants; import com.posdata.glue.context.PosContext; import com.posdata.glue.dao.PosGenericDao; import com.posdata.glue.dao.vo.PosParameter; public class PosDBEngine extends PosActivity { private Map sqlMap = new HashMap(); // 语句:sqlkey0, ... private Map paramName = new HashMap(); // 参数名:param-u0, param-i0, param-d0, ... private Map paramMap = new HashMap(); // 参数:语句使用参数( REG_ID ) private Map paramBool = new HashMap(); // 参数行已添加标志 private boolean writeLog; private class Paramter { public Object values = null; public int index = 0; public int increment = 0; } public String runActivity(PosContext ctx) { return PosBizControlConstants.SUCCESS; } protected void Initialize() { Initialize(!"false".equalsIgnoreCase(getProperty("writeLog"))); } protected void Initialize(boolean writeLog) { this.writeLog = writeLog; sqlMap.clear(); paramName.clear(); paramMap.clear(); paramBool.clear(); } protected PosGenericDao getDao() { String testdao = getProperty("dao"); if (testdao == null) testdao = "mesdao"; else testdao = testdao.trim(); return getDao(testdao); } protected void setParamMap(PosContext context, String sqlKey) { setParamMap(context, sqlKey, null, 0, false); } protected void setParamMap(PosContext context, String sqlKey, String paramKey) { setParamMap(context, sqlKey, paramKey, 0, false); } protected void setParamMap(PosContext context, String sqlKey, String paramKey, int length) { setParamMap(context, sqlKey, paramKey, length, false); } protected void setParamMap(PosContext context, String sqlKey, String paramKey, int length, boolean clear) { String sqlkey = getProperty(sqlKey); if (sqlkey == null) return; sqlMap.put(sqlKey, sqlkey.trim()); String param = getProperty(paramKey); if (param == null) return; if (clear) paramBool.clear(); String[] paramNames = param.replaceAll("[ ]+", "").split("\\|"); paramName.put(paramKey, paramNames); int cnt = paramNames.length; for (int j = 0; j < cnt; j++) { String key = paramNames[j]; Paramter paramter; if (!paramMap.containsKey(key)) { paramter = new Paramter(); paramter.values = context.get(key); if (paramter.values == null) paramter.index = -1; paramMap.put(key, paramter); } else paramter = (Paramter) paramMap.get(key); if (length > 0 && paramter.index > -1 && !paramBool.containsKey(key)) { paramBool.put(key, null); paramter.index += length; } } } protected void setParamIndex() { Iterator iterator = paramMap.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry entry = (Map.Entry) iterator.next(); Paramter paramter = (Paramter) entry.getValue(); if (paramter.index > -1) { if (paramter.values instanceof String[]) { int length = ((Object[]) paramter.values).length; if (length > 1) { paramter.index = length - paramter.index; if (paramter.index < 0) paramter.index = 0; } else { paramter.values = ((String[]) paramter.values)[0].trim().replaceAll("&", "&"); paramter.index = -1; } } else paramter.index = -1; } } paramBool.clear(); // 清除无用变量 } private Object getParamValue(String key) { Paramter paramter = (Paramter) paramMap.get(key); if (paramter == null) return null; if (paramter.index < 0) return paramter.values; if (paramter.values instanceof String[]) { String[] values = (String[]) paramter.values; String value; try { value = values[paramter.index].trim().replaceAll("&", "&"); paramter.increment = 1; } catch (Exception e) { value = values[0].trim().replaceAll("&", "&"); paramter.index = 0; } return value; } return paramter.values; } protected void adjustParamIndex() { Iterator iterator = paramMap.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry entry = (Map.Entry) iterator.next(); Paramter paramter = (Paramter) entry.getValue(); if (paramter.index > -1) { paramter.index += paramter.increment; paramter.increment = 0; } } } protected Object Execute(PosContext context, String sqlKey) { return Execute(context, sqlKey, null, 'f'); } protected Object Execute(PosContext context, String sqlKey, String paramKey) { return Execute(context, sqlKey, paramKey, 'f'); } protected Object Execute(PosContext context, String sqlKey, String paramKey, char style) { String sqlkey = (String) sqlMap.get(sqlKey); if (sqlkey == null || "".equals(sqlkey)) return null; PosParameter param = new PosParameter(); String[] params = (String[]) paramName.get(paramKey); if (params != null) { for (int i = 0; i < params.length; i++) { String key = params[i]; Object value = getParamValue(key); switch (style) { case 'I': case 'i': if (value == null) param.setValueParamter(i, context.get(key)); else param.setValueParamter(i, value); break; default: if (value == null) param.setWhereClauseParameter(i, context.get(key)); else param.setWhereClauseParameter(i, value); } } } Object result = null; List paramList; switch (style) { case 'I': case 'i': paramList = param.getValueParameters(); break; default: paramList = param.getWhereClauseParamters(); } PosGenericDao dao = getDao(); if (dao == null) return null; switch (style) { case 'U': case 'u': if(paramList.size()==9 && "B01001".endsWith(param.getWhereClauseParameter(7).toString())){ //调用存储过程 add by pbs20161121 try{ CallableStatement cstm = dao.getCallableStatement(sqlkey); cstm.setString(1, param.getWhereClauseParameter(0).toString()); cstm.setString(2, param.getWhereClauseParameter(1).toString()); cstm.setString(3, param.getWhereClauseParameter(2).toString()); cstm.setString(4, param.getWhereClauseParameter(3).toString()); cstm.setString(5, param.getWhereClauseParameter(4).toString()); cstm.setString(6, param.getWhereClauseParameter(5).toString()); cstm.setString(7, param.getWhereClauseParameter(6).toString()); cstm.setString(8, param.getWhereClauseParameter(7).toString()); cstm.setString(9, param.getWhereClauseParameter(8).toString()); cstm.execute(); result="SUCCESS"; }catch(Exception ex){ ex.printStackTrace(); result="FAIL"; } }else{ result = Integer.valueOf(dao.update(sqlkey, param) + ""); } break; case 'I': case 'i': result = Integer.valueOf(dao.insert(sqlkey, param) + ""); paramList = param.getValueParameters(); break; case 'D': case 'd': result = Integer.valueOf(dao.delete(sqlkey, param) + ""); break; default: result = dao.find(sqlkey, param); } if (writeLog) PosSiteLog.writeLog(context, dao, sqlkey, paramList); return result; } // protected Object findByQueryStatement(String sql) { // return findByQueryStatement(sql, null); // } // // protected Object findByQueryStatement(String sql, PosParameter param) { // PosGenericDao dao = getDao(); // if (dao == null) return null; // return dao.findByQueryStatement(sql, param); // } }