PageBuilder.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. package samples.formcollect;
  2. import java.sql.Timestamp;
  3. import java.util.ArrayList;
  4. import javax.servlet.http.HttpServletRequest;
  5. import org.apache.commons.logging.Log;
  6. import org.apache.commons.logging.LogFactory;
  7. import com.sysmodel.Util;
  8. import com.sysmodel.XFormFactory;
  9. import com.sysmodel.XFormModel;
  10. import com.sysmodel.collectmodel.xmlmodel.able.Action;
  11. import com.sysmodel.collectmodel.xmlmodel.able.FormCollection;
  12. import com.sysmodel.collectmodel.xmlmodel.able.PageBody;
  13. import com.sysmodel.collectmodel.xmlmodel.able.PageHead;
  14. import com.sysmodel.collectmodel.xmlmodel.able.PageRoot;
  15. import com.sysmodel.collectmodel.xmlmodel.able.ReportField;
  16. import com.sysmodel.collectmodel.xmlmodel.impl.PageBodyImpl;
  17. import com.sysmodel.collectmodel.xmlmodel.impl.ReportFieldImpl;
  18. import com.sysmodel.datamodel.xmlmodel.ModelFactory;
  19. import com.sysmodel.datamodel.xmlmodel.able.MdpAttribute;
  20. import com.sysmodel.datamodel.xmlmodel.able.MdpClass;
  21. import com.sysmodel.datamodel.xmlmodel.able.SysModel;
  22. import com.sysmodel.xformmodel.importcontrol.DateControl;
  23. import com.sysmodel.xformmodel.importcontrol.SelectControl;
  24. import com.sysmodel.xformmodel.importcontrol.TreeControl;
  25. import com.persistence.service.PersistenceFactory;
  26. import com.persistence.service.assitant.DataObject;
  27. import com.persistence.service.exception.PersistenceException;
  28. public class PageBuilder {
  29. private static Log log = LogFactory.getLog(PageBodyImpl.class);
  30. private XFormModel XFormModel = XFormFactory.getXFormModel();
  31. private FormCollection FormCollection = null;
  32. private DataObject dataobject = null;
  33. public PageBuilder(String pageid,DataObject dataobject){
  34. this.FormCollection = XFormModel.getFormCollection(pageid);
  35. this.dataobject = dataobject;
  36. }
  37. /**
  38. * 获得采集列表的标题
  39. * @return
  40. */
  41. public String produceTitleHtml(){
  42. PageHead PageHead = FormCollection.getPageHead();
  43. StringBuffer sb = new StringBuffer();
  44. sb.append("  "+PageHead.getPageTitle());
  45. return sb.toString();
  46. }
  47. /**
  48. * 产生采集页面的按钮
  49. * @param isDelete
  50. * @return
  51. */
  52. public String produceRootHtml(boolean isDelete) {
  53. PageRoot PageRoot = FormCollection.getPageRoot();
  54. ArrayList alAction = PageRoot.getAllActions();
  55. StringBuffer sb = new StringBuffer();
  56. for (int i = 0; i < alAction.size(); i++) {
  57. Action action = (Action)alAction.get(i);
  58. String buttonValue = action.getValue();
  59. if(!action.getType().equals("delete") && !isDelete){
  60. sb.append("<button id=\"bt"+i+"\" dojoType=\"dijit.form.Button\" onClick=\""+action.getMethod()+"\" type=\"button\" iconClass=\"plusIcon\">");
  61. sb.append(buttonValue);
  62. sb.append("</button>");
  63. }
  64. action = null;
  65. }
  66. return sb.toString();
  67. }
  68. /**
  69. * 生成页面填报数据项
  70. * @param req
  71. * @return
  72. */
  73. public String produceBodyHtml(HttpServletRequest req) {
  74. PageBody PageBody = FormCollection.getPageBody();
  75. int columnCount = PageBody.getColumnCount();
  76. //表头
  77. StringBuffer sb = new StringBuffer();
  78. // /**
  79. // * 产生页面中隐藏插入的字段
  80. // * 只限于在页面信增时候产生字段
  81. // */
  82. // if(dataobject != null){
  83. // ArrayList<HiddenField> alHiddenFields = PageBody.getAlHiddenFields();
  84. // if(dataobject.getObjectID() == null || dataobject.getObjectID().length()==0){
  85. // for (int i = 0; i < alHiddenFields.size(); i++) {
  86. // HiddenField field = (HiddenField) alHiddenFields.get(i);
  87. // sb.append(field.getHiddenInput(req)).append("\n");
  88. // }
  89. // }
  90. // }
  91. sb.append("<table align=\"center\" border=\"0\" cellpadding=\"1\" cellspacing=\"1\" width=\""+PageBody.getWidth() + "\"");
  92. String css = XFormModel.getCSSValue("ReportTableCss");
  93. if (!css.equals(""))
  94. sb.append(" class=\"" + css + "\"");
  95. if (!PageBody.getHeight().equals(""))
  96. sb.append(" height=\"" + PageBody.getHeight() + "\"");
  97. sb.append(">");
  98. //数据区
  99. boolean nextLine = true;
  100. int currentCount = 0;
  101. String FontTextCss = XFormModel.getCSSValue("FontTextCss");
  102. FontTextCss = FontTextCss.equals("")? "":" class=\"" + FontTextCss + "\"";
  103. //td 宽度
  104. short[] widthFlag = new short[columnCount * 2];
  105. String htmlLabelWidth = PageBody.getLabelWidth().equals("") ? "" : " width=\"" + PageBody.getLabelWidth() + "\"";
  106. String htmControlWidth = PageBody.getControlWidth().equals("") ? "" : " width=\"" + PageBody.getControlWidth() + "\"";
  107. String htmlLabelAlign = PageBody.getLabelAlign().equals("") ? "" : " align=\"" + PageBody.getLabelAlign()+ "\"";
  108. String htmControlAlign = PageBody.getControlAlign().equals("") ? "" : " align=\"" + PageBody.getControlAlign() + "\"";
  109. //隔行换色
  110. String LineCssA = XFormModel.getCSSValue("LineCssA");
  111. String LineCssB = XFormModel.getCSSValue("LineCssB");
  112. LineCssA = LineCssA.equals("") ? "" : " class=\"" + LineCssA + "\"";
  113. LineCssB = LineCssB.equals("") ? "" : " class=\"" + LineCssB + "\"";
  114. boolean currentLineCss = true;
  115. ArrayList<ReportFieldImpl> alReportFields = PageBody.getAllReportFields();
  116. for (int i = 0; i < alReportFields.size(); i++) {
  117. ReportField field = (ReportField) alReportFields.get(i);
  118. int result = currentCount + Integer.parseInt(field.getColumnWidth());
  119. if (result > columnCount) {//补上空位,结束该行
  120. int tdCount = columnCount - currentCount;
  121. for (int j = 0; j < tdCount; j++) {
  122. int index = currentCount + j;
  123. if (widthFlag[index] == 0) {
  124. widthFlag[index] = 1;
  125. widthFlag[index + 1] = 1;
  126. sb.append("<td").append(htmlLabelWidth + "></td>");
  127. sb.append("<td").append(htmControlWidth+ "></td>");
  128. } else {
  129. sb.append("<td></td>").append("<td></td>");
  130. }
  131. }
  132. sb.append("</tr>");
  133. nextLine = true;
  134. currentCount = 0;
  135. }
  136. if (nextLine) {
  137. //进行隔行换色
  138. if (currentLineCss)
  139. sb.append("<tr").append(LineCssA + ">");
  140. else
  141. sb.append("<tr").append(LineCssB + ">");
  142. nextLine = false;
  143. currentLineCss = !currentLineCss;
  144. }
  145. //生成控件
  146. if (field.getName().equals("")) {
  147. int ColumnWidth = Integer.parseInt(field.getColumnWidth()) * 2;
  148. sb.append("<td ColumnWidth=\"").append(ColumnWidth + "\" >");
  149. String DivCss = XFormModel.getCSSValue("DivCss");
  150. DivCss = DivCss.equals("")? "":" class=\"" + DivCss + "\"";
  151. sb.append("<div").append(DivCss);
  152. sb.append(">" + field.getLabel()).append("</div></td>");
  153. } else {
  154. sb.append("<td"+htmlLabelAlign);
  155. if (widthFlag[currentCount] == 0) {
  156. widthFlag[currentCount] = 1;
  157. sb.append(htmlLabelWidth);
  158. }
  159. sb.append(">");
  160. sb.append("<font").append(FontTextCss);
  161. sb.append(">" + field.getLabel()).append("</font>");
  162. if(field.getValidate().isNeed()) sb.append(" <span class=\"redcolor\">*</span>");
  163. sb.append("</td>");
  164. //control
  165. sb.append("<td"+htmControlAlign);
  166. if (Integer.parseInt(field.getColumnWidth()) > 1) {
  167. int ColumnWidth = Integer.parseInt(field.getColumnWidth()) * 2 - 1;
  168. sb.append(" ColumnWidth=\"").append(ColumnWidth + "\"");
  169. } else {
  170. if (widthFlag[currentCount + 1] == 0) {
  171. widthFlag[currentCount + 1] = 1;
  172. sb.append(htmControlWidth);
  173. }
  174. }
  175. sb.append(" nowrap>");
  176. sb.append(produceControlHtml(field, dataobject));
  177. sb.append("</td>");
  178. }
  179. //结束行
  180. if (result == columnCount) {
  181. sb.append("</tr>");
  182. nextLine = true;
  183. currentCount = 0;
  184. } else
  185. currentCount += Integer.parseInt(field.getColumnWidth());
  186. }
  187. if (currentCount > 0) {//确认tr封口
  188. int tdCount = columnCount - currentCount;
  189. for (int j = 0; j < tdCount; j++) {
  190. int index = currentCount + j;
  191. if (widthFlag[index] == 0) {
  192. widthFlag[index] = 1;
  193. widthFlag[index + 1] = 1;
  194. sb.append("<td").append(htmlLabelWidth + "></td>");
  195. sb.append("<td").append(htmControlWidth+ "></td>");
  196. } else {
  197. sb.append("<td></td>").append("<td></td>");
  198. }
  199. }
  200. sb.append("</tr>");
  201. }
  202. //end
  203. sb.append("</table>");
  204. return sb.toString();
  205. }
  206. /**
  207. * 获得页面中元素的验证JS
  208. * @return
  209. */
  210. public String produceCientValidateJs(){
  211. PageBody PageBody = FormCollection.getPageBody();
  212. ArrayList<ReportFieldImpl> alReportFields = PageBody.getAllReportFields();
  213. StringBuffer sb = new StringBuffer();
  214. for (int i = 0; i < alReportFields.size(); i++) {
  215. ReportField field = (ReportField) alReportFields.get(i);
  216. // sb.append(field.outputScript());
  217. }
  218. return sb.toString();
  219. }
  220. private String produceControlHtml(ReportField field, DataObject dataobject) {
  221. String value = "";
  222. String displayString = "";
  223. if (dataobject != null) {
  224. Object obj = dataobject.getValue(field.getName());
  225. if (field.getControlType().equals("tree")) {
  226. TreeControl treeControl = (TreeControl) field.getControl();
  227. value = obj.toString();
  228. if (treeControl.getType().equals("2") || treeControl.getType().equals("3")) {//constant
  229. displayString = ModelFactory.getSysmodel().getMdpConstantDisplayString(treeControl.getReferenceTable(), value);
  230. } else {//table
  231. displayString = getReferenceDisplayString(treeControl, value);
  232. }
  233. } else if (field.getControlType().equals("date")) {
  234. DateControl dateControl = (DateControl) field.getControl();
  235. if (!obj.equals(""))
  236. value = Util.getDateTime(dateControl.getFormat(), (Timestamp)obj);
  237. } else
  238. value = obj.toString();
  239. if (field.getControlType().equals("select")) {
  240. SelectControl SelectControl = (SelectControl) field.getControl();
  241. if(SelectControl.getType().equals("1")){
  242. SysModel sysmodel = ModelFactory.getSysmodel();
  243. MdpAttribute attributeObj = sysmodel.getMdpAttributeByName(Integer.parseInt(SelectControl.getIclassid()),field.getName());
  244. //资源数据库选择系统
  245. if(attributeObj.getReference().getReferenceTable().equals("907")){
  246. String StationId = (String)dataobject.getValue("StationId");
  247. displayString = " where StationId='"+ StationId +"'";
  248. }
  249. }
  250. }
  251. }
  252. return field.getControl().produceHtml(field.getName(), value, displayString, field.getValidate());
  253. }
  254. private String getReferenceDisplayString(TreeControl treeControl,String value){
  255. String retString = "";
  256. try {
  257. int classid = Integer.parseInt(treeControl.getReferenceTable());
  258. MdpClass mdpClass = ModelFactory.getSysmodel().getMdpClassByClassID(classid);
  259. String sql = "select "+treeControl.getDisplayName()+ " from " + mdpClass.getName()+ " where "+treeControl.getStoreCode()+"='"+value+"'";
  260. ArrayList list = PersistenceFactory.getInstance().getSearchResult(classid,sql);
  261. if(list.size()==1){
  262. String[] display = (String[])list.get(0);
  263. retString = display[0];
  264. }
  265. } catch (NumberFormatException e) {
  266. log.error(e);
  267. } catch (PersistenceException e) {
  268. log.error(e);
  269. }
  270. return retString;
  271. }
  272. }