package samples.formcollect; import java.sql.Timestamp; import java.util.ArrayList; import javax.servlet.http.HttpServletRequest; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.sysmodel.Util; import com.sysmodel.XFormFactory; import com.sysmodel.XFormModel; import com.sysmodel.collectmodel.xmlmodel.able.Action; import com.sysmodel.collectmodel.xmlmodel.able.FormCollection; import com.sysmodel.collectmodel.xmlmodel.able.PageBody; import com.sysmodel.collectmodel.xmlmodel.able.PageHead; import com.sysmodel.collectmodel.xmlmodel.able.PageRoot; import com.sysmodel.collectmodel.xmlmodel.able.ReportField; import com.sysmodel.collectmodel.xmlmodel.impl.PageBodyImpl; import com.sysmodel.collectmodel.xmlmodel.impl.ReportFieldImpl; 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.SysModel; import com.sysmodel.xformmodel.importcontrol.DateControl; import com.sysmodel.xformmodel.importcontrol.SelectControl; import com.sysmodel.xformmodel.importcontrol.TreeControl; import com.persistence.service.PersistenceFactory; import com.persistence.service.assitant.DataObject; import com.persistence.service.exception.PersistenceException; public class PageBuilder { private static Log log = LogFactory.getLog(PageBodyImpl.class); private XFormModel XFormModel = XFormFactory.getXFormModel(); private FormCollection FormCollection = null; private DataObject dataobject = null; public PageBuilder(String pageid,DataObject dataobject){ this.FormCollection = XFormModel.getFormCollection(pageid); this.dataobject = dataobject; } /** * 获得采集列表的标题 * @return */ public String produceTitleHtml(){ PageHead PageHead = FormCollection.getPageHead(); StringBuffer sb = new StringBuffer(); sb.append("  "+PageHead.getPageTitle()); return sb.toString(); } /** * 产生采集页面的按钮 * @param isDelete * @return */ public String produceRootHtml(boolean isDelete) { PageRoot PageRoot = FormCollection.getPageRoot(); ArrayList alAction = PageRoot.getAllActions(); StringBuffer sb = new StringBuffer(); for (int i = 0; i < alAction.size(); i++) { Action action = (Action)alAction.get(i); String buttonValue = action.getValue(); if(!action.getType().equals("delete") && !isDelete){ sb.append(""); } action = null; } return sb.toString(); } /** * 生成页面填报数据项 * @param req * @return */ public String produceBodyHtml(HttpServletRequest req) { PageBody PageBody = FormCollection.getPageBody(); int columnCount = PageBody.getColumnCount(); //表头 StringBuffer sb = new StringBuffer(); // /** // * 产生页面中隐藏插入的字段 // * 只限于在页面信增时候产生字段 // */ // if(dataobject != null){ // ArrayList alHiddenFields = PageBody.getAlHiddenFields(); // if(dataobject.getObjectID() == null || dataobject.getObjectID().length()==0){ // for (int i = 0; i < alHiddenFields.size(); i++) { // HiddenField field = (HiddenField) alHiddenFields.get(i); // sb.append(field.getHiddenInput(req)).append("\n"); // } // } // } sb.append(""); //数据区 boolean nextLine = true; int currentCount = 0; String FontTextCss = XFormModel.getCSSValue("FontTextCss"); FontTextCss = FontTextCss.equals("")? "":" class=\"" + FontTextCss + "\""; //td 宽度 short[] widthFlag = new short[columnCount * 2]; String htmlLabelWidth = PageBody.getLabelWidth().equals("") ? "" : " width=\"" + PageBody.getLabelWidth() + "\""; String htmControlWidth = PageBody.getControlWidth().equals("") ? "" : " width=\"" + PageBody.getControlWidth() + "\""; String htmlLabelAlign = PageBody.getLabelAlign().equals("") ? "" : " align=\"" + PageBody.getLabelAlign()+ "\""; String htmControlAlign = PageBody.getControlAlign().equals("") ? "" : " align=\"" + PageBody.getControlAlign() + "\""; //隔行换色 String LineCssA = XFormModel.getCSSValue("LineCssA"); String LineCssB = XFormModel.getCSSValue("LineCssB"); LineCssA = LineCssA.equals("") ? "" : " class=\"" + LineCssA + "\""; LineCssB = LineCssB.equals("") ? "" : " class=\"" + LineCssB + "\""; boolean currentLineCss = true; ArrayList alReportFields = PageBody.getAllReportFields(); for (int i = 0; i < alReportFields.size(); i++) { ReportField field = (ReportField) alReportFields.get(i); int result = currentCount + Integer.parseInt(field.getColumnWidth()); if (result > columnCount) {//补上空位,结束该行 int tdCount = columnCount - currentCount; for (int j = 0; j < tdCount; j++) { int index = currentCount + j; if (widthFlag[index] == 0) { widthFlag[index] = 1; widthFlag[index + 1] = 1; sb.append(""); sb.append(""); } else { sb.append("").append(""); } } sb.append(""); nextLine = true; currentCount = 0; } if (nextLine) { //进行隔行换色 if (currentLineCss) sb.append(""); else sb.append(""); nextLine = false; currentLineCss = !currentLineCss; } //生成控件 if (field.getName().equals("")) { int ColumnWidth = Integer.parseInt(field.getColumnWidth()) * 2; sb.append(""); } else { sb.append(""); sb.append("" + field.getLabel()).append(""); if(field.getValidate().isNeed()) sb.append(" *"); sb.append(""); //control sb.append(" 1) { int ColumnWidth = Integer.parseInt(field.getColumnWidth()) * 2 - 1; sb.append(" ColumnWidth=\"").append(ColumnWidth + "\""); } else { if (widthFlag[currentCount + 1] == 0) { widthFlag[currentCount + 1] = 1; sb.append(htmControlWidth); } } sb.append(" nowrap>"); sb.append(produceControlHtml(field, dataobject)); sb.append(""); } //结束行 if (result == columnCount) { sb.append(""); nextLine = true; currentCount = 0; } else currentCount += Integer.parseInt(field.getColumnWidth()); } if (currentCount > 0) {//确认tr封口 int tdCount = columnCount - currentCount; for (int j = 0; j < tdCount; j++) { int index = currentCount + j; if (widthFlag[index] == 0) { widthFlag[index] = 1; widthFlag[index + 1] = 1; sb.append(""); sb.append(""); } else { sb.append("").append(""); } } sb.append(""); } //end sb.append("
"); String DivCss = XFormModel.getCSSValue("DivCss"); DivCss = DivCss.equals("")? "":" class=\"" + DivCss + "\""; sb.append("" + field.getLabel()).append("
"); return sb.toString(); } /** * 获得页面中元素的验证JS * @return */ public String produceCientValidateJs(){ PageBody PageBody = FormCollection.getPageBody(); ArrayList alReportFields = PageBody.getAllReportFields(); StringBuffer sb = new StringBuffer(); for (int i = 0; i < alReportFields.size(); i++) { ReportField field = (ReportField) alReportFields.get(i); // sb.append(field.outputScript()); } return sb.toString(); } private String produceControlHtml(ReportField field, DataObject dataobject) { String value = ""; String displayString = ""; if (dataobject != null) { Object obj = dataobject.getValue(field.getName()); if (field.getControlType().equals("tree")) { TreeControl treeControl = (TreeControl) field.getControl(); value = obj.toString(); if (treeControl.getType().equals("2") || treeControl.getType().equals("3")) {//constant displayString = ModelFactory.getSysmodel().getMdpConstantDisplayString(treeControl.getReferenceTable(), value); } else {//table displayString = getReferenceDisplayString(treeControl, value); } } else if (field.getControlType().equals("date")) { DateControl dateControl = (DateControl) field.getControl(); if (!obj.equals("")) value = Util.getDateTime(dateControl.getFormat(), (Timestamp)obj); } else value = obj.toString(); if (field.getControlType().equals("select")) { SelectControl SelectControl = (SelectControl) field.getControl(); if(SelectControl.getType().equals("1")){ SysModel sysmodel = ModelFactory.getSysmodel(); MdpAttribute attributeObj = sysmodel.getMdpAttributeByName(Integer.parseInt(SelectControl.getIclassid()),field.getName()); //资源数据库选择系统 if(attributeObj.getReference().getReferenceTable().equals("907")){ String StationId = (String)dataobject.getValue("StationId"); displayString = " where StationId='"+ StationId +"'"; } } } } return field.getControl().produceHtml(field.getName(), value, displayString, field.getValidate()); } private String getReferenceDisplayString(TreeControl treeControl,String value){ String retString = ""; try { int classid = Integer.parseInt(treeControl.getReferenceTable()); MdpClass mdpClass = ModelFactory.getSysmodel().getMdpClassByClassID(classid); String sql = "select "+treeControl.getDisplayName()+ " from " + mdpClass.getName()+ " where "+treeControl.getStoreCode()+"='"+value+"'"; ArrayList list = PersistenceFactory.getInstance().getSearchResult(classid,sql); if(list.size()==1){ String[] display = (String[])list.get(0); retString = display[0]; } } catch (NumberFormatException e) { log.error(e); } catch (PersistenceException e) { log.error(e); } return retString; } }