If you are unable to create a new account, please email support@bspsoftware.com

 

News:

MetaManager - Administrative Tools for IBM Cognos
Pricing starting at $2,100
Download Now    Learn More

Main Menu

Sample code to update a database connection string

Started by gohabsgo, 01 Mar 2018 01:56:12 PM

Previous topic - Next topic

gohabsgo

Hey Folks,
Anyone have some sample code that will modify the connection string of an existing database connection?

Appreciate in advance

aussieadam

if you can find search path which should be something like
CAMID(:)/dataSource[@name=dataSource]/dataSourceConnection

you should be able to use the ContentManagerService_PortType object to find and modify it.

SearchPathMultipleObject spm = new SearchPathMultipleObject();
spm.set_value(dataSourceConnectionSearchPath);
PropEnum[] prop = new PropEnum[] { PropEnum.capabilities, PropEnum.permissions, PropEnum.searchPath, PropEnum.user, PropEnum.properties,
                    PropEnum.members, PropEnum.groupAndRoleSettings, PropEnum.defaultName,PropEnum.datasourceConnectionName };
DataSourceConnection dataSourceConnection = (DataSourceConnection) cmService.query(spm,props, new QueryOptions());
dataSourceConnection.setConnectionString(new StringProp(null,dataSourceConnectionString));
cmService.update(new BaseClass[] {dataSourceConnection},new UpdateOptions());
     


I wrote this without testing, so there may be some syntax errors, but that should generally do it.
let me know if it doesn't work and i can fully code an example and send it.

gohabsgo


aussieadam

#3
Decided to give a solution to everyone. I'm a big fan of the SDK, it's a free and powerful option for customizing and automating Cognos. so the more people who can use it the better.

Some notes:
If something isn't working I may be using a different import than your default, check my imports at the top just in case.
I use the 11.0.8 version of the Cognos SDK and axis.
It's bad to have all of this in 1 main class, bad for re usability and customization.
I also wouldn't store connection details in java, either pass through as a parameter or read from db or encrypted file.
With that said, below is the class that should do it. Let me know if you need any more help.

package app;

import java.net.URL;
import java.rmi.RemoteException;

import javax.xml.namespace.QName;

import com.cognos.developer.schemas.bibus._3.BaseClass;
import com.cognos.developer.schemas.bibus._3.BiBusHeader;
import com.cognos.developer.schemas.bibus._3.ContentManagerService_PortType;
import com.cognos.developer.schemas.bibus._3.ContentManagerService_ServiceLocator;
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.SearchPathMultipleObject;
import com.cognos.developer.schemas.bibus._3.Sort;
import com.cognos.developer.schemas.bibus._3.StringProp;
import com.cognos.developer.schemas.bibus._3.UpdateOptions;
import com.cognos.developer.schemas.bibus._3.XmlEncodedXML;
import com.cognos.org.apache.axis.client.Stub;
import com.cognos.org.apache.axis.message.SOAPHeaderElement;

public class ChangeDataSourceConnectionString {

    private static ContentManagerService_PortType cmService;
    private static String sessionID;
    private static final String DEVELOPER_URL = "http://developer.cognos.com/schemas/bibus/3/";
    private static final String BUS_HEADER = "biBusHeader";

    // constructor to hide the default public constructor
    private ChangeDataSourceConnectionString() {}

    public static void main(final String[] args) throws RemoteException {
        // change this to dispatcher URL
        final String dispatcherURL = "http://localhost:9300/p2pd/servlet/dispatch";
        final String nameSpaceID = "nameSpace";
        final String userName = "userName";
        final String password = "password";
       
        // get this from admin console
        final String dataSourceConnectionSearchPath = "CAMID(\":\")/dataSource[@name='datasource']/dataSourceConnection[@name='datasource']";
        final String dataSourceConnectionString = "newConnectionString";

        //these 2 methods are usually in a CognosConnection class with other connections besides cmService
        connectToCognos(dispatcherURL);
        logonToCognos(nameSpaceID, userName, password);

        final SearchPathMultipleObject spm = new SearchPathMultipleObject();
        spm.set_value(dataSourceConnectionSearchPath);
        final PropEnum[] props = new PropEnum[] { PropEnum.capabilities, PropEnum.permissions, PropEnum.searchPath, PropEnum.user,
                PropEnum.properties, PropEnum.members, PropEnum.groupAndRoleSettings, PropEnum.defaultName, PropEnum.connectionString };
        final DataSourceConnection dataSourceConnection = (DataSourceConnection) cmService
                .query(spm, props, new Sort[] {}, new QueryOptions())[0];

        dataSourceConnection.setConnectionString(new StringProp(null, dataSourceConnectionString));
        cmService.update(new BaseClass[] { dataSourceConnection }, new UpdateOptions());

        logoffFromCognos();

    }

    private static void connectToCognos(final String dispatcherURL) {
        final ContentManagerService_ServiceLocator cmServiceLocator = new ContentManagerService_ServiceLocator();
        try {
            final URL url = new URL(dispatcherURL);
            cmService = cmServiceLocator.getcontentManagerService(url);
        } catch (final Exception e) {
            throw new IllegalStateException("Unable to connect to Cognos", e);
        }

    }

    /**
     *  Bind credentials to the Cognos environment.
     * @param password
     * @param userName
     * @param nameSpaceID
     */
    private static void logonToCognos(final String nameSpaceID, final String userName, final String password) {

        final StringBuilder credentialXML = new StringBuilder();
        credentialXML.append("<credential>");
        credentialXML.append("<namespace>").append(nameSpaceID).append("</namespace>");
        credentialXML.append("<username>").append(userName).append("</username>");
        credentialXML.append("<password>").append(password).append("</password>");
        credentialXML.append("</credential>");

        final XmlEncodedXML xmlCredentials = new XmlEncodedXML();
        xmlCredentials.set_value(credentialXML.toString());

        try {
            //if you want any other services you need to add them here too, should be the same lines as below for other services
            cmService.logon(xmlCredentials, null);
           
            final SOAPHeaderElement temp = ((Stub) cmService).getResponseHeader(DEVELOPER_URL, BUS_HEADER);
            final BiBusHeader CMbibus = (BiBusHeader) temp.getValueAsType(new QName(DEVELOPER_URL, BUS_HEADER));

            if (CMbibus.getTracking().getSessionContext() != null) {
                sessionID = CMbibus.getTracking().getSessionContext();
            }

            ((Stub) cmService).setHeader(DEVELOPER_URL, BUS_HEADER, CMbibus);
        } catch (final Exception ex) {
            throw new IllegalStateException("Unable to authenticate against Cognos", ex);
        }

    }

    /**
     *  Log off from the Cognos environment.
     */
    public static void logoffFromCognos() {
        try {
            cmService.logoff();
        } catch (final Exception ex) {
            throw new IllegalStateException("Unable to log off from Cognos", ex);
        }
    }

}








raztos

Hello aussieadam,

I have question, which axis are you using in the code above ?

I got inspired of following link : https://www.ibm.com/support/pages/sdk-how-use-sdk-libraries-including-axis-jar-files-software-application

but my code was not working with STEP3.

Which axis have you imported to your java project ?

Thanks