123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375 |
- /**
- Licensed Materials - Property of IBM
- IBM Cognos Products: DOCS
- (C) Copyright IBM Corp. 2005, 2008
- US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with
- IBM Corp.
- */
- /**
- * SaveReport.java
- *
- * Copyright (C) 2008 Cognos ULC, an IBM Company. All rights reserved.
- * Cognos (R) is a trademark of Cognos ULC, (formerly Cognos Incorporated).
- *
- * Description: This file contains methods for executing different types of
- * report
- *
- */
- import javax.swing.JFrame;
- import javax.swing.JOptionPane;
- import com.cognos.developer.schemas.bibus._3.AsynchReply;
- import com.cognos.developer.schemas.bibus._3.AsynchReplyStatusEnum;
- import com.cognos.developer.schemas.bibus._3.AsynchRequest;
- import com.cognos.developer.schemas.bibus._3.AsynchSecondaryRequest;
- import com.cognos.developer.schemas.bibus._3.BaseParameter;
- import com.cognos.developer.schemas.bibus._3.MultilingualToken;
- import com.cognos.developer.schemas.bibus._3.Option;
- import com.cognos.developer.schemas.bibus._3.ParameterValue;
- import com.cognos.developer.schemas.bibus._3.PropEnum;
- import com.cognos.developer.schemas.bibus._3.ReportSaveAsEnum;
- import com.cognos.developer.schemas.bibus._3.RunOptionBoolean;
- import com.cognos.developer.schemas.bibus._3.RunOptionEnum;
- import com.cognos.developer.schemas.bibus._3.RunOptionSaveAs;
- import com.cognos.developer.schemas.bibus._3.RunOptionStringArray;
- import com.cognos.developer.schemas.bibus._3.SearchPathSingleObject;
- public class SaveReport
- {
- /**
- * saveReport will execute a query/report and save either the report
- * or the report output back in the content store, as specified by the
- * saveAs parameter.
- *
- * @param connect
- * @param report
- * @param saveAs
- *
- * @return
- */
- public String saveReport(CRNConnect connect, BaseClassWrapper report, boolean saveAs)
- {
- String output = new String();
- String reportName = report.getBaseClassObject().getSearchPath().getValue();
- if ((connect != null) && (report != null))
- {
- PropEnum props[] =
- new PropEnum[] { PropEnum.searchPath, PropEnum.defaultName };
- boolean success = false;
- // Get the list of parameters used by the report, including
- // optional parameters.
- ParameterValue reportParameters[] = new ParameterValue[] {};
- ReportParameters repParms = new ReportParameters();
- try
- {
- BaseParameter[] prm = repParms.getReportParameters(report, connect);
- if (prm != null && prm.length > 0)
- {
- reportParameters =
- ReportParameters.setReportParameters(prm);
- }
- }
- catch (Exception e)
- {
- System.out.println("An error occurred in the saveReport method.");
- e.printStackTrace();
- return "An error occurred in the saveReport method.";
- }
- output += "Running " + reportName + "\n";
- success =
- executeAndSave(
- reportName,
- connect,
- saveAs,
- reportParameters);
- if (!success)
- {
- output =
- output.concat(
- "Error occurred in saveReport()"
- + " - trying to execute report\n");
- }
- }
- else
- {
- output =
- output.concat("Invalid parameter(s) passed to saveReport.\n");
- }
- output += "Completed successfully.";
- return output;
- }
- /**
- * boolean executeAndSave( String reportName,
- * String reportPath,
- * boolean saveAs,
- * int reportType)
- *
- * This method executes a report and saves the output
- * or the report spec, as indicated by the saveAs flag
- *
- * @param reportName
- * Specifies search path of the report.
- * @param connect
- * Connection to Server
- * @param saveAs flag to indicate whether the report spec
- * should be saved under a new name, or
- * the output.
- * @param paramValues array of parameter values
- *
- * @return if the function succeeded
- *
- */
- public boolean executeAndSave(
- String reportName,
- CRNConnect connect,
- boolean saveAs,
- ParameterValue paramValues[])
- {
- Option executeRunOptions[] = new Option[3];
- Option saveAsRunOptions[] = new Option[1];
- RunOptionBoolean roSaveOutput = new RunOptionBoolean();
- RunOptionStringArray roOutputFormat = new RunOptionStringArray();
- RunOptionBoolean roPrompt = new RunOptionBoolean();
- AsynchReply asynchReply = null;
- AsynchRequest asynchRequest = null;
- String[] reportFormat = null;
- // We may want to save this output, but not by using
- // runOptions. That would be for another sample.
- roSaveOutput.setName(RunOptionEnum.saveOutput);
- roSaveOutput.setValue(false);
- //What format do we want the report in? HTML.
- reportFormat = new String[] { "HTML" };
- roOutputFormat.setName(RunOptionEnum.outputFormat);
- roOutputFormat.setValue(reportFormat);
- //Set the report not to prompt as we pass the parameter if any
- roPrompt.setName(RunOptionEnum.prompt);
- roPrompt.setValue(false);
- // Fill the array with the run options.
- executeRunOptions[0] = roSaveOutput;
- executeRunOptions[1] = roOutputFormat;
- executeRunOptions[2] = roPrompt;
- try
- {
- // Get the initial response
- if (reportName.startsWith("/content"))
- {
- asynchReply =
- connect.getReportService().run(
- new SearchPathSingleObject(reportName),
- paramValues,
- executeRunOptions);
- // If response is not immediately complete, call wait until complete
- if (!asynchReply.getStatus().equals(AsynchReplyStatusEnum.complete))
- {
- while (!asynchReply.getStatus().equals(AsynchReplyStatusEnum.complete))
- {
- //before calling wait, double check that it is okay
- if (hasSecondaryRequest(asynchReply, "wait"))
- {
- asynchReply =
- connect.getReportService().wait(
- asynchReply.getPrimaryRequest(),
- new ParameterValue[] {},
- new Option[] {});
- }
- else
- {
- //wait method not available as expected
- JFrame message = new JFrame();
- JOptionPane.showMessageDialog(
- message,
- "Error: Wait method not available as expected");
- System.out.println("\n\nError: Wait method not available as expected");
- return false;
- }
- }
- }
- asynchRequest = asynchReply.getPrimaryRequest();
- }
- else
- {
- JFrame message = new JFrame();
- JOptionPane.showMessageDialog(
- message,
- "Please supply a search path.");
- System.out.println("\n\nPlease supply a search path.");
- }
- }
- catch (java.rmi.RemoteException remoteEx)
- {
- System.out.println("Caught Remote Exception:\n");
- remoteEx.printStackTrace();
- }
- if (asynchRequest != null)
- {
- if (saveAs)
- {
- String newReportName = "";
- int position = reportName.indexOf("/report");
- if (position >= 0)
- {
- newReportName = "NewReportJ";
- }
- else
- {
- position = reportName.indexOf("/query");
- newReportName = "NewQueryJ";
- }
- String packagePath = reportName.substring(0, position);
- MultilingualToken nameTokens[] = new MultilingualToken[2];
- nameTokens[0] = new MultilingualToken();
- nameTokens[0].setLocale("en-us");
- nameTokens[0].setValue(newReportName);
- RunOptionSaveAs roSaveAs = new RunOptionSaveAs();
- roSaveAs.setName(RunOptionEnum.saveAs);
- roSaveAs.setObjectClass(ReportSaveAsEnum.reportView);
- roSaveAs.setObjectName(nameTokens);
- roSaveAs.setParentSearchPath(packagePath);
- saveAsRunOptions[0] = roSaveAs;
- try
- {
- //call deliver with saveAs option
- asynchReply = connect.getReportService().deliver(asynchRequest, new ParameterValue[] {}, saveAsRunOptions);
- // If response is not immediately complete, call wait until complete
- if ((!asynchReply.getStatus().equals(AsynchReplyStatusEnum.complete)) && (!asynchReply.getStatus().equals(AsynchReplyStatusEnum.conversationComplete)))
- {
- while ((!asynchReply.getStatus().equals(AsynchReplyStatusEnum.complete))
- && (!asynchReply.getStatus().equals(AsynchReplyStatusEnum.conversationComplete)))
- {
- //before calling wait, double check that it is okay
- if (hasSecondaryRequest(asynchReply, "wait"))
- {
- asynchReply =
- connect.getReportService().wait(
- asynchReply.getPrimaryRequest(),
- new ParameterValue[] {},
- new Option[] {});
- }
- else
- {
- //wait method not available as expected
- JFrame message = new JFrame();
- JOptionPane.showMessageDialog(
- message,
- "Error: Wait method not available as expected");
- System.out.println("\n\nError: Wait method not available as expected");
- return false;
- }
- }
- }
- return true;
- }
- catch (java.rmi.RemoteException remoteEx)
- {
- System.out.println("Caught Remote Exception:\n");
- remoteEx.printStackTrace();
- }
- }
- else
- {
- Option saveRunOptions[] = new Option[1];
- RunOptionBoolean bSaveOutput = new RunOptionBoolean();
- bSaveOutput.setName(RunOptionEnum.saveOutput);
- bSaveOutput.setValue(true);
- saveRunOptions[0] = bSaveOutput;
- try
- {
- //call deliver with option to save
- asynchReply = connect.getReportService().deliver(asynchRequest, new ParameterValue[] {}, saveRunOptions);
-
- // If response is not immediately complete, call wait until complete
- if ((!asynchReply.getStatus().equals(AsynchReplyStatusEnum.complete)) && (!asynchReply.getStatus().equals(AsynchReplyStatusEnum.conversationComplete)))
- {
- while ((!asynchReply.getStatus().equals(AsynchReplyStatusEnum.complete))
- && (!asynchReply.getStatus().equals(AsynchReplyStatusEnum.conversationComplete)))
- {
- //before calling wait, double check that it is okay
- if (hasSecondaryRequest(asynchReply, "wait"))
- {
- asynchReply =
- connect.getReportService().wait(
- asynchReply.getPrimaryRequest(),
- new ParameterValue[] {},
- new Option[] {});
- }
- else
- {
- //wait method not available as expected
- JFrame message = new JFrame();
- JOptionPane.showMessageDialog(
- message,
- "Error: Wait method not available as expected");
- System.out.println("\n\nError: Wait method not available as expected");
- return false;
- }
- }
- }
- return true;
- }
- catch (java.rmi.RemoteException remoteEx)
- {
- System.out.println("Caught Remote Exception:\n");
- remoteEx.printStackTrace();
- }
- }
- }
- else
- {
- System.out.println("execute() failed to return a valid report");
- }
- return false;
- }
- public boolean hasSecondaryRequest(
- AsynchReply response,
- String secondaryRequest)
- {
- AsynchSecondaryRequest[] secondaryRequests =
- response.getSecondaryRequests();
- for (int i = 0; i < secondaryRequests.length; i++)
- {
- if (secondaryRequests[i].getName().compareTo(secondaryRequest)
- == 0)
- {
- return true;
- }
- }
- return false;
- }
- }
|