/*************************************************** * Copyright 2008-12-20, -sunzg,All rights reserved. * Create date : 2008-12-20 * Author : sunzg **************************************************/ package com.extUi.dao; import java.io.UnsupportedEncodingException; import java.sql.Timestamp; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.StringTokenizer; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import com.persistence.service.PersistenceFactory; import com.persistence.service.SysPersistence; import com.persistence.service.assitant.DataObject; import com.persistence.service.assitant.Field; import com.persistence.service.exception.PersistenceException; import com.persistence.service.exception.TransformTypeException; import com.sysmodel.Util; import com.sysmodel.datamodel.xmlmodel.ModelFactory; import com.sysmodel.datamodel.xmlmodel.Reference; import com.sysmodel.datamodel.xmlmodel.able.MdpAttribute; import com.sysmodel.datamodel.xmlmodel.able.MdpClass; import com.sysmodel.datamodel.xmlmodel.able.MdpConstant; import com.sysmodel.datamodel.xmlmodel.able.SysModel; import com.sysmodel.datamodel.xmlmodel.impl.MdpAttributeImpl; public class DataFormatJsonableImpl implements DataFormatJsonable{ private Log log = LogFactory.getLog(DataFormatJsonableImpl.class); /* * (non-Javadoc) * * @see * com.extUi.dao.DataFormatJsonable#getJsonByFieldOfDataObject(com.persistence * .service.assitant.DataObject) */ public String getJsonByFieldOfDataObject(DataObject dataobject) { String classid = dataobject.getClassid(); SysModel sysmodel = ModelFactory.getSysmodel(); MdpClass mdpClass = sysmodel.getMdpClassByClassID(new Integer(classid)); Iterator it = dataobject.getAttributes().iterator(); Map map = new HashMap(); map.put("FD_OBJECTID" + classid, dataobject.getObjectID()); while (it.hasNext()) { Field field = it.next(); // log.info(field.getFieldname() + " 类型 :" + field.getDataType() + // " 值:"+ field.getFieldvalue()); if (field.getDataType().equals("date")) { Timestamp valueTemp = (Timestamp) field.getFieldvalue(); map.put(field.getFieldname() + "_" + classid, Util.getDateTime(field.getScale(), valueTemp)); } else { MdpAttribute mdpAttribute = mdpClass.getMdpAttributeByName(field.getFieldname()); if (mdpAttribute.getReferenceType() == 2 && mdpAttribute.getReference() != null) { MdpConstant mdpConstant = sysmodel.getMdpConstantByName(mdpAttribute .getReference().getReferenceTable()); map.put(field.getFieldname() + "_" + classid + "_show", mdpConstant .getMdpConstantDisplayString((String) field.getFieldvalue()).trim()); } else if (mdpAttribute.getReferenceType() == 1 && mdpAttribute.getReference() != null) { Reference reference = mdpAttribute.getReference(); map.put(field.getFieldname() + "_" + classid + "_show", (String) this.getReferenceFieldName(reference, field).getValue( reference.getDisplayName())); } // add by sunzg to set null to "" Object tempObj = field.getFieldvalue(); if (tempObj == null) tempObj = new String(""); map.put(field.getFieldname() + "_" + classid, tempObj); } } JSONObject jsonObject = JSONObject.fromObject(map); return jsonObject.toString(); } @SuppressWarnings("rawtypes") public Map FormatDataObject(DataObject dataobject) { String classid = dataobject.getClassid(); SysModel sysmodel = ModelFactory.getSysmodel(); MdpClass mdpClass = sysmodel.getMdpClassByClassID(new Integer(classid)); Iterator it = dataobject.getAttributes().iterator(); Map map = new HashMap(); map.put("FD_OBJECTID" + classid, dataobject.getObjectID()); while (it.hasNext()) { Field field = it.next(); // log.info(field.getFieldname() + " 类型 :" + field.getDataType() + // " 值:"+ field.getFieldvalue()); if (field.getDataType().equals("date")) { Timestamp valueTemp = (Timestamp) field.getFieldvalue(); map.put(field.getFieldname() + "_" + classid, Util.getDateTime(field.getScale(), valueTemp)); } else { MdpAttribute mdpAttribute = mdpClass.getMdpAttributeByName(field.getFieldname()); if (mdpAttribute.getReferenceType() == 2 && mdpAttribute.getReference() != null) { MdpConstant mdpConstant = sysmodel.getMdpConstantByName(mdpAttribute .getReference().getReferenceTable()); map.put(field.getFieldname() + "_" + classid + "_show", mdpConstant .getMdpConstantDisplayString((String) field.getFieldvalue()).trim()); } else if (mdpAttribute.getReferenceType() == 1 && mdpAttribute.getReference() != null) { Reference reference = mdpAttribute.getReference(); map.put(field.getFieldname() + "_" + classid + "_show", (String) this.getReferenceFieldName(reference, field).getValue( reference.getDisplayName())); } // add by sunzg to set null to "" Object tempObj = field.getFieldvalue(); if (tempObj == null) tempObj = new String(""); map.put(field.getFieldname() + "_" + classid, tempObj); } } return map; } public String getJsonByFieldOfDataObjectdateString(DataObject dataobject) { String classid = dataobject.getClassid(); SysModel sysmodel = ModelFactory.getSysmodel(); MdpClass mdpClass = sysmodel.getMdpClassByClassID(new Integer(classid)); Iterator it = dataobject.getAttributes().iterator(); Map map = new HashMap(); map.put("FD_OBJECTID" + classid, dataobject.getObjectID()); while (it.hasNext()) { Field field = it.next(); // log.info(field.getFieldname() + " 类型 :" + field.getDataType() + // " 值:"+ field.getFieldvalue()); if (field.getDataType().equals("date")) { Timestamp valueTemp = Timestamp.valueOf(String.valueOf(field.getFieldvalue())); map.put(field.getFieldname() + "_" + classid, Util.getDateTime(field.getScale(), valueTemp)); } else { MdpAttribute mdpAttribute = mdpClass.getMdpAttributeByName(field.getFieldname()); if (mdpAttribute.getReferenceType() == 2 && mdpAttribute.getReference() != null) { MdpConstant mdpConstant = sysmodel.getMdpConstantByName(mdpAttribute .getReference().getReferenceTable()); map.put(field.getFieldname() + "_" + classid + "_show", mdpConstant .getMdpConstantDisplayString((String) field.getFieldvalue()).trim()); } else if (mdpAttribute.getReferenceType() == 1 && mdpAttribute.getReference() != null) { Reference reference = mdpAttribute.getReference(); map.put(field.getFieldname() + "_" + classid + "_show", (String) this.getReferenceFieldName(reference, field).getValue( reference.getDisplayName())); } // add by sunzg to set null to "" Object tempObj = field.getFieldvalue(); if (tempObj == null) tempObj = new String(""); map.put(field.getFieldname() + "_" + classid, tempObj); } } JSONObject jsonObject = JSONObject.fromObject(map); return jsonObject.toString(); } // add by wuxiao 2009-04-17 private DataObject getReferenceFieldName(Reference reference, Field field) { DataObject DataObject = new DataObject(); SysModel sysmodel = ModelFactory.getSysmodel(); SysPersistence persistence = PersistenceFactory.getInstance(sysmodel); String con = ""; ArrayList dataObjects = new ArrayList(); if (field.getDataType().equals("number")) { con = " where " + reference.getStoreName() + "=" + (String) field.getFieldvalue(); } else { con = " where " + reference.getStoreName() + "='" + (String) field.getFieldvalue() + "'"; } try { dataObjects = persistence .searchAllData(new Integer(reference.getReferenceTable()), con); } catch (NumberFormatException e) { e.printStackTrace(); } catch (TransformTypeException e) { e.printStackTrace(); } catch (PersistenceException e) { e.printStackTrace(); } if (!dataObjects.isEmpty() && dataObjects.size() > 0) { DataObject = dataObjects.get(0); } return DataObject; } public String getJsonByFieldOfDataObjectForStrDate(DataObject dataobject) { String classid = dataobject.getClassid(); SysModel sysmodel = ModelFactory.getSysmodel(); MdpClass mdpClass = sysmodel.getMdpClassByClassID(new Integer(classid)); Iterator it = dataobject.getAttributes().iterator(); Map map = new HashMap(); map.put("FD_OBJECTID" + classid, dataobject.getObjectID()); while (it.hasNext()) { Field field = it.next(); // log.info(field.getFieldname() + " 类型 :" + field.getDataType() + // " 值:"+ field.getFieldvalue()); MdpAttribute mdpAttribute = mdpClass.getMdpAttributeByName(field.getFieldname()); if (mdpAttribute.getReferenceType() == 2 && mdpAttribute.getReference() != null) { MdpConstant mdpConstant = sysmodel.getMdpConstantByName(mdpAttribute.getReference() .getReferenceTable()); map.put(field.getFieldname() + "_" + classid + "_show", mdpConstant .getMdpConstantDisplayString((String) field.getFieldvalue()).trim()); } // add by sunzg to set null to "" Object tempObj = field.getFieldvalue(); if (tempObj == null) tempObj = new String(""); map.put(field.getFieldname() + "_" + classid, tempObj); } JSONObject jsonObject = JSONObject.fromObject(map); return jsonObject.toString(); } public StringBuffer getJsonByFieldOfDataObjects(ArrayList dataArry) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < dataArry.size(); i++) { DataObject dataobject = dataArry.get(i); sb.append(this.getJsonByFieldOfDataObject(dataobject)); } return sb; } public StringBuffer getJsonByListidAndMaps(String listid, ArrayList> dataArry) { StringBuffer sb = new StringBuffer(); /** * 算法:先获得所要进行查询的SELECT列集合,包括显示列,主键列,参数列 * 拿出从持久化服务层中获得的MAP数据,把上述集合格式化成大写,与其KEY做对比,相同的把用上边的集合替换KEY */ return sb; } public StringBuffer getJsonByStringArrys(ArrayList dataArry) { StringBuffer sb = new StringBuffer(); /** * 针对持久化服务层,由组合SQL查询出的数组数据集合 把这类数据格式化成JSON数组形式的数据进行返回 */ return sb; } public StringBuffer getJsonByMaps(ArrayList> dataArry) { StringBuffer sb = new StringBuffer(); return sb; } @SuppressWarnings("unused") public static void main(String[] args) throws Exception { String json = "{name:'wuxiao',date:'2009-12-15',children:[{child:'123',have:'hand'},{child:'456',have:'head'}]}"; JSONObject jsonMap = JSONObject.fromObject(json); JSONArray children = jsonMap.getJSONArray("children"); for (int i = 0; i < children.size(); i++) { JSONObject child = children.getJSONObject(i); } } public DataObject getDataObjectBySplit(String classid, String params) { SysModel sysmodel = ModelFactory.getSysmodel(); MdpClass mdpClass = sysmodel.getMdpClassByClassID(new Integer(classid)); List attributes = mdpClass.getAllMdpAttributes(); DataObject object = new DataObject(); object.setClassid(classid); try { params = java.net.URLDecoder.decode(params, "UTF-8"); for (int i = 0; i < attributes.size(); i++) { MdpAttributeImpl attribute = attributes.get(i); if (!attribute.isValidate()) continue; // 这个地方只接受页面中DATAMODEL中名称+ “-” 表ID的采集科目 String value = getParameter(attribute.getName() + "_" + classid, params) == null ? "" : getParameter(attribute.getName() + "_" + classid, params); if (!value.equals("")) { Field field = new Field(); field.setFieldname(attribute.getName()); field.setFieldvalue(value); object.addAttribute(field); } else { if (attribute.getName().equalsIgnoreCase("UpdateDate")) { Field field = new Field(); field.setFieldname(attribute.getName()); field.setFieldvalue(Util.getDateTime()); object.addAttribute(field); } } } } catch (UnsupportedEncodingException e) { log.error("字符转换出现错误:", e); } return object; } /** * 获取服务参数 * * @author wzj * @param key * --参数名,params--服务得到的参数集合字符串 */ public static String getParameter(String key, String params) { String str = ""; String key_value = key + "="; String temp_str = ""; StringTokenizer commaToker = new StringTokenizer(params, "&"); while (commaToker.hasMoreTokens()) { temp_str = commaToker.nextToken(); if (temp_str.indexOf(key_value, 0) == 0) { if (key_value.equals(temp_str)) { } else { str += temp_str.substring(key_value.length(), temp_str.length()) + ","; } } } String result = ""; if (str.equals("")) { result = null; } else { result = str.substring(0, str.length() - 1); } return result; } }