调用Cognos sdk接口生成报表

来源:互联网 发布:vr淘宝宣传视频 编辑:程序博客网 时间:2024/06/05 22:06


分类: Cognos 476人阅读 评论(0)收藏 举报

 java调用SDK提供的接口实现生成报表静态文件至本机(html,excel格式)的一个demo

 

[java] view plaincopy
  1. /** 
  2. * 连接cognos服务器 
  3. */  
  4. public class CognosServerConnectAL  {  
  5. /** 
  6.   * cognos内容库服务Locater对象 
  7.   */  
  8. private ContentManagerService_ServiceLocator cmServiceLocator;  
  9. /** 
  10.   * cognos报表服务Locater对象 
  11.   */  
  12. private ReportService_ServiceLocator reportServiceLocator;  
  13. /** 
  14.   * cognos内容库服务对象 
  15.   */  
  16. private ContentManagerService_Port cmService;  
  17. /** 
  18.   * cognos报表服务对象 
  19.   */  
  20. private ReportService_Port repService;  
  21. /** 
  22.   * 获取内容库服务Locater对象 
  23.   * @return 
  24.   */  
  25. public ContentManagerService_ServiceLocator   
  26.   
  27. getCmServiceLocator() {  
  28.   return cmServiceLocator;  
  29. }  
  30. /** 
  31.   * 设置内容库服务Locater对象 
  32.   * @param cmServiceLocator 
  33.   */  
  34. public void setCmServiceLocator(  
  35.    ContentManagerService_ServiceLocator cmServiceLocator) {  
  36.   this.cmServiceLocator = cmServiceLocator;  
  37. }  
  38. /** 
  39.   * 获取报表服务Locater对象 
  40.   * @return 
  41.   */  
  42. public ReportService_ServiceLocator getReportServiceLocator() {  
  43.   return reportServiceLocator;  
  44. }  
  45. /** 
  46.   * 设置报表服务Locater对象 
  47.   * @param reportServiceLocator 
  48.   */  
  49. public void setReportServiceLocator(  
  50.    ReportService_ServiceLocator reportServiceLocator) {  
  51.   this.reportServiceLocator = reportServiceLocator;  
  52. }  
  53.   
  54. /** 
  55.   * 获得内容库连接 
  56.   *  
  57.   * @param cognosUrl cognos服务器URL 
  58.   * @return 
  59.   * @throws ServiceException 
  60.   * @throws MalformedURLException 
  61.   */  
  62. public ContentManagerService_Port connectToCognosServer(String   
  63.   
  64. cognosUrl)  
  65.    throws ServiceException, MalformedURLException {  
  66.   java.net.URL serverURL = new java.net.URL(cognosUrl);  
  67.   
  68.   cmService = cmServiceLocator.getcontentManagerService  
  69.   
  70. (serverURL);  
  71.   repService = reportServiceLocator.getreportService  
  72.   
  73. (serverURL);  
  74.   ((Stub) cmService).setTimeout(0);//内容库超时  
  75.   ((Stub) repService).setTimeout(0);//报表服务超时  
  76.   return cmService;  
  77. }  
  78.   
  79. /** 
  80.   * 获得cognos内容库服务对象 
  81.   *  
  82.   * @return 
  83.   */  
  84. public ContentManagerService_Port getCMService() {  
  85.   return cmService;  
  86. }  
  87.   
  88. /** 
  89.   * 获得cognos报表服务对象 
  90.   *  
  91.   * @return 
  92.   */  
  93. public ReportService_Port getReportService() {  
  94.   return repService;  
  95. }  
  96. }  
  97.   
  98. 注:连接Cognos服务未使用权限。  
  99.   
  100.   
  101.   
  102.   
  103. 设置报表参数:  
  104.   
  105. /* 
  106. * PURPOSE : 设置报表参数 
  107. */  
  108.   
  109. import java.text.SimpleDateFormat;  
  110. import java.util.ArrayList;  
  111. import java.util.Calendar;  
  112. import java.util.GregorianCalendar;  
  113. import java.util.List;  
  114. import com.cognos.developer.schemas.bibus._3.ParameterValue;  
  115. import com.cognos.developer.schemas.bibus._3.ParmValueItem;  
  116. import   
  117.   
  118. com.cognos.developer.schemas.bibus._3.SimpleParmValueItem;  
  119.   
  120. /** 
  121. * 报表参数设置 
  122.  
  123. * @author 
  124. */  
  125. public class SetReportParameterAL {  
  126.   
  127. /** 
  128.   * 设置报表所需的所有参数值 
  129.   *  
  130.   * @param p 
  131.   * @return 
  132.   */  
  133. public ParameterValue[] setReportParameters(RepParameterHelper   
  134.   
  135. p) {  
  136.   // 获取频率  
  137.   int repFrequency = p.getReportPutInfoDomain().getReportFreq  
  138.   
  139. ();  
  140.   //获取结束日期  
  141.   Calendar endDate = p.getReportDate();  
  142.   //获取参数、参数值  
  143.   List<ReportPropertyDomain> list = p.getProperties();  
  144.   int propNum = 0;  
  145.   if (endDate != null) {  
  146.   
  147.    /* 根据期末日期和报表频率获得报告期间的开始日期 */  
  148.    Calendar startDate = getStartDateByRepFrequency  
  149.   
  150. (repFrequency,  
  151.      endDate);  
  152.   
  153.    if (list != null) {  
  154.     propNum = list.size();  
  155.    }  
  156.    //报表运行参数对象  
  157.    ParameterValue[] parameters=null;  
  158.    parameters = new ParameterValue[propNum + 1]; // 固定参数数  
  159.   
  160. 组+开始结束日期  
  161.    ParameterValue parameter = null;  
  162.    ReportPropertyDomain propDomain = null;  
  163.    for (int i = 0; i < propNum; i++) {  
  164.     propDomain = list.get(i);  
  165.     //设置参数名,参数值至参数对象  
  166.     parameter = setParameter(p, propDomain.getParameterName  
  167.   
  168. (),propDomain.getParameterValue());  
  169.     parameters = parameter;  
  170.    }  
  171.    //设置结束日期  
  172.    parameters[propNum] = setParameter(p,   
  173.   
  174. ParameterUtil.END_DATE,  
  175.      calendarToString(endDate));  
  176.    //设置开始日期  
  177.    parameters[propNum + 1] = setParameter(p,   
  178.   
  179. ParameterUtil.START_DATE,  
  180.      calendarToString(startDate));  
  181.    return parameters;  
  182.   } else {  
  183.    return null;  
  184.   }  
  185. }  
  186.   
  187. /** 
  188.   * 根据频率和结束日期,获得开始日期 
  189.   *  
  190.   * @param repFrequency 
  191.   *            报表频率,(日,周,月,季,半年,年 分别对应 
  192.  
  193. 1,2,3,4,5,6) 
  194.   * @param endDate 
  195.   *            结束日期 
  196.   * @return 
  197.   */  
  198. private Calendar getStartDateByRepFrequency(int repFrequency,  
  199.    Calendar endDate) {  
  200.   Calendar startDate = new GregorianCalendar();  
  201.   startDate.set(Calendar.YEAR, endDate.get(Calendar.YEAR));//设  
  202.   
  203. 置年  
  204.   startDate.set(Calendar.MONTH, endDate.get(Calendar.MONTH));//  
  205.   
  206. 设置月  
  207.   startDate.set(Calendar.DAY_OF_MONTH, endDate.get  
  208.   
  209. (Calendar.DAY_OF_MONTH));//设置日  
  210.     
  211.   switch (repFrequency) {  
  212.   
  213.   // 日报的开始日期,开始日期和结束日期同一日  
  214.   case ParameterUtil.DAY:  
  215.    return startDate;  
  216.   
  217.    // 周报的开始日期,开始日期为星期一  
  218.   case ParameterUtil.WEEK:  
  219.    startDate.set(Calendar.DAY_OF_WEEK, 1);  
  220.    break;  
  221.   
  222.   // 月报的开始日期,开始日期为月初  
  223.   case ParameterUtil.MONTH:  
  224.    startDate.set(Calendar.DAY_OF_MONTH, 1);  
  225.    break;  
  226.   
  227.   // 季报的开始日期  
  228.   case ParameterUtil.SEASON:  
  229.    switch (endDate.get(Calendar.MONTH)) {  
  230.    case Calendar.MARCH://第一季度  
  231.     startDate.set(Calendar.MONTH, 0);//开始日期,1月1日  
  232.     startDate.set(Calendar.DAY_OF_MONTH, 1);  
  233.     break;  
  234.    case Calendar.JUNE://第二季度  
  235.     startDate.set(Calendar.MONTH, 3);//开始日期,4月1日  
  236.     startDate.set(Calendar.DAY_OF_MONTH, 1);  
  237.     break;  
  238.    case Calendar.SEPTEMBER://第三季度  
  239.     startDate.set(Calendar.MONTH, 6);//开始日期,7月1日  
  240.     startDate.set(Calendar.DAY_OF_MONTH, 1);  
  241.     break;  
  242.    case Calendar.DECEMBER://第四季度  
  243.     startDate.set(Calendar.MONTH, 9);//开始日期,10月1日  
  244.     startDate.set(Calendar.DAY_OF_MONTH, 1);  
  245.     break;  
  246.    default:  
  247.    }  
  248.    ;  
  249.    break;  
  250.   
  251.   // 半年报的开始日期  
  252.   case ParameterUtil.HALF_YEAR:  
  253.    if (endDate.get(Calendar.MONTH) == Calendar.JUNE) {//如果结  
  254.   
  255. 束日期为6月份  
  256.     startDate.set(Calendar.MONTH, 0);//开始日期,1月1日  
  257.     startDate.set(Calendar.DAY_OF_MONTH, 1);  
  258.    } else {  
  259.     startDate.set(Calendar.MONTH, 6);//开始日期,7月1日  
  260.     startDate.set(Calendar.DAY_OF_MONTH, 1);  
  261.    }  
  262.    ;  
  263.    break;  
  264.   
  265.   // 年报的开始日期  
  266.   case ParameterUtil.YEAR:  
  267.    startDate.set(Calendar.MONTH, 0);//开始日期1月1日  
  268.    startDate.set(Calendar.DAY_OF_MONTH, 1);  
  269.    break;  
  270.   default:  
  271.   }  
  272.   
  273.   return startDate;  
  274. }  
  275.   
  276. /** 
  277.   * Calendar转换为日期格式的字符串 
  278.   *  
  279.   * @param calendar 
  280.   * @return 
  281.   */  
  282. private String calendarToString(Calendar calendar) {  
  283.   SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-  
  284.   
  285. dd");  
  286.   String date = dateFormat.format(calendar.getTime());  
  287.   
  288.   return date;  
  289. }  
  290.   
  291. /** 
  292.   * 设置参数 
  293.   *  
  294.   * @param paramName 
  295.   *            参数名 
  296.   * @param paramValue 
  297.   *            参数值 
  298.   * @return 
  299.   */  
  300. private ParameterValue setParameter(RepParameterHelper p,   
  301.   
  302. String paramName,  
  303.    String paramValue) {  
  304.   //参数对象  
  305.   ParameterValue parameter = new ParameterValue();  
  306.   /* 
  307.   同个参数名但有一组值(in(a,b,c,d)) 
  308.   */  
  309.   paramValues = paramValue.split(",");//分割一组值  
  310.   int paramNum = paramValues.length;  
  311.   ParmValueItem[] pvi = new ParmValueItem[paramNum];  
  312.   //分割后循环依次设置到参数对象中  
  313.   for (int i = 0; i < paramNum; i++) {  
  314.    SimpleParmValueItem item = new SimpleParmValueItem();  
  315.    item.setUse(paramValues);//使用值  
  316.    pvi = item;  
  317.   }  
  318.   parameter.setName(paramName);  
  319.   parameter.setValue(pvi);  
  320.   
  321.   return parameter;  
  322. }  
  323. }  


 

[java] view plaincopy
  1. /* 
  2. * 运行导出报表 
  3. */  
  4. import java.io.File;  
  5. import java.io.FileOutputStream;  
  6. import java.io.IOException;  
  7. import java.rmi.RemoteException;  
  8. import com.cognos.developer.schemas.bibus._3.AsynchDetailReportOutput;  
  9. import com.cognos.developer.schemas.bibus._3.AsynchDetailReportStatus;  
  10. import com.cognos.developer.schemas.bibus._3.AsynchDetailReportStatusEnum;  
  11. import com.cognos.developer.schemas.bibus._3.AsynchOptionEnum;  
  12. import com.cognos.developer.schemas.bibus._3.AsynchOptionInt;  
  13. import com.cognos.developer.schemas.bibus._3.AsynchReply;  
  14. import com.cognos.developer.schemas.bibus._3.AsynchReplyStatusEnum;  
  15. import com.cognos.developer.schemas.bibus._3.AsynchSecondaryRequest;  
  16. import com.cognos.developer.schemas.bibus._3.BaseClass;  
  17. import com.cognos.developer.schemas.bibus._3.ContentManagerService_Port;  
  18. import com.cognos.developer.schemas.bibus._3.Graphic;  
  19. import com.cognos.developer.schemas.bibus._3.Option;  
  20. import com.cognos.developer.schemas.bibus._3.Output;  
  21. import com.cognos.developer.schemas.bibus._3.OutputEncapsulationEnum;  
  22. import com.cognos.developer.schemas.bibus._3.ParameterValue;  
  23. import com.cognos.developer.schemas.bibus._3.PropEnum;  
  24. import com.cognos.developer.schemas.bibus._3.QueryOptions;  
  25. import com.cognos.developer.schemas.bibus._3.RunOptionBoolean;  
  26. import com.cognos.developer.schemas.bibus._3.RunOptionEnum;  
  27. import com.cognos.developer.schemas.bibus._3.RunOptionLanguageArray;  
  28. import com.cognos.developer.schemas.bibus._3.RunOptionOutputEncapsulation;  
  29. import com.cognos.developer.schemas.bibus._3.RunOptionStringArray;  
  30. import com.cognos.developer.schemas.bibus._3.SearchPathMultipleObject;  
  31. import com.cognos.developer.schemas.bibus._3.SearchPathSingleObject;  
  32. import com.cognos.developer.schemas.bibus._3.Sort;  
  33.   
  34. /** 
  35. * 运行报表 
  36.  
  37. * @author  
  38. */  
  39. public class RunReportAL  {  
  40. /** 
  41.   * 设置报表参数AL 
  42.   */  
  43. private SetReportParameterAL setReportParameterAL;  
  44. public SetReportParameterAL getSetReportParameterAL() {  
  45.   return setReportParameterAL;  
  46. }  
  47. public void setSetReportParameterAL(  
  48.    SetReportParameterAL setReportParameterAL) {  
  49.   this.setReportParameterAL = setReportParameterAL;  
  50. }  
  51. /** 
  52.   * 运行报表 
  53.   *  
  54.   * @param connect 
  55.   *            报表服务器连接AL 
  56.   * @param reportSearchPath 
  57.   *            报表搜索路径 
  58.   * @param reportType 
  59.   *            报表类型:1:XLS 2:HTML 
  60.   * @param parameters 
  61.   *            报表参数helper 
  62.   * @return 
  63.   * @throws IOException 
  64.   */  
  65. public String run(CognosServerConnectAL connect, String reportSearchPath,  
  66.    int reportType, RepParameterHelper parameters) throws IOException {  
  67.   String output = null;  
  68.   try{  
  69.   if ((connect != null) && (reportSearchPath != null)) {  
  70.    /* 通过reportSearchPath【报表存放路劲】获得报表对象 
  71.    */  
  72.    BaseClass[] reportDomain = getRoportModel(reportSearchPath, connect  
  73.      .getCMService());  
  74.    if (reportDomain.length > 0) {  
  75.     ParameterValue[] emptyParameterValues = new ParameterValue[] {}; // 空参  
  76.     ParameterValue[] reportParameterValues = null;//  
  77.     String compagers = null;  
  78.     String savePath = null;  
  79.     String partOfFileoutPutName = null;//生成报表静态文件名称  
  80.     if (parameters != null) {  
  81.      // 设置报表参数  
  82.      reportParameterValues = setReportParameterAL  
  83.        .setReportParameters(parameters);  
  84.      compagers = parameters.getCompagers();  
  85.      savePath = parameters.getSavePath();  
  86.      /* 
  87.      文件名组成:报表ID+报表英文名称 
  88.      */  
  89.      partOfFileoutPutName = parameters.getReportPutInfoDomain()  
  90.        .getReportID()  
  91.        + parameters.getReportPutInfoDomain()  
  92.          .getReportEnglishName();  
  93.     }  
  94.     //如果传入的参数数组为空则数组为null  
  95.     if ((reportParameterValues == null)  
  96.       || (reportParameterValues.length <= 0)) {  
  97.      //emptyParameterValues  上面定义的一组空参(执行报表必须要有参数数组,数组内容可为空)  
  98.      reportParameterValues = emptyParameterValues;  
  99.     }  
  100.     //执行报表  
  101.     /* 
  102.      reportSearchPath:报表存放地址 
  103.      connect:连接对象 
  104.      reportType:报表文件类型(html,excel) 
  105.      reportParameterValues:参数数组 
  106.     */  
  107.     output = executeReport(reportSearchPath, connect, reportType,  
  108.       reportParameterValues, compagers, savePath,  
  109.       partOfFileoutPutName);  
  110.    }  
  111.   }  
  112.   return output;  
  113.   }catch (Exception e) {  
  114.    e.printStackTrace();  
  115.    return output;  
  116.   }  
  117. }  
  118. /** 
  119.   * 执行报表 
  120.   *  
  121.   * @param reportSearchPath 
  122.   *            报表搜索路径 
  123.   * @param connect 
  124.   *            报表服务器连接AL 
  125.   * @param reportType 
  126.   *            报表类型:1:XLS 2:HTML 
  127.   * @param paramValueArray 
  128.   *            报表参数 
  129.   * @param compagers 
  130.   * @param savePath 
  131.   *            保存路径 
  132.   * @param partOfFileoutPutName 
  133.   *            报表英文名称 
  134.   * @return 
  135.   * @throws IOException 
  136.   */  
  137. private String executeReport(String reportSearchPath,  
  138.    CognosServerConnectAL connect, int reportType,  
  139.    ParameterValue[] paramValueArray, String compagers,  
  140.    String savePath, String partOfFileoutPutName) throws IOException {  
  141.   try{  
  142.   Option[] execReportRunOptions = getRunOptions(reportType); // 报表运行配置  
  143.   AsynchReply rsr = null// 报表内容  
  144.   // main  
  145.   rsr = connect.getReportService().run(  
  146.     new SearchPathSingleObject(reportSearchPath), paramValueArray,  
  147.     execReportRunOptions);  
  148.   // 输出对象没有立刻获得时,执行等待,直到获得输出对象为止  
  149.   if (!rsr.getStatus().equals(AsynchReplyStatusEnum.complete)  
  150.     && !rsr.getStatus().equals(  
  151.       AsynchReplyStatusEnum.conversationComplete)) {  
  152.    while (!rsr.getStatus().equals(AsynchReplyStatusEnum.complete)  
  153.      && !rsr.getStatus().equals(  
  154.        AsynchReplyStatusEnum.conversationComplete)) {  
  155.     // 执行等待之前,进行确认  
  156.     if (!hasSecondaryRequest(rsr, "wait")) {  
  157.      return null;  
  158.     }  
  159.     rsr = connect.getReportService().wait(rsr.getPrimaryRequest(),  
  160.       new ParameterValue[] {}, new Option[] {});  
  161.    }  
  162.    // 确保获得输出对象前,输出对象已经完成提交  
  163.    if (outputIsReady(rsr)) {  
  164.     rsr = connect.getReportService().getOutput(  
  165.       rsr.getPrimaryRequest(), new ParameterValue[] {},  
  166.       new Option[] {});  
  167.    } else {  
  168.     return null;  
  169.    }  
  170.   }  
  171.   // 导出  
  172.   return textOrBinaryOutput(connect, rsr, partOfFileoutPutName,  
  173.     reportType, compagers, savePath);  
  174.   }catch (Exception e) {  
  175.    e.printStackTrace();  
  176.    return null;  
  177.   }  
  178. }  
  179. /** 
  180.   * 报表运行选项设置 
  181.   *  
  182.   * @param reportType 
  183.   * @return 
  184.   */  
  185. private Option[] getRunOptions(int reportType) {  
  186.   //定义运行报表选项数组  
  187.   Option[] execReportRunOptions = new Option[7];  
  188.   //指定是否应该将报表输出保存到内容库中  
  189.   RunOptionBoolean saveOutputRunOption = new RunOptionBoolean();  
  190.   //导出文件格式  
  191.   RunOptionStringArray outputFormat = new RunOptionStringArray();  
  192.   //是否跳过提示页面  
  193.   RunOptionBoolean promptFlag = new RunOptionBoolean();  
  194.   //指定存储输出的位置  
  195.   RunOptionOutputEncapsulation outputEncapsulation = new RunOptionOutputEncapsulation();  
  196.   saveOutputRunOption.setName(RunOptionEnum.saveOutput);  
  197.   saveOutputRunOption.setValue(false);//true为保存,false反之  
  198.   outputFormat.setName(RunOptionEnum.outputFormat);  
  199.   String[] reportFormat = null;  
  200.   reportFormat = setFormatByType(reportType);  
  201.   outputFormat.setValue(reportFormat);//setValue值应为String类型的数组  
  202.   promptFlag.setName(RunOptionEnum.prompt);  
  203.   promptFlag.setValue(false);//false:跳过提示页面运行报表  
  204.   //开始之前等待报表完成的初始时间,以秒为单位,默认值为 7 秒  
  205.   AsynchOptionInt primaryWaitThreshold = new AsynchOptionInt();  
  206.   primaryWaitThreshold.setName(AsynchOptionEnum.primaryWaitThreshold);  
  207.   primaryWaitThreshold.setValue(20);  
  208.     
  209.   //在匿名会话的过程中,等待状态检查的时间,以秒为单位  
  210.   AsynchOptionInt secondaryWait = new AsynchOptionInt();  
  211.   secondaryWait.setName(AsynchOptionEnum.secondaryWaitThreshold);  
  212.   secondaryWait.setValue(65);  
  213.     
  214.   //设置语言本地化  
  215.   RunOptionLanguageArray outputLocale = new RunOptionLanguageArray();  
  216.   outputLocale.setName(RunOptionEnum.outputLocale);  
  217.   outputLocale.setValue(new String[]{"zh"});  
  218.     
  219.   outputEncapsulation.setName(RunOptionEnum.outputEncapsulation);  
  220.   /* 
  221.   枚举none:输出不存入临时内容库对象中 
  222.       (如果报表输出的内容较大如果保存至临时内容库会造成内存溢出 
  223.   */  
  224.   outputEncapsulation.setValue(OutputEncapsulationEnum.none);  
  225.   //添加各个运行选项  
  226.   execReportRunOptions[0] = saveOutputRunOption;  
  227.   execReportRunOptions[1] = outputFormat;  
  228.   execReportRunOptions[2] = promptFlag;  
  229.   execReportRunOptions[3] = primaryWaitThreshold;  
  230.   execReportRunOptions[4] = outputLocale;  
  231.   execReportRunOptions[5] = outputEncapsulation;  
  232.   execReportRunOptions[6] = secondaryWait;  
  233.   //返回 选项 数组  
  234.   return execReportRunOptions;  
  235. }  
  236. /** 
  237.   * 设置导出格式 
  238.   *  
  239.   * @param fileType 
  240.   *            1:XLS 2:HTML 
  241.   * @return 
  242.   */  
  243. private String[] setFormatByType(int fileType) {  
  244.   switch (fileType) {  
  245.   case ParameterUtil.XLS:  
  246.    return new String[] { "singleXLS" };//excel格式  
  247.   case ParameterUtil.HTML:  
  248.    return new String[] { "HTML" };//html格式  
  249.   default:  
  250.    return new String[] { "singleXLS" };  
  251.   }  
  252.   /* 
  253.    格式还可以为CSV , HTMLFragment, MHT, PDF, singleXLS, XHTML, XLS, XLWA, XML 
  254.   */  
  255. }  
  256. /** 
  257.   * 检查响应是否进入了secondaryRequest状态 
  258.   *  
  259.   * @param response 
  260.   *            报表内容 
  261.   * @param secondaryRequest 
  262.   *            wait 
  263.   * @return 
  264.   */  
  265. private boolean hasSecondaryRequest(AsynchReply response,  
  266.    String secondaryRequest) {  
  267.   AsynchSecondaryRequest[] secondaryRequests = response  
  268.     .getSecondaryRequests();  
  269.   for (int i = 0; i < secondaryRequests.length; i++) {  
  270.    if (secondaryRequests.getName().compareTo(secondaryRequest) == 0) {  
  271.     return true;  
  272.    }  
  273.   }  
  274.   return false;  
  275. }  
  276. /** 
  277.   * 输出对象是否已经完全获得 
  278.   *  
  279.   * @param response 
  280.   *            报表内容 
  281.   * @return 
  282.   */  
  283. private boolean outputIsReady(AsynchReply response) {  
  284.   for (int i = 0; i < response.getDetails().length; i++) {  
  285.    if ((response.getDetails() instanceof AsynchDetailReportStatus)  
  286.      && (((AsynchDetailReportStatus) response.getDetails())  
  287.        .getStatus() == AsynchDetailReportStatusEnum.responseReady)  
  288.      && (hasSecondaryRequest(response, "getOutput"))) {  
  289.     return true;  
  290.    }  
  291.   }  
  292.   return false;  
  293. }  
  294. /** 
  295.   * 生成不同格式的报表 
  296.   *  
  297.   * @param connect 
  298.   *            报表服务器连接AL 
  299.   * @param rsr 
  300.   *            报表内容 
  301.   * @param partOfFileoutPutName 
  302.   *            报表英文名称 
  303.   * @param reportType 
  304.   *            报表类型 
  305.   * @param compagers 
  306.   * @param savePath 
  307.   *            保存路径 
  308.   * @return 
  309.   * @throws IOException 
  310.   */  
  311. private String textOrBinaryOutput(CognosServerConnectAL connect,  
  312.    AsynchReply rsr, String partOfFileoutPutName, int reportType,  
  313.    String compagers, String savePath) throws IOException {  
  314.   String textOutput = null;  
  315.   if (reportType == ParameterUtil.XLS) {  
  316.    textOutput = saveBinaryOutput(connect, rsr, partOfFileoutPutName,  
  317.      reportType, compagers, savePath);  
  318.   }  
  319.   if (reportType == ParameterUtil.HTML) {  
  320.    textOutput = getOutputPage(connect, rsr, partOfFileoutPutName,  
  321.      reportType, compagers, savePath);  
  322.   }  
  323.   return textOutput;  
  324. }  
  325. /** 
  326.   * 导出xls格式的报表,并且写入文件 
  327.   *  
  328.   * @param connection 
  329.   * @param response 
  330.   * @param partOfFileoutPutName 
  331.   * @param reportType 
  332.   * @param compagers 
  333.   * @param savePath 
  334.   * @return 
  335.   * @throws IOException 
  336.   */  
  337. private String saveBinaryOutput(CognosServerConnectAL connection,  
  338.    AsynchReply response, String partOfFileoutPutName, int reportType,  
  339.    String compagers, String savePath) throws IOException {  
  340.   String fileSaveUrl = null;  
  341.   byte[] binaryOutput = null;  
  342.   AsynchDetailReportOutput reportOutput = null;  
  343.   for (int i = 0; i < response.getDetails().length; i++) {  
  344.    if (response.getDetails() instanceof AsynchDetailReportOutput) {  
  345.     reportOutput = (AsynchDetailReportOutput) response.getDetails();  
  346.     break;  
  347.    }  
  348.   }  
  349.   binaryOutput = (reportOutput.getOutputPages()[0]).getBytes("UTF-8");  
  350.   if (binaryOutput == null) {  
  351.    return null;  
  352.   }  
  353.   //创建文件  
  354.   createNewFile(savePath);  
  355.   File oFile = new File(setFilenameByType(savePath, partOfFileoutPutName,  
  356.     reportType, compagers));  
  357.   FileOutputStream fos = new FileOutputStream(oFile);  
  358.   fos.write(binaryOutput);  
  359.   fos.flush();  
  360.   fos.close();  
  361.   //获取绝对路劲  
  362.   fileSaveUrl = oFile.getAbsolutePath();  
  363.   return fileSaveUrl;  
  364. }  
  365. /** 
  366.   * 导出html格式的报表,并且写入文件 
  367.   *  
  368.   * @param connect 
  369.   * @param response 
  370.   * @param partOfFileoutPutName 
  371.   * @param reportType 
  372.   * @param compagers 
  373.   * @param savePath 
  374.   * @return 
  375.   * @throws IOException 
  376.   */  
  377. private String getOutputPage(CognosServerConnectAL connect,  
  378.    AsynchReply response, String partOfFileoutPutName, int reportType,  
  379.    String compagers, String savePath) throws IOException {  
  380.   String fileSaveUrl = null;  
  381.   String textOutput = "";  
  382.   AsynchDetailReportOutput reportOutput = null;  
  383.   for (int i = 0; i < response.getDetails().length; i++) {  
  384.    if (response.getDetails() instanceof AsynchDetailReportOutput) {  
  385.     reportOutput = (AsynchDetailReportOutput) response.getDetails();  
  386.     break;  
  387.    }  
  388.   }  
  389.   if (reportOutput == null) {  
  390.    return null;  
  391.   }  
  392.   if (reportOutput.getOutputObjects().length > 0) {  
  393.    textOutput = replaceLocalGraphicsInOutput(connect, reportOutput,  
  394.      savePath, partOfFileoutPutName, compagers);  
  395.   } else {  
  396.    textOutput = reportOutput.getOutputPages()[0].toString();  
  397.   }  
  398.   createNewFile(savePath);  
  399.   File oFile = new File(setFilenameByType(savePath, partOfFileoutPutName,  
  400.     reportType, compagers));  
  401.   FileOutputStream fos = new FileOutputStream(oFile);  
  402.   fos.write(textOutput.getBytes("UTF-8"));  
  403.   fos.flush();  
  404.   fos.close();  
  405.   fileSaveUrl = oFile.getAbsolutePath();  
  406.   return fileSaveUrl;  
  407. }  
  408. /** 
  409.   * 包含图表的报表特殊处理 
  410.   * 这一部分不是很熟悉 
  411.   * @param connect 
  412.   * @param reportOutput 
  413.   * @param savePath 
  414.   * @param partOfFileoutPutName 
  415.   * @param compages 
  416.   * @return 
  417.   * @throws IOException 
  418.   */  
  419. private String replaceLocalGraphicsInOutput(CognosServerConnectAL connect,  
  420.    AsynchDetailReportOutput reportOutput, String savePath,  
  421.    String partOfFileoutPutName, String compages) throws IOException {  
  422.   BaseClass[] bcGraphic;  
  423.   SearchPathMultipleObject graphicSearchPath = new SearchPathMultipleObject();  
  424.   graphicSearchPath.setValue(reportOutput.getOutputObjects()[0]  
  425.     .getSearchPath().getValue());  
  426.   bcGraphic = connect.getCMService().query(graphicSearchPath,  
  427.     new PropEnum[] { PropEnum.searchPath }, new Sort[] {},  
  428.     new QueryOptions());  
  429.   Output out = null;  
  430.   if ((bcGraphic.length > 0) && (bcGraphic[0instanceof Output)) {  
  431.    SearchPathMultipleObject outSearchPath = new SearchPathMultipleObject();  
  432.    out = (Output) bcGraphic[0];  
  433.    outSearchPath.setValue(out.getSearchPath().getValue() + "/graphic");  
  434.    BaseClass[] g = connect.getCMService().query(  
  435.      outSearchPath,  
  436.      new PropEnum[] { PropEnum.searchPath, PropEnum.data,  
  437.        PropEnum.dataType }, new Sort[] {},  
  438.      new QueryOptions());  
  439.    StringBuffer finalHtml = new StringBuffer();  
  440.    String[] pages = reportOutput.getOutputPages();  
  441.    String html = pages[0].toString();  
  442.    String start = null;  
  443.    String end = null;  
  444.    String graphicFile = null;  
  445.    // 替换html文件中img标签的属性  
  446.    for (int i = 0; i < g.length; i++) {  
  447.     String pictrueName = partOfFileoutPutName + "_" + compages + i  
  448.       + ".png";  
  449.     graphicFile = savePath + "/" + pictrueName;  
  450.     // 图表保存本地  
  451.     File gFile = new File(graphicFile);  
  452.     FileOutputStream fos = new FileOutputStream(gFile);  
  453.     fos.write(((Graphic) g).getData().getValue());  
  454.     fos.flush();  
  455.     fos.close();  
  456.     int index = 0;  
  457.     index = html.indexOf("<img"0);  
  458.     start = html.substring(0, index);  
  459.     end = html.substring(html.indexOf(">", index) + 1);  
  460.     finalHtml.append(start + "<img src='" + pictrueName + "'>");  
  461.     html = end;  
  462.    }  
  463.    finalHtml.append(html);  
  464.    return finalHtml.toString();  
  465.   }  
  466.   return null;  
  467. }  
  468. /** 
  469.   * 根据格式设置文件名 
  470.   *  
  471.   * @param filePath文件路劲 
  472.   * @param reportName文件名称 
  473.   * @param fileType文件类型 
  474.   * @return 
  475.   */  
  476. private String setFilenameByType(String filePath,  
  477.    String partOfFileoutPutName, int fileType, String compagers) {  
  478.   switch (fileType) {  
  479.   case ParameterUtil.XLS:  
  480.    return filePath + System.getProperty("file.separator")  
  481.      + partOfFileoutPutName + "_" + compagers + ".xls";  
  482.   case ParameterUtil.HTML:  
  483.    return filePath + System.getProperty("file.separator")  
  484.      + partOfFileoutPutName + "_" + compagers + ".html";  
  485.   default:  
  486.    return null;  
  487.   }  
  488. }  
  489. /** 
  490.   * 创建目录 
  491.   *  
  492.   * @param savePath 
  493.   */  
  494. private void createNewFile(String savePath) {  
  495.   File file = new File(savePath);  
  496.   if (!file.exists()) {  
  497.    file.mkdirs();  
  498.   }  
  499. }  
  500. /** 
  501.   * 内容库报表对象查询 
  502.   *  
  503.   * @param searchPath 
  504.   * @return 
  505.   * @throws RemoteException 
  506.   */  
  507. private BaseClass[] getRoportModel(String searchPath,  
  508.    ContentManagerService_Port cmService) throws RemoteException {  
  509.   PropEnum[] props = { PropEnum.searchPath };  
  510.   BaseClass[] bc = null;  
  511.   SearchPathMultipleObject spMulti = new SearchPathMultipleObject();  
  512.   spMulti.setValue(searchPath);  
  513.   bc = cmService.query(spMulti, props, new Sort[] {}, new QueryOptions());  
  514.   return bc;  
  515. }  
  516. }  

[java] view plaincopy
  1. /* 
  2. *  根据报告日期判定报告频率 
  3. */  
  4. import java.util.Calendar;  
  5. import java.util.Date;  
  6. import java.util.GregorianCalendar;  
  7. /** 
  8. * 工作日,周末、月末、季末、半年末,年末的判断 
  9.  
  10.  
  11. */  
  12. public class MeasureReportTypeByRepDateAL {  
  13. private ITjFactDealday tjFactDealday;  
  14. /** 
  15.   *  
  16.   * @return 
  17.   */  
  18. public ITjFactDealday getTjFactDealday() {  
  19.   return tjFactDealday;  
  20. }  
  21. /** 
  22.   *  
  23.   * @param tjFactDealday 
  24.   */  
  25. public void setTjFactDealday(ITjFactDealday tjFactDealday) {  
  26.   this.tjFactDealday = tjFactDealday;  
  27. }  
  28. /** 
  29.   * 判断某日期是否为其所在周、月、季、半年、年的最后一个工作日,即周末,月末,季末,半年末,年末 
  30.   *  
  31.   * @param reportDate 
  32.   *            报告日期 
  33.   * @param freq 
  34.   *            频率:2:周,3:月,4:季,5:半年,6:年 
  35.   * @return 
  36.   */  
  37. public boolean isEndDayByFreq(Date reportDate, int freq) {  
  38.   boolean isEndDay = false;  
  39.   Calendar reportDay = dateToCalendar(reportDate);  
  40.   Calendar nextDay_1 = new GregorianCalendar(); // nextDay_1:reportDay的下一天  
  41.   nextDay_1.set(Calendar.YEAR, reportDay.get(Calendar.YEAR));//下一天 所处的年份  
  42.   nextDay_1.set(Calendar.DAY_OF_YEAR,reportDay.get(Calendar.DAY_OF_YEAR) + 1);  
  43.   boolean nextDayIsWorkDay = isWorkDay(nextDay_1.getTime());//下一天是否是工作日  
  44.   Date nextDay_2 = null// nextDay_2:reportDay的下一个工作日  
  45.   if (!nextDayIsWorkDay) {//如果下一天非工作日  
  46.    nextDay_2 = getAfterWorkDay(nextDay_1.getTime());//获取工作日 日期  
  47.   } else {//下一天为工作日  
  48.    nextDay_2 = nextDay_1.getTime();//获取工作日 日期  
  49.   }  
  50.   Calendar nextWorkDay = dateToCalendar(nextDay_2);//转换为Calendar类型  
  51.   switch (freq) {  
  52.   // 判断当前日期是否为该日期所在自然周的最后一个工作日  
  53.   case ParameterUtil.WEEK:// 周的常量值:2  
  54.    //如果报告日期所属当年的周与下一个工作日所属当年的周不相等  
  55.    if (reportDay.get(Calendar.WEEK_OF_YEAR) != nextWorkDay.get(Calendar.WEEK_OF_YEAR)) {  
  56.     isEndDay = true;//则报告日期为本周最后一个工作日  
  57.    }  
  58.    ;  
  59.    break;  
  60.   // 判断当前日期是否为该日期所在月的最后一个工作日  
  61.   case ParameterUtil.MONTH://月的常量值:3  
  62.    if (reportDay.get(Calendar.MONTH) != nextWorkDay.get(Calendar.MONTH)) {  
  63.     isEndDay = true;  
  64.    }  
  65.    ;  
  66.    break;  
  67.   // 判断当前日期是否为该日期所在季度的最后一个工作日  
  68.   case ParameterUtil.SEASON://季度的常量值:4  
  69.    if ((reportDay.get(Calendar.MONTH) == Calendar.MARCH)  
  70.      && (nextWorkDay.get(Calendar.MONTH) != Calendar.MARCH)) {  
  71.     isEndDay = true;  
  72.    } else if ((reportDay.get(Calendar.MONTH) == Calendar.JUNE)  
  73.      && (nextWorkDay.get(Calendar.MONTH) != Calendar.JUNE)) {  
  74.     isEndDay = true;  
  75.    } else if ((reportDay.get(Calendar.MONTH) == Calendar.SEPTEMBER)  
  76.      && (nextWorkDay.get(Calendar.MONTH) != Calendar.SEPTEMBER)) {  
  77.     isEndDay = true;  
  78.    } else if ((reportDay.get(Calendar.MONTH) == Calendar.DECEMBER)  
  79.      && (nextWorkDay.get(Calendar.MONTH) != Calendar.DECEMBER)) {  
  80.     isEndDay = true;  
  81.    }  
  82.    ;  
  83.    break;  
  84.   // 判断当前日期是否为该日期所在半年的最后一个工作日  
  85.   case ParameterUtil.HALF_YEAR://半年的常量值:5  
  86.    if ((reportDay.get(Calendar.MONTH) == Calendar.JUNE)  
  87.      && (nextWorkDay.get(Calendar.MONTH) != Calendar.JUNE)) {  
  88.     isEndDay = true;  
  89.    } else if ((reportDay.get(Calendar.MONTH) == Calendar.DECEMBER)  
  90.      && (nextWorkDay.get(Calendar.MONTH) != Calendar.DECEMBER)) {  
  91.     isEndDay = true;  
  92.    }  
  93.    ;  
  94.    break;  
  95.   // 判断当前日期是否为该日期所在年份的最后一个工作日  
  96.   case ParameterUtil.YEAR://年的常量值:6  
  97.    if (reportDay.get(Calendar.YEAR) != nextWorkDay.get(Calendar.YEAR)) {  
  98.     isEndDay = true;  
  99.    }  
  100.    ;  
  101.    break;  
  102.   default:  
  103.   }  
  104.   return isEndDay;  
  105. }  
  106. /** 
  107.   * 获取reportDate的下一个工作日,注:reportDate必须为非工作日 
  108.   * 此方法根据自己的需求获取工作日 
  109.   * @param reportDate 
  110.   * @return 
  111.   */  
  112. public Date getAfterWorkDay(Date reportDate) {  
  113.   return tjFactDealday.afterDay(reportDate);  
  114. }  
  115. /** 
  116.   * Date类型数据转换为Calendar类型数据 
  117.   *  
  118.   * @param date 
  119.   * @return 
  120.   */  
  121. private Calendar dateToCalendar(Date date) {  
  122.   int year = DamsDateUtils.getYear(date);  
  123.   int month = DamsDateUtils.getMonth(date);  
  124.   int day = DamsDateUtils.getDay(date);  
  125.   Calendar calendar = new GregorianCalendar();  
  126.   calendar.set(Calendar.YEAR, year);  
  127.   calendar.set(Calendar.MONTH, month); // Calendar的月份0-11代表1月到12月  
  128.   calendar.set(Calendar.DAY_OF_MONTH, day);  
  129.   return calendar;  
  130. }