889bb6ada2c3b8664a84dc95d07533d81c6d55e9.svn-base 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /***************************************************
  2. * Copyright 2008-12-20, -sunzg,All rights reserved.
  3. * Create date : 2008-12-20
  4. * Author : sunzg
  5. **************************************************/
  6. package com.extUi.dao;
  7. import java.io.UnsupportedEncodingException;
  8. import java.sql.Timestamp;
  9. import java.util.ArrayList;
  10. import java.util.HashMap;
  11. import java.util.Iterator;
  12. import java.util.List;
  13. import java.util.Map;
  14. import java.util.StringTokenizer;
  15. import org.apache.commons.logging.Log;
  16. import org.apache.commons.logging.LogFactory;
  17. import net.sf.json.JSONArray;
  18. import net.sf.json.JSONObject;
  19. import com.persistence.service.PersistenceFactory;
  20. import com.persistence.service.SysPersistence;
  21. import com.persistence.service.assitant.DataObject;
  22. import com.persistence.service.assitant.Field;
  23. import com.persistence.service.exception.PersistenceException;
  24. import com.persistence.service.exception.TransformTypeException;
  25. import com.sysmodel.Util;
  26. import com.sysmodel.datamodel.xmlmodel.ModelFactory;
  27. import com.sysmodel.datamodel.xmlmodel.Reference;
  28. import com.sysmodel.datamodel.xmlmodel.able.MdpAttribute;
  29. import com.sysmodel.datamodel.xmlmodel.able.MdpClass;
  30. import com.sysmodel.datamodel.xmlmodel.able.MdpConstant;
  31. import com.sysmodel.datamodel.xmlmodel.able.SysModel;
  32. import com.sysmodel.datamodel.xmlmodel.impl.MdpAttributeImpl;
  33. public class DataFormatJsonableImpl implements DataFormatJsonable{
  34. private Log log = LogFactory.getLog(DataFormatJsonableImpl.class);
  35. /*
  36. * (non-Javadoc)
  37. *
  38. * @see
  39. * com.extUi.dao.DataFormatJsonable#getJsonByFieldOfDataObject(com.persistence
  40. * .service.assitant.DataObject)
  41. */
  42. public String getJsonByFieldOfDataObject(DataObject dataobject) {
  43. String classid = dataobject.getClassid();
  44. SysModel sysmodel = ModelFactory.getSysmodel();
  45. MdpClass mdpClass = sysmodel.getMdpClassByClassID(new Integer(classid));
  46. Iterator<Field> it = dataobject.getAttributes().iterator();
  47. Map<String, Object> map = new HashMap<String, Object>();
  48. map.put("FD_OBJECTID" + classid, dataobject.getObjectID());
  49. while (it.hasNext()) {
  50. Field field = it.next();
  51. // log.info(field.getFieldname() + " 类型 :" + field.getDataType() +
  52. // " 值:"+ field.getFieldvalue());
  53. if (field.getDataType().equals("date")) {
  54. Timestamp valueTemp = (Timestamp) field.getFieldvalue();
  55. map.put(field.getFieldname() + "_" + classid,
  56. Util.getDateTime(field.getScale(), valueTemp));
  57. } else {
  58. MdpAttribute mdpAttribute = mdpClass.getMdpAttributeByName(field.getFieldname());
  59. if (mdpAttribute.getReferenceType() == 2 && mdpAttribute.getReference() != null) {
  60. MdpConstant mdpConstant = sysmodel.getMdpConstantByName(mdpAttribute
  61. .getReference().getReferenceTable());
  62. map.put(field.getFieldname() + "_" + classid + "_show", mdpConstant
  63. .getMdpConstantDisplayString((String) field.getFieldvalue()).trim());
  64. } else if (mdpAttribute.getReferenceType() == 1
  65. && mdpAttribute.getReference() != null) {
  66. Reference reference = mdpAttribute.getReference();
  67. map.put(field.getFieldname() + "_" + classid + "_show",
  68. (String) this.getReferenceFieldName(reference, field).getValue(
  69. reference.getDisplayName()));
  70. }
  71. // add by sunzg to set null to ""
  72. Object tempObj = field.getFieldvalue();
  73. if (tempObj == null)
  74. tempObj = new String("");
  75. map.put(field.getFieldname() + "_" + classid, tempObj);
  76. }
  77. }
  78. JSONObject jsonObject = JSONObject.fromObject(map);
  79. return jsonObject.toString();
  80. }
  81. @SuppressWarnings("rawtypes")
  82. public Map FormatDataObject(DataObject dataobject) {
  83. String classid = dataobject.getClassid();
  84. SysModel sysmodel = ModelFactory.getSysmodel();
  85. MdpClass mdpClass = sysmodel.getMdpClassByClassID(new Integer(classid));
  86. Iterator<Field> it = dataobject.getAttributes().iterator();
  87. Map<String, Object> map = new HashMap<String, Object>();
  88. map.put("FD_OBJECTID" + classid, dataobject.getObjectID());
  89. while (it.hasNext()) {
  90. Field field = it.next();
  91. // log.info(field.getFieldname() + " 类型 :" + field.getDataType() +
  92. // " 值:"+ field.getFieldvalue());
  93. if (field.getDataType().equals("date")) {
  94. Timestamp valueTemp = (Timestamp) field.getFieldvalue();
  95. map.put(field.getFieldname() + "_" + classid,
  96. Util.getDateTime(field.getScale(), valueTemp));
  97. } else {
  98. MdpAttribute mdpAttribute = mdpClass.getMdpAttributeByName(field.getFieldname());
  99. if (mdpAttribute.getReferenceType() == 2 && mdpAttribute.getReference() != null) {
  100. MdpConstant mdpConstant = sysmodel.getMdpConstantByName(mdpAttribute
  101. .getReference().getReferenceTable());
  102. map.put(field.getFieldname() + "_" + classid + "_show", mdpConstant
  103. .getMdpConstantDisplayString((String) field.getFieldvalue()).trim());
  104. } else if (mdpAttribute.getReferenceType() == 1
  105. && mdpAttribute.getReference() != null) {
  106. Reference reference = mdpAttribute.getReference();
  107. map.put(field.getFieldname() + "_" + classid + "_show",
  108. (String) this.getReferenceFieldName(reference, field).getValue(
  109. reference.getDisplayName()));
  110. }
  111. // add by sunzg to set null to ""
  112. Object tempObj = field.getFieldvalue();
  113. if (tempObj == null)
  114. tempObj = new String("");
  115. map.put(field.getFieldname() + "_" + classid, tempObj);
  116. }
  117. }
  118. return map;
  119. }
  120. public String getJsonByFieldOfDataObjectdateString(DataObject dataobject) {
  121. String classid = dataobject.getClassid();
  122. SysModel sysmodel = ModelFactory.getSysmodel();
  123. MdpClass mdpClass = sysmodel.getMdpClassByClassID(new Integer(classid));
  124. Iterator<Field> it = dataobject.getAttributes().iterator();
  125. Map<String, Object> map = new HashMap<String, Object>();
  126. map.put("FD_OBJECTID" + classid, dataobject.getObjectID());
  127. while (it.hasNext()) {
  128. Field field = it.next();
  129. // log.info(field.getFieldname() + " 类型 :" + field.getDataType() +
  130. // " 值:"+ field.getFieldvalue());
  131. if (field.getDataType().equals("date")) {
  132. Timestamp valueTemp = Timestamp.valueOf(String.valueOf(field.getFieldvalue()));
  133. map.put(field.getFieldname() + "_" + classid,
  134. Util.getDateTime(field.getScale(), valueTemp));
  135. } else {
  136. MdpAttribute mdpAttribute = mdpClass.getMdpAttributeByName(field.getFieldname());
  137. if (mdpAttribute.getReferenceType() == 2 && mdpAttribute.getReference() != null) {
  138. MdpConstant mdpConstant = sysmodel.getMdpConstantByName(mdpAttribute
  139. .getReference().getReferenceTable());
  140. map.put(field.getFieldname() + "_" + classid + "_show", mdpConstant
  141. .getMdpConstantDisplayString((String) field.getFieldvalue()).trim());
  142. } else if (mdpAttribute.getReferenceType() == 1
  143. && mdpAttribute.getReference() != null) {
  144. Reference reference = mdpAttribute.getReference();
  145. map.put(field.getFieldname() + "_" + classid + "_show",
  146. (String) this.getReferenceFieldName(reference, field).getValue(
  147. reference.getDisplayName()));
  148. }
  149. // add by sunzg to set null to ""
  150. Object tempObj = field.getFieldvalue();
  151. if (tempObj == null)
  152. tempObj = new String("");
  153. map.put(field.getFieldname() + "_" + classid, tempObj);
  154. }
  155. }
  156. JSONObject jsonObject = JSONObject.fromObject(map);
  157. return jsonObject.toString();
  158. }
  159. // add by wuxiao 2009-04-17
  160. private DataObject getReferenceFieldName(Reference reference, Field field) {
  161. DataObject DataObject = new DataObject();
  162. SysModel sysmodel = ModelFactory.getSysmodel();
  163. SysPersistence persistence = PersistenceFactory.getInstance(sysmodel);
  164. String con = "";
  165. ArrayList<DataObject> dataObjects = new ArrayList<DataObject>();
  166. if (field.getDataType().equals("number")) {
  167. con = " where " + reference.getStoreName() + "=" + (String) field.getFieldvalue();
  168. } else {
  169. con = " where " + reference.getStoreName() + "='" + (String) field.getFieldvalue()
  170. + "'";
  171. }
  172. try {
  173. dataObjects = persistence
  174. .searchAllData(new Integer(reference.getReferenceTable()), con);
  175. } catch (NumberFormatException e) {
  176. e.printStackTrace();
  177. } catch (TransformTypeException e) {
  178. e.printStackTrace();
  179. } catch (PersistenceException e) {
  180. e.printStackTrace();
  181. }
  182. if (!dataObjects.isEmpty() && dataObjects.size() > 0) {
  183. DataObject = dataObjects.get(0);
  184. }
  185. return DataObject;
  186. }
  187. public String getJsonByFieldOfDataObjectForStrDate(DataObject dataobject) {
  188. String classid = dataobject.getClassid();
  189. SysModel sysmodel = ModelFactory.getSysmodel();
  190. MdpClass mdpClass = sysmodel.getMdpClassByClassID(new Integer(classid));
  191. Iterator<Field> it = dataobject.getAttributes().iterator();
  192. Map<String, Object> map = new HashMap<String, Object>();
  193. map.put("FD_OBJECTID" + classid, dataobject.getObjectID());
  194. while (it.hasNext()) {
  195. Field field = it.next();
  196. // log.info(field.getFieldname() + " 类型 :" + field.getDataType() +
  197. // " 值:"+ field.getFieldvalue());
  198. MdpAttribute mdpAttribute = mdpClass.getMdpAttributeByName(field.getFieldname());
  199. if (mdpAttribute.getReferenceType() == 2 && mdpAttribute.getReference() != null) {
  200. MdpConstant mdpConstant = sysmodel.getMdpConstantByName(mdpAttribute.getReference()
  201. .getReferenceTable());
  202. map.put(field.getFieldname() + "_" + classid + "_show", mdpConstant
  203. .getMdpConstantDisplayString((String) field.getFieldvalue()).trim());
  204. }
  205. // add by sunzg to set null to ""
  206. Object tempObj = field.getFieldvalue();
  207. if (tempObj == null)
  208. tempObj = new String("");
  209. map.put(field.getFieldname() + "_" + classid, tempObj);
  210. }
  211. JSONObject jsonObject = JSONObject.fromObject(map);
  212. return jsonObject.toString();
  213. }
  214. public StringBuffer getJsonByFieldOfDataObjects(ArrayList<DataObject> dataArry) {
  215. StringBuffer sb = new StringBuffer();
  216. for (int i = 0; i < dataArry.size(); i++) {
  217. DataObject dataobject = dataArry.get(i);
  218. sb.append(this.getJsonByFieldOfDataObject(dataobject));
  219. }
  220. return sb;
  221. }
  222. public StringBuffer getJsonByListidAndMaps(String listid,
  223. ArrayList<Map<String, String>> dataArry) {
  224. StringBuffer sb = new StringBuffer();
  225. /**
  226. * 算法:先获得所要进行查询的SELECT列集合,包括显示列,主键列,参数列
  227. * 拿出从持久化服务层中获得的MAP数据,把上述集合格式化成大写,与其KEY做对比,相同的把用上边的集合替换KEY
  228. */
  229. return sb;
  230. }
  231. public StringBuffer getJsonByStringArrys(ArrayList<String[]> dataArry) {
  232. StringBuffer sb = new StringBuffer();
  233. /**
  234. * 针对持久化服务层,由组合SQL查询出的数组数据集合 把这类数据格式化成JSON数组形式的数据进行返回
  235. */
  236. return sb;
  237. }
  238. public StringBuffer getJsonByMaps(ArrayList<Map<String, String>> dataArry) {
  239. StringBuffer sb = new StringBuffer();
  240. return sb;
  241. }
  242. @SuppressWarnings("unused")
  243. public static void main(String[] args) throws Exception {
  244. String json = "{name:'wuxiao',date:'2009-12-15',children:[{child:'123',have:'hand'},{child:'456',have:'head'}]}";
  245. JSONObject jsonMap = JSONObject.fromObject(json);
  246. JSONArray children = jsonMap.getJSONArray("children");
  247. for (int i = 0; i < children.size(); i++) {
  248. JSONObject child = children.getJSONObject(i);
  249. }
  250. }
  251. public DataObject getDataObjectBySplit(String classid, String params) {
  252. SysModel sysmodel = ModelFactory.getSysmodel();
  253. MdpClass mdpClass = sysmodel.getMdpClassByClassID(new Integer(classid));
  254. List<MdpAttributeImpl> attributes = mdpClass.getAllMdpAttributes();
  255. DataObject object = new DataObject();
  256. object.setClassid(classid);
  257. try {
  258. params = java.net.URLDecoder.decode(params, "UTF-8");
  259. for (int i = 0; i < attributes.size(); i++) {
  260. MdpAttributeImpl attribute = attributes.get(i);
  261. if (!attribute.isValidate())
  262. continue;
  263. // 这个地方只接受页面中DATAMODEL中名称+ “-” 表ID的采集科目
  264. String value = getParameter(attribute.getName() + "_" + classid, params) == null ? ""
  265. : getParameter(attribute.getName() + "_" + classid, params);
  266. if (!value.equals("")) {
  267. Field field = new Field();
  268. field.setFieldname(attribute.getName());
  269. field.setFieldvalue(value);
  270. object.addAttribute(field);
  271. } else {
  272. if (attribute.getName().equalsIgnoreCase("UpdateDate")) {
  273. Field field = new Field();
  274. field.setFieldname(attribute.getName());
  275. field.setFieldvalue(Util.getDateTime());
  276. object.addAttribute(field);
  277. }
  278. }
  279. }
  280. } catch (UnsupportedEncodingException e) {
  281. log.error("字符转换出现错误:", e);
  282. }
  283. return object;
  284. }
  285. /**
  286. * 获取服务参数
  287. *
  288. * @author wzj
  289. * @param key
  290. * --参数名,params--服务得到的参数集合字符串
  291. */
  292. public static String getParameter(String key, String params) {
  293. String str = "";
  294. String key_value = key + "=";
  295. String temp_str = "";
  296. StringTokenizer commaToker = new StringTokenizer(params, "&");
  297. while (commaToker.hasMoreTokens()) {
  298. temp_str = commaToker.nextToken();
  299. if (temp_str.indexOf(key_value, 0) == 0) {
  300. if (key_value.equals(temp_str)) {
  301. } else {
  302. str += temp_str.substring(key_value.length(), temp_str.length()) + ",";
  303. }
  304. }
  305. }
  306. String result = "";
  307. if (str.equals("")) {
  308. result = null;
  309. } else {
  310. result = str.substring(0, str.length() - 1);
  311. }
  312. return result;
  313. }
  314. }