12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409 |
- package com.formaction.dao.assistant;
- import java.io.Reader;
- import java.sql.Blob;
- import java.sql.Clob;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Map;
- import net.sf.json.JSONArray;
- import net.sf.json.JSONObject;
- import org.apache.log4j.Logger;
- import com.formaction.Utils;
- import com.formaction.vo.ReqDataObject;
- import com.formaction.vo.RowSet;
- import com.persistence.DbConnection;
- import com.persistence.service.JDBCHelper;
- import com.persistence.service.PersistenceFactory;
- import com.persistence.service.SysPersistence;
- import com.persistence.service.assitant.DataObject;
- import com.persistence.service.exception.PersistenceException;
- import com.sysmodel.datamodel.xmlmodel.ModelFactory;
- import com.sysmodel.datamodel.xmlmodel.able.MdpAttribute;
- import com.sysmodel.datamodel.xmlmodel.able.MdpClass;
- import com.sysmodel.datamodel.xmlmodel.able.MdpDataSource;
- import com.sysmodel.datamodel.xmlmodel.able.SysModel;
- import com.sysmodel.datamodel.xmlmodel.impl.MdpAttributeImpl;
- import com.sysmodel.datamodel.xmlmodel.impl.MdpClassImpl;
- import com.sysmodel.listmodel.xmlmodel.able.ListField;
- import com.sysmodel.listmodel.xmlmodel.impl.FormListImpl;
- import com.sysmodel.listmodel.xmlmodel.impl.ListFieldImpl;
- import com.sysmodel.listmodel.xmlmodel.impl.PramFieldImpl;
- import com.sysmodel.listmodel.xmlmodel.impl.QueryFieldImpl;
- import com.toolkit.date.DateHelper;
- public class AssistantImpl implements AssistantAble{
- /**
- * 将前台表单提交到后台的数据,格式化成通用DataObject对象
- *
- * @param params
- * 表单数据
- * @return DataObject 通用数据对象
- */
- public ReqDataObject generalDataObjectFromSoaPrams(String params) {
- String classid = Utils.getParameter("classid", params);
- int iClassid = new Integer(classid).intValue();
- MdpClass mdpClass = sysmodel.getMdpClassByClassID(iClassid);
- List<MdpAttributeImpl> attributes = mdpClass.getAllMdpAttributes();
- ReqDataObject object = new ReqDataObject();
- object.setClassid(classid);
- // 设置主键id值
- String objectID = Utils.getParameter("FD_OBJECTID" + classid, params);
- if (objectID != null && !objectID.equals(""))
- object.setObjectID(objectID);
- for (int i = 0; i < attributes.size(); i++) {
- MdpAttributeImpl attribute = attributes.get(i);
- if (!attribute.isValidate())
- continue;
- // 这个地方只接受页面中DATAMODEL中名称+ “-” 表ID的采集科目
- String value = Utils.getParameter(attribute.getName().toUpperCase() + "_" + classid,
- params);// == null ? "" :
- // 由于在修改时对于空字符不能更新到数据库--2010-8-11 王志军
- if (value != null) {
- object.addAttribute(Utils.getFieldObj(attribute.getName(), value));
- log.info(attribute.getName() + " : " + value);
- }
- if (attribute.getName().equalsIgnoreCase("UpdateDate")) {
- object.addAttribute(Utils.getFieldObj(attribute.getName(), DateHelper.getDateTime()));
- }
- if (attribute.getName().equalsIgnoreCase("Is_Del")) {
- object.addAttribute(Utils.getFieldObj(attribute.getName(), "0"));
- }
- }
- // 获取子表数据对象集合
- String childrenParms = Utils.getParameter("children", params) == null ? "" : Utils
- .getParameter("children", params);
- if (!childrenParms.equals("")) {
- object.getChildobject().addAll(this.getChildrenDatasFromJsonParams(childrenParms));
- }
- return object;
- }
- /**
- * 将前台表单提交到后台的子表数据,格式化成通用DataObject对象集合
- *
- * @param params
- * 表单数据
- * @return DataObject 通用数据对象
- */
- public ArrayList<ReqDataObject> getChildrenDatasFromJsonParams(String childrenParms) {
- ArrayList<ReqDataObject> childObjects = new ArrayList<ReqDataObject>();
- childrenParms = "{children:" + childrenParms + "}";
- JSONObject jsonMap = JSONObject.fromObject(childrenParms);
- JSONArray children = jsonMap.getJSONArray("children");
- for (int i = 0; i < children.size(); i++) {
- JSONObject childObj = children.getJSONObject(i);
- String classid = childObj.getString("classid");
- int iClassid = new Integer(classid).intValue();
- MdpClass mdpClass = sysmodel.getMdpClassByClassID(iClassid);
- log.info("...............子表记录" + (i + 1) + "[" + mdpClass.getName()
- + mdpClass.getDescription() + "] ................");
- List<MdpAttributeImpl> attributes = mdpClass.getAllMdpAttributes();
- ReqDataObject dataObject = new ReqDataObject();
- dataObject.setClassid(classid);
- log.info(childrenParms);
- String objectID = childObj.getString("FD_OBJECTID" + classid);
- log.info("objectID----=======" + objectID + "===-----");
- log.info("dataObject=======" + dataObject.getObjectID());
- // log.info(objectID!= "");
- if (objectID != null && !objectID.trim().equals("")) {
- // dataObject.setObjectID(objectID);
- }
- log.info("dataObject=======" + dataObject.getObjectID());
- for (int j = 0; j < attributes.size(); j++) {
- MdpAttributeImpl attribute = attributes.get(j);
- if (!attribute.isValidate())
- continue;
- // 这个地方只接受页面中DATAMODEL中名称+ “-” 表ID的采集科目
- String key = attribute.getName().toUpperCase() + "_" + classid;
- if (childObj.has(key)) {
- String value = childObj.getString(key);
- log.info(attribute.getName() + " : " + value);
- if (!attribute.getName().toUpperCase().equals("FD_PARENTID")) {
- if (value != null && !value.equals("null")) {
- dataObject.addAttribute(Utils.getFieldObj(attribute.getName(), value));
- } else {
- dataObject.addAttribute(Utils.getFieldObj(attribute.getName(), ""));
- }
- }
- } else {
- if (attribute.getName().equalsIgnoreCase("UpdateDate")) {
- dataObject.addAttribute(Utils.getFieldObj(attribute.getName(),
- DateHelper.getDateTime()));
- } else if (attribute.getName().equalsIgnoreCase("Is_Del")) {
- dataObject.addAttribute(Utils.getFieldObj(attribute.getName(), "0"));
- }
- }
- }
- childObjects.add(dataObject);
- }
- return childObjects;
- }
- /**
- * 删除相关从表数据
- *
- * @param classidArr
- * 从表集合
- * @param fd_parentid
- * 父表主键id
- * @param helper
- * 数据持久化操作对象
- * @return 是否删除成功
- */
- public boolean deleteChildren(String[] classidArr, String fd_parentid, JDBCHelper helper)
- throws PersistenceException {
- boolean flag = true;
- for (int i = 0; i < classidArr.length; i++) {
- String classid = classidArr[i];
- log.info(classid);
- int iClassid = new Integer(classid).intValue();
- MdpClass mdpClass = sysmodel.getMdpClassByClassID(iClassid);
- flag = flag
- && this.delByParentId(mdpClass.getName(), fd_parentid, helper,
- this.isRealDel(mdpClass));
- }
- return flag;
- }
- /**
- * 根据从表记录集合,获取所属配置classid数组
- *
- * @param childrens
- * 从表记录集合
- * @return
- */
- public String[] getChildrenClassidArr(ArrayList<DataObject> childrens) {
- String[] classidArr = new String[childrens.size()];
- if (childrens.size() > 0) {
- StringBuffer classids = new StringBuffer();
- for (int i = 0; i < childrens.size(); i++) {
- DataObject childObj = childrens.get(i);
- classidArr[i] = childObj.getClassid();
- if (classids.indexOf(childObj.getClassid()) < 0) {
- classids.append(childObj.getClassid()).append(",");
- }
- }
- if (classids.indexOf(",") > 0) {
- classids.reverse();
- classids.deleteCharAt(0);
- classids.reverse();
- }
- }
- return classidArr;
- }
- /**
- * 根据表配置,判断记录是否需要逻辑删除
- *
- * @param mdpClass
- * 内存表配置对象
- * @return
- */
- public boolean isRealDel(MdpClass mdpClass) {
- boolean flag = true;
- List<MdpAttributeImpl> attributes = mdpClass.getAllMdpAttributes();
- for (int j = 0; j < attributes.size(); j++) {
- MdpAttributeImpl attribute = attributes.get(j);
- if (!attribute.isValidate())
- continue;
- if (attribute.getName().equalsIgnoreCase("is_Del")) {
- flag = false;
- break;
- }
- }
- return flag;
- }
- private boolean delByParentId(String table, String fd_parentid, JDBCHelper helper,
- boolean realDel) throws PersistenceException {
- boolean flag = true;
- String sql = "";
- Connection con = helper.getConnection();
- PreparedStatement ps = null;
- try {
- int count = 0;
- if (realDel) {
- sql = "delete from " + table + " where fd_parentid ='" + fd_parentid + "'";
- ps = con.prepareStatement(sql);
- count = ps.executeUpdate();
- } else {
- sql = "update " + table + " set is_Del=?,UpdateDate=? where fd_parentid ='"
- + fd_parentid + "'";
- ps = con.prepareStatement(sql);
- ps.setString(1, "1");
- ps.setTimestamp(2, DateHelper.getTimestampDateTime());
- count = ps.executeUpdate();
- }
- if (count <= 0) {
- String search = "select count(*) from " + table + " where fd_parentid ='"
- + fd_parentid + "'";
- ps = con.prepareStatement(search);
- ResultSet rs = ps.executeQuery();
- if (rs.next())
- flag = rs.getInt(1) == 0;
- rs.close();
- }
- ps.close();
- } catch (SQLException e) {
- throw new PersistenceException(e.getMessage());
- }
- return flag;
- }
- /**
- * 将前台提交的参数(默认条件、查询表单参数)格式化为统一的条件
- *
- * @param params
- * 服务获取到的参数字符串
- * @return
- */
- @SuppressWarnings({ "rawtypes", "unused" })
- public String getSQLCondition(String params, FormListImpl formList) {
- // FormListImpl formList =
- // (FormListImpl)XFormFactory.getXFormModel().getFormList(listId);
- String conditionStr = Utils.getParameter("condition", params) == null ? "" : Utils
- .getParameter("condition", params);
- if (conditionStr.indexOf("$") > -1) {
- conditionStr = conditionStr.replace("$", "%");
- }
- log.info("params===" + params);
- StringBuffer condition = new StringBuffer();
- String conditionTemp = "";
- ArrayList queryFields = formList.getQueryFields();
- for (int i = 0; i < queryFields.size(); i++) {
- QueryFieldImpl queryField = (QueryFieldImpl) queryFields.get(i);
- String fieldName = queryField.getDataName().toUpperCase() + "_"
- + queryField.getClassid();
- String fieldValue = Utils.getParameter(fieldName, params) == null ? "" : Utils
- .getParameter(fieldName, params);
- String operator = queryField.getOperator();
- MdpClassImpl mdpClass = (MdpClassImpl) sysmodel.getMdpClassByClassID(queryField
- .getClassid());
- String tableName = mdpClass.getName();
- if (!fieldValue.equals("")) {
- String fieldNameDataType = null;
- if (mdpClass.getMdpAttributeByName(queryField.getDataName()).getDataType()
- .equals("string")) {
- if (operator.equals("like")) {
- condition.append(mdpClass.getName() + "." + queryField.getDataName()
- + " like '%" + fieldValue + "%' and ");
- } else {
- condition.append(mdpClass.getName() + "." + queryField.getDataName() + "='"
- + fieldValue + "' and ");
- }
- } else if (mdpClass.getMdpAttributeByName(queryField.getDataName()).getDataType()
- .equals("number")) {
- condition.append(mdpClass.getName() + "." + queryField.getDataName() + "="
- + fieldValue + " and ");
- } else if (mdpClass.getMdpAttributeByName(queryField.getDataName()).getDataType()
- .equals("date")) {
- String operation = "";
- String code = "";
- if (queryField.getOperator().equals("lessThan")) {
- operation = "<=";
- } else if (queryField.getOperator().equals("moreThan")) {
- operation = ">=";
- } else {
- operation = "=";
- }
- if (operation.equals("=")) {
- if (sysmodel.getDataSourceByCode(mdpClass.getDataSource()).getDataBase()
- .equals("db2")) {
- } else if (sysmodel.getDataSourceByCode(mdpClass.getDataSource())
- .getDataBase().equals("sqlserver")) {
- code = "(" + mdpClass.getName() + "." + queryField.getDataName()
- + " between '" + fieldValue + " 00:00:00' and '" + fieldValue
- + " 23:59:59') and ";
- } else if (sysmodel.getDataSourceByCode(mdpClass.getDataSource())
- .getDataBase().equals("oracle")) {
- code = "(" + mdpClass.getName() + "." + queryField.getDataName()
- + " between " + "to_date('" + fieldValue
- + " 00:00:00','yyyy-mm-dd hh24:mi:ss') and to_date('"
- + fieldValue + " 23:59:59','yyyy-mm-dd hh24:mi:ss')) and ";
- } else if (sysmodel.getDataSourceByCode(mdpClass.getDataSource())
- .getDataBase().equals("mysql")) {
- code = "(" + mdpClass.getName() + "." + queryField.getDataName()
- + " between " + "date_format('" + fieldValue
- + " 00:00:00','%Y-%m-%d %T') and date_format('" + fieldValue
- + " 23:59:59','%Y-%m-%d %T')) and ";
- } else if (sysmodel.getDataSourceByCode(mdpClass.getDataSource())
- .getDataBase().equals("dameng")) {
- code = "(" + mdpClass.getName() + "." + queryField.getDataName()
- + " between " + "to_date('" + fieldValue
- + " 00:00:00','yyyy-mm-dd hh24:mi:ss') and to_date('"
- + fieldValue + " 23:59:59','yyyy-mm-dd hh24:mi:ss')) and ";
- }
- } else if (operation.equals("<=") || operation.equals(">=")) {
- if (sysmodel.getDataSourceByCode(mdpClass.getDataSource()).getDataBase()
- .equals("db2")) {
- } else if (sysmodel.getDataSourceByCode(mdpClass.getDataSource())
- .getDataBase().equals("sqlserver")) {
- code = mdpClass.getName() + "." + queryField.getDataName() + " "
- + operation + " '" + fieldValue + " ' and ";
- } else if (sysmodel.getDataSourceByCode(mdpClass.getDataSource())
- .getDataBase().equals("oracle")) {
- code = mdpClass.getName() + "." + queryField.getDataName() + " "
- + operation + " to_date('" + fieldValue
- + " ','yyyy-mm-dd hh24:mi:ss') and ";
- } else if (sysmodel.getDataSourceByCode(mdpClass.getDataSource())
- .getDataBase().equals("mysql")) {
- code = mdpClass.getName() + "." + queryField.getDataName() + " "
- + operation + " date_format('" + fieldValue
- + " ','%Y-%m-%d %T') and ";
- } else if (sysmodel.getDataSourceByCode(mdpClass.getDataSource())
- .getDataBase().equals("dameng")) {
- code = mdpClass.getName() + "." + queryField.getDataName() + " "
- + operation + " to_date('" + fieldValue
- + " ','yyyy-mm-dd hh24:mi:ss') and ";
- }
- } else {
- code = mdpClass.getName() + "." + queryField.getDataName() + " "
- + operation + " '" + fieldValue + " ' and ";
- }
- condition.append(code);
- }
- // //***********************增加模型中关于时间、数据段查询的功能(by peterkong
- // 2010.08.17)*************************************************//
- // if((fieldName.indexOf("_FROM") != -1 ||
- // fieldName.indexOf("_TO") != -1) && fieldValue != null &&
- // !fieldValue.equals("")){
- // if(fieldName.indexOf("_FROM") != -1){
- // fieldNameDataType =
- // fieldName.substring(0,fieldName.indexOf("_FROM"));
- // }else if(fieldName.indexOf("_TO") != -1){
- // fieldNameDataType =
- // fieldName.substring(0,fieldName.indexOf("_TO"));
- // }
- // // fieldNameDataType =
- // fieldName.substring(0,fieldName.indexOf("_FROM"));
- // fieldName = fieldNameDataType;
- // }
- // String dataType =
- // mdpClass.getMdpAttributeByName(queryField.getDataName())
- // .getDataType();
- // if ("string".equals(dataType)) {
- // if ("like".equals(operator)) {
- // condition.append(tableName).append(".").append(fieldName).append(" like '%").append(fieldValue).append("%'").append(" and ");
- // } else {
- // condition.append(tableName).append(".").append(fieldName).append("='").append(fieldValue).append("'").append(" and ");
- // }
- // } else {
- // if ("lessThan".equals(operator)) {
- // if ("number".equals(dataType)){
- // condition.append(tableName).append(".").append(fieldName).append(" <= ").append(fieldValue).append(" and ");
- // }else{
- // condition.append(tableName).append(".").append(fieldName).append(" <= '").append(fieldValue).append("'").append(" and ");
- // }
- // } else if ("moreThan".equals(operator)) {
- // if ("number".equals(dataType)){
- // condition.append(tableName).append(".").append(fieldName).append(" >= ").append(fieldValue).append(" and ");
- // }else{
- // condition.append(tableName).append(".").append(fieldName).append(" >= '").append(fieldValue).append("'").append(" and ");
- // }
- //
- // } else {
- // condition.append(tableName).append(".").append(fieldName).append("=").append(fieldValue).append(" and ");
- // }
- // }
- // //************************************结束**************************************************************************************
- }
- }
- if (condition.lastIndexOf("and") > -1) {
- conditionTemp = condition.substring(0, condition.lastIndexOf("and"));
- }
- if (conditionTemp.length() > 0) {
- if (conditionStr.length() > 0) {
- conditionStr = conditionTemp + " and " + conditionStr;
- } else {
- conditionStr = conditionTemp;
- }
- }
- log.info("conditionStr---" + conditionStr);
- return conditionStr;
- }
- /**
- * 根据传入的参数,拼装sql语句
- *
- * @param listId
- * @param condition
- * @return
- */
- @SuppressWarnings({ "rawtypes", "null", "unused" })
- public StringBuffer getSearchSql(FormListImpl formList, String condition) {
- // StringBuffer queryTable = new StringBuffer();
- // StringBuffer queryWhere = new StringBuffer();
- SysModel sysmodel = ModelFactory.getSysmodel();
- StringBuffer sql = new StringBuffer();
- ArrayList<String> tables = formList.getQueryTables();
- String tName = "";
- int dbFlag = 0; // 0:oracle , 1:db2 , 2:sqlserver2005
- /**
- * sqlserver中,如果加 order by 排序,会抛异常 异常消息为: 除非另外还指定了 TOP 或 FOR
- * XML,否则,ORDER BY 子句在视图、内联函数、派生表、子查询和公用表表达式中无效。 加上top的话,就没问题了, 赵敏
- *
- * 但是加上10的话,分页就没用了
- */
- int pageSize = formList.getQueryList().getPageSize();
- MdpDataSource dataSource = sysmodel.getDataSourceByCode(1);
- String dataBaseType = dataSource.getDataBase();
- if (dataBaseType.equals("sqlserver")) {
- sql.append("select top 100 PERCENT ");
- } else if (dataBaseType.equals("oracle")) {
- sql.append("select ");
- } else if (dataBaseType.equals("mysql")) {
- sql.append("select ");
- } else if (dataBaseType.equals("dameng")) {
- sql.append("select ");
- } else {
- sql.append("select ");
- }
- for (int i = 0; i < tables.size(); i++) {
- String table = tables.get(i);
- if (table != null || !table.equals("")) {
- String afield = "FD_OBJECTID" + table;
- int iclassid = Integer.parseInt(table);
- MdpClass mdpClass = sysmodel.getMdpClassByClassID(iclassid);
- if (sysmodel.getDataSourceByCode(mdpClass.getDataSource()).getDataBase()
- .equals("db2")) {
- dbFlag = 1;
- } else if (sysmodel.getDataSourceByCode(mdpClass.getDataSource()).getDataBase()
- .equals("sqlserver")) {
- dbFlag = 2;
- }
- sql.append(mdpClass.getName()).append(".FD_OBJECTID").append(" ").append(afield)
- .append(",");
- }
- }
- // log.info(FormList.getListid()+ FormList.getDescription());
- List listFields = formList.getQueryList().getlistFields();
- for (int j = 0; j < listFields.size(); j++) {
- ListField field = (ListField) listFields.get(j);
- if (field.getShowType().equals("0")) {
- MdpClass mdpClass = sysmodel.getMdpClassByClassID(field.getClassid());
- MdpAttribute MdpAttribute = mdpClass.getMdpAttributeByName(field.getDataName());
- String afield = field.getDataName() + "_" + mdpClass.getClassid();
- // log.info(afield);
- // if (MdpAttribute.getReferenceType() == 1
- // && MdpAttribute.getReference() != null) {
- // // String tableName = "";
- // // String referenceTable = MdpAttribute.getReference()
- // // .getReferenceTable();
- // // if (referenceTable != null || "".equals(referenceTable)) {
- // // tableName = sysmodel.getMdpClassByClassID(
- // // Integer.parseInt(referenceTable)).getName();
- // // }
- // // if(queryTable.indexOf(tableName)<0){
- // // queryTable.append("").append(tableName).append(",");
- // // }
- // //
- // // queryWhere.append(" ").append(mdpClass.getName()).append(
- // // ".").append(field.getDataName()).append("=")
- // // .append(tableName).append(".").append(
- // // MdpAttribute.getReference().getStoreName())
- // // .append(" and");
- // // sql.append(tableName + "."
- // // + MdpAttribute.getReference().getStoreName() + " "
- // // + afield + " , " + tableName + "."
- // // + MdpAttribute.getReference().getDisplayName()
- // // + " " + afield + "_show ,");
- // } else {
- sql.append(mdpClass.getName() + "." + field.getDataName() + " " + afield + " , ");
- // }
- }
- }
- List paramFields = formList.getlistPramField();
- for (int i = 0; i < paramFields.size(); i++) {
- PramFieldImpl PramField = (PramFieldImpl) paramFields.get(i);
- MdpClass mdpClass = sysmodel.getMdpClassByClassID(PramField.getClassid());
- String afield = "param" + PramField.getName() + "_" + mdpClass.getClassid();
- sql.append(mdpClass.getName() + "." + PramField.getName() + " " + afield + " , ");
- }
- // if( dbFlag == 1){//db2
- // sql.append("rownumber() over(ORDER BY ");
- // if(!formList.getQueryList().getOrderBy().equals("")){
- // sql.append(formList.getQueryList().getOrderBy());
- // if(formList.getQueryList().getOrderBy().indexOf(".")==-1){
- // tName = formList.getQueryList().getOrderBy().substring(1,
- // formList.getQueryList().getOrderBy().indexOf("."));
- // }
- // } else {
- // int iclassid = Integer.parseInt((String)tables.get(0));
- // sql.append(sysmodel.getMdpClassByClassID(iclassid).getName()).append(".FD_OBJECTID ");
- // tName = sysmodel.getMdpClassByClassID(iclassid).getName();
- // }
- // sql.append(" ) AS ROWNUM_");
- // } else if (dbFlag == 0){//oracle
- // sql.deleteCharAt(sql.lastIndexOf(","));
- // } else if (dbFlag == 2){//sql server 2005
- // sql.append(" ROW_NUMBER() Over(order by ");
- // if(!formList.getQueryList().getOrderBy().equals("")){
- // sql.append(formList.getQueryList().getOrderBy());
- // if(formList.getQueryList().getOrderBy().indexOf(".")==-1){
- // tName = formList.getQueryList().getOrderBy().substring(1,
- // formList.getQueryList().getOrderBy().indexOf("."));
- // }
- // } else {
- // int iclassid = Integer.parseInt((String)tables.get(0));
- // sql.append(sysmodel.getMdpClassByClassID(iclassid).getName()).append(".FD_OBJECTID ");
- // tName = sysmodel.getMdpClassByClassID(iclassid).getName();
- // }
- // sql.append(") as ROWNUM_ ");
- // }
- sql.deleteCharAt(sql.lastIndexOf(","));
- sql.append(" from ");
- for (int i = 0; i < tables.size(); i++) {
- String table = (String) tables.get(i);
- if (table != null || !table.equals("")) {
- int iclassid = Integer.parseInt(table);
- MdpClass mdpClass = sysmodel.getMdpClassByClassID(iclassid);
- sql.append(mdpClass.getName()).append(",");
- }
- }
- // sql.append(queryTable);
- sql.deleteCharAt(sql.lastIndexOf(","));
- condition += formList.getQueryList().equals("") ? "" : " "
- + formList.getQueryList().getCondition();
- // if( dbFlag == 2){
- if (!condition.trim().equals("")) {
- sql.append(" where ").append(condition);
- }
- // String query = queryWhere.toString() == null ? "" : queryWhere
- // .toString();
- //
- // if (!query.equals("")) {
- // queryWhere.reverse();
- // queryWhere.delete(0, 3);
- // queryWhere.reverse();
- // if (sql.indexOf("where") == -1) {
- // sql.append(" where ").append(queryWhere);
- // } else {
- // sql.append(" and ").append(queryWhere);
- // }
- //
- // }
- // }
- // if( dbFlag == 1){//db2
- // sql.append(") as ").append(tName).append(" where ").append(tName).append(".ROWNUM_");
- // }
- // log.info("查询sql = " + sql);
- return sql;
- }
- /**
- * 由于查询附件列表数据时候 为了提高查询速度不应该把附件内容查询出来,该方法拼装sql语句时不查询附件内容字段
- *
- * @param listId
- * @param condition
- * @param notfield
- * 不需要拼装的属性 张勇波 2012-4-11
- * @return
- */
- @SuppressWarnings({ "rawtypes", "unused", "null" })
- public StringBuffer getSearSqlNotFiled(FormListImpl formList, String condition, String notfield) {
- // StringBuffer queryTable = new StringBuffer();
- // StringBuffer queryWhere = new StringBuffer();
- SysModel sysmodel = ModelFactory.getSysmodel();
- StringBuffer sql = new StringBuffer();
- ArrayList<String> tables = formList.getQueryTables();
- String tName = "";
- int dbFlag = 0; // 0:oracle , 1:db2 , 2:sqlserver2005
- /**
- * sqlserver中,如果加 order by 排序,会抛异常 异常消息为: 除非另外还指定了 TOP 或 FOR
- * XML,否则,ORDER BY 子句在视图、内联函数、派生表、子查询和公用表表达式中无效。 加上top的话,就没问题了, 赵敏
- *
- * 但是加上10的话,分页就没用了
- */
- int pageSize = formList.getQueryList().getPageSize();
- MdpDataSource dataSource = sysmodel.getDataSourceByCode(1);
- String dataBaseType = dataSource.getDataBase();
- if (dataBaseType.equals("sqlserver")) {
- sql.append("select top 100 PERCENT ");
- } else if (dataBaseType.equals("oracle")) {
- sql.append("select ");
- }
- for (int i = 0; i < tables.size(); i++) {
- String table = tables.get(i);
- if (table != null || !table.equals("")) {
- String afield = "FD_OBJECTID" + table;
- int iclassid = Integer.parseInt(table);
- MdpClass mdpClass = sysmodel.getMdpClassByClassID(iclassid);
- if (sysmodel.getDataSourceByCode(mdpClass.getDataSource()).getDataBase()
- .equals("db2")) {
- dbFlag = 1;
- } else if (sysmodel.getDataSourceByCode(mdpClass.getDataSource()).getDataBase()
- .equals("sqlserver")) {
- dbFlag = 2;
- }
- sql.append(mdpClass.getName()).append(".FD_OBJECTID").append(" ").append(afield)
- .append(",");
- }
- }
- // log.info(FormList.getListid()+ FormList.getDescription());
- List listFields = formList.getQueryList().getlistFields();
- for (int j = 0; j < listFields.size(); j++) {
- ListField field = (ListField) listFields.get(j);
- if (field.getShowType().equals("0")) {
- MdpClass mdpClass = sysmodel.getMdpClassByClassID(field.getClassid());
- String fieldname = field.getDataName();
- // 过滤不需要拼装的属性
- if (!fieldname.equals(notfield)) {
- String afield = field.getDataName() + "_" + mdpClass.getClassid();
- sql.append(mdpClass.getName() + "." + field.getDataName() + " " + afield
- + " , ");
- }
- }
- }
- log.info("Sql=" + sql);
- List paramFields = formList.getlistPramField();
- for (int i = 0; i < paramFields.size(); i++) {
- PramFieldImpl PramField = (PramFieldImpl) paramFields.get(i);
- MdpClass mdpClass = sysmodel.getMdpClassByClassID(PramField.getClassid());
- String afield = "param" + PramField.getName() + "_" + mdpClass.getClassid();
- sql.append(mdpClass.getName() + "." + PramField.getName() + " " + afield + " , ");
- }
- sql.deleteCharAt(sql.lastIndexOf(","));
- sql.append(" from ");
- for (int i = 0; i < tables.size(); i++) {
- String table = (String) tables.get(i);
- if (table != null || !table.equals("")) {
- int iclassid = Integer.parseInt(table);
- MdpClass mdpClass = sysmodel.getMdpClassByClassID(iclassid);
- sql.append(mdpClass.getName()).append(",");
- }
- }
- sql.deleteCharAt(sql.lastIndexOf(","));
- condition += formList.getQueryList().equals("") ? "" : " "
- + formList.getQueryList().getCondition();
- // if( dbFlag == 2){
- if (!condition.trim().equals("")) {
- sql.append(" where ").append(condition);
- }
- return sql;
- }
- /**
- * 查询总记录数
- *
- * @return
- */
- public int getTotalLines(String sql) {
- int num = 0;
- sql = "SELECT count(*) FROM ( " + sql + ") t";
- try {
- num = persistence.getFunctionNumber(99, sql);
- } catch (PersistenceException e) {
- e.printStackTrace();
- }
- return num;
- }
- /**
- * 根据系统默认数据源、第一条记录数、页面显示限制条数,计算最后一条记录数
- *
- * @param start
- * 第一条记录数
- * @param limit
- * 页面显示限制条数
- * @return
- */
- public int getLastNumByDataSourse(int start, int limit, String dataBase) {
- int end = 0;
- if (dataBase.equals("sqlserver")) {
- end = start + limit + 1;
- } else if (dataBase.equals("db2")) {
- if (start == 0) {
- end = start + limit;
- } else {
- end = start + limit - 1;
- }
- }
- return end;
- }
- /**
- * 根据传入的参数,拼装分页sql语句
- *
- * @param listId
- * 配置id
- * @param sql
- * 查询语句
- * @return
- */
- public StringBuffer getPagingSearchSql(StringBuffer sql, String firstPageNum, String limit) {
- MdpDataSource dataSource = sysmodel.getDataSourceByCode(sysmodel.getMdpClassByClassID(99)
- .getDataSource());
- StringBuffer pagingSql = new StringBuffer();
- // int lastPageNum = this.getLastNumByDataSourse(new
- // Integer(firstPageNum),
- // new Integer(limit), dataSource.getDataBase());
- int startRecordNum = Integer.parseInt(firstPageNum) * Integer.parseInt(limit);
- if (dataSource.getDataBase().equals("oracle")) {
- // pagingSql.append("SELECT * FROM ( SELECT row_.*, rownum rownum_ FROM (");
- pagingSql.append(sql);
- // pagingSql.append(") row_ WHERE rownum <= ").append(startRecordNum).append(") WHERE rownum_ > ").append(firstPageNum);
- } else if (dataSource.getDataBase().equals("dameng")) {
- // pagingSql.append("SELECT * FROM ( SELECT row_.*, rownum rownum_ FROM (");
- pagingSql.append(sql);
- // pagingSql.append(") row_ WHERE rownum <= ").append(startRecordNum).append(") WHERE rownum_ > ").append(firstPageNum);
- } else if (dataSource.getDataBase().equals("db2")) {
- pagingSql.append("SELECT * FROM ( ");
- pagingSql.append(sql);
- pagingSql.append(")as a where ROWNUM_ BETWEEN ").append(firstPageNum).append(" and ")
- .append(startRecordNum);
- } else if (dataSource.getDataBase().equals("sqlserver")) {
- pagingSql.append("SELECT * FROM (");
- pagingSql.append(sql);
- pagingSql.append(") as t where t.ROWNUM_ > " + firstPageNum + " and t.ROWNUM_ < "
- + startRecordNum);
- } else if (dataSource.getDataBase().equals("mysql")) {
- // int startRecordNum =
- // Integer.parseInt(firstPageNum)*Integer.parseInt(limit);
- pagingSql.append("select * from (");
- pagingSql.append(sql);
- pagingSql.append(") t ");
- pagingSql.append(" limit " + startRecordNum + "," + limit);
- }
- log.info("分页查询sql = " + pagingSql);
- return pagingSql;
- }
- /**
- * 根据sql查询表数据
- *
- * @param sql
- * @return
- */
- public RowSet queryAllDatas(FormListImpl formList, String sql) {
- log.info("查询sql:" + sql);
- Connection con = null;
- PreparedStatement ps = null;
- ResultSet rs = null;
- RowSet res = new RowSet();
- ArrayList<ListFieldImpl> listFields = formList.getQueryList().getlistFields();
- try {
- DbConnection DbConnection = new DbConnection();
- con = DbConnection.getConnection();
- ps = con.prepareStatement(sql);
- rs = ps.executeQuery();
- int i = 0; // 字段数
- // String[] cols=null; //记录行值数组
- Map<String, String> map = null;
- i = rs.getMetaData().getColumnCount();
- String[] fileds = new String[i];
- for (int m = 0; m < i; m++)
- fileds[m] = rs.getMetaData().getColumnName(m + 1).toUpperCase();
- while (rs.next()) {
- map = new HashMap<String, String>();
- // for (int j = 0; j < i; j++) {
- // map.put(fileds[j], rs.getString(j+1));
- // }
- map.put("classid", formList.getListid());
- for (int n = 0; n < i; n++) {
- // 拼sql时,字段已经增加了CLASSID
- String key = fileds[n];
- String value = "";
- int type = rs.getMetaData().getColumnType(n + 1);
- // ----赵志强 2011-10-25 修改日期格式化问题 出现1970 bug-----begin--//
- if (type == java.sql.Types.TIME) {
- SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
- java.sql.Timestamp value_time = rs.getTimestamp(n + 1);
- if (value_time != null)
- value = sf.format(value_time);
- } else if (type == java.sql.Types.TIMESTAMP) {
- java.sql.Timestamp value_tt = rs.getTimestamp(n + 1);
- SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
- if (value_tt != null)
- value = sf.format(value_tt);
- } else if (type == java.sql.Types.DATE) {
- java.sql.Timestamp value_tt = rs.getTimestamp(n + 1);
- SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- if (value_tt != null)
- value = sf.format(value_tt);
- } else {
- value = rs.getString(n + 1);
- }
- // ----赵志强 2011-10-25 修改日期格式化问题-----end--//
- map.put(key, value);
- for (int j = 0; j < listFields.size(); j++) {
- ListFieldImpl ListFieldImpl = (ListFieldImpl) listFields.get(j);
- String keyTemp = ListFieldImpl.getDataName().toUpperCase() + "_"
- + ListFieldImpl.getClassid();
- if (keyTemp.equals(key)) {
- if (ListFieldImpl.getShowType().equals("0")) {
- MdpClass mdpClass = sysmodel.getMdpClassByClassID(ListFieldImpl
- .getClassid());
- MdpAttribute MdpAttribute = mdpClass
- .getMdpAttributeByName(ListFieldImpl.getDataName());
- if (MdpAttribute.getReferenceType() == 1) {
- String showKkey = ListFieldImpl.getDataName().toUpperCase()
- + "_" + ListFieldImpl.getClassid() + "_SHOW";
- String showValue = value;
- if (MdpAttribute.getReference() != null) {
- String referenceTable = MdpAttribute.getReference()
- .getReferenceTable();
- String displayName = MdpAttribute.getReference()
- .getDisplayName();
- String storeName = MdpAttribute.getReference()
- .getStoreName();
- showValue = this.getReferenceShow(value,
- Integer.valueOf(referenceTable), displayName,
- storeName);
- }
- map.put(showKkey, showValue);
- }
- if (MdpAttribute.getReferenceType() == 2) {
- String showKkey = ListFieldImpl.getDataName().toUpperCase()
- + "_" + ListFieldImpl.getClassid() + "_SHOW";
- String showValue = value;
- if (MdpAttribute.getReference() != null) {
- String referenceTable = MdpAttribute.getReference()
- .getReferenceTable();
- showValue = sysmodel.getMdpConstantByName(referenceTable)
- .getMdpConstantDisplayString(value);
- }
- map.put(showKkey, showValue);
- }
- }
- }
- }
- }
- res.getRows().add(map);
- }
- } catch (SQLException e) {
- e.printStackTrace();
- log.error(e.toString());
- } catch (ClassNotFoundException e) {
- log.error(e.toString());
- } catch (NumberFormatException e) {
- log.error(e.toString());
- } catch (PersistenceException e) {
- log.error(e.toString());
- } finally {
- try {
- if (rs != null)
- rs.close();
- if (ps != null)
- ps.close();
- if (con != null)
- con.close();
- } catch (SQLException e) {
- log.error(e.toString());
- }
- }
- return res;
- }
- public RowSet queryPagingAllDatas(FormListImpl formList, String sql, String firstPageNum,
- String limit) {
- // log.info("查询firstPageNum:" + firstPageNum);
- // log.info("查询limit:" + limit);
- // log.info("查询sql:" + sql);
- // conn.prepareStatement(sql,游标类型,能否更新记录);
- // 游标类型:
- // ResultSet.TYPE_FORWORD_ONLY:只进游标
- // ResultSet.TYPE_SCROLL_INSENSITIVE:可滚动。但是不受其他用户对数据库更改的影响。
- // ResultSet.TYPE_SCROLL_SENSITIVE:可滚动。当其他用户更改数据库时这个记录也会改变。
- // 能否更新记录:
- // ResultSet.CONCUR_READ_ONLY,只读
- // ResultSet.CONCUR_UPDATABLE,可更新
- Connection con = null;
- PreparedStatement pstat = null;
- ResultSet rs = null;
- RowSet res = new RowSet();
- ArrayList<ListFieldImpl> listFields = formList.getQueryList().getlistFields();
- try {
- // ServerConfigure sc = ServerConfigure.getServerConfigerInstance();
- // con = sc.getDataSource().getConnection();
- DbConnection DbConnection = new DbConnection();
- con = DbConnection.getConnection();
- /**
- * 适用于qui
- */
- int maxCount = Integer.valueOf(limit);
- int startNo = (Integer.valueOf(firstPageNum) - 1) * maxCount + 1;
- int MaxRows = startNo + maxCount - 1;
- // int maxCount = Integer.valueOf(limit);
- // int startNo = Integer.valueOf(firstPageNum) + 1;
- // int MaxRows = startNo + maxCount - 1;
- /**
- * [start] 当使用mysql数据库时 方法中的参数sql已包含分页如:select * from table limit
- * 0,5 游标定死
- */
- MdpDataSource dataSource = sysmodel.getDataSourceByCode(sysmodel.getMdpClassByClassID(
- 99).getDataSource());
- if (dataSource.getDataBase().equals("mysql")) {
- startNo = 1;
- MaxRows = maxCount;
- }
- /**** [end] ****/
- pstat = con.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE,
- ResultSet.CONCUR_READ_ONLY);
- // 最大查询到第几条记录
- pstat.setMaxRows(MaxRows);
- rs = pstat.executeQuery();
- if (null != rs && rs.next()) {
- // 将游标移动到第一条记录
- rs.first();
- // 游标移动到要输出的第一条记录
- rs.relative(startNo - 2);
- // PreparedStatement ps = con.prepareStatement(sql);
- // ResultSet rs = ps.executeQuery();
- // boolean k = true;
- int i = 0; // 字段数
- // String[] cols = null; // 记录行值数组
- Map<String, String> map = null;
- i = rs.getMetaData().getColumnCount();
- String[] fileds = new String[i];
- for (int m = 0; m < i; m++)
- fileds[m] = rs.getMetaData().getColumnName(m + 1).toUpperCase();
- while (rs.next()) {
- map = new HashMap<String, String>();
- // MdpClass mdpClass =
- // sysmodel.getMdpClassByClassID(classid);
- // DataObject dataobject =
- // TypeTransformer.getSearchDataObject(mdpClass);
- map.put("classid", formList.getListid());
- for (int n = 0; n < i; n++) {
- String key = fileds[n];
- String value = "";
- int type = rs.getMetaData().getColumnType(n + 1);
- if (type == java.sql.Types.TIME) {
- SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
- java.sql.Timestamp value_time = rs.getTimestamp(n + 1);
- if (value_time != null)
- value = sf.format(value_time);
- } else if (type == java.sql.Types.TIMESTAMP) {
- java.sql.Timestamp value_tt = rs.getTimestamp(n + 1);
- SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
- if (value_tt != null)
- value = sf.format(value_tt);
- } else if (type == java.sql.Types.DATE) {
- java.sql.Timestamp value_tt = rs.getTimestamp(n + 1);
- SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- if (value_tt != null)
- value = sf.format(value_tt);
- } else if (type == java.sql.Types.CLOB) {
- Clob clob = rs.getClob(n + 1);
- if (clob != null && clob.length() != 0) {
- // value = clob.getSubString((long) 1, (int)
- // clob.length());
- value = Clob2String(clob);// 调用clob转string的方法
- }
- } else if (type == java.sql.Types.BLOB) {
- Blob blob = rs.getBlob(n + 1);
- if (blob != null && blob.length() != 0) {
- value = Blob2String(blob);// 调用blob转string的方法
- }
- } else {
- value = rs.getString(n + 1);
- }
- map.put(key, value);
- for (int j = 0; j < listFields.size(); j++) {
- ListFieldImpl ListFieldImpl = (ListFieldImpl) listFields.get(j);
- String keyTemp = ListFieldImpl.getDataName().toUpperCase() + "_"
- + ListFieldImpl.getClassid();
- if (keyTemp.equals(key)) {
- if (ListFieldImpl.getShowType().equals("0")) {
- MdpClass mdpClass = sysmodel.getMdpClassByClassID(ListFieldImpl
- .getClassid());
- MdpAttribute MdpAttribute = mdpClass
- .getMdpAttributeByName(ListFieldImpl.getDataName());
- if (MdpAttribute.getReferenceType() == 1) {
- String showKkey = ListFieldImpl.getDataName().toUpperCase()
- + "_" + ListFieldImpl.getClassid() + "_SHOW";
- String showValue = value;
- if (MdpAttribute.getReference() != null) {
- String referenceTable = MdpAttribute.getReference()
- .getReferenceTable();
- String displayName = MdpAttribute.getReference()
- .getDisplayName();
- String storeName = MdpAttribute.getReference()
- .getStoreName();
- showValue = this.getReferenceShow(value,
- Integer.valueOf(referenceTable), displayName,
- storeName);
- }
- map.put(showKkey, showValue);
- }
- if (MdpAttribute.getReferenceType() == 2) {
- String showKkey = ListFieldImpl.getDataName().toUpperCase()
- + "_" + ListFieldImpl.getClassid() + "_SHOW";
- String showValue = value;
- if (MdpAttribute.getReference() != null) {
- String referenceTable = MdpAttribute.getReference()
- .getReferenceTable();
- showValue = sysmodel.getMdpConstantByName(
- referenceTable).getMdpConstantDisplayString(
- value);
- }
- map.put(showKkey, showValue);
- }
- }
- }
- }
- }
- res.getRows().add(map);
- }
- }
- } catch (SQLException e) {
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (NumberFormatException e) {
- e.printStackTrace();
- } catch (PersistenceException e) {
- e.printStackTrace();
- } finally {
- try {
- if (rs != null)
- rs.close();
- if (pstat != null)
- pstat.close();
- if (con != null)
- con.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- return res;
- }
- /**
- * clob to string 大字符串格式转换STRING
- *
- * @param clob
- * @return 大字符串
- *
- */
- public String Clob2String(Clob clob) {// Clob转换成String 的方法
- String content = null;
- StringBuffer stringBuf = new StringBuffer();
- try {
- int length = 0;
- Reader inStream = clob.getCharacterStream(); // 取得大字侧段对象数据输出流
- char[] buffer = new char[10];
- while ((length = inStream.read(buffer)) != -1) // 读取数据库 //每10个10个读取
- {
- for (int i = 0; i < length; i++) {
- stringBuf.append(buffer[i]);
- }
- }
- inStream.close();
- content = stringBuf.toString();
- } catch (Exception ex) {
- log.error(ex.getMessage(), ex);
- }
- return content;
- }
- /**
- * blob to string 大字符串格式转换STRING
- *
- * @param clob
- * @return 大字符串
- *
- */
- public String Blob2String(Blob blob) {// blob转换成String 的方法
- String content = null;
- try {
- byte[] bytes = blob.getBytes((long) 1, (int) blob.length()); // 取得大字侧段对象数据输出流
- content = new String(bytes);
- } catch (Exception ex) {
- log.error(ex.getMessage(), ex);
- }
- return content;
- }
- private SysModel sysmodel = ModelFactory.getSysmodel();
- private SysPersistence persistence = PersistenceFactory.getInstance(sysmodel);
- private final Logger log = Logger.getLogger(getClass());
- // public RowSet queryAllDatas(String sql) {
- // Connection con = null;
- // RowSet res = new RowSet();
- // ServerConfigure sc = ServerConfigure.getServerConfigerInstance();
- // try {
- // con = sc.getDataSource().getConnection();
- //
- // PreparedStatement ps = con.prepareStatement(sql);
- // ResultSet rs = ps.executeQuery();
- // boolean k=true;
- // int i=0; //字段数
- // String[] cols=null; //记录行值数组
- // while (rs.next()) {
- // if (k) {
- // i = rs.getMetaData().getColumnCount();
- // for(int m=0;m<i;m++)
- // res.getFields().add(rs.getMetaData().getColumnName(m+1).toUpperCase());
- // cols = new String[i];
- // k=false;
- // }
- //
- // for (int j = 0; j < i; j++) {
- // cols[j]=rs.getString(j+1);
- // }
- // ColumnSet cs = new ColumnSet(cols);
- // res.getRows().add(cs);
- //
- // }
- // } catch (SQLException e) {
- // e.printStackTrace();
- // log.error(e.toString());
- // }finally{
- // if(con!=null)
- // try {
- // con.close();
- // } catch (SQLException e) {
- // log.error(e.toString());
- // e.printStackTrace();
- // }
- // }
- // return res;
- // }
- /**
- * 修改日期格式的问题
- *
- * @author koufengzhuo peterkong 项目中心项目中提取
- */
- @SuppressWarnings({ "rawtypes", "unchecked" })
- public RowSet transformDate(RowSet rowSet, FormListImpl formList) {
- ArrayList<String> dateColumns = new ArrayList<String>();
- ArrayList<String> dateFormat = new ArrayList<String>();
- SysModel sysmodel = ModelFactory.getSysmodel();
- List<ListFieldImpl> listFields = formList.getQueryList().getlistFields();
- int classid = 0;
- MdpClassImpl mdpClass = null;
- for (ListFieldImpl field : listFields) {
- classid = field.getClassid();
- mdpClass = (MdpClassImpl) sysmodel.getMdpClassByClassID(classid);
- if (null == mdpClass) {
- log.error(classid + ":对应的数据模型不存在!请检查DataModel.xml");
- continue;
- } else {
- for (MdpAttributeImpl attribute : mdpClass.getAllMdpAttributes()) {
- if (attribute.getDataType().toLowerCase().indexOf("date") < 0) {
- continue;
- } else {
- dateColumns.add(attribute.getName().toUpperCase() + "_" + classid);
- dateFormat.add(attribute.getScale());
- }
- }
- }
- }
- List<Map> rows = rowSet.getRows();
- for (Map row : rows) {
- Iterator it = row.entrySet().iterator();
- while (it.hasNext()) {
- Map.Entry entry = (Map.Entry) it.next();
- String key = (String) entry.getKey();
- String value = (String) entry.getValue();
- String format = "yyyy-MM-dd";
- if (!dateColumns.contains(key)) {
- continue;
- } else {
- for (int i = 0; i < dateColumns.size(); i++) {
- if (key.equals(dateColumns.get(i))) {
- format = dateFormat.get(i);
- break;
- }
- }
- value = chageFormt(value, format);
- row.put(key, value);
- }
- }
- }
- return rowSet;
- }
- /**
- * 转换日期格式,默认yyyy-MM-dd HH:mm:ss.S
- *
- * @author koufengzhuo peterkong
- * @param dateStr
- * @param format
- * @return
- */
- private String chageFormt(String dateStr, String format) {
- if (null == dateStr)
- return null;
- SimpleDateFormat sdf = new SimpleDateFormat(format);
- SimpleDateFormat sdf_source = new SimpleDateFormat("yyyy-MM-dd HH:mm");
- try {
- if (null != dateStr && !dateStr.equals("")) {
- dateStr = sdf.format(sdf_source.parse(dateStr));
- }
- } catch (ParseException e) {
- e.printStackTrace();
- }
- return dateStr;
- }
- private String getReferenceShow(String value, int classid, String displayName, String storeName)
- throws PersistenceException {
- String sql = "";
- if (sysmodel.getMdpClassByClassID(classid).getName().equals("Sys_Department")) {
- sql = "select " + displayName + " from "
- + sysmodel.getMdpClassByClassID(classid).getName() + " where is_del ='0' and "
- + storeName + " = '" + value + "'";
- } else {
- /*sql = "select " + displayName + " from "
- + sysmodel.getMdpClassByClassID(classid).getName() + " where " + storeName
- + " = '" + value + "'";*/
- if(value != null){
- String temp[] = value.split(",");
- StringBuffer sbtemp = new StringBuffer("");
- int len = temp.length;
- for (int i = 0; i < len; i++) {
- if(i == (len-1)){
- sbtemp.append("'"+temp[i]+"'");
- }else{
- sbtemp.append("'"+temp[i]+"',");
- }
- }
- value = sbtemp.toString();
- }
- sql = "select " + displayName + " from "
- + sysmodel.getMdpClassByClassID(classid).getName() + " where " + storeName
- + " in (" + value + ")";
- }
- // log.info("referenceSql =="+sql);
- ArrayList<Map<String, String>> list = persistence.getSearchResultToMap(classid, sql);
- String result = "";
- /*if (list.size() > 0) {
- result = list.get(0).get(displayName.toUpperCase());
- }*/
- int listLength =list.size();
- if ( listLength > 0) {
- for (int i = 0; i < listLength; i++) {
- if(i == (listLength-1)){
- result += list.get(i).get(displayName.toUpperCase());
- }else{
- result += list.get(i).get(displayName.toUpperCase())+",";
- }
-
- }
-
- }
- return result;
- }
- }
|