zoukankan      html  css  js  c++  java
  • 扩展报表-JavaSet

    前言

    使用商业分析中的扩展报表平台,可以很方便的进行数据分析,进行图表化直观展示。一般情况下使用SQL数据集进行SQL的编写,进而配合扩展报表平台进行数据分析图表的绘制,但SQL数据集针对固定的参数进行条件查询,当需要动态条件查询,查询条件的不同,进而需要SQL根据不同的条件,拼接组装不同的查询SQL。此时,JavaSet就派上用场了。

    使用文档介绍

    标准接口

     1 /**
     2      * 缓存过滤参数Map(key, String)<br>
     3      * key:参数页签定义的参数名称<br>
     4      * String:参数值,字符串形式传入,其中,日期为:yyyy-MM-dd, 时间戳为:yyyy-MM-dd HH:mm:ss, 时间为: HH:mm:ss
     5      */
     6     public void setFilterParam(Map filterParamMap);
     7 
     8     /**
     9      * 返回拼装好的SQL语句
    10      * 
    11      * @param parent
    12      * @return
    13      * @throws Exception
    14      */
    15     public String getCustomSQL(Window parent) throws Exception;
    16 
    17     /**
    18      * 返回IRowSet数组
    19      * 
    20      * @param parent
    21      * @param otherDataCenter :外部数据中心标识,为null或者空,使用本地登录的数据中心
    22      * @return
    23      * @throws Exception
    24      */
    25     public IRowSet[] getCustomRowSet(Window parent, String otherDataCenter) throws Exception;
    26     /**
    27      * 返回输出参数
    28      * 
    29      * @return
    30      */
    31     public Map getOutputParam() throws Exception;

    既然要使用JavaSet,那就得继承标准给定的抽象类AbstractJavaDataSet或者实现接口IJavaDataSet。为了保持扩展性,建议必须实现抽象类

    Demo:

      1 public class JavaDataSetDemo extends AbstractJavaDataSet
      2 {
      3     /**
      4      * 返回拼装好的SQL语句
      5      * 
      6      * @see com.kingdee.eas.rpts.ctrlsqldesign.param.AbstractJavaDataSet#getCustomSQL(java.awt.Window)
      7      */
      8     public String getCustomSQL(Window parent) throws Exception
      9     {
     10         // 弹出用户自定义的过滤界面
     11         KDPanel customUI = new KDPanel();
     12         customUI.setLayout(new BorderLayout());
     13         customUI.setSize(450, 350);
     14         KDLabel label = new KDLabel();
     15         label.setText("用户自定义过滤界面, 把过滤值作为输出参数显示到报表");
     16         customUI.add(label);
     17         ReportDialog.showDialog(customUI, "自定义过滤界面", true, false, parent);// 请换成自己的弹出过滤框代码
     18 
     19 
     20         // 判断过滤参数,拼装SQL,如果是方言,记得加入/*dialect*/
     21         StringBuffer sbSQL = new StringBuffer();
     22         sbSQL.append("SELECT FID, FNumber, FName_L2, FGender, FBirthday FROM T_BD_Person Where 0=0");
     23 
     24         Object value = this.filterParamMap.get("number"); // 是否存在编码
     25         if (value != null && ((String) value).length() > 0)
     26         {
     27             sbSQL.append(" AND FNumber = '").append((String) value).append("'");
     28         }
     29 
     30         value = this.filterParamMap.get("name"); // 是否存在姓名
     31         if (value != null && ((String) value).length() > 0)
     32         {
     33             sbSQL.append(" AND FName_L2 like '%").append((String) value).append("%'");
     34         }
     35 
     36         value = this.filterParamMap.get("sex");
     37         if (value != null && ((String) value).length() > 0)
     38         {
     39             sbSQL.append(" AND FGender = ").append((String) value);
     40         }
     41 
     42         value = this.filterParamMap.get("dateFrom");
     43         if (value != null && ((String) value).length() > 0)
     44         {
     45             sbSQL.append(" AND FBirthday >= {d '").append((String) value).append("'}");
     46         }
     47 
     48         value = this.filterParamMap.get("dateTo");
     49         if (value != null && ((String) value).length() > 0)
     50         {
     51             sbSQL.append(" AND FBirthday <= {d '").append((String) value).append("'}");
     52         }
     53 
     54         return sbSQL.toString();
     55     }
     56 
     57     /**
     58      * 返回IRowSet数组,当前版本仅支持一个结果集
     59      * 
     60      * @see com.kingdee.eas.rpts.ctrlsqldesign.param.AbstractJavaDataSet#getCustomRowSet(java.awt.Window, java.lang.String)
     61      */
     62     public IRowSet[] getCustomRowSet(Window parent, String otherDataCenter) throws Exception
     63     {
     64         String sql = this.getCustomSQL(parent);
     65 
     66         // 可以使用ExtDBUtil提供的executeQuery(Context ctx, String sql)返回结果集,也可以自己另写代码返回结果集
     67         IRowSet irs = ExtDBUtil.executeQuery(null, sql, otherDataCenter);
     68 
     69         // 得到的结果集,进行其他操作处理,例如合并多个结果集等操作
     70 
     71         IRowSet[] iRowSets = new IRowSet[1]; // 当前版本仅支持一个结果集
     72         iRowSets[0] = irs;
     73 
     74         return iRowSets;
     75     }
     76     /**
     77      * 返回输出参数
     78      * 
     79      * @see com.kingdee.eas.rpts.ctrlsqldesign.param.AbstractJavaDataSet#getOutputParam()
     80      */
     81     public Map getOutputParam() throws Exception
     82     {
     83         HashMap outputParamMap = new HashMap();
     84 
     85         // 字符串输出参数,名称是title,在公示中可以用 =@title 取到
     86         outputParamMap.put("title", "扩展报表题头示例");
     87 
     88         // 数值型
     89         outputParamMap.put("count", "2");
     90         outputParamMap.put("pi", "3.14159");
     91 
     92         // 日期,必须是yyyy-MM-dd格式
     93         outputParamMap.put("dateFrom", "2009-09-09");
     94 
     95         // 时间戳,必须是yyyy-MM-dd HH:mm:ss格式
     96         outputParamMap.put("dateTimeFrom", "2009-09-09 09:09:09");
     97 
     98         // 时间,必须是HH:mm:ss格式
     99         outputParamMap.put("timeFrom", "09:09:09");
    100 
    101         return outputParamMap;
    102     }
    103 }

    项目中实际开发应用:

    插曲:刚开始写的时候,继承的抽象类,我自己把  AbstractJavaDataSet 给复制到自己定义的package里面了,导致数据集不起作用。(好傻逼。。。)直接继承,导包即可,包里是存在这个抽象类的!

      1 package com.kingdee.eas.custom.report;
      2 
      3 import java.awt.BorderLayout;
      4 import java.awt.Window;
      5 import java.util.HashMap;
      6 import java.util.Map;
      7 
      8 import org.slf4j.Logger;
      9 import org.slf4j.LoggerFactory;
     10 
     11 import com.kingdee.bos.ctrl.swing.KDLabel;
     12 import com.kingdee.bos.ctrl.swing.KDPanel;
     13 import com.kingdee.eas.rpts.ctrlreport.client.ReportDialog;
     14 import com.kingdee.eas.rpts.ctrlsqldesign.model.ExtDBUtil;
     15 import com.kingdee.eas.rpts.ctrlsqldesign.param.AbstractJavaDataSet;
     16 import com.kingdee.jdbc.rowset.IRowSet;
     17 
     18 public class JavaDataSetRiskItem extends AbstractJavaDataSet {
     19     Logger LOGGER = LoggerFactory.getLogger(JavaDataSetRiskItem.class);
     20 
     21     /**
     22      * (非 Javadoc)
     23      * <p>
     24      * Title: getCustomRowSet
     25      * </p>
     26      * <p>
     27      * Description:
     28      * </p>
     29      * 返回IRowSet数组,当前版本仅支持一个结果集
     30      * 
     31      * @param parent
     32      * @param otherDataCenter
     33      * @return
     34      * @throws Exception
     35      * @see com.kingdee.eas.custom.report.AbstractJavaDataSet#getCustomRowSet(java.awt.Window,
     36      *      java.lang.String)
     37      */
     38     @Override
     39     public IRowSet[] getCustomRowSet(Window parent, String otherDataCenter)
     40             throws Exception {
     41         String customSQL = this.getCustomSQL(parent);
     42         IRowSet irs = ExtDBUtil.executeQuery(null, customSQL, otherDataCenter);
     43 
     44         IRowSet[] iRowSets = new IRowSet[1]; // 当前版本仅支持一个结果集
     45         iRowSets[0] = irs;
     46 
     47         return iRowSets;
     48     }
     49 
     50     /**
     51      * (非 Javadoc)
     52      * <p>
     53      * Title: getCustomSQL
     54      * </p>
     55      * <p>
     56      * Description:
     57      * </p>
     58      * 返回拼装好的SQL语句
     59      * 
     60      * @param parent
     61      * @return
     62      * @throws Exception
     63      * @see com.kingdee.eas.custom.report.AbstractJavaDataSet#getCustomSQL(java.awt.Window)
     64      */
     65     @Override
     66     public String getCustomSQL(Window parent) throws Exception {
     67         // 弹出用户自定义的过滤界面
     68         /*
     69          * KDPanel customUI = new KDPanel(); customUI.setLayout(new
     70          * BorderLayout()); customUI.setSize(450, 350); KDLabel label = new
     71          * KDLabel();
     72          * label.setText("                    用户自定义过滤界面, 把过滤值作为输出参数显示到报表");
     73          * customUI.add(label); ReportDialog.showDialog(customUI, "自定义过滤界面",
     74          * true, false, parent);// 请换成自己的弹出过滤框代码
     75          */
     76 
     77         // 判断过滤参数,拼装SQL,如果是方言,记得加入/*dialect*/
     78         /*
     79          * 公司名称:companyName 预警指标(item) 年(year) 起始月(startMon) 结束月(endMon)
     80          */
     81         StringBuffer sbSQL = new StringBuffer();
     82         sbSQL.append(" select ");
     83         sbSQL.append(" cfmonth as 月份, ");
     84         sbSQL.append(" CT_FIN_SasacEntry.cfpoor as 平均值, ");
     85         sbSQL.append(" CT_FIN_SasacEntry.cfaverage as 较差值, ");
     86 
     87         /* 组装主查询 */
     88         String item = (String) this.filterParamMap.get("item");
     89         if (item != null && item.length() > 0) {
     90             if (item.equals("资产负债率")) {
     91                 sbSQL.append(" cftotalLiabilites/cftotalAssets as  企业数据 ");
     92             } else if (item.equals("营业利润")) {
     93                 sbSQL.append(" cftrafficProfit/10000 as  企业数据 ");
     94             } else if (item.equals("己获利息倍数")) {
     95                 sbSQL
     96                         .append(" (cftotalProfit+cfinterestExpense)/cfinterestExpense as  企业数据 ");
     97             } else if (item.equals("流动比率")) {
     98                 sbSQL.append(" cfcurrentAssets/cfcurrentLiabilities as  企业数据 ");
     99             } else if (item.equals("两金占流动资产比重")) {
    100                 sbSQL
    101                         .append(" (cfaccountReceivable+cfstock)/cfcurrentAssets as  企业数据 ");
    102             } else if (item.equals("速动比率")) {
    103                 sbSQL
    104                         .append(" (cfcurrentAssets-cfstock)/cfcurrentLiabilities  as  企业数据 ");
    105             } else if (item.equals("现金流动负债比率")) {
    106                 sbSQL.append(" cfoperaCashFlow/cfcurrentLiabilities as  企业数据 ");
    107             } else if (item.equals("带息负债比率")) {
    108                 sbSQL
    109                         .append(" (cfshortLoan+CFOneYearLongLiabilities+cflongLoan+CFBondsPayable+CFInterestPayable)/cftotalLiabilites as  企业数据 ");
    110             } else if (item.equals("或有负债比率")) {
    111                 sbSQL
    112                         .append(" (CFDraft+CFBalance+CFAmountOfmatter+CFContingentLiabilities)/cfownerRights as  企业数据 ");
    113             } else if (item.equals("平均融资成本率")) {
    114                 sbSQL.append(" CFTotalCosts/CFFinancingCost as  企业数据 ");
    115             }
    116         }
    117 
    118         // 组装表 条件查询
    119         sbSQL.append(" FROM CT_FIN_RiskItem as CT_FIN_RiskItem ");
    120         sbSQL
    121                 .append(" INNER join CT_FIN_Sasac as CT_FIN_Sasac on CT_FIN_RiskItem.cfyear = CT_FIN_Sasac.cfyear ");
    122         sbSQL
    123                 .append(" INNER join CT_FIN_SasacEntry as CT_FIN_SasacEntry on CT_FIN_Sasac.fid = CT_FIN_SasacEntry.fparentid ");
    124         sbSQL.append(" WHERE 1=1 ");
    125 
    126         String companyName = (String) this.filterParamMap.get("companyName");
    127         if (companyName != null && companyName.length() > 0) {
    128             sbSQL.append(" and CT_FIN_RiskItem.cfcompanyName = " + "'"
    129                     + companyName + "'");
    130         }
    131 
    132         String year = (String) this.filterParamMap.get("year");
    133         if (year != null && year.length() > 0) {
    134             sbSQL.append(" and CT_FIN_RiskItem.cfyear = " + year);
    135         }
    136 
    137         if (item != null && item.length() > 0) {
    138             sbSQL.append(" and CT_FIN_SasacEntry.cfname = " + "'" + item + "'");
    139         }
    140 
    141         String monStart = (String) this.filterParamMap.get("monStart");
    142         if (monStart != null && monStart.length() > 0) {
    143             sbSQL.append(" and CT_FIN_RiskItem.cfmonth >= " + monStart);
    144         }
    145 
    146         String monEnd = (String) this.filterParamMap.get("monEnd");
    147         if (monEnd != null && monEnd.length() > 0) {
    148             sbSQL.append(" and CT_FIN_RiskItem.cfmonth <= " + monEnd);
    149         }
    150 
    151         LOGGER.info(sbSQL.toString());
    152         return sbSQL.toString();
    153     }
    154 
    155     /**
    156      * (非 Javadoc)
    157      * <p>
    158      * Title: getOutputParam
    159      * </p>
    160      * <p>
    161      * Description:
    162      * </p>
    163      * 返回输出参数
    164      * 
    165      * @return
    166      * @throws Exception
    167      * @see com.kingdee.eas.custom.report.AbstractJavaDataSet#getOutputParam()
    168      */
    169     @Override
    170     public Map getOutputParam() throws Exception {
    171         HashMap outputParamMap = new HashMap();
    172 
    173         // 字符串输出参数,名称是title,在公示中可以用 =@title 取到
    174         outputParamMap.put("title", "扩展报表题头示例");
    175 
    176         // 数值型
    177         outputParamMap.put("count", "2");
    178         outputParamMap.put("pi", "3.14159");
    179 
    180         // 日期,必须是yyyy-MM-dd格式
    181         outputParamMap.put("dateFrom", "2009-09-09");
    182 
    183         // 时间戳,必须是yyyy-MM-dd HH:mm:ss格式
    184         outputParamMap.put("dateTimeFrom", "2009-09-09 09:09:09");
    185 
    186         // 时间,必须是HH:mm:ss格式
    187         outputParamMap.put("timeFrom", "09:09:09");
    188 
    189         return outputParamMap;
    190     }
    191 
    192     /**
    193      * (非 Javadoc)
    194      * <p>
    195      * Title: isUIType
    196      * </p>
    197      * <p>
    198      * Description:
    199      * </p>
    200      * 不是GUI 所用数据集就返回false
    201      * 
    202      * @return
    203      * @see com.kingdee.eas.rpts.ctrlsqldesign.param.AbstractJavaDataSet#isUIType()
    204      */
    205     @Override
    206     public boolean isUIType() {
    207         return false;
    208     }
    209 
    210 }
  • 相关阅读:
    5.对象创建型模式-原型PROTOTYPE
    4.对象创建型模式-工厂方法
    3.对象创建型模式-生成器
    一个小应用的dbcp和c3p0配置实例
    利用 java.lang.Runtime.addShutdownHook() 钩子程序,保证java程序安全退出
    初探maven插件机制
    【转载】Git push时重复输入用户名密码的问题
    【转载】 ERROR 1045 (28000): Access denied for user root@localhost (using password: NO)
    【转载】[Java]读取文件方法大全
    【原创】iframe与父页面之间,变量、方法互相调用
  • 原文地址:https://www.cnblogs.com/lyc-smile/p/7287425.html
Copyright © 2011-2022 走看看