You are on page 1of 7

/*

* Licensed Materials - Property of IBM


* IBM Cognos Products: SDK Support
* (C) Copyright IBM Corp. 2003, 2009
* US Government Users Restricted Rights - Use, duplication or disclosure restri
cted by GSA ADP Schedule Contract with IBM Corp.
*/
/*
* CreateReportView.java
* Description: 1367562 - SDK sample to schedule a report to run and create a re
port view. - Cognos 8.3
* Tested with: IBM Cognos BI 8.3
*/

import java.math.BigInteger;
import java.util.*;
import com.cognos.developer.schemas.bibus._3.*;
public class CreateReportView
{
public ContentManagerService_Port cmService = null;
public CreateReportView(String endPoint)
{
ContentManagerService_ServiceLocator cmServiceLocator = new Cont
entManagerService_ServiceLocator();
try
{
cmService = cmServiceLocator.getcontentManagerService(ne
w java.net.URL(endPoint));
}
catch (Exception e)
{
System.out.println(e);
}
}
public String quickLogon(String namespace, String uid, String pwd)
{
try
{
StringBuffer credentialXML = new StringBuffer();
credentialXML.append("<credential>");
credentialXML.append("<namespace>").append(namespace).ap
pend("</namespace>");
credentialXML.append("<username>").append(uid).append("<
/username>");
credentialXML.append("<password>").append(pwd).append("<
/password>");
credentialXML.append("</credential>");
String encodedCredentials = credentialXML.toString();
cmService.logon(new XmlEncodedXML(encodedCredentials), n
ull);
}
catch(Exception e)
{
System.out.println(e);
}
return ("Logon successful as " + uid);
}
public static void showUsage()
{
System.out.println( "Run a report and save the output as HTML.\n
" );
System.out.println( "usage:\n" );
System.out.println( "-host hostName Host name of a co
gnos 8 server. " );
System.out.println( " Default: localhos
t" );
System.out.println( "-port portNumber Port number of th
e cognos 8 server 'host'." );
System.out.println( " Default: 80" );
System.out.println( "-report searchPath Search path in co
gnos 8 to a report." );
System.out.println( " Default: /content
/package[@name='GO Data Warehouse (analysis)']/folder[@name='Report Studio Repor
t Samples']/report[@name='Positions to fill']");
System.out.println( "-newreport reportName New report view n
ame." );
System.out.println( " Default: 'New Rep
ortView'" );
System.out.println( "-newreportpath searchPath Search path in co
gnos 8 to a folder." );
System.out.println( " Default: /content
/package[@name='GO Data Warehouse (analysis)']/folder[@name='Report Studio Repor
t Samples']" );
System.out.println( "-user userName User to log on as
. Must exist in 'userNamespace'." );
System.out.println( " Default: none" );
System.out.println( "-password userPassword Password for 'use
rName' in 'userNamespace'." );
System.out.println( " Default: none" );
System.out.println( "-namespace userNamespace Security namespac
e to log on to." );
System.out.println( " Default: none" );
}
public void createSchedule(String path, String parentSearchPath, String
newReportName)
{
Schedule sched = new Schedule();
NmtokenProp tp = new NmtokenProp();
NmtokenProp endType = new NmtokenProp();
DateTimeProp startDate = new DateTimeProp();
PositiveIntegerProp frequency = new PositiveIntegerProp();
NmtokenProp dailyPeriod = new NmtokenProp();
BooleanProp active = new BooleanProp();
RunOptionBoolean prompt = new RunOptionBoolean();
RunOptionStringArray outputFormat = new RunOptionStringArray();
// setup save as properties (view, new name, parent folder).
RunOption[] saveAsRunOptions = new RunOption[4];
// The report name can have a different value for each locale (l
anguage).
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(parentSearchPath);
saveAsRunOptions[0] = roSaveAs;

// set prompt to false.


prompt.setName(RunOptionEnum.prompt);
prompt.setValue(false);
saveAsRunOptions[1] = prompt;
// set output format
outputFormat.setName(RunOptionEnum.outputFormat);
String[] html = new String[1];
html[0] = "HTML";
outputFormat.setValue(html);
saveAsRunOptions[2] = outputFormat;
// set up schedule object properties
// it will be added to the content store later.
// assign the run options to the sched object.
sched.setRunOptions(new RunOptionArrayProp());
sched.getRunOptions().setValue(saveAsRunOptions);
// a few more sched options.
active.setValue(true);
dailyPeriod.setValue("minute");
frequency.setValue(BigInteger.valueOf(3));
startDate.setValue(Calendar.getInstance());
tp.setValue("daily");

sched.setActive(active);
sched.setDailyPeriod(dailyPeriod);
sched.setEveryNPeriods(frequency);
sched.setType(tp);
sched.setStartDate(startDate);

endType.setValue("indefinite");
sched.setEndType(endType);
//get the search path of the user that is currently logged in
PropEnum[] properties =
{ PropEnum.defaultName, PropEnum.searchPath };
SearchPathMultipleObject qPath = new SearchPathMultipleObject();
qPath.setValue("~");
BaseClass[] results= new BaseClass[1];
try
{
results =
cmService.query(qPath, properties, new Sort[]{}
, new QueryOptions());
}
catch( Exception ex )
{
System.out.println( "Unhandled exception!" );
System.out.println( "Message: " + ex.getMessage() );
ex.printStackTrace();
return;
}
//set the credential used to run the report to the current user
or "Anonymous" user.
BaseClassArrayProp credentials = new BaseClassArrayProp();
Credential crd = new Credential();
StringProp crdPath = new StringProp();
//comment out the following line if you want to use current SDK
users credential
//crdPath.setValue("CAMID('::Anonymous')/credential[@name='Crede
ntial']");
//comment out the following line if you want to use "Anonymous"
users credential
crdPath.setValue(results[0].getSearchPath().getValue() + "/crede
ntial[@name='Credential']");
crd.setSearchPath(crdPath);
sched.setCredential(credentials);
sched.getCredential().setValue(new BaseClass[] { crd });
// set language and locale.
RunOptionLanguageArray runoptLangArray = new RunOptionLanguageAr
ray();
runoptLangArray.setName(RunOptionEnum.outputLocale);
runoptLangArray.setValue(new String[]{"en-us"});
saveAsRunOptions[3] = runoptLangArray;
try
{
// update existing report schedule if it already exists.
if it doesn't exist, it will be added.
AddOptions ao = new AddOptions();
BaseClass[] bcReturn = new BaseClass[1];
ao.setUpdateAction(UpdateActionEnum.update);
SearchPathSingleObject searchPath = new SearchPathSingle
Object();
searchPath.setValue(path);
bcReturn = cmService.add(searchPath, new BaseClass[] { s
ched } , ao);
if (bcReturn != null)
{
System.out.println ("Schedule Created");
}
else
{
System.out.println ("Schedule NOT Created");
}
}
catch( Exception ex )
{
System.out.println( "Unhandled exception!" );
System.out.println( "Message: " + ex.getMessage() );
ex.printStackTrace();
}
}
public static void main(String[] args)
{
// *****************************
// Default values for the host, port, report, new report view na
me, new path etc.
// values can be changed by passing them in on the command line.
// Change this URL if your server is in another location or a
// different port.
String serverHost = "localhost";
String serverPort = "9300";
// Change this search path if you want to run a different report
.
String reportPath = "/content/package[@name='GO Data Warehouse (
analysis)']/folder[@name='Report Studio Report Samples']/report[@name='Positions
to fill']";
// Change the newReportName and path to create the reportview in
a different location.
String newReportName = "New ReportView";
String newReportPath = "/content/package[@name='GO Data Warehous
e (analysis)']/folder[@name='Report Studio Report Samples']";
// Specify a user to log in as. (if not passed on the command li
ne).
String userName = "";
String userPassword = "";
String userNamespace = "";
// Parse the command-line arguments.
boolean exit = false;
for( int idx = 0; idx < args.length; idx++ )
{
if( args[idx].equals( "-host" ) )
{
idx++;
serverHost = args[idx];
}
else if( args[idx].equals( "-port" ) )
{
idx++;
serverPort = args[idx];
}
else if( args[idx].equals( "-report" ) )
{
idx++;
reportPath = args[idx];
}
else if( args[idx].equals( "-newreport" ) )
{
idx++;
newReportName = args[idx];
}
else if( args[idx].equals( "-newreportpath" ) )
{
idx++;
newReportPath = args[idx];
}
else if( args[idx].equals( "-user" ) )
{
idx++;
userName = args[idx];
}
else if( args[idx].equals( "-password" ) )
{
idx++;
userPassword = args[idx];
}
else if( args[idx].equals( "-namespace" ) )
{
idx++;
userNamespace = args[idx];
}
else if (exit == false)
{
System.out.println( "Unknown argument: " + args[
idx] );
showUsage();
exit = true;
}
}
if( !exit )
{
// Create a connection to a Cognos 8 server.
String C8_URL = "http://" + serverHost + ":" + serverPor
t + "/p2pd/servlet/dispatch";
System.out.println( "Creating cognos 8 connection to " +
serverHost + ":" + serverPort + "..." );
System.out.println( "Server URL: " + C8_URL );
CreateReportView crv = new CreateReportView(C8_URL);
System.out.println( "... done." );
// Set up the biBusHeader for a logon.
if( userName.length() > 0 && userPassword.length() > 0 &
& userNamespace.length() > 0 )
{
System.out.println( "Logging on as " + userName
+ " in the " + userNamespace + " namespace." );
crv.quickLogon(userNamespace, userName, userPass
word);
}
// schedule the report.
crv.createSchedule(reportPath,newReportPath,newReportNam
e);
}
}
}

You might also like