Monday, December 21, 2009

sdk to find the Datasources in Cognos

package com.ntrs.nform.metadata;
import java.util.regex.Matcher;import java.util.regex.Pattern;
import com.cognos.developer.schemas.bibus._3.BaseClass;import com.cognos.developer.schemas.bibus._3.DataSourceConnection;import com.cognos.developer.schemas.bibus._3.PropEnum;import com.cognos.developer.schemas.bibus._3.QueryOptions;import com.cognos.developer.schemas.bibus._3.Sort;import com.ntrs.nform.Utils;import com.ntrs.nform.c8.C8SDK;import com.ntrs.nform.c8.ContentManagerService;
public class DatasourceList { public static void main(String[] args) {
String[] cognos; try { cognos = Utils.getCognosConfig(); Pattern p = Pattern.compile("http://((npc)(sdc))cog([a-z])"); Matcher m = p.matcher(cognos[0]);
String env = null; if (m.find()) { env = m.group(4); env = ("p".equals(env) ? "prod" : "u".equals(env) ? "uat" : "dev"); } else { throw new RuntimeException("Invalid URL " + cognos[0]); }
C8SDK c8 = new C8SDK(); c8.logon(cognos[0], cognos[1], cognos[2], cognos[3]); ContentManagerService cms = c8.getContentManagerService(); listDataSourcesAndConnections(cms); } catch (Exception e) { e.printStackTrace(); System.exit(-1); } }
public static void listDataSourcesAndConnections( ContentManagerService cmService) throws Exception { PropEnum dataSourceProps[] = new PropEnum[] { PropEnum.defaultName, PropEnum.searchPath }; PropEnum dataConnectionProps[] = new PropEnum[] { PropEnum.defaultName, PropEnum.connectionString };
BaseClass[] dataSources = cmService.getContent( "CAMID(\":\")//dataSource", dataSourceProps, new Sort[] {}, new QueryOptions());
for (int i = 0; i < dataSources.length; i++) { System.out.println("\nData Source name: " + dataSources[i].getDefaultName().getValue()); BaseClass[] dataConnections = cmService.getContent(dataSources[i] .getSearchPath().getValue() + "//dataSourceConnection", dataConnectionProps, new Sort[] {}, new QueryOptions()); for (int j = 0; j < dataConnections.length; j++) { System.out.println("\tData Connection name: " + dataConnections[j].getDefaultName().getValue()); System.out.println("\tConnection string: " + ((DataSourceConnection) dataConnections[j]) .getConnectionString().getValue()); } } }}

Wednesday, December 9, 2009

reading Xml from spec

ByteArrayInputStream bais1 = new ByteArrayInputStream(sReportSpec.getBytes("UTF-8"));
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse(bais1);
NodeList lstQueryItems = doc.getElementsByTagName("queryItem");
//strQueryData = strQueryData + bcReports[l].getDefaultName().getValue() + "," + lstQueryItems.getLength() + ",--------------\n";
System.out.println("Number of Query Items are: " + lstQueryItems.getLength());
String strDataItem = null;
String strColumnName = null;
for (int j = 0; j < lstQueryItems.getLength(); j++)
{
Node ndDataItem = lstQueryItems.item(j);
if (ndDataItem != null)
{
Element eleExpression = (Element)ndDataItem;
//NodeList lstExpression = eleExpression.getElementsByTagName("name");
NodeList lstExpression = eleExpression.getElementsByTagName("name");
if (lstExpression != null)
{
Element eleExpress = (Element)lstExpression.item(0);
NodeList lstExpressionNd = eleExpress.getChildNodes();
//System.out.println("Number lstExpressionNd: " + lstExpressionNd.getLength());
for (int k = 0; k < lstExpressionNd.getLength(); k++)
{
strDataItem = (lstExpressionNd.item(k)).getNodeValue();
//System.out.println("strDataItem : " + strDataItem);
//System.out.println("strDataItem" + strDataItem);
}
}
else
{
System.out.println("no");
}
//System.exit(0);
}
try
{
Node ndDataItem1 = lstQueryItems.item(j);
if (ndDataItem != null)
{
Element eleExpression1 = (Element)ndDataItem1;
//NodeList lstExpression = eleExpression.getElementsByTagName("name");
NodeList lstExpression1 = eleExpression1.getElementsByTagName("expression");
for (int s = 0; s < lstExpression1.getLength(); s++)
{
Node fstNode = lstExpression1.item(s);
if (fstNode.getNodeType() == Node.ELEMENT_NODE)
{
Element fstElmnt = (Element)fstNode;
NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("refobj");
Element fstNmElmnt = (Element)fstNmElmntLst.item(0);
NodeList fstNm = fstNmElmnt.getChildNodes();
//System.out.println("First Name : " + ((Node)fstNm.item(0)).getNodeValue());
strColumnName = ((Node)fstNm.item(0)).getNodeValue();
if (strColumnName != null)
{
String arrColumnName[] = null;
arrColumnName = strColumnName.split("]");
int intTemp = arrColumnName.length;
int temp1 = 0;
//System.out.println("intTemp : " + intTemp);
if (intTemp > 0)
{
strColumnName = arrColumnName[intTemp - 1];
temp1 = strColumnName.length();
// System.out.println("temp1 + " + temp1);
if (temp1 >= 2)
{
strColumnName = strColumnName.substring(2, temp1);
}
else
{
strColumnName = strColumnName;
}
//strColumnName = strColumnName;
}
else
{
strColumnName = strColumnName;
}
}
}
}
//System.exit(0);
}
}
catch (Exception E)
{
System.out.println("Exception");
}
strQueryData = strQueryData + strReportModel + "," + strDataItem + "," + strColumnName + "\n";
} // end of if
}

Thursday, August 20, 2009

sdk

// Licensed Material - Property of IBM
// © 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("").append(ns).append("");
credentialXML.append("").append(user).append("");
credentialXML.append("").append(pass).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);
}
}
}
}