Java tutorial
/* * Copyright (C) 2010---2014 (wuweixing)<349446658@qq.com> * * This file is part of Wabacus * * Wabacus is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package com.wabacus.config.print; import java.util.ArrayList; import java.util.List; import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.wabacus.config.Config; import com.wabacus.config.component.ComponentConfigLoadAssistant; import com.wabacus.config.component.ComponentConfigLoadManager; import com.wabacus.config.component.IComponentConfigBean; import com.wabacus.config.component.application.report.ReportBean; import com.wabacus.config.component.container.AbsContainerConfigBean; import com.wabacus.config.template.TemplateParser; import com.wabacus.config.xml.XmlElementBean; import com.wabacus.exception.WabacusConfigLoadingException; import com.wabacus.system.ReportRequest; import com.wabacus.system.assistant.WabacusAssistant; import com.wabacus.system.buttons.AbsButtonType; import com.wabacus.system.buttons.PrintButton; import com.wabacus.system.print.AbsPrintProvider; import com.wabacus.util.Consts; import com.wabacus.util.Tools; public abstract class AbsPrintProviderConfigBean implements Cloneable { private final static Log log = LogFactory.getLog(AbsPrintProviderConfigBean.class); protected String jobname;//????title?????request/session?????????? protected Map<String, String> mDynJobnameParts; protected String button; protected boolean isPreview; protected String previewbutton; protected boolean isSetting;//??? protected String settingbutton; protected String printpagesize; protected IComponentConfigBean owner; protected List<String> lstIncludeApplicationIds; protected String includeApplicationids;//?lstApplicationIds? protected Map<String, Integer> mReportidAndPagesize; protected List<PrintSubPageBean> lstPrintPageBeans; protected String printPageInfo; protected Boolean isTemplatePrintValue; protected boolean isUseDefaultPrintTemplate;//?<print/>????????<print/>??<print/>????? protected XmlElementBean elePrintBean;//<print/>doPolstLoad()???null private int placeholderIndex = 0; public AbsPrintProviderConfigBean(IComponentConfigBean owner) { this.owner = owner; } public IComponentConfigBean getOwner() { return owner; } public void setOwner(IComponentConfigBean owner) { this.owner = owner; } public String getJobname(ReportRequest rrequest) { String realjobname = null; if (jobname == null || jobname.trim().equals("")) {//??? realjobname = this.owner.getTitle(rrequest); if (realjobname == null || realjobname.trim().equals("")) realjobname = this.owner.getId(); } else { realjobname = WabacusAssistant.getInstance().getStringValueWithDynPart(rrequest, this.jobname, this.mDynJobnameParts, ""); } return realjobname; } public boolean isPreview() { return isPreview; } public boolean isSetting() { return isSetting; } public String getPreviewbutton() { return previewbutton; } public String getSettingbutton() { return settingbutton; } public List<String> getLstIncludeApplicationIds() { return lstIncludeApplicationIds; } public String getIncludeApplicationids() { return includeApplicationids; } public String getButton() { return button; } public List<PrintSubPageBean> getLstPrintPageBeans() { return lstPrintPageBeans; } public boolean isUseGlobalDefaultPrintTemplate() { return isUseDefaultPrintTemplate; } public void initPrint(ReportRequest rrequest) { } public Boolean isTemplatePrintValue() { if (isTemplatePrintValue == null) return false; return isTemplatePrintValue.booleanValue(); } public int getPrintPageSize(String reportid) { if (this.mReportidAndPagesize == null || this.mReportidAndPagesize.get(reportid) == null || mReportidAndPagesize.get(reportid) == Integer.MIN_VALUE) return 0; return this.mReportidAndPagesize.get(reportid).intValue(); } public Map<String, Integer> getMReportidAndPagesize() { return mReportidAndPagesize; } public int getPlaceholderIndex() { return placeholderIndex++; } public abstract AbsPrintProvider createPrintProvider(ReportRequest rrequest); public void loadConfig(XmlElementBean elePrintBean) { String printbtn = elePrintBean.attributeValue("button"); if (printbtn != null && !printbtn.trim().equals("")) { this.button = Config.getInstance().getResourceString(null, this.owner.getPageBean(), printbtn, false); } String preview = elePrintBean.attributeValue("preview"); this.isPreview = preview != null && preview.toLowerCase().trim().equals("true"); String previewbtn = elePrintBean.attributeValue("previewbutton"); if (previewbtn != null && !previewbtn.trim().equals("")) { this.previewbutton = Config.getInstance().getResourceString(null, this.owner.getPageBean(), previewbtn, false); } String setting = elePrintBean.attributeValue("setting"); this.isSetting = setting != null && setting.toLowerCase().trim().equals("true"); String settingbtn = elePrintBean.attributeValue("settingbutton"); if (settingbtn != null && !settingbtn.trim().equals("")) { this.settingbutton = Config.getInstance().getResourceString(null, this.owner.getPageBean(), settingbtn, false); } this.printpagesize = elePrintBean.attributeValue("printpagesize"); String include = elePrintBean.attributeValue("include"); if (include != null && !include.trim().equals("")) { this.lstIncludeApplicationIds = Tools.parseStringToList(include, ";", false); } String jobname = elePrintBean.attributeValue("jobname"); if (jobname != null) { Object[] objArr = WabacusAssistant.getInstance().parseStringWithDynPart(this.owner.getPageBean(), jobname.trim()); this.jobname = (String) objArr[0]; this.mDynJobnameParts = (Map<String, String>) objArr[1]; } XmlElementBean elePageinfoBean = elePrintBean.getChildElementByName("pageinfo"); if (elePageinfoBean != null) { String pageinfo = elePageinfoBean.getContent(); this.printPageInfo = pageinfo == null ? "" : pageinfo.trim(); if (Tools.isDefineKey("$", this.printPageInfo)) this.printPageInfo = Config.getInstance().getResourceString(null, this.owner.getPageBean(), this.printPageInfo, true); } else { this.printPageInfo = Config.getInstance().getResourceString(null, this.owner.getPageBean(), "${print.pageinfo.default}", true);//?<pageinfo/>?? } // width=width==null?"":width.trim(); this.elePrintBean = elePrintBean; } public void doPostLoad() { checkedAndAddButtons(Consts.PRINTTYPE_PRINT); if (this.isPreview) checkedAndAddButtons(Consts.PRINTTYPE_PRINTPREVIEW); if (this.isSetting) checkedAndAddButtons(Consts.PRINTTYPE_PRINTSETTING); processIncludeApplicationIds(); this.lstPrintPageBeans = new ArrayList<PrintSubPageBean>(); loadSubpageConfig();//<print/><subpage/> createPrintJsScript(); this.owner.getPageBean().addPrintBean(this); } protected void checkedAndAddButtons(String printButtonType) { if (!(this.owner instanceof ReportBean) && !(this.owner instanceof AbsContainerConfigBean)) { throw new WabacusConfigLoadingException("" + this.owner.getPath() + "???<print/>???<print/>??"); } List<AbsButtonType> lstPrintButtons = null; if (this.owner.getButtonsBean() != null) lstPrintButtons = this.owner.getButtonsBean().getLstPrintTypeButtons(printButtonType); if (lstPrintButtons == null || lstPrintButtons.size() == 0) { AbsButtonType buttonObj = Config.getInstance().getResourceButton(null, this.owner, Consts.M_PRINT_DEFAULTBUTTONS.get(printButtonType), PrintButton.class); buttonObj.setDefaultNameIfNoName(); if (this.owner instanceof AbsContainerConfigBean) { buttonObj.setPosition("top"); } ComponentConfigLoadManager.addButtonToPositions(this.owner, buttonObj); } } private void processIncludeApplicationIds() { Object[] objResult = ComponentConfigLoadAssistant.getInstance().parseIncludeApplicationids(this.owner, this.lstIncludeApplicationIds); this.includeApplicationids = (String) objResult[0]; this.lstIncludeApplicationIds = (List<String>) objResult[1]; this.mReportidAndPagesize = (Map<String, Integer>) objResult[2]; } protected void addIncludeApplicationId(String appid) { if (appid == null || appid.trim().equals("") || this.lstIncludeApplicationIds.contains(appid)) return; appid = appid.trim(); if (this.owner.getPageBean().getApplicationChild(appid, true) == null) { throw new WabacusConfigLoadingException("" + this.owner.getPath() + "??include?ID" + appid + "?"); } ReportBean rbean = this.owner.getPageBean().getReportChild(appid, true); if (rbean != null) this.mReportidAndPagesize.put(appid, Integer.MIN_VALUE); this.lstIncludeApplicationIds.add(appid); this.includeApplicationids += appid + ";"; } private void loadSubpageConfig() { if (this.elePrintBean == null) return; List<XmlElementBean> lstEleSubpageBeans = this.elePrintBean.getLstChildElementsByName("subpage");//?<subpage/>? PrintSubPageBean pspagebeanTmp; String printContentTmp; if (lstEleSubpageBeans == null || lstEleSubpageBeans.size() == 0) {//?<subpage/><print/><subpage/> printContentTmp = this.elePrintBean.getContent(); if (printContentTmp != null && !printContentTmp.trim().equals("")) {//<print/>?? pspagebeanTmp = new PrintSubPageBean(this); pspagebeanTmp.addIncludeSplitPrintReportids(this.lstIncludeApplicationIds); parsePrintContent(pspagebeanTmp, printContentTmp); this.lstPrintPageBeans.add(pspagebeanTmp); } } else {//?<subpage/> for (XmlElementBean eleSubpageBeanTmp : lstEleSubpageBeans) { pspagebeanTmp = new PrintSubPageBean(this); String mergeup = eleSubpageBeanTmp.attributeValue("mergeup"); pspagebeanTmp.setMergeUp(mergeup == null || !mergeup.toLowerCase().trim().equals("false")); String splitprintreport = eleSubpageBeanTmp.attributeValue("splitprintreport"); if (splitprintreport != null && !splitprintreport.trim().equals("")) { pspagebeanTmp .addIncludeSplitPrintReportids(Tools.parseStringToList(splitprintreport, ";", false)); } if (pspagebeanTmp.isSplitPrintPage()) { String minpagecount = eleSubpageBeanTmp.attributeValue("minpagecount"); if (minpagecount != null && !minpagecount.trim().equals("")) { try { pspagebeanTmp.setMinpagecount(Integer.parseInt(minpagecount)); } catch (NumberFormatException e) { log.warn("" + this.owner.getPath() + "?<subpage/>minpagecount" + minpagecount + "?", e); } } String maxpagecount = eleSubpageBeanTmp.attributeValue("maxpagecount"); if (maxpagecount != null && !maxpagecount.trim().equals("")) { try { pspagebeanTmp.setMaxpagecount(Integer.parseInt(maxpagecount)); } catch (NumberFormatException e) { log.warn("" + this.owner.getPath() + "?<subpage/>maxpagecount" + maxpagecount + "?", e); } } } printContentTmp = eleSubpageBeanTmp.getContent(); if (printContentTmp == null || printContentTmp.trim().equals("")) continue;//<print/>?<subpage/>??? parsePrintContent(pspagebeanTmp, printContentTmp); this.lstPrintPageBeans.add(pspagebeanTmp); } } this.elePrintBean = null;//?????null PrintTemplateElementBean ptelebeanTmp; if (this.lstPrintPageBeans.size() == 0) {//<print/>??? this.isTemplatePrintValue = Boolean.TRUE; if (this.owner instanceof ReportBean) { pspagebeanTmp = new PrintSubPageBean(this); pspagebeanTmp.addIncludeSplitPrintReportids(this.lstIncludeApplicationIds); ptelebeanTmp = new PrintTemplateElementBean(this.getPlaceholderIndex()); ptelebeanTmp.setType(PrintTemplateElementBean.ELEMENT_TYPE_APPLICATIONID); ptelebeanTmp.setValueObj(this.owner.getId());//???????<report/>printwidth??????printwidth // ptelebeanTmp.setValueObj(Config.getInstance().getDefaultReportPrintTplBean());//???? pspagebeanTmp.addPrintElement(ptelebeanTmp); this.lstPrintPageBeans.add(pspagebeanTmp); isUseDefaultPrintTemplate = true; } else { for (String appidTmp : this.lstIncludeApplicationIds) { pspagebeanTmp = new PrintSubPageBean(this); pspagebeanTmp.setMergeUp(false);//??? pspagebeanTmp.addIncludeSplitPrintReportid(appidTmp); ptelebeanTmp = new PrintTemplateElementBean(this.getPlaceholderIndex()); ptelebeanTmp.setType(PrintTemplateElementBean.ELEMENT_TYPE_APPLICATIONID); ptelebeanTmp.setValueObj(appidTmp); pspagebeanTmp.addPrintElement(ptelebeanTmp); this.lstPrintPageBeans.add(pspagebeanTmp); } } } } protected void parsePrintContent(PrintSubPageBean pspagebean, String printContent) { this.isTemplatePrintValue = Boolean.TRUE; PrintTemplateElementBean ptelebeanTmp = new PrintTemplateElementBean(this.getPlaceholderIndex()); if (ComponentConfigLoadAssistant.getInstance().isStaticTemplateResource(printContent)) { ptelebeanTmp.setType(PrintTemplateElementBean.ELEMENT_TYPE_STATICTPL); ptelebeanTmp.setValueObj(ComponentConfigLoadAssistant.getInstance() .getStaticTemplateBeanByConfig(this.owner.getPageBean(), printContent)); } else if (Tools.isDefineKey("jsp", printContent)) { ptelebeanTmp.setType(PrintTemplateElementBean.ELEMENT_TYPE_DYNTPL); ptelebeanTmp.setValueObj(Tools.getRealKeyByDefine("jsp", printContent)); } else {//<print></print>???? ptelebeanTmp.setType(PrintTemplateElementBean.ELEMENT_TYPE_STATICTPL); ptelebeanTmp.setValueObj(TemplateParser.parseTemplateByContent(printContent)); } pspagebean.addPrintElement(ptelebeanTmp); } protected abstract void createPrintJsScript(); public String getPrintJsMethodName() { return this.owner.getGuid() + "_print"; } public Object clone() { try { return super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); return null; } } }