47c976a026e622d25bb783b48b7a3d7b01d543b1.svn-base 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. package xin.glue.ui.common;
  2. import java.sql.CallableStatement;
  3. import java.util.HashMap;
  4. import java.util.Iterator;
  5. import java.util.List;
  6. import java.util.Map;
  7. import xin.glue.ui.common.component.PosSiteLog;
  8. import com.posdata.glue.biz.activity.PosActivity;
  9. import com.posdata.glue.biz.constants.PosBizControlConstants;
  10. import com.posdata.glue.context.PosContext;
  11. import com.posdata.glue.dao.PosGenericDao;
  12. import com.posdata.glue.dao.vo.PosParameter;
  13. public class PosDBEngine extends PosActivity {
  14. private Map sqlMap = new HashMap(); // 语句:sqlkey0, ...
  15. private Map paramName = new HashMap(); // 参数名:param-u0, param-i0, param-d0, ...
  16. private Map paramMap = new HashMap(); // 参数:语句使用参数( REG_ID )
  17. private Map paramBool = new HashMap(); // 参数行已添加标志
  18. private boolean writeLog;
  19. private class Paramter {
  20. public Object values = null;
  21. public int index = 0;
  22. public int increment = 0;
  23. }
  24. public String runActivity(PosContext ctx) {
  25. return PosBizControlConstants.SUCCESS;
  26. }
  27. protected void Initialize() {
  28. Initialize(!"false".equalsIgnoreCase(getProperty("writeLog")));
  29. }
  30. protected void Initialize(boolean writeLog) {
  31. this.writeLog = writeLog;
  32. sqlMap.clear();
  33. paramName.clear();
  34. paramMap.clear();
  35. paramBool.clear();
  36. }
  37. protected PosGenericDao getDao() {
  38. String testdao = getProperty("dao");
  39. if (testdao == null)
  40. testdao = "mesdao";
  41. else
  42. testdao = testdao.trim();
  43. return getDao(testdao);
  44. }
  45. protected void setParamMap(PosContext context, String sqlKey) {
  46. setParamMap(context, sqlKey, null, 0, false);
  47. }
  48. protected void setParamMap(PosContext context, String sqlKey, String paramKey) {
  49. setParamMap(context, sqlKey, paramKey, 0, false);
  50. }
  51. protected void setParamMap(PosContext context, String sqlKey, String paramKey, int length) {
  52. setParamMap(context, sqlKey, paramKey, length, false);
  53. }
  54. protected void setParamMap(PosContext context, String sqlKey, String paramKey, int length, boolean clear) {
  55. String sqlkey = getProperty(sqlKey);
  56. if (sqlkey == null) return;
  57. sqlMap.put(sqlKey, sqlkey.trim());
  58. String param = getProperty(paramKey);
  59. if (param == null) return;
  60. if (clear) paramBool.clear();
  61. String[] paramNames = param.replaceAll("[ ]+", "").split("\\|");
  62. paramName.put(paramKey, paramNames);
  63. int cnt = paramNames.length;
  64. for (int j = 0; j < cnt; j++) {
  65. String key = paramNames[j];
  66. Paramter paramter;
  67. if (!paramMap.containsKey(key)) {
  68. paramter = new Paramter();
  69. paramter.values = context.get(key);
  70. if (paramter.values == null)
  71. paramter.index = -1;
  72. paramMap.put(key, paramter);
  73. } else
  74. paramter = (Paramter) paramMap.get(key);
  75. if (length > 0 && paramter.index > -1 && !paramBool.containsKey(key)) {
  76. paramBool.put(key, null);
  77. paramter.index += length;
  78. }
  79. }
  80. }
  81. protected void setParamIndex() {
  82. Iterator iterator = paramMap.entrySet().iterator();
  83. while (iterator.hasNext()) {
  84. Map.Entry entry = (Map.Entry) iterator.next();
  85. Paramter paramter = (Paramter) entry.getValue();
  86. if (paramter.index > -1) {
  87. if (paramter.values instanceof String[]) {
  88. int length = ((Object[]) paramter.values).length;
  89. if (length > 1) {
  90. paramter.index = length - paramter.index;
  91. if (paramter.index < 0) paramter.index = 0;
  92. } else {
  93. paramter.values = ((String[]) paramter.values)[0].trim().replaceAll("&", "&amp;");
  94. paramter.index = -1;
  95. }
  96. } else
  97. paramter.index = -1;
  98. }
  99. }
  100. paramBool.clear(); // 清除无用变量
  101. }
  102. private Object getParamValue(String key) {
  103. Paramter paramter = (Paramter) paramMap.get(key);
  104. if (paramter == null)
  105. return null;
  106. if (paramter.index < 0)
  107. return paramter.values;
  108. if (paramter.values instanceof String[]) {
  109. String[] values = (String[]) paramter.values;
  110. String value;
  111. try {
  112. value = values[paramter.index].trim().replaceAll("&", "&amp;");
  113. paramter.increment = 1;
  114. } catch (Exception e) {
  115. value = values[0].trim().replaceAll("&", "&amp;");
  116. paramter.index = 0;
  117. }
  118. return value;
  119. }
  120. return paramter.values;
  121. }
  122. protected void adjustParamIndex() {
  123. Iterator iterator = paramMap.entrySet().iterator();
  124. while (iterator.hasNext()) {
  125. Map.Entry entry = (Map.Entry) iterator.next();
  126. Paramter paramter = (Paramter) entry.getValue();
  127. if (paramter.index > -1) {
  128. paramter.index += paramter.increment;
  129. paramter.increment = 0;
  130. }
  131. }
  132. }
  133. protected Object Execute(PosContext context, String sqlKey) {
  134. return Execute(context, sqlKey, null, 'f');
  135. }
  136. protected Object Execute(PosContext context, String sqlKey, String paramKey) {
  137. return Execute(context, sqlKey, paramKey, 'f');
  138. }
  139. protected Object Execute(PosContext context, String sqlKey, String paramKey, char style) {
  140. String sqlkey = (String) sqlMap.get(sqlKey);
  141. if (sqlkey == null || "".equals(sqlkey)) return null;
  142. PosParameter param = new PosParameter();
  143. String[] params = (String[]) paramName.get(paramKey);
  144. if (params != null) {
  145. for (int i = 0; i < params.length; i++) {
  146. String key = params[i];
  147. Object value = getParamValue(key);
  148. switch (style) {
  149. case 'I':
  150. case 'i':
  151. if (value == null)
  152. param.setValueParamter(i, context.get(key));
  153. else
  154. param.setValueParamter(i, value);
  155. break;
  156. default:
  157. if (value == null)
  158. param.setWhereClauseParameter(i, context.get(key));
  159. else
  160. param.setWhereClauseParameter(i, value);
  161. }
  162. }
  163. }
  164. Object result = null;
  165. List paramList;
  166. switch (style) {
  167. case 'I':
  168. case 'i':
  169. paramList = param.getValueParameters();
  170. break;
  171. default:
  172. paramList = param.getWhereClauseParamters();
  173. }
  174. PosGenericDao dao = getDao();
  175. if (dao == null) return null;
  176. switch (style) {
  177. case 'U':
  178. case 'u':
  179. if(paramList.size()==9 && "B01001".endsWith(param.getWhereClauseParameter(7).toString())){
  180. //调用存储过程 add by pbs20161121
  181. try{
  182. CallableStatement cstm = dao.getCallableStatement(sqlkey);
  183. cstm.setString(1, param.getWhereClauseParameter(0).toString());
  184. cstm.setString(2, param.getWhereClauseParameter(1).toString());
  185. cstm.setString(3, param.getWhereClauseParameter(2).toString());
  186. cstm.setString(4, param.getWhereClauseParameter(3).toString());
  187. cstm.setString(5, param.getWhereClauseParameter(4).toString());
  188. cstm.setString(6, param.getWhereClauseParameter(5).toString());
  189. cstm.setString(7, param.getWhereClauseParameter(6).toString());
  190. cstm.setString(8, param.getWhereClauseParameter(7).toString());
  191. cstm.setString(9, param.getWhereClauseParameter(8).toString());
  192. cstm.execute();
  193. result="SUCCESS";
  194. }catch(Exception ex){
  195. ex.printStackTrace();
  196. result="FAIL";
  197. }
  198. }else{
  199. result = Integer.valueOf(dao.update(sqlkey, param) + "");
  200. }
  201. break;
  202. case 'I':
  203. case 'i':
  204. result = Integer.valueOf(dao.insert(sqlkey, param) + "");
  205. paramList = param.getValueParameters();
  206. break;
  207. case 'D':
  208. case 'd':
  209. result = Integer.valueOf(dao.delete(sqlkey, param) + "");
  210. break;
  211. default:
  212. result = dao.find(sqlkey, param);
  213. }
  214. if (writeLog)
  215. PosSiteLog.writeLog(context, dao, sqlkey, paramList);
  216. return result;
  217. }
  218. // protected Object findByQueryStatement(String sql) {
  219. // return findByQueryStatement(sql, null);
  220. // }
  221. //
  222. // protected Object findByQueryStatement(String sql, PosParameter param) {
  223. // PosGenericDao dao = getDao();
  224. // if (dao == null) return null;
  225. // return dao.findByQueryStatement(sql, param);
  226. // }
  227. }