// © Copyright IBM Corp. 2003, 2009
/**
* ExtractQueryItems.java
*
* Copyright © Cognos Incorporated. All Rights Reserved.
* Cognos and the Cognos logo are trademarks of Cognos Incorporated.
*
* Description: 1373042 - SDK Sample to retrieve the Query Item for each Data item from a report spec
*
*/
import java.io.ByteArrayInputStream;
import java.io.FileWriter;
import java.io.Writer;
import java.util.List;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import com.cognos.developer.schemas.bibus._3.*;
public class ExtractQueryItems
{
public ContentManagerService_Port cmService = null;
public Document oDocument;
String contents;
public void connectToReportServer (String endPoint) throws Exception
{
ContentManagerService_ServiceLocator cmServiceLocator = new ContentManagerService_ServiceLocator();
try
{
cmService = cmServiceLocator.getcontentManagerService(new java.net.URL(endPoint));
}
catch (Exception e)
{
System.out.println(e);
}
}
public void logon(String ns, String user, String pass)
{
//logon first. namespaceID, username, password
StringBuffer credentialXML = new StringBuffer();
credentialXML.append("
credentialXML.append("
credentialXML.append("
credentialXML.append("
credentialXML.append("
String encodedCredentials = credentialXML.toString();
try
{
cmService.logon(new XmlEncodedXML(encodedCredentials), null);
}
catch (Exception ex)
{
System.out.println("exception thrown " + ex);
}
}
public void getModel(String packageSearchPath) throws Exception
{
PropEnum props[] = new PropEnum[]{PropEnum.searchPath, PropEnum.report, PropEnum.specification};
BaseClass[] model = cmService.query(new SearchPathMultipleObject(packageSearchPath), props, new Sort[]{}, new QueryOptions());
for(int i = 0; i < model.length; i++)
{
System.out.println("\n**********************************************************\n");
System.out.println("Model: " + model[i].getSearchPath().getValue() + "\n");
System.out.println(((Model)model[i]).getModel().getValue());
System.out.println("\n**********************************************************\n");
//If you want to write the model data to a file that can be opened from Framework Manager,
//Uncomment the following lines
String modelFile="d:\\temp\\model"+i+".xml";
File oFile = new File(modelFile);
FileOutputStream fos = new FileOutputStream(oFile);
String temp=((Report)model[i]).getSpecification().getValue();
ByteArrayInputStream bais = new ByteArrayInputStream(temp.getBytes("UTF-8"));
System.out.println("Writing model data to file"+modelFile);
while (bais.available() > 0) {
fos.write(bais.read());
};
fos.flush();
fos.close();
}
}
public static void main(String[] args)
{
{
// connection to the Cognos 8 service
String endPoint = "http://sdccogt01:9300/p2pd/servlet/dispatch";
// log in as a System Administrator to ensure you have the necessary permissions to access the model
String namespaceID = "cognos7ldap";
String userID = "administrator";
String password = "admin1234";
// search path of the package from which the model will be extracted
String packageSearchPath = "//report";//report";
//GetModel test = new GetModel();
ExtractQueryItems test = new ExtractQueryItems();
try
{
test.connectToReportServer(endPoint);
test.logon(namespaceID, userID, password);
test.getModel(packageSearchPath);
System.out.println("\nDone.");
}
catch (Exception e)
{
System.out.println(e);
}
}
}
}
