Technical Articles
Using a Program Object that utilizes the RESTful Web services SDK to change a Webi doc’s universe data provider from one universe to another
Purpose
The purpose of this article is to describe a program object that utilizes the RESTful Web services SDK to change a Webi doc’s universe data provider from one universe to another. Note, SAP moved the SDK that handles the Webi Report Engine (e.g., changing the Universe data provider) from Java to the RESTful Web Services SDK.
Background
My name is Jeffrey Jonathan Jennings, a BusinessObjects consultant since 1996. With specialization in Data Analytics, Customization, and Training.
Why the need for this Program Object?
At a previous engagement, one of my team members accidentally deleted a universe used by over 4,700+ Webi docs. So, you can imagine the problem our user community had. So, with RESTful Web Services SDK to the rescue. I built this program object to relink all those orphan Webi doc universe data providers to the recovered Universe. Plus, an added benefit of this program object is it can perform a bulk universe change of a set of Webi docs’ data provider(s) from one Universe to another.
Disclaimer
I am releasing this work as is. This program object resolved all my client’s needs. However, your needs may be different, so in good faith, I cannot guarantee that this solution is adequate. Nevertheless, please feel free to leave a comment on this wiki. When I have the time, I may be able to address any problems you may encounter. Please feel free to look at the source code as a guide if you need to customize it for your particular need.
Program Object Source Code
The Program Object is comprised of the two .java source code modules:
SetWebiDocUniverse.java
/*****************************************************************************************************
* *
* Author : Jeffrey Jonathan Jennings *
* Module : SetWebiDocUniverse.java *
* *
*****************************************************************************************************/
import com.crystaldecisions.sdk.exception.SDKException;
import com.crystaldecisions.sdk.framework.IEnterpriseSession;
import com.crystaldecisions.sdk.occa.infostore.*;
import com.crystaldecisions.sdk.plugin.desktop.program.IProgramBase;
import com.crystaldecisions.sdk.properties.IProperties;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
public class SetWebiDocUniverse implements IProgramBase
{
static final String _CONST_STR_LOG_NAME = "setwebidocuniverse-run-";
private boolean _EchoArgs;
private boolean _VerifyMapping;
private String _FilePath = "";
private int _OldUniverseId;
private int _NewUniverseId;
private String _AdminUser;
private String _BiprwsUrls;
ArrayList<String> _WebDocIds;
private String _LogPath;
private String _LogFilePath;
/* ----------------------------------------------------------------------------------------------
| Name : run(iesSession, iisInfoStore, strCmdLineArgs) |
| Purpose : The BI platform Program Job Server calls this method when executing this class |
| as a schedulable BI platform Java Program Object. |
---------------------------------------------------------------------------------------------- */
public void run(IEnterpriseSession iesSession, IInfoStore iisInfoStore, String[] strCmdLineArgs) throws SDKException
{
_WebDocIds = new ArrayList<String>();
boolean blnSucceedOnce = false;
String strCurrentWebiDocId = "";
if(setClassPropertiesBasedOnCmdLineArgs(iisInfoStore, strCmdLineArgs))
try
{
ReadInWebDocIdsFromFile();
// ---
BipWebServices bipWebServices = new BipWebServices();
String[] strBiprwsUrlsBrokenOut = getBiprwsUrlsFlag().trim().split(",");
for(int intUrlIndex = 0; intUrlIndex < strBiprwsUrlsBrokenOut.length; intUrlIndex++)
{
bipWebServices.SetUrl(strBiprwsUrlsBrokenOut[intUrlIndex]);
bipWebServices.SetBiUserName(getAdminUserFlag());
bipWebServices.SetSuccessfulLogon(bipWebServices.biTrustedLogon(bipWebServices.getBiUserName()));
if(bipWebServices.getIsSuccessfulLogon())
{
Logger.getLogger(SetWebiDocUniverse.class.getName()).log(Level.INFO, "Successfully connected to WACS: " + strBiprwsUrlsBrokenOut[intUrlIndex]);
break;
}
else
Logger.getLogger(SetWebiDocUniverse.class.getName()).log(Level.WARNING, "Failed to connect to WACS: " + strBiprwsUrlsBrokenOut[intUrlIndex]);
}
if(bipWebServices.getIsSuccessfulLogon())
try
{
for(int intIndex = 0; intIndex < _WebDocIds.size(); intIndex++)
{
blnSucceedOnce = false;
strCurrentWebiDocId = _WebDocIds.get(intIndex);
for(int intUrlIndex = 0; intUrlIndex < strBiprwsUrlsBrokenOut.length; intUrlIndex++)
{
bipWebServices.SetUrl(strBiprwsUrlsBrokenOut[intUrlIndex]);
if(bipWebServices.changeWebiDocUniverse(intIndex + 1, _WebDocIds.get(intIndex), getOldUniverseIdFlag(), getNewUniverseIdFlag(), getDonotVerifyMappingFlag()))
{
blnSucceedOnce = true;
strCurrentWebiDocId = "";
break;
}
else
Logger.getLogger(SetWebiDocUniverse.class.getName()).log(Level.WARNING, "Failed with WACS: " + strBiprwsUrlsBrokenOut[intUrlIndex]);
}
if(!blnSucceedOnce)
Logger.getLogger(SetWebiDocUniverse.class.getName()).log(Level.SEVERE, "SKIPPING Webi Doc #" + String.valueOf(intIndex + 1) + " ID: " + strCurrentWebiDocId);
}
}
catch(Exception e)
{
Logger.getLogger(SetWebiDocUniverse.class.getName()).log(Level.SEVERE, "Fail to Execute Webi Docs " + e.getMessage());
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
/* ------------------------------------------------------------------------------------------------------------
| Name : universeExist(iInfoStore, intUniverseID) |
| Purpose : This method confirms that the 'intUniverseId' exist in the CMS. If it does, it returns true. |
| Otherwise, false is returned. |
------------------------------------------------------------------------------------------------------------ */
private boolean universeExist(IInfoStore iInfoStore, int intUniverseID)
{
try
{
IInfoObjects iInfoObjects = iInfoStore.query("SELECT SI_ID FROM CI_APPOBJECTS WHERE SI_KIND = 'Universe' AND SI_ID = " + String.valueOf(intUniverseID));
if (iInfoObjects != null)
if (iInfoObjects.size() != 0)
return ((IProperties)((IInfoObject) iInfoObjects.get(0)).properties()).getInt("SI_ID") == intUniverseID ? true : false;
}
catch(SDKException e)
{
Logger.getLogger(SetWebiDocUniverse.class.getName()).log(Level.SEVERE, "Failed because the SI_ID " + String.valueOf(intUniverseID) + " for the Universe does not exist in the CMS: " + e.getErrorCodeString() + " " + e.getDetailMessage());
}
return false;
}
/* ----------------------------------------------------------------------------------------------
| Property : getLogFilePath() |
| Purpose : This accessor method returns the value of the _LogFilePath private variable. |
---------------------------------------------------------------------------------------------- */
public String getLogFilePath()
{
return _LogFilePath;
}
/* ----------------------------------------------------------------------------------------------
| Property : SetLogFilePath(strLogPath) |
| Purpose : This mutator method sets the value of the _LogFilePath private variable. |
---------------------------------------------------------------------------------------------- */
public void SetLogFilePath(String strLogFilePath)
{
this._LogFilePath = strLogFilePath;
}
/* ----------------------------------------------------------------------------------------------
| Property : getLogPathFlag() |
| Purpose : This accessor method returns the value of the _LogPath private variable. |
---------------------------------------------------------------------------------------------- */
public String getLogPathFlag()
{
return _LogPath;
}
/* ----------------------------------------------------------------------------------------------
| Property : SetLogPathFlag(strLogPath) |
| Purpose : This mutator method sets the value of the _LogPath private variable. |
---------------------------------------------------------------------------------------------- */
public void SetLogPathFlag(String strLogPath)
{
this._LogPath = strLogPath;
}
/* ----------------------------------------------------------------------------------------------
| Name : getFilePathFlag() |
| Purpose : This accessor method returns the value of the _FilePath private variable. |
---------------------------------------------------------------------------------------------- */
private String getFilePathFlag()
{
return this._FilePath;
}
/* ----------------------------------------------------------------------------------------------
| Name : SetFilePathFlag(strFilePath) |
| Purpose : This mutator method sets the value of the _FilePath private variable. |
---------------------------------------------------------------------------------------------- */
private void SetFilePathFlag(String strFilePath)
{
this._FilePath = strFilePath;
}
/* ----------------------------------------------------------------------------------------------
| Name : getBiprwsUrlsFlag() |
| Purpose : This accessor method returns the value of the _BiprwsUrls private variable. |
---------------------------------------------------------------------------------------------- */
private String getBiprwsUrlsFlag()
{
return this._BiprwsUrls.trim();
}
/* ----------------------------------------------------------------------------------------------
| Name : SetBiprwsUrlsFlag(strBiprwsUrl) |
| Purpose : This mutator method sets the value of the _BiprwsUrls private variable. |
---------------------------------------------------------------------------------------------- */
private void SetBiprwsUrlsFlag(String strBiprwsUrls)
{
this._BiprwsUrls = strBiprwsUrls.trim();
}
/* ----------------------------------------------------------------------------------------------
| Name : getAdminUserFlag() |
| Purpose : This accessor method returns the value of the _AdminUser private variable. |
---------------------------------------------------------------------------------------------- */
private String getAdminUserFlag()
{
return this._AdminUser;
}
/* ----------------------------------------------------------------------------------------------
| Name : SetAdminUserFlag(strAdminUser) |
| Purpose : This mutator method sets the value of the _AdminUser private variable. |
---------------------------------------------------------------------------------------------- */
private void SetAdminUserFlag(String strAdminUser)
{
this._AdminUser = strAdminUser;
}
/* ----------------------------------------------------------------------------------------------
| Name : getOldUniverseIdFlag() |
| Purpose : This accessor method returns the value of the _OldUniverseId private variable. |
---------------------------------------------------------------------------------------------- */
private int getOldUniverseIdFlag()
{
return this._OldUniverseId;
}
/* ----------------------------------------------------------------------------------------------
| Name : SetOldUniverseIDFlag(intOldUniverseId) |
| Purpose : This mutator method sets the value of the _OldUniverseId private variable. |
---------------------------------------------------------------------------------------------- */
private void SetOldUniverseIDFlag(int intOldUniverseId)
{
this._OldUniverseId = intOldUniverseId;
}
/* ----------------------------------------------------------------------------------------------
| Name : getNewUniverseIdFlag() |
| Purpose : This accessor method returns the value of the _NewUniverseId private variable. |
---------------------------------------------------------------------------------------------- */
private int getNewUniverseIdFlag()
{
return this._NewUniverseId;
}
/* ----------------------------------------------------------------------------------------------
| Name : SetNewUniverseIDFlag(intNewUniverseId) |
| Purpose : This mutator method sets the value of the _NewUniverseId private variable. |
---------------------------------------------------------------------------------------------- */
private void SetNewUniverseIDFlag(int intNewUniverseId)
{
this._NewUniverseId = intNewUniverseId;
}
/* ----------------------------------------------------------------------------------------------
| Name : getEchoArgsFlag() |
| Purpose : This accessor method returns the value of the _EchoArgs private variable. |
---------------------------------------------------------------------------------------------- */
private boolean getEchoArgsFlag()
{
return this._EchoArgs;
}
/* ----------------------------------------------------------------------------------------------
| Name : SetEchoArgsFlag(blnEchoArgs) |
| Purpose : This mutator method sets the value of the _EchoArgs private variable. |
---------------------------------------------------------------------------------------------- */
private void SetEchoArgsFlag(boolean blnEchoArgs)
{
this._EchoArgs = blnEchoArgs;
}
/* ----------------------------------------------------------------------------------------------
| Name : getDonotVerifyMappingFlag() |
| Purpose : This accessor method returns the value of the _VerifyMapping private variable. |
---------------------------------------------------------------------------------------------- */
private boolean getDonotVerifyMappingFlag()
{
return this._VerifyMapping;
}
/* ----------------------------------------------------------------------------------------------
| Name : SetDonotVerifyMappingFlag(blnVerifyMapping) |
| Purpose : This mutator method sets the value of the _VerifyMapping private variable. |
---------------------------------------------------------------------------------------------- */
private void SetDonotVerifyMappingFlag(boolean blnVerifyMapping)
{
this._VerifyMapping = blnVerifyMapping;
}
/* ----------------------------------------------------------------------------------------------
| Name : AppLogInitializer(strFileName) |
| Purpose : Creates a new log file or append to the existing one. |
---------------------------------------------------------------------------------------------- */
public void AppLogInitializer(String strFileName)
{
try
{
PrintWriter printWriter = new PrintWriter(strFileName);
printWriter.close();
}
catch (Exception e)
{
}
FileHandler fhAppLog = null;
try
{
// ---Creates a new log file or append to the existing one
fhAppLog = new FileHandler(strFileName, true);
}
catch (Exception e)
{
e.printStackTrace();
}
// ---
Logger lAppLog = Logger.getLogger("");
fhAppLog.setFormatter(new SimpleFormatter());
lAppLog.addHandler(fhAppLog);
lAppLog.setLevel(Level.CONFIG);
}
/* ----------------------------------------------------------------------------------------------
| Name : setClassPropertiesBasedOnCmdLineArgs(iisInfoStore, strCmdLineArgs) |
| Purpose : Read from the program parameter's arguments field for this program object. |
---------------------------------------------------------------------------------------------- */
public boolean setClassPropertiesBasedOnCmdLineArgs(IInfoStore iisInfoStore, String[] strCmdLineArgs)
{
boolean blnMandatoryArg1Supplied = false, blnMandatoryArg2Supplied = false, blnMandatoryArg3Supplied = false, blnMandatoryArg4Supplied = false, blnMandatoryArg5Supplied = false;
// ---Set default value(s)
SetEchoArgsFlag(false);
SetOldUniverseIDFlag(0);
SetNewUniverseIDFlag(0);
SetFilePathFlag("");
SetAdminUserFlag("");
SetBiprwsUrlsFlag("");
SetLogPathFlag("");
SetDonotVerifyMappingFlag(false);
if(strCmdLineArgs.length > 0)
for(String strArgument : strCmdLineArgs)
if(strArgument.contains("-logpath"))
{
SetLogPathFlag(getArgumentStringValue(strArgument));
blnMandatoryArg1Supplied = true;
}
else
if(strArgument.contains("-newuniverseid"))
{
SetNewUniverseIDFlag(getArgumentIntValue(strArgument));
if(universeExist(iisInfoStore, getNewUniverseIdFlag()))
{
Logger.getLogger(SetWebiDocUniverse.class.getName()).log(Level.INFO, "New Universe ID " + getNewUniverseIdFlag() + " is valid.");
blnMandatoryArg2Supplied = true;
}
else
Logger.getLogger(SetWebiDocUniverse.class.getName()).log(Level.SEVERE, "New Universe ID " + getNewUniverseIdFlag() + " does not exist.");
}
else
if(strArgument.contains("-filepath"))
{
SetFilePathFlag(getArgumentStringValue(strArgument));
blnMandatoryArg3Supplied = true;
}
else
if(strArgument.contains("-adminuser"))
{
SetAdminUserFlag(getArgumentStringValue(strArgument));
blnMandatoryArg4Supplied = true;
}
else
if(strArgument.contains("-biprwsurls"))
{
SetBiprwsUrlsFlag(getArgumentStringValue(strArgument));
blnMandatoryArg5Supplied = true;
}
else
if(strArgument.contains("-echoargs"))
SetEchoArgsFlag(true);
else
if(strArgument.contains("-donotverifymapping"))
SetDonotVerifyMappingFlag(true);
else
if(strArgument.contains("-olduniverseid"))
{
SetOldUniverseIDFlag(getArgumentIntValue(strArgument));
if(universeExist(iisInfoStore, getOldUniverseIdFlag()))
Logger.getLogger(SetWebiDocUniverse.class.getName()).log(Level.INFO, "Old Universe ID " + getOldUniverseIdFlag() + " is valid.");
else
{
Logger.getLogger(SetWebiDocUniverse.class.getName()).log(Level.SEVERE, "Old Universe ID " + getOldUniverseIdFlag() + " does not exist.");
blnMandatoryArg2Supplied = false;
}
}
// --- Only continue if the -logpath flag was specified
if(blnMandatoryArg1Supplied)
{
SetLogFilePath(getLogPathFlag() + _CONST_STR_LOG_NAME + (new SimpleDateFormat("-yyyyMMdd_HHmmss").format(new Date())) + ".log");
AppLogInitializer(getLogFilePath());
if(!blnMandatoryArg1Supplied || !blnMandatoryArg2Supplied || !blnMandatoryArg3Supplied || !blnMandatoryArg4Supplied || !blnMandatoryArg5Supplied)
Logger.getLogger(SetWebiDocUniverse.class.getName()).log(Level.SEVERE, "SetWebiDocUniverse.jar Program Object: 'mandatory arguments (-adminuser, -logpath, -filepath, -newuniverseid and -biprwsurls) must be supplied to this program object.");
else
if(getEchoArgsFlag())
{
Logger.getLogger(SetWebiDocUniverse.class.getName()).log(Level.INFO, "--------------------------------------------------");
Logger.getLogger(SetWebiDocUniverse.class.getName()).log(Level.INFO, "SetWebiDocUniverse.jar Program Object");
Logger.getLogger(SetWebiDocUniverse.class.getName()).log(Level.INFO, "--------------------------------------------------");
Logger.getLogger(SetWebiDocUniverse.class.getName()).log(Level.INFO, "Arguments:");
Logger.getLogger(SetWebiDocUniverse.class.getName()).log(Level.INFO, "-adminuser : " + getAdminUserFlag());
Logger.getLogger(SetWebiDocUniverse.class.getName()).log(Level.INFO, "-biprwsurls : " + getBiprwsUrlsFlag());
Logger.getLogger(SetWebiDocUniverse.class.getName()).log(Level.INFO, "-filepath : " + getFilePathFlag());
Logger.getLogger(SetWebiDocUniverse.class.getName()).log(Level.INFO, "-newuniverseid : " + getNewUniverseIdFlag());
Logger.getLogger(SetWebiDocUniverse.class.getName()).log(Level.INFO, "-logpath : " + getLogPathFlag());
Logger.getLogger(SetWebiDocUniverse.class.getName()).log(Level.INFO, "-olduniverseid : " + getOldUniverseIdFlag());
Logger.getLogger(SetWebiDocUniverse.class.getName()).log(Level.INFO, "-donotverifymapping: " + getDonotVerifyMappingFlag());
Logger.getLogger(SetWebiDocUniverse.class.getName()).log(Level.INFO, "---------------------------------------------------");
}
}
return blnMandatoryArg1Supplied && blnMandatoryArg2Supplied && blnMandatoryArg3Supplied && blnMandatoryArg4Supplied && blnMandatoryArg5Supplied;
}
/* ----------------------------------------------------------------------------------------------
| Name : getArgumentStringValue(strArgument) |
| Purpose : Parses out the argument value. |
---------------------------------------------------------------------------------------------- */
private String getArgumentStringValue(String strArgument)
{
return strArgument.substring(strArgument.indexOf(":") + 1, strArgument.length());
}
/* ----------------------------------------------------------------------------------------------
| Name : getArgumentIntValue(strValue) |
| Purpose : Parses out the argument value. |
---------------------------------------------------------------------------------------------- */
private int getArgumentIntValue(String strValue)
{
try
{
return Integer.parseInt(getArgumentStringValue(strValue).trim());
}
catch(NumberFormatException e)
{
return 0;
}
}
/* ----------------------------------------------------------------------------------------------
| Name : ReadInWebDocIdsFromFile() |
| Purpose : This method reads the list of Webi Docs SI_ID from a specified file. |
---------------------------------------------------------------------------------------------- */
private void ReadInWebDocIdsFromFile()
{
try
{
BufferedReader brContent = new BufferedReader(new FileReader(getFilePathFlag()));
String strLine = null;
while ((strLine = brContent.readLine()) != null)
try
{
Long.parseLong(getArgumentStringValue(strLine).trim());
_WebDocIds.add(strLine);
}
catch(NumberFormatException e)
{
Logger.getLogger(SetWebiDocUniverse.class.getName()).log(Level.WARNING, strLine + " is not a number because " + e.getMessage());
}
brContent.close();
}
catch(IOException e)
{
Logger.getLogger(SetWebiDocUniverse.class.getName()).log(Level.SEVERE, "Failed to read the Webi Doc IDs from the " + getFilePathFlag() + " file because " + e.getMessage());
}
}
}
BipWebServices.java
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.logging.*;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class BipWebServices
{
private String _Url, _BiUserName, _LogonToken, _RequestContent, _ResponseContent, _ResponseMessage;
private int _ResponseCode;
private Map<String, List<String>> _ResponseHeaders;
private boolean _SuccessfulLogon;
/* ----------------------------------------------------------------------------------------------
| Constructor: BipWebServices() |
| Purpose :
---------------------------------------------------------------------------------------------- */
public BipWebServices()
{
}
/* ----------------------------------------------------------------------------------------------
| Property : getBiUserName() |
| Purpose : Returns the value of _BiUserName. |
---------------------------------------------------------------------------------------------- */
public String getBiUserName()
{
return _BiUserName;
}
/* ----------------------------------------------------------------------------------------------
| Property : SetBiUserName(strBiUserName) |
| Purpose : Sets the value _BiUserName. |
---------------------------------------------------------------------------------------------- */
public void SetBiUserName(String strBiUserName)
{
this._BiUserName = strBiUserName;
}
/* ----------------------------------------------------------------------------------------------
| Property : getIsSuccessfulLogon() |
| Purpose : Returns the value of _SuccessfulLogon. |
---------------------------------------------------------------------------------------------- */
public boolean getIsSuccessfulLogon()
{
return _SuccessfulLogon;
}
/* ----------------------------------------------------------------------------------------------
| Property : SetSuccessfulLogon(blnSuccessfulLogon) |
| Purpose : Sets the value _SuccessfulLogon. |
---------------------------------------------------------------------------------------------- */
public void SetSuccessfulLogon(boolean blnSuccessfulLogon)
{
this._SuccessfulLogon = blnSuccessfulLogon;
}
/* ----------------------------------------------------------------------------------------------
| Property : getUrl() |
| Purpose : Returns the value of _Url. |
---------------------------------------------------------------------------------------------- */
public String getUrl()
{
return _Url;
}
/* ----------------------------------------------------------------------------------------------
| Property : SetUrl(strUrl) |
| Purpose : Sets the value _Url. |
---------------------------------------------------------------------------------------------- */
public void SetUrl(String strUrl)
{
this._Url = strUrl;
}
/* ----------------------------------------------------------------------------------------------
| Property : getLogonToken() |
| Purpose : Returns the value of _LogonToken. |
---------------------------------------------------------------------------------------------- */
public String getLogonToken()
{
return _LogonToken;
}
/* ----------------------------------------------------------------------------------------------
| Property : SetLogonToken(strLogonToken) |
| Purpose : Sets the value of the _LogonToken with strLogonToken. |
---------------------------------------------------------------------------------------------- */
public void SetLogonToken(String strLogonToken)
{
this._LogonToken = strLogonToken;
}
/* ----------------------------------------------------------------------------------------------
| Property : getResponseCode() |
| Purpose : Returns the _ResponseCode variable content. |
---------------------------------------------------------------------------------------------- */
public int getResponseCode()
{
return _ResponseCode;
}
/* ----------------------------------------------------------------------------------------------
| Property : getResponseMessage() |
| Purpose : Returns the _ResponseMessage variable content. |
---------------------------------------------------------------------------------------------- */
public String getResponseMessage()
{
return _ResponseMessage;
}
/* ----------------------------------------------------------------------------------------------
| Property : getRequestContent() |
| Purpose : Returns the _RequestContent variable content. |
---------------------------------------------------------------------------------------------- */
public String getRequestContent()
{
return _RequestContent;
}
/* ----------------------------------------------------------------------------------------------
| Property : getResponseContent() |
| Purpose : Returns the _ResponseContent variable content. |
---------------------------------------------------------------------------------------------- */
public String getResponseContent()
{
return _ResponseContent;
}
/* ----------------------------------------------------------------------------------------------
| Property : getResponseHeaders() |
| Purpose : Returns the _ResponseHeaders variable content. |
---------------------------------------------------------------------------------------------- */
public Map<String, List<String>> getResponseHeaders()
{
return _ResponseHeaders;
}
/* ----------------------------------------------------------------------------------------------
| Name : sendRequest(strMethod, strUrl, strXmlContent, strAccept) |
| Purpose : Sends HTTP request, and add XML content to the request. |
| response as the return value. |
---------------------------------------------------------------------------------------------- */
public boolean sendRequest(String strMethod, String strUrl, String strXmlContent, String strAccept)
{
HttpURLConnection httpUrlConnection = connectBiUrl(strMethod, strUrl, strXmlContent, strAccept);
if(httpUrlConnection != null)
{
String strMessage = getPayload(httpUrlConnection);
if(strMessage != "")
{
if(strMessage.contains("HTTP response code: 401"))
{
Logger.getLogger(BipWebServices.class.getName()).log(Level.WARNING, "Attempting to relogon and try again...");
biLogoff();
SetSuccessfulLogon(biTrustedLogon(getBiUserName()));
if(getIsSuccessfulLogon())
{
httpUrlConnection = connectBiUrl(strMethod, strUrl, strXmlContent, strAccept);
if(httpUrlConnection != null)
{
strMessage = getPayload(httpUrlConnection);
if(strMessage != "")
Logger.getLogger(BipWebServices.class.getName()).log(Level.WARNING, strMessage);
}
else
return true;
}
else
Logger.getLogger(BipWebServices.class.getName()).log(Level.SEVERE, "Wasn't able to relogon again.");
}
else
Logger.getLogger(BipWebServices.class.getName()).log(Level.WARNING, strMessage);
}
else
return true;
}
return false;
}
/* ----------------------------------------------------------------------------------------------
| Name : getPayload(httpUrlConnection) |
| Purpose :
---------------------------------------------------------------------------------------------- */
private String getPayload(HttpURLConnection httpUrlConnection)
{
String strMessage = "";
InputStream isInput = null;
try
{
isInput = (InputStream) httpUrlConnection.getContent();
Scanner scanner = new Scanner(isInput);
scanner.useDelimiter("\\A");
_ResponseContent = scanner.hasNext() ? scanner.next() : "";
scanner.close();
Logger.getLogger(BipWebServices.class.getName()).log(Level.INFO, "_ResponseContent: " + _ResponseContent);
try
{
isInput.close();
}
catch(IOException e)
{
Logger.getLogger(BipWebServices.class.getName()).log(Level.SEVERE, "Failed to close the input stream because " + e.getMessage());
strMessage = "Failed to close the input stream because " + e.getMessage();
}
}
catch (IOException e)
{
strMessage = "Failed to getContent() from connection because " + e.getMessage();
isInput = httpUrlConnection.getErrorStream();
}
httpUrlConnection.disconnect();
return strMessage;
}
/* ----------------------------------------------------------------------------------------------
| Name : connectBiUrl(strMethod, strUrl, strXmlContent, strAccept) |
| Purpose :
---------------------------------------------------------------------------------------------- */
private HttpURLConnection connectBiUrl(String strMethod, String strUrl, String strXmlContent, String strAccept)
{
// -- Clears the request information, response content and SAP-specific headers
_RequestContent = _ResponseContent = _ResponseMessage = null;
_ResponseHeaders = null;
_ResponseCode = 0;
HttpURLConnection httpUrlConnection = null;
try
{
httpUrlConnection = (HttpURLConnection) new URL(getUrl() + strUrl).openConnection();
}
catch(IOException e)
{
Logger.getLogger(BipWebServices.class.getName()).log(Level.SEVERE, "Failed to connect to " + getUrl() + strUrl + " because " + e.getMessage());
return null;
}
try
{
httpUrlConnection.setRequestMethod(strMethod);
}
catch(ProtocolException e)
{
Logger.getLogger(BipWebServices.class.getName()).log(Level.SEVERE, "Failed to request method " + strMethod + " because " + e.getMessage());
return null;
}
if (strAccept != null)
httpUrlConnection.setRequestProperty("Accept", strAccept);
if (this._LogonToken != null)
httpUrlConnection.setRequestProperty("X-SAP-LogonToken", this._LogonToken);
if (strXmlContent != null)
{
httpUrlConnection.setDoOutput(true);
httpUrlConnection.setRequestProperty("Content-Type", "application/xml");
httpUrlConnection.setRequestProperty("Content-Length", String.valueOf(strXmlContent.getBytes().length));
try
{
DataOutputStream dosOutput = new DataOutputStream(httpUrlConnection.getOutputStream());
dosOutput.writeBytes(strXmlContent);
dosOutput.flush();
dosOutput.close();
}
catch(IOException e)
{
Logger.getLogger(BipWebServices.class.getName()).log(Level.SEVERE, "Failed to retrieve the XML content " + strXmlContent + " from the Data Output Stream because " + e.getMessage());
return null;
}
}
_RequestContent = strXmlContent;
try
{
_ResponseCode = httpUrlConnection.getResponseCode();
_ResponseMessage = httpUrlConnection.getResponseMessage();
_ResponseHeaders = httpUrlConnection.getHeaderFields();
}
catch(IOException e)
{
Logger.getLogger(BipWebServices.class.getName()).log(Level.SEVERE, "Failed to retrieve the ResponseCode, ResponseMessage, and HeaderFields because " + e.getMessage());
return null;
}
return httpUrlConnection;
}
/* ----------------------------------------------------------------------------------------------
| Name : biTrustedLogon(strBiUserName) |
| Purpose : BI Platform trusted logon via RESTful Web Services. |
---------------------------------------------------------------------------------------------- */
public boolean biTrustedLogon(String strBiUserName)
{
HttpURLConnection httpUrlConnection = connectTrustedUrl(strBiUserName);
if(httpUrlConnection != null)
{
String strMessage = getPayload(httpUrlConnection);
if(strMessage != "")
Logger.getLogger(BipWebServices.class.getName()).log(Level.SEVERE, strMessage);
else
{
SetLogonToken(getResponseHeaders().get("X-SAP-LogonToken").get(0));
return true;
}
}
return false;
}
/* ----------------------------------------------------------------------------------------------
| Name : connectTrustedUrl(strBiUserName) |
| Purpose :
---------------------------------------------------------------------------------------------- */
private HttpURLConnection connectTrustedUrl(String strBiUserName)
{
// -- Clears the request information, response content and SAP-specific headers
_RequestContent = _ResponseContent = _ResponseMessage = null;
_ResponseHeaders = null;
_ResponseCode = 0;
// -- Cycle through the URLs until I hit a good one in this network environment
HttpURLConnection httpUrlConnection = null;
try
{
httpUrlConnection = (HttpURLConnection) new URL(getUrl() + "/logon/trusted").openConnection();
}
catch(IOException e)
{
Logger.getLogger(BipWebServices.class.getName()).log(Level.SEVERE, "Failed to connect to " + getUrl() + "/logon/trusted" + " because " + e.getMessage());
httpUrlConnection = null;
}
if(httpUrlConnection != null)
{
try
{
httpUrlConnection.setRequestMethod("GET");
}
catch(ProtocolException e)
{
Logger.getLogger(BipWebServices.class.getName()).log(Level.SEVERE, "Failed to set Request Method to 'GET' because " + e.getMessage());
httpUrlConnection.disconnect();
httpUrlConnection = null;
}
if(httpUrlConnection != null)
{
httpUrlConnection.setRequestProperty("Accept", "application/json");
httpUrlConnection.setRequestProperty("X-SAP-TRUSTED-USER", strBiUserName);
_RequestContent = null;
try
{
_ResponseCode = httpUrlConnection.getResponseCode();
_ResponseMessage = httpUrlConnection.getResponseMessage();
_ResponseHeaders = httpUrlConnection.getHeaderFields();
}
catch(IOException e)
{
Logger.getLogger(BipWebServices.class.getName()).log(Level.SEVERE, "Failed to retrieve the ResponseCode, ResponseMessage, and HeaderFields because " + e.getMessage());
httpUrlConnection.disconnect();
httpUrlConnection = null;
}
}
}
return httpUrlConnection;
}
/* ----------------------------------------------------------------------------------------------
| Name : bilogoff() |
| Purpose : Logs off from the CMS. |
---------------------------------------------------------------------------------------------- */
public boolean biLogoff()
{
if (getLogonToken() != null)
if(connectBiUrl("POST", "/logoff", null, "application/xml") != null)
{
SetLogonToken("");
return true;
}
return false;
}
/* -----------------------------------------------------------------------------------------------
| Name : changeWebiDocUniverse(intWebDocNumber, strWebDocID, intOldUniverseId, |
| intNewUniverseId, blnDonotVerifyMapping) |
| Purpose :
----------------------------------------------------------------------------------------------- */
public boolean changeWebiDocUniverse(int intWebDocNumber, String strWebDocId, int intOldUniverseId, int intNewUniverseId, boolean blnDonotVerifyMapping)
{
boolean blnResult = false;
String strDataProviderIDs = "";
JSONArray jsonArrayDataProviders = null;
Logger.getLogger(BipWebServices.class.getName()).log(Level.INFO, "");
Logger.getLogger(BipWebServices.class.getName()).log(Level.INFO, "----------------------------------------------------------------------");
Logger.getLogger(BipWebServices.class.getName()).log(Level.INFO, "intWebDocNumber: " + intWebDocNumber + " strWebDocID: " + strWebDocId + " " + " intUniverseID: " + String.valueOf(intNewUniverseId));
if(sendRequest("GET", "/raylight/v1/documents/" + strWebDocId + "/dataproviders", null, "application/json"))
{
try
{
Logger.getLogger(BipWebServices.class.getName()).log(Level.INFO, "Data Provider JSON:");
Logger.getLogger(BipWebServices.class.getName()).log(Level.INFO, getResponseContent());
jsonArrayDataProviders = new JSONObject(getResponseContent()).getJSONObject("dataproviders").getJSONArray("dataprovider");
blnResult = true;
}
catch(JSONException e)
{
Logger.getLogger(BipWebServices.class.getName()).log(Level.INFO, "Failed due to a JSONException because " + e.getMessage());
}
if(blnResult)
{
blnResult = false;
for(int intIndex = 0; intIndex < jsonArrayDataProviders.length(); intIndex++)
{
String strDataProviderID = "", strDataProviderSourceID = "", strDataProviderType = "";
try
{
JSONObject jsonObjectDataProvider = jsonArrayDataProviders.getJSONObject(intIndex);
strDataProviderID = jsonObjectDataProvider.getString("id");
strDataProviderType = jsonObjectDataProvider.getString("dataSourceType");
strDataProviderSourceID = jsonObjectDataProvider.getString("dataSourceId");
blnResult = true;
if(!strDataProviderSourceID.isEmpty() || !strDataProviderSourceID.equals(""))
if(!strDataProviderSourceID.equals(String.valueOf(intOldUniverseId)))
continue;
else
{
strDataProviderIDs += strDataProviderID + ",";
Logger.getLogger(BipWebServices.class.getName()).log(Level.INFO, "DataProvider ID: " + strDataProviderID + " has a Universe that matches the old: " + String.valueOf(intOldUniverseId));
}
}
catch(JSONException e)
{
if(e.getMessage().contains("dataSourceId"))
{
if(!strDataProviderType.equalsIgnoreCase("unv"))
continue;
else
{
Logger.getLogger(BipWebServices.class.getName()).log(Level.INFO, "DataProvider ID: " + strDataProviderID + " does not have a Universe.");
strDataProviderSourceID = "";
intOldUniverseId = intNewUniverseId;
strDataProviderIDs += strDataProviderID + ",";
}
}
else
{
Logger.getLogger(BipWebServices.class.getName()).log(Level.WARNING, "Failed due to a JSONException because " + e.getMessage());
break;
}
}
blnResult = false;
}
if(strDataProviderIDs.length() > 1)
{
strDataProviderIDs = strDataProviderIDs.substring(0, strDataProviderIDs.length() - 1);
String strGetUrl = "/raylight/v1/documents/" + strWebDocId + "/dataproviders/mappings?originDataproviderIds=" + strDataProviderIDs + "&targetDatasourceId=" + String.valueOf(intOldUniverseId);
String strPostUrl = "/raylight/v1/documents/" + strWebDocId + "/dataproviders/mappings?originDataproviderIds=" + strDataProviderIDs + "&targetDatasourceId=" + String.valueOf(intNewUniverseId) + ((blnDonotVerifyMapping) ? "" : "&skipChecking=true");
// -- Get Default Universe Mappings
if(sendRequest("GET", strGetUrl, null, "application/xml"))
if(sendRequest("POST", strPostUrl, getRequestContent(), "application/xml"))
{
Logger.getLogger(BipWebServices.class.getName()).log(Level.WARNING, "DataProvider IDs: " + strDataProviderIDs + " was successfully linked to Universe ID: " + String.valueOf(intNewUniverseId));
blnResult = true;
}
}
// -- Save Document
if(blnResult)
if(sendRequest("GET", "/raylight/v1/documents/" + strWebDocId, null, "application/xml"))
if((blnResult = sendRequest("POST", "/raylight/v1/documents/" + strWebDocId, getResponseContent(), "application/xml")))
Logger.getLogger(BipWebServices.class.getName()).log(Level.INFO, "Saved Webi doc " + strWebDocId + " back.");
}
}
return blnResult;
}
}
JARs the Program Object uses
The JARs below are supplied by SAP, and can be found in your SBOP installation subfolder ../java/lib:
- Boconfig.jar
- Cecore.jar
- Celib.jar
- Ceplugins_core.jar
- Cesession.jar
- Corbaidl.jar
- Ebus405.jar
The JAR below is be found at http://www.java2s.com/Code/Jar/j/Downloadjavajsonjar.htm:
- Java-json.jar
How to use the Program Object?
The program object takes in 6 arguments:
- -adminuser, specifics the BI account that has access to all the Webi docs in the text file specified by the -filepath argument
- -biprwsurls, specifics the BI Platform RESTful Webservice URL(s) for given BCS environment
- -filepath, specifics the pathname of the text file that contains a list of all the CUIDs (a.k.a., SL_IDs) of the Webi docs to be changed
- -newuniverseid, specifies the Universe ID that the Webi docs’ Universe data provider(s) should now use
- -olduniverseid, specifies the Universe ID the program object will look for if specified to be changed to the new Universe ID specified by the -newuniverseid argument. Note, when the program object is used for bulk Universe change this argument is not specified
- -donotverifymapping, specifies if the universe should be mapped should be verified. Note, only set this flag to false if you are not sure the objects do not exist in the universe you are mapping to.
- -logpath, specifies where the log file the program object generates is stored
Using the values from the arguments above, the program object iterates through the list of Webi docs in a text file (specified by the -filepath argument) Universe data provider(s), and changes the data provider’s Universe ID to the one specified by the -newuniverseid argument.