123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358 |
- /***************************************************
- * 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<Field> it = dataobject.getAttributes().iterator();
- Map<String, Object> map = new HashMap<String, Object>();
- 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<Field> it = dataobject.getAttributes().iterator();
- Map<String, Object> map = new HashMap<String, Object>();
- 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<Field> it = dataobject.getAttributes().iterator();
- Map<String, Object> map = new HashMap<String, Object>();
- 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<DataObject> dataObjects = new ArrayList<DataObject>();
- 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<Field> it = dataobject.getAttributes().iterator();
- Map<String, Object> map = new HashMap<String, Object>();
- 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<DataObject> 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<Map<String, String>> dataArry) {
- StringBuffer sb = new StringBuffer();
- /**
- * 算法:先获得所要进行查询的SELECT列集合,包括显示列,主键列,参数列
- * 拿出从持久化服务层中获得的MAP数据,把上述集合格式化成大写,与其KEY做对比,相同的把用上边的集合替换KEY
- */
- return sb;
- }
- public StringBuffer getJsonByStringArrys(ArrayList<String[]> dataArry) {
- StringBuffer sb = new StringBuffer();
- /**
- * 针对持久化服务层,由组合SQL查询出的数组数据集合 把这类数据格式化成JSON数组形式的数据进行返回
- */
- return sb;
- }
- public StringBuffer getJsonByMaps(ArrayList<Map<String, String>> 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<MdpAttributeImpl> 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;
- }
- }
|