简单介绍:
代码是通过构造XML, 模拟运行(runSpecification)获取运行XML,解析XML获取参数集合List.
package test;
import java.io.ByteArrayInputStream;
import java.util.List;
import org.dom4j.io.SAXReader;
import org.dom4j.tree.DefaultElement;
import com.cognos.developer.schemas.bibus._3.AsynchDetailParameters;
import com.cognos.developer.schemas.bibus._3.AsynchDetailReportOutput;
import com.cognos.developer.schemas.bibus._3.AsynchDetailReportStatus;
import com.cognos.developer.schemas.bibus._3.AsynchDetailReportStatusEnum;
import com.cognos.developer.schemas.bibus._3.AsynchReply;
import com.cognos.developer.schemas.bibus._3.AsynchReplyStatusEnum;
import com.cognos.developer.schemas.bibus._3.AsynchSecondaryRequest;
import com.cognos.developer.schemas.bibus._3.BaseParameter;
import com.cognos.developer.schemas.bibus._3.Option;
import com.cognos.developer.schemas.bibus._3.Parameter;
import com.cognos.developer.schemas.bibus._3.ParameterValue;
import com.cognos.developer.schemas.bibus._3.ReportServiceReportSpecification;
import com.cognos.developer.schemas.bibus._3.ReportServiceSpecification;
import com.cognos.developer.schemas.bibus._3.ReportService_PortType;
import com.cognos.developer.schemas.bibus._3.RunOptionBoolean;
import com.cognos.developer.schemas.bibus._3.RunOptionEnum;
import com.cognos.developer.schemas.bibus._3.RunOptionStringArray;
import com.cognos.developer.schemas.bibus._3.SearchPathSingleObject;
import com.cognos.developer.schemas.bibus._3.Specification;
public class getLOVOfReportParameters
{
// Report Service: Run Reports
private ReportService_PortType reportService = null;
public String executeReportAsXML(String spec)
{
String outXml = null;
AsynchReply rsr;
Option[] runOptions = new Option[3];
RunOptionBoolean rob = new RunOptionBoolean();
RunOptionStringArray rosa = new RunOptionStringArray();
RunOptionBoolean rop = new RunOptionBoolean();
// We do not want to save this output
rob.setName(RunOptionEnum.saveOutput);
rob.setValue(false);
// What format do we want the report in: PDF, HTML, or XML?
rosa.setName(RunOptionEnum.outputFormat);
rosa.setValue(new String[]{ "XML" });
// Set the report not to prompt as we pass the parameter if any
rop.setName(RunOptionEnum.prompt);
rop.setValue(false);
// Fill the array with the run options.
runOptions[0] = rob;
runOptions[1] = rosa;
runOptions[2] = rop;
try
{
Specification reportSpec = new Specification();
reportSpec.set_value(spec);
ReportServiceSpecification asyncSpec = new ReportServiceReportSpecification();
asyncSpec.setValue(reportSpec);
rsr = this.reportService.runSpecification(asyncSpec, new ParameterValue[] {}, runOptions);
if (!rsr.getStatus().equals(AsynchReplyStatusEnum.complete))
{
while (!rsr.getStatus().equals(AsynchReplyStatusEnum.complete))
{
if (!hasSecondaryRequest(rsr, "wait"))
{
System.out.println("error on secondary request.");
}
rsr = this.reportService.wait(rsr.getPrimaryRequest(), new ParameterValue[] {}, new Option[] {});
}
if (outputIsReady(rsr))
{
rsr = this.reportService.getOutput(rsr.getPrimaryRequest(), new ParameterValue[] {}, new Option[] {});
}
else
{
System.out.println("output is not ready!");
}
}
outXml = getOutputPage(rsr);
}
catch (Exception e)
{
e.printStackTrace();
}
return outXml;
}
public String getOutputPage(AsynchReply response)
{
AsynchDetailReportOutput reportOutput = null;
for (int i = 0; i < response.getDetails().length; i++)
{
if (response.getDetails()[i] instanceof AsynchDetailReportOutput)
{
reportOutput = (AsynchDetailReportOutput) response.getDetails()[i];
break;
}
}
// text based output is split into pages -- return the current page
return reportOutput.getOutputPages()[0].toString();
}
public boolean outputIsReady(AsynchReply response)
{
for (int i = 0; i < response.getDetails().length; i++)
{
if ((response.getDetails()[i] instanceof AsynchDetailReportStatus) && (((AsynchDetailReportStatus) response.getDetails()[i]).getStatus() == AsynchDetailReportStatusEnum.responseReady) && (hasSecondaryRequest(response, "getOutput")))
{
return true;
}
}
return false;
}
public static 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;
}
public BaseParameter[] getReportParameters(String reportPathString) throws java.rmi.RemoteException
{
BaseParameter params[] = new Parameter[] {};
AsynchReply response;
SearchPathSingleObject reportPath = new SearchPathSingleObject();
reportPath.set_value(reportPathString);
response = this.reportService.getParameters(reportPath, new ParameterValue[] {}, new Option[] {});
// If response is not immediately complete, call wait until complete
if (!response.getStatus().equals(AsynchReplyStatusEnum.conversationComplete))
{
while (!response.getStatus().equals(AsynchReplyStatusEnum.conversationComplete))
{
response = this.reportService.wait(response.getPrimaryRequest(), new ParameterValue[] {}, new Option[] {});
}
}
for (int i = 0; i < response.getDetails().length; i++)
{
if (response.getDetails()[i] instanceof AsynchDetailParameters)
params = ((AsynchDetailParameters) response.getDetails()[i]).getParameters();
}
return params;
}
public void createJobsAndViews(String searchPath)
{
try
{
BaseParameter myParams[] = getReportParameters(searchPath);
String parmValues = ((Parameter) myParams[1]).getModelFilterItem();
String modelPah ="";
String xml = "<report xmlns=\"http://developer.cognos.com/schemas/report/7.0/\" expressionLocale=\"en-ca\">";
xml = xml + " <modelPath>+modelPah+</modelPath>";
xml = xml + " <queries>";
xml = xml + " <query name=\"TestQuery\">";
xml = xml + " <source><model/></source>";
xml = xml + " <selection>";
xml = xml + " <dataItem name=\"Product line\" aggregate=\"none\">";
xml = xml + " <expression>" + parmValues + "</expression>";
xml = xml + " </dataItem>";
xml = xml + " </selection> <detailFilters>" + "<detailFilter>" + "<filterExpression>" + parmValues + "<>'ICG'</filterExpression>" + "</detailFilter>" + "</detailFilters> <queryHints>" + " <useForParameterInfo value=\"true\" />" + " <maxRowsRetrieved />" + "</queryHints>";
xml = xml + " </query>";
xml = xml + " </queries>";
xml = xml + " <layouts>";
xml = xml + " <layout>";
xml = xml + " <reportPages>";
xml = xml + " <page name=\"Page1\"><style><defaultStyles><defaultStyle refStyle=\"pg\"/></defaultStyles></style>";
xml = xml + " <pageBody><style><defaultStyles><defaultStyle refStyle=\"pb\"/></defaultStyles></style>";
xml = xml + " <contents>";
xml = xml + " <list refQuery=\"<span style="font-family: Arial, Helvetica, sans-serif;">TestQuery</span>\">";
xml = xml + " <style><defaultStyles><defaultStyle refStyle=\"ls\"/></defaultStyles><CSS value=\"border-collapse:collapse\"/></style>";
xml = xml + " <listColumns><listColumn><listColumnTitle><style><defaultStyles><defaultStyle refStyle=\"lt\"/></defaultStyles></style>";
xml = xml + " <contents>";
xml = xml + " <textItem><dataSource><dataItemLabel refDataItem=\"Product line\"/></dataSource></textItem>";
xml = xml + " </contents></listColumnTitle>";
xml = xml + " <listColumnBody><style><defaultStyles><defaultStyle refStyle=\"lc\"/></defaultStyles></style>";
xml = xml + " <contents>";
xml = xml + " <textItem>";
xml = xml + " <dataSource>";
xml = xml + " <dataItemValue refDataItem=\"Product line\"/>";
xml = xml + " </dataSource>";
xml = xml + " </textItem>";
xml = xml + " </contents>";
xml = xml + " </listColumnBody>";
xml = xml + " </listColumn>";
xml = xml + " </listColumns>";
xml = xml + " </list>";
xml = xml + " </contents>";
xml = xml + " </pageBody>";
xml = xml + " </page>";
xml = xml + " </reportPages>";
xml = xml + " </layout>";
xml = xml + " </layouts>";
xml = xml + "</report>";
String start = null;
org.dom4j.Document oDocument;
SAXReader xmlReader = new SAXReader();
String sReportSpec = executeReportAsXML(xml);
int index = sReportSpec.indexOf("xmlns=", 0);
if (index >= 0)
{
start = sReportSpec.substring(0, index);
String end = sReportSpec.substring(sReportSpec.indexOf("http://developer.cognos.com/schemas/xmldata/1/") + 47);
sReportSpec = start + end;
}
ByteArrayInputStream bais1 = new ByteArrayInputStream(sReportSpec.getBytes("UTF-8"));
oDocument = xmlReader.read(bais1);
org.dom4j.Element e = (org.dom4j.Element) oDocument.selectSingleNode("//data");
List<DefaultElement> els = e.selectNodes("//value");
for (DefaultElement de : els)
{
System.out.println(de.getStringValue());
}
bais1.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
String searchPath = "/content/folder[@name='Test']/report[@name='Test']";
String endpoint = "http://XXXX/p2pd/servlet/dispatch";
String nameSpaceID = "XXXX";
String userName = "XXXX";
String password = "XXXXXX";
CRNConnect crn = new CRNConnect();
crn.connectToCognosServer(endpoint);
crn.quickLogon(nameSpaceID, userName, password);
getLOVOfReportParameters cv = new getLOVOfReportParameters();
cv.reportService = crn.getReportService();
cv.createJobsAndViews(searchPath);
}
}
详细见: