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
}