a3864eec4d8eb339444c085d5ceefdd060538c3b.svn-base 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409
  1. package com.formaction.dao.assistant;
  2. import java.io.Reader;
  3. import java.sql.Blob;
  4. import java.sql.Clob;
  5. import java.sql.Connection;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.text.ParseException;
  10. import java.text.SimpleDateFormat;
  11. import java.util.ArrayList;
  12. import java.util.HashMap;
  13. import java.util.Iterator;
  14. import java.util.List;
  15. import java.util.Map;
  16. import net.sf.json.JSONArray;
  17. import net.sf.json.JSONObject;
  18. import org.apache.log4j.Logger;
  19. import com.formaction.Utils;
  20. import com.formaction.vo.ReqDataObject;
  21. import com.formaction.vo.RowSet;
  22. import com.persistence.DbConnection;
  23. import com.persistence.service.JDBCHelper;
  24. import com.persistence.service.PersistenceFactory;
  25. import com.persistence.service.SysPersistence;
  26. import com.persistence.service.assitant.DataObject;
  27. import com.persistence.service.exception.PersistenceException;
  28. import com.sysmodel.datamodel.xmlmodel.ModelFactory;
  29. import com.sysmodel.datamodel.xmlmodel.able.MdpAttribute;
  30. import com.sysmodel.datamodel.xmlmodel.able.MdpClass;
  31. import com.sysmodel.datamodel.xmlmodel.able.MdpDataSource;
  32. import com.sysmodel.datamodel.xmlmodel.able.SysModel;
  33. import com.sysmodel.datamodel.xmlmodel.impl.MdpAttributeImpl;
  34. import com.sysmodel.datamodel.xmlmodel.impl.MdpClassImpl;
  35. import com.sysmodel.listmodel.xmlmodel.able.ListField;
  36. import com.sysmodel.listmodel.xmlmodel.impl.FormListImpl;
  37. import com.sysmodel.listmodel.xmlmodel.impl.ListFieldImpl;
  38. import com.sysmodel.listmodel.xmlmodel.impl.PramFieldImpl;
  39. import com.sysmodel.listmodel.xmlmodel.impl.QueryFieldImpl;
  40. import com.toolkit.date.DateHelper;
  41. public class AssistantImpl implements AssistantAble{
  42. /**
  43. * 将前台表单提交到后台的数据,格式化成通用DataObject对象
  44. *
  45. * @param params
  46. * 表单数据
  47. * @return DataObject 通用数据对象
  48. */
  49. public ReqDataObject generalDataObjectFromSoaPrams(String params) {
  50. String classid = Utils.getParameter("classid", params);
  51. int iClassid = new Integer(classid).intValue();
  52. MdpClass mdpClass = sysmodel.getMdpClassByClassID(iClassid);
  53. List<MdpAttributeImpl> attributes = mdpClass.getAllMdpAttributes();
  54. ReqDataObject object = new ReqDataObject();
  55. object.setClassid(classid);
  56. // 设置主键id值
  57. String objectID = Utils.getParameter("FD_OBJECTID" + classid, params);
  58. if (objectID != null && !objectID.equals(""))
  59. object.setObjectID(objectID);
  60. for (int i = 0; i < attributes.size(); i++) {
  61. MdpAttributeImpl attribute = attributes.get(i);
  62. if (!attribute.isValidate())
  63. continue;
  64. // 这个地方只接受页面中DATAMODEL中名称+ “-” 表ID的采集科目
  65. String value = Utils.getParameter(attribute.getName().toUpperCase() + "_" + classid,
  66. params);// == null ? "" :
  67. // 由于在修改时对于空字符不能更新到数据库--2010-8-11 王志军
  68. if (value != null) {
  69. object.addAttribute(Utils.getFieldObj(attribute.getName(), value));
  70. log.info(attribute.getName() + " : " + value);
  71. }
  72. if (attribute.getName().equalsIgnoreCase("UpdateDate")) {
  73. object.addAttribute(Utils.getFieldObj(attribute.getName(), DateHelper.getDateTime()));
  74. }
  75. if (attribute.getName().equalsIgnoreCase("Is_Del")) {
  76. object.addAttribute(Utils.getFieldObj(attribute.getName(), "0"));
  77. }
  78. }
  79. // 获取子表数据对象集合
  80. String childrenParms = Utils.getParameter("children", params) == null ? "" : Utils
  81. .getParameter("children", params);
  82. if (!childrenParms.equals("")) {
  83. object.getChildobject().addAll(this.getChildrenDatasFromJsonParams(childrenParms));
  84. }
  85. return object;
  86. }
  87. /**
  88. * 将前台表单提交到后台的子表数据,格式化成通用DataObject对象集合
  89. *
  90. * @param params
  91. * 表单数据
  92. * @return DataObject 通用数据对象
  93. */
  94. public ArrayList<ReqDataObject> getChildrenDatasFromJsonParams(String childrenParms) {
  95. ArrayList<ReqDataObject> childObjects = new ArrayList<ReqDataObject>();
  96. childrenParms = "{children:" + childrenParms + "}";
  97. JSONObject jsonMap = JSONObject.fromObject(childrenParms);
  98. JSONArray children = jsonMap.getJSONArray("children");
  99. for (int i = 0; i < children.size(); i++) {
  100. JSONObject childObj = children.getJSONObject(i);
  101. String classid = childObj.getString("classid");
  102. int iClassid = new Integer(classid).intValue();
  103. MdpClass mdpClass = sysmodel.getMdpClassByClassID(iClassid);
  104. log.info("...............子表记录" + (i + 1) + "[" + mdpClass.getName()
  105. + mdpClass.getDescription() + "] ................");
  106. List<MdpAttributeImpl> attributes = mdpClass.getAllMdpAttributes();
  107. ReqDataObject dataObject = new ReqDataObject();
  108. dataObject.setClassid(classid);
  109. log.info(childrenParms);
  110. String objectID = childObj.getString("FD_OBJECTID" + classid);
  111. log.info("objectID----=======" + objectID + "===-----");
  112. log.info("dataObject=======" + dataObject.getObjectID());
  113. // log.info(objectID!= "");
  114. if (objectID != null && !objectID.trim().equals("")) {
  115. // dataObject.setObjectID(objectID);
  116. }
  117. log.info("dataObject=======" + dataObject.getObjectID());
  118. for (int j = 0; j < attributes.size(); j++) {
  119. MdpAttributeImpl attribute = attributes.get(j);
  120. if (!attribute.isValidate())
  121. continue;
  122. // 这个地方只接受页面中DATAMODEL中名称+ “-” 表ID的采集科目
  123. String key = attribute.getName().toUpperCase() + "_" + classid;
  124. if (childObj.has(key)) {
  125. String value = childObj.getString(key);
  126. log.info(attribute.getName() + " : " + value);
  127. if (!attribute.getName().toUpperCase().equals("FD_PARENTID")) {
  128. if (value != null && !value.equals("null")) {
  129. dataObject.addAttribute(Utils.getFieldObj(attribute.getName(), value));
  130. } else {
  131. dataObject.addAttribute(Utils.getFieldObj(attribute.getName(), ""));
  132. }
  133. }
  134. } else {
  135. if (attribute.getName().equalsIgnoreCase("UpdateDate")) {
  136. dataObject.addAttribute(Utils.getFieldObj(attribute.getName(),
  137. DateHelper.getDateTime()));
  138. } else if (attribute.getName().equalsIgnoreCase("Is_Del")) {
  139. dataObject.addAttribute(Utils.getFieldObj(attribute.getName(), "0"));
  140. }
  141. }
  142. }
  143. childObjects.add(dataObject);
  144. }
  145. return childObjects;
  146. }
  147. /**
  148. * 删除相关从表数据
  149. *
  150. * @param classidArr
  151. * 从表集合
  152. * @param fd_parentid
  153. * 父表主键id
  154. * @param helper
  155. * 数据持久化操作对象
  156. * @return 是否删除成功
  157. */
  158. public boolean deleteChildren(String[] classidArr, String fd_parentid, JDBCHelper helper)
  159. throws PersistenceException {
  160. boolean flag = true;
  161. for (int i = 0; i < classidArr.length; i++) {
  162. String classid = classidArr[i];
  163. log.info(classid);
  164. int iClassid = new Integer(classid).intValue();
  165. MdpClass mdpClass = sysmodel.getMdpClassByClassID(iClassid);
  166. flag = flag
  167. && this.delByParentId(mdpClass.getName(), fd_parentid, helper,
  168. this.isRealDel(mdpClass));
  169. }
  170. return flag;
  171. }
  172. /**
  173. * 根据从表记录集合,获取所属配置classid数组
  174. *
  175. * @param childrens
  176. * 从表记录集合
  177. * @return
  178. */
  179. public String[] getChildrenClassidArr(ArrayList<DataObject> childrens) {
  180. String[] classidArr = new String[childrens.size()];
  181. if (childrens.size() > 0) {
  182. StringBuffer classids = new StringBuffer();
  183. for (int i = 0; i < childrens.size(); i++) {
  184. DataObject childObj = childrens.get(i);
  185. classidArr[i] = childObj.getClassid();
  186. if (classids.indexOf(childObj.getClassid()) < 0) {
  187. classids.append(childObj.getClassid()).append(",");
  188. }
  189. }
  190. if (classids.indexOf(",") > 0) {
  191. classids.reverse();
  192. classids.deleteCharAt(0);
  193. classids.reverse();
  194. }
  195. }
  196. return classidArr;
  197. }
  198. /**
  199. * 根据表配置,判断记录是否需要逻辑删除
  200. *
  201. * @param mdpClass
  202. * 内存表配置对象
  203. * @return
  204. */
  205. public boolean isRealDel(MdpClass mdpClass) {
  206. boolean flag = true;
  207. List<MdpAttributeImpl> attributes = mdpClass.getAllMdpAttributes();
  208. for (int j = 0; j < attributes.size(); j++) {
  209. MdpAttributeImpl attribute = attributes.get(j);
  210. if (!attribute.isValidate())
  211. continue;
  212. if (attribute.getName().equalsIgnoreCase("is_Del")) {
  213. flag = false;
  214. break;
  215. }
  216. }
  217. return flag;
  218. }
  219. private boolean delByParentId(String table, String fd_parentid, JDBCHelper helper,
  220. boolean realDel) throws PersistenceException {
  221. boolean flag = true;
  222. String sql = "";
  223. Connection con = helper.getConnection();
  224. PreparedStatement ps = null;
  225. try {
  226. int count = 0;
  227. if (realDel) {
  228. sql = "delete from " + table + " where fd_parentid ='" + fd_parentid + "'";
  229. ps = con.prepareStatement(sql);
  230. count = ps.executeUpdate();
  231. } else {
  232. sql = "update " + table + " set is_Del=?,UpdateDate=? where fd_parentid ='"
  233. + fd_parentid + "'";
  234. ps = con.prepareStatement(sql);
  235. ps.setString(1, "1");
  236. ps.setTimestamp(2, DateHelper.getTimestampDateTime());
  237. count = ps.executeUpdate();
  238. }
  239. if (count <= 0) {
  240. String search = "select count(*) from " + table + " where fd_parentid ='"
  241. + fd_parentid + "'";
  242. ps = con.prepareStatement(search);
  243. ResultSet rs = ps.executeQuery();
  244. if (rs.next())
  245. flag = rs.getInt(1) == 0;
  246. rs.close();
  247. }
  248. ps.close();
  249. } catch (SQLException e) {
  250. throw new PersistenceException(e.getMessage());
  251. }
  252. return flag;
  253. }
  254. /**
  255. * 将前台提交的参数(默认条件、查询表单参数)格式化为统一的条件
  256. *
  257. * @param params
  258. * 服务获取到的参数字符串
  259. * @return
  260. */
  261. @SuppressWarnings({ "rawtypes", "unused" })
  262. public String getSQLCondition(String params, FormListImpl formList) {
  263. // FormListImpl formList =
  264. // (FormListImpl)XFormFactory.getXFormModel().getFormList(listId);
  265. String conditionStr = Utils.getParameter("condition", params) == null ? "" : Utils
  266. .getParameter("condition", params);
  267. if (conditionStr.indexOf("$") > -1) {
  268. conditionStr = conditionStr.replace("$", "%");
  269. }
  270. log.info("params===" + params);
  271. StringBuffer condition = new StringBuffer();
  272. String conditionTemp = "";
  273. ArrayList queryFields = formList.getQueryFields();
  274. for (int i = 0; i < queryFields.size(); i++) {
  275. QueryFieldImpl queryField = (QueryFieldImpl) queryFields.get(i);
  276. String fieldName = queryField.getDataName().toUpperCase() + "_"
  277. + queryField.getClassid();
  278. String fieldValue = Utils.getParameter(fieldName, params) == null ? "" : Utils
  279. .getParameter(fieldName, params);
  280. String operator = queryField.getOperator();
  281. MdpClassImpl mdpClass = (MdpClassImpl) sysmodel.getMdpClassByClassID(queryField
  282. .getClassid());
  283. String tableName = mdpClass.getName();
  284. if (!fieldValue.equals("")) {
  285. String fieldNameDataType = null;
  286. if (mdpClass.getMdpAttributeByName(queryField.getDataName()).getDataType()
  287. .equals("string")) {
  288. if (operator.equals("like")) {
  289. condition.append(mdpClass.getName() + "." + queryField.getDataName()
  290. + " like '%" + fieldValue + "%' and ");
  291. } else {
  292. condition.append(mdpClass.getName() + "." + queryField.getDataName() + "='"
  293. + fieldValue + "' and ");
  294. }
  295. } else if (mdpClass.getMdpAttributeByName(queryField.getDataName()).getDataType()
  296. .equals("number")) {
  297. condition.append(mdpClass.getName() + "." + queryField.getDataName() + "="
  298. + fieldValue + " and ");
  299. } else if (mdpClass.getMdpAttributeByName(queryField.getDataName()).getDataType()
  300. .equals("date")) {
  301. String operation = "";
  302. String code = "";
  303. if (queryField.getOperator().equals("lessThan")) {
  304. operation = "<=";
  305. } else if (queryField.getOperator().equals("moreThan")) {
  306. operation = ">=";
  307. } else {
  308. operation = "=";
  309. }
  310. if (operation.equals("=")) {
  311. if (sysmodel.getDataSourceByCode(mdpClass.getDataSource()).getDataBase()
  312. .equals("db2")) {
  313. } else if (sysmodel.getDataSourceByCode(mdpClass.getDataSource())
  314. .getDataBase().equals("sqlserver")) {
  315. code = "(" + mdpClass.getName() + "." + queryField.getDataName()
  316. + " between '" + fieldValue + " 00:00:00' and '" + fieldValue
  317. + " 23:59:59') and ";
  318. } else if (sysmodel.getDataSourceByCode(mdpClass.getDataSource())
  319. .getDataBase().equals("oracle")) {
  320. code = "(" + mdpClass.getName() + "." + queryField.getDataName()
  321. + " between " + "to_date('" + fieldValue
  322. + " 00:00:00','yyyy-mm-dd hh24:mi:ss') and to_date('"
  323. + fieldValue + " 23:59:59','yyyy-mm-dd hh24:mi:ss')) and ";
  324. } else if (sysmodel.getDataSourceByCode(mdpClass.getDataSource())
  325. .getDataBase().equals("mysql")) {
  326. code = "(" + mdpClass.getName() + "." + queryField.getDataName()
  327. + " between " + "date_format('" + fieldValue
  328. + " 00:00:00','%Y-%m-%d %T') and date_format('" + fieldValue
  329. + " 23:59:59','%Y-%m-%d %T')) and ";
  330. } else if (sysmodel.getDataSourceByCode(mdpClass.getDataSource())
  331. .getDataBase().equals("dameng")) {
  332. code = "(" + mdpClass.getName() + "." + queryField.getDataName()
  333. + " between " + "to_date('" + fieldValue
  334. + " 00:00:00','yyyy-mm-dd hh24:mi:ss') and to_date('"
  335. + fieldValue + " 23:59:59','yyyy-mm-dd hh24:mi:ss')) and ";
  336. }
  337. } else if (operation.equals("<=") || operation.equals(">=")) {
  338. if (sysmodel.getDataSourceByCode(mdpClass.getDataSource()).getDataBase()
  339. .equals("db2")) {
  340. } else if (sysmodel.getDataSourceByCode(mdpClass.getDataSource())
  341. .getDataBase().equals("sqlserver")) {
  342. code = mdpClass.getName() + "." + queryField.getDataName() + " "
  343. + operation + " '" + fieldValue + " ' and ";
  344. } else if (sysmodel.getDataSourceByCode(mdpClass.getDataSource())
  345. .getDataBase().equals("oracle")) {
  346. code = mdpClass.getName() + "." + queryField.getDataName() + " "
  347. + operation + " to_date('" + fieldValue
  348. + " ','yyyy-mm-dd hh24:mi:ss') and ";
  349. } else if (sysmodel.getDataSourceByCode(mdpClass.getDataSource())
  350. .getDataBase().equals("mysql")) {
  351. code = mdpClass.getName() + "." + queryField.getDataName() + " "
  352. + operation + " date_format('" + fieldValue
  353. + " ','%Y-%m-%d %T') and ";
  354. } else if (sysmodel.getDataSourceByCode(mdpClass.getDataSource())
  355. .getDataBase().equals("dameng")) {
  356. code = mdpClass.getName() + "." + queryField.getDataName() + " "
  357. + operation + " to_date('" + fieldValue
  358. + " ','yyyy-mm-dd hh24:mi:ss') and ";
  359. }
  360. } else {
  361. code = mdpClass.getName() + "." + queryField.getDataName() + " "
  362. + operation + " '" + fieldValue + " ' and ";
  363. }
  364. condition.append(code);
  365. }
  366. // //***********************增加模型中关于时间、数据段查询的功能(by peterkong
  367. // 2010.08.17)*************************************************//
  368. // if((fieldName.indexOf("_FROM") != -1 ||
  369. // fieldName.indexOf("_TO") != -1) && fieldValue != null &&
  370. // !fieldValue.equals("")){
  371. // if(fieldName.indexOf("_FROM") != -1){
  372. // fieldNameDataType =
  373. // fieldName.substring(0,fieldName.indexOf("_FROM"));
  374. // }else if(fieldName.indexOf("_TO") != -1){
  375. // fieldNameDataType =
  376. // fieldName.substring(0,fieldName.indexOf("_TO"));
  377. // }
  378. // // fieldNameDataType =
  379. // fieldName.substring(0,fieldName.indexOf("_FROM"));
  380. // fieldName = fieldNameDataType;
  381. // }
  382. // String dataType =
  383. // mdpClass.getMdpAttributeByName(queryField.getDataName())
  384. // .getDataType();
  385. // if ("string".equals(dataType)) {
  386. // if ("like".equals(operator)) {
  387. // condition.append(tableName).append(".").append(fieldName).append(" like '%").append(fieldValue).append("%'").append(" and ");
  388. // } else {
  389. // condition.append(tableName).append(".").append(fieldName).append("='").append(fieldValue).append("'").append(" and ");
  390. // }
  391. // } else {
  392. // if ("lessThan".equals(operator)) {
  393. // if ("number".equals(dataType)){
  394. // condition.append(tableName).append(".").append(fieldName).append(" <= ").append(fieldValue).append(" and ");
  395. // }else{
  396. // condition.append(tableName).append(".").append(fieldName).append(" <= '").append(fieldValue).append("'").append(" and ");
  397. // }
  398. // } else if ("moreThan".equals(operator)) {
  399. // if ("number".equals(dataType)){
  400. // condition.append(tableName).append(".").append(fieldName).append(" >= ").append(fieldValue).append(" and ");
  401. // }else{
  402. // condition.append(tableName).append(".").append(fieldName).append(" >= '").append(fieldValue).append("'").append(" and ");
  403. // }
  404. //
  405. // } else {
  406. // condition.append(tableName).append(".").append(fieldName).append("=").append(fieldValue).append(" and ");
  407. // }
  408. // }
  409. // //************************************结束**************************************************************************************
  410. }
  411. }
  412. if (condition.lastIndexOf("and") > -1) {
  413. conditionTemp = condition.substring(0, condition.lastIndexOf("and"));
  414. }
  415. if (conditionTemp.length() > 0) {
  416. if (conditionStr.length() > 0) {
  417. conditionStr = conditionTemp + " and " + conditionStr;
  418. } else {
  419. conditionStr = conditionTemp;
  420. }
  421. }
  422. log.info("conditionStr---" + conditionStr);
  423. return conditionStr;
  424. }
  425. /**
  426. * 根据传入的参数,拼装sql语句
  427. *
  428. * @param listId
  429. * @param condition
  430. * @return
  431. */
  432. @SuppressWarnings({ "rawtypes", "null", "unused" })
  433. public StringBuffer getSearchSql(FormListImpl formList, String condition) {
  434. // StringBuffer queryTable = new StringBuffer();
  435. // StringBuffer queryWhere = new StringBuffer();
  436. SysModel sysmodel = ModelFactory.getSysmodel();
  437. StringBuffer sql = new StringBuffer();
  438. ArrayList<String> tables = formList.getQueryTables();
  439. String tName = "";
  440. int dbFlag = 0; // 0:oracle , 1:db2 , 2:sqlserver2005
  441. /**
  442. * sqlserver中,如果加 order by 排序,会抛异常 异常消息为: 除非另外还指定了 TOP 或 FOR
  443. * XML,否则,ORDER BY 子句在视图、内联函数、派生表、子查询和公用表表达式中无效。 加上top的话,就没问题了, 赵敏
  444. *
  445. * 但是加上10的话,分页就没用了
  446. */
  447. int pageSize = formList.getQueryList().getPageSize();
  448. MdpDataSource dataSource = sysmodel.getDataSourceByCode(1);
  449. String dataBaseType = dataSource.getDataBase();
  450. if (dataBaseType.equals("sqlserver")) {
  451. sql.append("select top 100 PERCENT ");
  452. } else if (dataBaseType.equals("oracle")) {
  453. sql.append("select ");
  454. } else if (dataBaseType.equals("mysql")) {
  455. sql.append("select ");
  456. } else if (dataBaseType.equals("dameng")) {
  457. sql.append("select ");
  458. } else {
  459. sql.append("select ");
  460. }
  461. for (int i = 0; i < tables.size(); i++) {
  462. String table = tables.get(i);
  463. if (table != null || !table.equals("")) {
  464. String afield = "FD_OBJECTID" + table;
  465. int iclassid = Integer.parseInt(table);
  466. MdpClass mdpClass = sysmodel.getMdpClassByClassID(iclassid);
  467. if (sysmodel.getDataSourceByCode(mdpClass.getDataSource()).getDataBase()
  468. .equals("db2")) {
  469. dbFlag = 1;
  470. } else if (sysmodel.getDataSourceByCode(mdpClass.getDataSource()).getDataBase()
  471. .equals("sqlserver")) {
  472. dbFlag = 2;
  473. }
  474. sql.append(mdpClass.getName()).append(".FD_OBJECTID").append(" ").append(afield)
  475. .append(",");
  476. }
  477. }
  478. // log.info(FormList.getListid()+ FormList.getDescription());
  479. List listFields = formList.getQueryList().getlistFields();
  480. for (int j = 0; j < listFields.size(); j++) {
  481. ListField field = (ListField) listFields.get(j);
  482. if (field.getShowType().equals("0")) {
  483. MdpClass mdpClass = sysmodel.getMdpClassByClassID(field.getClassid());
  484. MdpAttribute MdpAttribute = mdpClass.getMdpAttributeByName(field.getDataName());
  485. String afield = field.getDataName() + "_" + mdpClass.getClassid();
  486. // log.info(afield);
  487. // if (MdpAttribute.getReferenceType() == 1
  488. // && MdpAttribute.getReference() != null) {
  489. // // String tableName = "";
  490. // // String referenceTable = MdpAttribute.getReference()
  491. // // .getReferenceTable();
  492. // // if (referenceTable != null || "".equals(referenceTable)) {
  493. // // tableName = sysmodel.getMdpClassByClassID(
  494. // // Integer.parseInt(referenceTable)).getName();
  495. // // }
  496. // // if(queryTable.indexOf(tableName)<0){
  497. // // queryTable.append("").append(tableName).append(",");
  498. // // }
  499. // //
  500. // // queryWhere.append(" ").append(mdpClass.getName()).append(
  501. // // ".").append(field.getDataName()).append("=")
  502. // // .append(tableName).append(".").append(
  503. // // MdpAttribute.getReference().getStoreName())
  504. // // .append(" and");
  505. // // sql.append(tableName + "."
  506. // // + MdpAttribute.getReference().getStoreName() + " "
  507. // // + afield + " , " + tableName + "."
  508. // // + MdpAttribute.getReference().getDisplayName()
  509. // // + " " + afield + "_show ,");
  510. // } else {
  511. sql.append(mdpClass.getName() + "." + field.getDataName() + " " + afield + " , ");
  512. // }
  513. }
  514. }
  515. List paramFields = formList.getlistPramField();
  516. for (int i = 0; i < paramFields.size(); i++) {
  517. PramFieldImpl PramField = (PramFieldImpl) paramFields.get(i);
  518. MdpClass mdpClass = sysmodel.getMdpClassByClassID(PramField.getClassid());
  519. String afield = "param" + PramField.getName() + "_" + mdpClass.getClassid();
  520. sql.append(mdpClass.getName() + "." + PramField.getName() + " " + afield + " , ");
  521. }
  522. // if( dbFlag == 1){//db2
  523. // sql.append("rownumber() over(ORDER BY ");
  524. // if(!formList.getQueryList().getOrderBy().equals("")){
  525. // sql.append(formList.getQueryList().getOrderBy());
  526. // if(formList.getQueryList().getOrderBy().indexOf(".")==-1){
  527. // tName = formList.getQueryList().getOrderBy().substring(1,
  528. // formList.getQueryList().getOrderBy().indexOf("."));
  529. // }
  530. // } else {
  531. // int iclassid = Integer.parseInt((String)tables.get(0));
  532. // sql.append(sysmodel.getMdpClassByClassID(iclassid).getName()).append(".FD_OBJECTID ");
  533. // tName = sysmodel.getMdpClassByClassID(iclassid).getName();
  534. // }
  535. // sql.append(" ) AS ROWNUM_");
  536. // } else if (dbFlag == 0){//oracle
  537. // sql.deleteCharAt(sql.lastIndexOf(","));
  538. // } else if (dbFlag == 2){//sql server 2005
  539. // sql.append(" ROW_NUMBER() Over(order by ");
  540. // if(!formList.getQueryList().getOrderBy().equals("")){
  541. // sql.append(formList.getQueryList().getOrderBy());
  542. // if(formList.getQueryList().getOrderBy().indexOf(".")==-1){
  543. // tName = formList.getQueryList().getOrderBy().substring(1,
  544. // formList.getQueryList().getOrderBy().indexOf("."));
  545. // }
  546. // } else {
  547. // int iclassid = Integer.parseInt((String)tables.get(0));
  548. // sql.append(sysmodel.getMdpClassByClassID(iclassid).getName()).append(".FD_OBJECTID ");
  549. // tName = sysmodel.getMdpClassByClassID(iclassid).getName();
  550. // }
  551. // sql.append(") as ROWNUM_ ");
  552. // }
  553. sql.deleteCharAt(sql.lastIndexOf(","));
  554. sql.append(" from ");
  555. for (int i = 0; i < tables.size(); i++) {
  556. String table = (String) tables.get(i);
  557. if (table != null || !table.equals("")) {
  558. int iclassid = Integer.parseInt(table);
  559. MdpClass mdpClass = sysmodel.getMdpClassByClassID(iclassid);
  560. sql.append(mdpClass.getName()).append(",");
  561. }
  562. }
  563. // sql.append(queryTable);
  564. sql.deleteCharAt(sql.lastIndexOf(","));
  565. condition += formList.getQueryList().equals("") ? "" : " "
  566. + formList.getQueryList().getCondition();
  567. // if( dbFlag == 2){
  568. if (!condition.trim().equals("")) {
  569. sql.append(" where ").append(condition);
  570. }
  571. // String query = queryWhere.toString() == null ? "" : queryWhere
  572. // .toString();
  573. //
  574. // if (!query.equals("")) {
  575. // queryWhere.reverse();
  576. // queryWhere.delete(0, 3);
  577. // queryWhere.reverse();
  578. // if (sql.indexOf("where") == -1) {
  579. // sql.append(" where ").append(queryWhere);
  580. // } else {
  581. // sql.append(" and ").append(queryWhere);
  582. // }
  583. //
  584. // }
  585. // }
  586. // if( dbFlag == 1){//db2
  587. // sql.append(") as ").append(tName).append(" where ").append(tName).append(".ROWNUM_");
  588. // }
  589. // log.info("查询sql = " + sql);
  590. return sql;
  591. }
  592. /**
  593. * 由于查询附件列表数据时候 为了提高查询速度不应该把附件内容查询出来,该方法拼装sql语句时不查询附件内容字段
  594. *
  595. * @param listId
  596. * @param condition
  597. * @param notfield
  598. * 不需要拼装的属性 张勇波 2012-4-11
  599. * @return
  600. */
  601. @SuppressWarnings({ "rawtypes", "unused", "null" })
  602. public StringBuffer getSearSqlNotFiled(FormListImpl formList, String condition, String notfield) {
  603. // StringBuffer queryTable = new StringBuffer();
  604. // StringBuffer queryWhere = new StringBuffer();
  605. SysModel sysmodel = ModelFactory.getSysmodel();
  606. StringBuffer sql = new StringBuffer();
  607. ArrayList<String> tables = formList.getQueryTables();
  608. String tName = "";
  609. int dbFlag = 0; // 0:oracle , 1:db2 , 2:sqlserver2005
  610. /**
  611. * sqlserver中,如果加 order by 排序,会抛异常 异常消息为: 除非另外还指定了 TOP 或 FOR
  612. * XML,否则,ORDER BY 子句在视图、内联函数、派生表、子查询和公用表表达式中无效。 加上top的话,就没问题了, 赵敏
  613. *
  614. * 但是加上10的话,分页就没用了
  615. */
  616. int pageSize = formList.getQueryList().getPageSize();
  617. MdpDataSource dataSource = sysmodel.getDataSourceByCode(1);
  618. String dataBaseType = dataSource.getDataBase();
  619. if (dataBaseType.equals("sqlserver")) {
  620. sql.append("select top 100 PERCENT ");
  621. } else if (dataBaseType.equals("oracle")) {
  622. sql.append("select ");
  623. }
  624. for (int i = 0; i < tables.size(); i++) {
  625. String table = tables.get(i);
  626. if (table != null || !table.equals("")) {
  627. String afield = "FD_OBJECTID" + table;
  628. int iclassid = Integer.parseInt(table);
  629. MdpClass mdpClass = sysmodel.getMdpClassByClassID(iclassid);
  630. if (sysmodel.getDataSourceByCode(mdpClass.getDataSource()).getDataBase()
  631. .equals("db2")) {
  632. dbFlag = 1;
  633. } else if (sysmodel.getDataSourceByCode(mdpClass.getDataSource()).getDataBase()
  634. .equals("sqlserver")) {
  635. dbFlag = 2;
  636. }
  637. sql.append(mdpClass.getName()).append(".FD_OBJECTID").append(" ").append(afield)
  638. .append(",");
  639. }
  640. }
  641. // log.info(FormList.getListid()+ FormList.getDescription());
  642. List listFields = formList.getQueryList().getlistFields();
  643. for (int j = 0; j < listFields.size(); j++) {
  644. ListField field = (ListField) listFields.get(j);
  645. if (field.getShowType().equals("0")) {
  646. MdpClass mdpClass = sysmodel.getMdpClassByClassID(field.getClassid());
  647. String fieldname = field.getDataName();
  648. // 过滤不需要拼装的属性
  649. if (!fieldname.equals(notfield)) {
  650. String afield = field.getDataName() + "_" + mdpClass.getClassid();
  651. sql.append(mdpClass.getName() + "." + field.getDataName() + " " + afield
  652. + " , ");
  653. }
  654. }
  655. }
  656. log.info("Sql=" + sql);
  657. List paramFields = formList.getlistPramField();
  658. for (int i = 0; i < paramFields.size(); i++) {
  659. PramFieldImpl PramField = (PramFieldImpl) paramFields.get(i);
  660. MdpClass mdpClass = sysmodel.getMdpClassByClassID(PramField.getClassid());
  661. String afield = "param" + PramField.getName() + "_" + mdpClass.getClassid();
  662. sql.append(mdpClass.getName() + "." + PramField.getName() + " " + afield + " , ");
  663. }
  664. sql.deleteCharAt(sql.lastIndexOf(","));
  665. sql.append(" from ");
  666. for (int i = 0; i < tables.size(); i++) {
  667. String table = (String) tables.get(i);
  668. if (table != null || !table.equals("")) {
  669. int iclassid = Integer.parseInt(table);
  670. MdpClass mdpClass = sysmodel.getMdpClassByClassID(iclassid);
  671. sql.append(mdpClass.getName()).append(",");
  672. }
  673. }
  674. sql.deleteCharAt(sql.lastIndexOf(","));
  675. condition += formList.getQueryList().equals("") ? "" : " "
  676. + formList.getQueryList().getCondition();
  677. // if( dbFlag == 2){
  678. if (!condition.trim().equals("")) {
  679. sql.append(" where ").append(condition);
  680. }
  681. return sql;
  682. }
  683. /**
  684. * 查询总记录数
  685. *
  686. * @return
  687. */
  688. public int getTotalLines(String sql) {
  689. int num = 0;
  690. sql = "SELECT count(*) FROM ( " + sql + ") t";
  691. try {
  692. num = persistence.getFunctionNumber(99, sql);
  693. } catch (PersistenceException e) {
  694. e.printStackTrace();
  695. }
  696. return num;
  697. }
  698. /**
  699. * 根据系统默认数据源、第一条记录数、页面显示限制条数,计算最后一条记录数
  700. *
  701. * @param start
  702. * 第一条记录数
  703. * @param limit
  704. * 页面显示限制条数
  705. * @return
  706. */
  707. public int getLastNumByDataSourse(int start, int limit, String dataBase) {
  708. int end = 0;
  709. if (dataBase.equals("sqlserver")) {
  710. end = start + limit + 1;
  711. } else if (dataBase.equals("db2")) {
  712. if (start == 0) {
  713. end = start + limit;
  714. } else {
  715. end = start + limit - 1;
  716. }
  717. }
  718. return end;
  719. }
  720. /**
  721. * 根据传入的参数,拼装分页sql语句
  722. *
  723. * @param listId
  724. * 配置id
  725. * @param sql
  726. * 查询语句
  727. * @return
  728. */
  729. public StringBuffer getPagingSearchSql(StringBuffer sql, String firstPageNum, String limit) {
  730. MdpDataSource dataSource = sysmodel.getDataSourceByCode(sysmodel.getMdpClassByClassID(99)
  731. .getDataSource());
  732. StringBuffer pagingSql = new StringBuffer();
  733. // int lastPageNum = this.getLastNumByDataSourse(new
  734. // Integer(firstPageNum),
  735. // new Integer(limit), dataSource.getDataBase());
  736. int startRecordNum = Integer.parseInt(firstPageNum) * Integer.parseInt(limit);
  737. if (dataSource.getDataBase().equals("oracle")) {
  738. // pagingSql.append("SELECT * FROM ( SELECT row_.*, rownum rownum_ FROM (");
  739. pagingSql.append(sql);
  740. // pagingSql.append(") row_ WHERE rownum <= ").append(startRecordNum).append(") WHERE rownum_ > ").append(firstPageNum);
  741. } else if (dataSource.getDataBase().equals("dameng")) {
  742. // pagingSql.append("SELECT * FROM ( SELECT row_.*, rownum rownum_ FROM (");
  743. pagingSql.append(sql);
  744. // pagingSql.append(") row_ WHERE rownum <= ").append(startRecordNum).append(") WHERE rownum_ > ").append(firstPageNum);
  745. } else if (dataSource.getDataBase().equals("db2")) {
  746. pagingSql.append("SELECT * FROM ( ");
  747. pagingSql.append(sql);
  748. pagingSql.append(")as a where ROWNUM_ BETWEEN ").append(firstPageNum).append(" and ")
  749. .append(startRecordNum);
  750. } else if (dataSource.getDataBase().equals("sqlserver")) {
  751. pagingSql.append("SELECT * FROM (");
  752. pagingSql.append(sql);
  753. pagingSql.append(") as t where t.ROWNUM_ > " + firstPageNum + " and t.ROWNUM_ < "
  754. + startRecordNum);
  755. } else if (dataSource.getDataBase().equals("mysql")) {
  756. // int startRecordNum =
  757. // Integer.parseInt(firstPageNum)*Integer.parseInt(limit);
  758. pagingSql.append("select * from (");
  759. pagingSql.append(sql);
  760. pagingSql.append(") t ");
  761. pagingSql.append(" limit " + startRecordNum + "," + limit);
  762. }
  763. log.info("分页查询sql = " + pagingSql);
  764. return pagingSql;
  765. }
  766. /**
  767. * 根据sql查询表数据
  768. *
  769. * @param sql
  770. * @return
  771. */
  772. public RowSet queryAllDatas(FormListImpl formList, String sql) {
  773. log.info("查询sql:" + sql);
  774. Connection con = null;
  775. PreparedStatement ps = null;
  776. ResultSet rs = null;
  777. RowSet res = new RowSet();
  778. ArrayList<ListFieldImpl> listFields = formList.getQueryList().getlistFields();
  779. try {
  780. DbConnection DbConnection = new DbConnection();
  781. con = DbConnection.getConnection();
  782. ps = con.prepareStatement(sql);
  783. rs = ps.executeQuery();
  784. int i = 0; // 字段数
  785. // String[] cols=null; //记录行值数组
  786. Map<String, String> map = null;
  787. i = rs.getMetaData().getColumnCount();
  788. String[] fileds = new String[i];
  789. for (int m = 0; m < i; m++)
  790. fileds[m] = rs.getMetaData().getColumnName(m + 1).toUpperCase();
  791. while (rs.next()) {
  792. map = new HashMap<String, String>();
  793. // for (int j = 0; j < i; j++) {
  794. // map.put(fileds[j], rs.getString(j+1));
  795. // }
  796. map.put("classid", formList.getListid());
  797. for (int n = 0; n < i; n++) {
  798. // 拼sql时,字段已经增加了CLASSID
  799. String key = fileds[n];
  800. String value = "";
  801. int type = rs.getMetaData().getColumnType(n + 1);
  802. // ----赵志强 2011-10-25 修改日期格式化问题 出现1970 bug-----begin--//
  803. if (type == java.sql.Types.TIME) {
  804. SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
  805. java.sql.Timestamp value_time = rs.getTimestamp(n + 1);
  806. if (value_time != null)
  807. value = sf.format(value_time);
  808. } else if (type == java.sql.Types.TIMESTAMP) {
  809. java.sql.Timestamp value_tt = rs.getTimestamp(n + 1);
  810. SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
  811. if (value_tt != null)
  812. value = sf.format(value_tt);
  813. } else if (type == java.sql.Types.DATE) {
  814. java.sql.Timestamp value_tt = rs.getTimestamp(n + 1);
  815. SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  816. if (value_tt != null)
  817. value = sf.format(value_tt);
  818. } else {
  819. value = rs.getString(n + 1);
  820. }
  821. // ----赵志强 2011-10-25 修改日期格式化问题-----end--//
  822. map.put(key, value);
  823. for (int j = 0; j < listFields.size(); j++) {
  824. ListFieldImpl ListFieldImpl = (ListFieldImpl) listFields.get(j);
  825. String keyTemp = ListFieldImpl.getDataName().toUpperCase() + "_"
  826. + ListFieldImpl.getClassid();
  827. if (keyTemp.equals(key)) {
  828. if (ListFieldImpl.getShowType().equals("0")) {
  829. MdpClass mdpClass = sysmodel.getMdpClassByClassID(ListFieldImpl
  830. .getClassid());
  831. MdpAttribute MdpAttribute = mdpClass
  832. .getMdpAttributeByName(ListFieldImpl.getDataName());
  833. if (MdpAttribute.getReferenceType() == 1) {
  834. String showKkey = ListFieldImpl.getDataName().toUpperCase()
  835. + "_" + ListFieldImpl.getClassid() + "_SHOW";
  836. String showValue = value;
  837. if (MdpAttribute.getReference() != null) {
  838. String referenceTable = MdpAttribute.getReference()
  839. .getReferenceTable();
  840. String displayName = MdpAttribute.getReference()
  841. .getDisplayName();
  842. String storeName = MdpAttribute.getReference()
  843. .getStoreName();
  844. showValue = this.getReferenceShow(value,
  845. Integer.valueOf(referenceTable), displayName,
  846. storeName);
  847. }
  848. map.put(showKkey, showValue);
  849. }
  850. if (MdpAttribute.getReferenceType() == 2) {
  851. String showKkey = ListFieldImpl.getDataName().toUpperCase()
  852. + "_" + ListFieldImpl.getClassid() + "_SHOW";
  853. String showValue = value;
  854. if (MdpAttribute.getReference() != null) {
  855. String referenceTable = MdpAttribute.getReference()
  856. .getReferenceTable();
  857. showValue = sysmodel.getMdpConstantByName(referenceTable)
  858. .getMdpConstantDisplayString(value);
  859. }
  860. map.put(showKkey, showValue);
  861. }
  862. }
  863. }
  864. }
  865. }
  866. res.getRows().add(map);
  867. }
  868. } catch (SQLException e) {
  869. e.printStackTrace();
  870. log.error(e.toString());
  871. } catch (ClassNotFoundException e) {
  872. log.error(e.toString());
  873. } catch (NumberFormatException e) {
  874. log.error(e.toString());
  875. } catch (PersistenceException e) {
  876. log.error(e.toString());
  877. } finally {
  878. try {
  879. if (rs != null)
  880. rs.close();
  881. if (ps != null)
  882. ps.close();
  883. if (con != null)
  884. con.close();
  885. } catch (SQLException e) {
  886. log.error(e.toString());
  887. }
  888. }
  889. return res;
  890. }
  891. public RowSet queryPagingAllDatas(FormListImpl formList, String sql, String firstPageNum,
  892. String limit) {
  893. // log.info("查询firstPageNum:" + firstPageNum);
  894. // log.info("查询limit:" + limit);
  895. // log.info("查询sql:" + sql);
  896. // conn.prepareStatement(sql,游标类型,能否更新记录);
  897. // 游标类型:
  898. // ResultSet.TYPE_FORWORD_ONLY:只进游标
  899. // ResultSet.TYPE_SCROLL_INSENSITIVE:可滚动。但是不受其他用户对数据库更改的影响。
  900. // ResultSet.TYPE_SCROLL_SENSITIVE:可滚动。当其他用户更改数据库时这个记录也会改变。
  901. // 能否更新记录:
  902. // ResultSet.CONCUR_READ_ONLY,只读
  903. // ResultSet.CONCUR_UPDATABLE,可更新
  904. Connection con = null;
  905. PreparedStatement pstat = null;
  906. ResultSet rs = null;
  907. RowSet res = new RowSet();
  908. ArrayList<ListFieldImpl> listFields = formList.getQueryList().getlistFields();
  909. try {
  910. // ServerConfigure sc = ServerConfigure.getServerConfigerInstance();
  911. // con = sc.getDataSource().getConnection();
  912. DbConnection DbConnection = new DbConnection();
  913. con = DbConnection.getConnection();
  914. /**
  915. * 适用于qui
  916. */
  917. int maxCount = Integer.valueOf(limit);
  918. int startNo = (Integer.valueOf(firstPageNum) - 1) * maxCount + 1;
  919. int MaxRows = startNo + maxCount - 1;
  920. // int maxCount = Integer.valueOf(limit);
  921. // int startNo = Integer.valueOf(firstPageNum) + 1;
  922. // int MaxRows = startNo + maxCount - 1;
  923. /**
  924. * [start] 当使用mysql数据库时 方法中的参数sql已包含分页如:select * from table limit
  925. * 0,5 游标定死
  926. */
  927. MdpDataSource dataSource = sysmodel.getDataSourceByCode(sysmodel.getMdpClassByClassID(
  928. 99).getDataSource());
  929. if (dataSource.getDataBase().equals("mysql")) {
  930. startNo = 1;
  931. MaxRows = maxCount;
  932. }
  933. /**** [end] ****/
  934. pstat = con.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE,
  935. ResultSet.CONCUR_READ_ONLY);
  936. // 最大查询到第几条记录
  937. pstat.setMaxRows(MaxRows);
  938. rs = pstat.executeQuery();
  939. if (null != rs && rs.next()) {
  940. // 将游标移动到第一条记录
  941. rs.first();
  942. // 游标移动到要输出的第一条记录
  943. rs.relative(startNo - 2);
  944. // PreparedStatement ps = con.prepareStatement(sql);
  945. // ResultSet rs = ps.executeQuery();
  946. // boolean k = true;
  947. int i = 0; // 字段数
  948. // String[] cols = null; // 记录行值数组
  949. Map<String, String> map = null;
  950. i = rs.getMetaData().getColumnCount();
  951. String[] fileds = new String[i];
  952. for (int m = 0; m < i; m++)
  953. fileds[m] = rs.getMetaData().getColumnName(m + 1).toUpperCase();
  954. while (rs.next()) {
  955. map = new HashMap<String, String>();
  956. // MdpClass mdpClass =
  957. // sysmodel.getMdpClassByClassID(classid);
  958. // DataObject dataobject =
  959. // TypeTransformer.getSearchDataObject(mdpClass);
  960. map.put("classid", formList.getListid());
  961. for (int n = 0; n < i; n++) {
  962. String key = fileds[n];
  963. String value = "";
  964. int type = rs.getMetaData().getColumnType(n + 1);
  965. if (type == java.sql.Types.TIME) {
  966. SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
  967. java.sql.Timestamp value_time = rs.getTimestamp(n + 1);
  968. if (value_time != null)
  969. value = sf.format(value_time);
  970. } else if (type == java.sql.Types.TIMESTAMP) {
  971. java.sql.Timestamp value_tt = rs.getTimestamp(n + 1);
  972. SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
  973. if (value_tt != null)
  974. value = sf.format(value_tt);
  975. } else if (type == java.sql.Types.DATE) {
  976. java.sql.Timestamp value_tt = rs.getTimestamp(n + 1);
  977. SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  978. if (value_tt != null)
  979. value = sf.format(value_tt);
  980. } else if (type == java.sql.Types.CLOB) {
  981. Clob clob = rs.getClob(n + 1);
  982. if (clob != null && clob.length() != 0) {
  983. // value = clob.getSubString((long) 1, (int)
  984. // clob.length());
  985. value = Clob2String(clob);// 调用clob转string的方法
  986. }
  987. } else if (type == java.sql.Types.BLOB) {
  988. Blob blob = rs.getBlob(n + 1);
  989. if (blob != null && blob.length() != 0) {
  990. value = Blob2String(blob);// 调用blob转string的方法
  991. }
  992. } else {
  993. value = rs.getString(n + 1);
  994. }
  995. map.put(key, value);
  996. for (int j = 0; j < listFields.size(); j++) {
  997. ListFieldImpl ListFieldImpl = (ListFieldImpl) listFields.get(j);
  998. String keyTemp = ListFieldImpl.getDataName().toUpperCase() + "_"
  999. + ListFieldImpl.getClassid();
  1000. if (keyTemp.equals(key)) {
  1001. if (ListFieldImpl.getShowType().equals("0")) {
  1002. MdpClass mdpClass = sysmodel.getMdpClassByClassID(ListFieldImpl
  1003. .getClassid());
  1004. MdpAttribute MdpAttribute = mdpClass
  1005. .getMdpAttributeByName(ListFieldImpl.getDataName());
  1006. if (MdpAttribute.getReferenceType() == 1) {
  1007. String showKkey = ListFieldImpl.getDataName().toUpperCase()
  1008. + "_" + ListFieldImpl.getClassid() + "_SHOW";
  1009. String showValue = value;
  1010. if (MdpAttribute.getReference() != null) {
  1011. String referenceTable = MdpAttribute.getReference()
  1012. .getReferenceTable();
  1013. String displayName = MdpAttribute.getReference()
  1014. .getDisplayName();
  1015. String storeName = MdpAttribute.getReference()
  1016. .getStoreName();
  1017. showValue = this.getReferenceShow(value,
  1018. Integer.valueOf(referenceTable), displayName,
  1019. storeName);
  1020. }
  1021. map.put(showKkey, showValue);
  1022. }
  1023. if (MdpAttribute.getReferenceType() == 2) {
  1024. String showKkey = ListFieldImpl.getDataName().toUpperCase()
  1025. + "_" + ListFieldImpl.getClassid() + "_SHOW";
  1026. String showValue = value;
  1027. if (MdpAttribute.getReference() != null) {
  1028. String referenceTable = MdpAttribute.getReference()
  1029. .getReferenceTable();
  1030. showValue = sysmodel.getMdpConstantByName(
  1031. referenceTable).getMdpConstantDisplayString(
  1032. value);
  1033. }
  1034. map.put(showKkey, showValue);
  1035. }
  1036. }
  1037. }
  1038. }
  1039. }
  1040. res.getRows().add(map);
  1041. }
  1042. }
  1043. } catch (SQLException e) {
  1044. e.printStackTrace();
  1045. } catch (ClassNotFoundException e) {
  1046. e.printStackTrace();
  1047. } catch (NumberFormatException e) {
  1048. e.printStackTrace();
  1049. } catch (PersistenceException e) {
  1050. e.printStackTrace();
  1051. } finally {
  1052. try {
  1053. if (rs != null)
  1054. rs.close();
  1055. if (pstat != null)
  1056. pstat.close();
  1057. if (con != null)
  1058. con.close();
  1059. } catch (SQLException e) {
  1060. e.printStackTrace();
  1061. }
  1062. }
  1063. return res;
  1064. }
  1065. /**
  1066. * clob to string 大字符串格式转换STRING
  1067. *
  1068. * @param clob
  1069. * @return 大字符串
  1070. *
  1071. */
  1072. public String Clob2String(Clob clob) {// Clob转换成String 的方法
  1073. String content = null;
  1074. StringBuffer stringBuf = new StringBuffer();
  1075. try {
  1076. int length = 0;
  1077. Reader inStream = clob.getCharacterStream(); // 取得大字侧段对象数据输出流
  1078. char[] buffer = new char[10];
  1079. while ((length = inStream.read(buffer)) != -1) // 读取数据库 //每10个10个读取
  1080. {
  1081. for (int i = 0; i < length; i++) {
  1082. stringBuf.append(buffer[i]);
  1083. }
  1084. }
  1085. inStream.close();
  1086. content = stringBuf.toString();
  1087. } catch (Exception ex) {
  1088. log.error(ex.getMessage(), ex);
  1089. }
  1090. return content;
  1091. }
  1092. /**
  1093. * blob to string 大字符串格式转换STRING
  1094. *
  1095. * @param clob
  1096. * @return 大字符串
  1097. *
  1098. */
  1099. public String Blob2String(Blob blob) {// blob转换成String 的方法
  1100. String content = null;
  1101. try {
  1102. byte[] bytes = blob.getBytes((long) 1, (int) blob.length()); // 取得大字侧段对象数据输出流
  1103. content = new String(bytes);
  1104. } catch (Exception ex) {
  1105. log.error(ex.getMessage(), ex);
  1106. }
  1107. return content;
  1108. }
  1109. private SysModel sysmodel = ModelFactory.getSysmodel();
  1110. private SysPersistence persistence = PersistenceFactory.getInstance(sysmodel);
  1111. private final Logger log = Logger.getLogger(getClass());
  1112. // public RowSet queryAllDatas(String sql) {
  1113. // Connection con = null;
  1114. // RowSet res = new RowSet();
  1115. // ServerConfigure sc = ServerConfigure.getServerConfigerInstance();
  1116. // try {
  1117. // con = sc.getDataSource().getConnection();
  1118. //
  1119. // PreparedStatement ps = con.prepareStatement(sql);
  1120. // ResultSet rs = ps.executeQuery();
  1121. // boolean k=true;
  1122. // int i=0; //字段数
  1123. // String[] cols=null; //记录行值数组
  1124. // while (rs.next()) {
  1125. // if (k) {
  1126. // i = rs.getMetaData().getColumnCount();
  1127. // for(int m=0;m<i;m++)
  1128. // res.getFields().add(rs.getMetaData().getColumnName(m+1).toUpperCase());
  1129. // cols = new String[i];
  1130. // k=false;
  1131. // }
  1132. //
  1133. // for (int j = 0; j < i; j++) {
  1134. // cols[j]=rs.getString(j+1);
  1135. // }
  1136. // ColumnSet cs = new ColumnSet(cols);
  1137. // res.getRows().add(cs);
  1138. //
  1139. // }
  1140. // } catch (SQLException e) {
  1141. // e.printStackTrace();
  1142. // log.error(e.toString());
  1143. // }finally{
  1144. // if(con!=null)
  1145. // try {
  1146. // con.close();
  1147. // } catch (SQLException e) {
  1148. // log.error(e.toString());
  1149. // e.printStackTrace();
  1150. // }
  1151. // }
  1152. // return res;
  1153. // }
  1154. /**
  1155. * 修改日期格式的问题
  1156. *
  1157. * @author koufengzhuo peterkong 项目中心项目中提取
  1158. */
  1159. @SuppressWarnings({ "rawtypes", "unchecked" })
  1160. public RowSet transformDate(RowSet rowSet, FormListImpl formList) {
  1161. ArrayList<String> dateColumns = new ArrayList<String>();
  1162. ArrayList<String> dateFormat = new ArrayList<String>();
  1163. SysModel sysmodel = ModelFactory.getSysmodel();
  1164. List<ListFieldImpl> listFields = formList.getQueryList().getlistFields();
  1165. int classid = 0;
  1166. MdpClassImpl mdpClass = null;
  1167. for (ListFieldImpl field : listFields) {
  1168. classid = field.getClassid();
  1169. mdpClass = (MdpClassImpl) sysmodel.getMdpClassByClassID(classid);
  1170. if (null == mdpClass) {
  1171. log.error(classid + ":对应的数据模型不存在!请检查DataModel.xml");
  1172. continue;
  1173. } else {
  1174. for (MdpAttributeImpl attribute : mdpClass.getAllMdpAttributes()) {
  1175. if (attribute.getDataType().toLowerCase().indexOf("date") < 0) {
  1176. continue;
  1177. } else {
  1178. dateColumns.add(attribute.getName().toUpperCase() + "_" + classid);
  1179. dateFormat.add(attribute.getScale());
  1180. }
  1181. }
  1182. }
  1183. }
  1184. List<Map> rows = rowSet.getRows();
  1185. for (Map row : rows) {
  1186. Iterator it = row.entrySet().iterator();
  1187. while (it.hasNext()) {
  1188. Map.Entry entry = (Map.Entry) it.next();
  1189. String key = (String) entry.getKey();
  1190. String value = (String) entry.getValue();
  1191. String format = "yyyy-MM-dd";
  1192. if (!dateColumns.contains(key)) {
  1193. continue;
  1194. } else {
  1195. for (int i = 0; i < dateColumns.size(); i++) {
  1196. if (key.equals(dateColumns.get(i))) {
  1197. format = dateFormat.get(i);
  1198. break;
  1199. }
  1200. }
  1201. value = chageFormt(value, format);
  1202. row.put(key, value);
  1203. }
  1204. }
  1205. }
  1206. return rowSet;
  1207. }
  1208. /**
  1209. * 转换日期格式,默认yyyy-MM-dd HH:mm:ss.S
  1210. *
  1211. * @author koufengzhuo peterkong
  1212. * @param dateStr
  1213. * @param format
  1214. * @return
  1215. */
  1216. private String chageFormt(String dateStr, String format) {
  1217. if (null == dateStr)
  1218. return null;
  1219. SimpleDateFormat sdf = new SimpleDateFormat(format);
  1220. SimpleDateFormat sdf_source = new SimpleDateFormat("yyyy-MM-dd HH:mm");
  1221. try {
  1222. if (null != dateStr && !dateStr.equals("")) {
  1223. dateStr = sdf.format(sdf_source.parse(dateStr));
  1224. }
  1225. } catch (ParseException e) {
  1226. e.printStackTrace();
  1227. }
  1228. return dateStr;
  1229. }
  1230. private String getReferenceShow(String value, int classid, String displayName, String storeName)
  1231. throws PersistenceException {
  1232. String sql = "";
  1233. if (sysmodel.getMdpClassByClassID(classid).getName().equals("Sys_Department")) {
  1234. sql = "select " + displayName + " from "
  1235. + sysmodel.getMdpClassByClassID(classid).getName() + " where is_del ='0' and "
  1236. + storeName + " = '" + value + "'";
  1237. } else {
  1238. /*sql = "select " + displayName + " from "
  1239. + sysmodel.getMdpClassByClassID(classid).getName() + " where " + storeName
  1240. + " = '" + value + "'";*/
  1241. if(value != null){
  1242. String temp[] = value.split(",");
  1243. StringBuffer sbtemp = new StringBuffer("");
  1244. int len = temp.length;
  1245. for (int i = 0; i < len; i++) {
  1246. if(i == (len-1)){
  1247. sbtemp.append("'"+temp[i]+"'");
  1248. }else{
  1249. sbtemp.append("'"+temp[i]+"',");
  1250. }
  1251. }
  1252. value = sbtemp.toString();
  1253. }
  1254. sql = "select " + displayName + " from "
  1255. + sysmodel.getMdpClassByClassID(classid).getName() + " where " + storeName
  1256. + " in (" + value + ")";
  1257. }
  1258. // log.info("referenceSql =="+sql);
  1259. ArrayList<Map<String, String>> list = persistence.getSearchResultToMap(classid, sql);
  1260. String result = "";
  1261. /*if (list.size() > 0) {
  1262. result = list.get(0).get(displayName.toUpperCase());
  1263. }*/
  1264. int listLength =list.size();
  1265. if ( listLength > 0) {
  1266. for (int i = 0; i < listLength; i++) {
  1267. if(i == (listLength-1)){
  1268. result += list.get(i).get(displayName.toUpperCase());
  1269. }else{
  1270. result += list.get(i).get(displayName.toUpperCase())+",";
  1271. }
  1272. }
  1273. }
  1274. return result;
  1275. }
  1276. }