Sunday, January 27, 2008

Sending A Message - Using Essbase Custom Defined Functions

In my last post, I talked about how the messaging capabilities contained within the Smart Space product can be leveraged in non-traditional ways. One of the best ways is by using the Smart Space Java API.

I am a big fan of the best OLAP database on the planet: Essbase. Essbase has a great feature where a developer can write their own custom Java functions that can then be called by the Essbase calculator. I have always wanted to have the ability from within an Essbase calculation script to notify Essbase users that a calculation has completed. The combination of the Smart Space messaging Java API and the Essbase custom defined function (CDF) will now allow me to fulfill that dream.

When writing to the Smart Space Java API, there are a number of JAR files that are required from Smart Space (inform.jar, smack.jar, smackx.jar, smackx-debug.jar and log4j.jar). There are four main operations in the Smart Space API: connecting, disconnecting, creating the message and sending the message. As of today, both a discussion message (a message that uses the discussion dialog) and a notification message (a message that uses the Smart Space toast dialog) can be sent using the Java API.

This 9.3.1 code sample shows the three Smart Space messaging operations exposed in the proper format to be used as an Essbase custom defined function.

package com.hyperion.essbase.cdf.smartspace;

import com.hyperion.smartspace.inform.info.*;
import com.hyperion.smartspace.inform.impl.InformImpl;
import java.util.List;
import java.util.ArrayList;

public class SSMessage {
private static InformImpl inform = new InformImpl();
private static String sServer;

public static void main(com.hyperion.essbase.calculator.Context ctx,String[] args)
{
sendMessage(args[0],args[1],args[2]);
}

public static void connect(String sColabServer, String sUser, String sPassword) {
inform = new InformImpl();
try
{
System.out.println("Trying to connect");
sServer = sColabServer;
inform.connect(sServer, 5222, sUser + "\\40native\\20directory@" + sColabServer, sPassword);
}
catch (Exception e1)
{
System.out.println("Error: " + e1.getMessage().toString());
}
}

public static void disconnect() {
try
{
System.out.println("Disconnecting");
inform.disconnect();
inform = null;
}
catch (Exception e1)
{
System.out.println("Error: " + e1.getMessage().toString());
}
}

public static void sendMessage(String sType, String sToUser, String sMessage) {
MessageType mType = new MessageType();

try
{

if (sType.toUpperCase().equals("DISCUSS")) {
mType = MessageType.Chat;
} else if (sType.toUpperCase().equals("MEET")) {
mType = MessageType.GroupChat;
}
else
{
mType = MessageType.Headline;
}

List recipients = new ArrayList();
recipients.add(sToUser + "\\40native\\20directory@" + sServer);

//send created message
if (inform.isConnected() == true)
{
//create jabber message with recepients list and type
Message message = inform.createMessage("t", "t", recipients, mType);
//create body
message.setBody(sMessage);

inform.sendMessage(message, recipients);
System.out.println("Message sent");
}
else
{
System.out.println("Message not sent");
}

}
catch (Exception e1)
{
System.out.println("Error: " + e1.getMessage().toString());
}
}
}

Once this code has been compiled and registered with Essbase as a CDF, the functions can then be accessed from with an Essbase calculation script. This example script sends a discussion message for every value in the database where there is a negative variance for January Sales using the Sample Basic Essbase database. It also shows how to send a notification message to any user.

Set UpdateCalc Off;Set ClearUpdateStatus After;

RUNJAVA com.hyperion.essbase.cdf.smartspace.SSConnect
"servername"
"essbaseBOT"
"password";

RUNJAVA com.hyperion.essbase.cdf.smartspace.SSMessage
"DISCUSS"
"mlarimer"
"The following items currently have a negative variance to budget for Jan Sales:" ;


Fix (@LevMbrs("Market",0),@LevMbrs("Product",0),"Jan","Actual")

"Sales" (IF("Variance" <>
@JechoString(@Name(@CurrMbr("Market")));
@JsendMessage( "DISCUSS", "mlarimer", @JgetConcatenate(@LIST(@Name(@CurrMbr("Market"))," -> ", @Alias(@CurrMbr("Product"))," = ", @JgetString("Variance"))));
ENDIF);
EndFix

RUNJAVA com.hyperion.essbase.cdf.smartspace.SSMessage "NOTIFY" "mlarimer" "one" ;
RUNJAVA com.hyperion.essbase.cdf.smartspace.SSMessage "NOTIFY" "mlarimer" "two" ;
RUNJAVA com.hyperion.essbase.cdf.smartspace.SSMessage "NOTIFY" "mlarimer" "three" ;

RUNJAVA com.hyperion.essbase.cdf.smartspace.SSDisconnect;


Note: This example is very simplistic as it uses a hardcoded username and password in the script itself. There are a number of ways to use variables that can replace the username and password and thus not have them hardcoded in the script.

I am really excited about the future of a product like Oracle EPM Smart Space. It will enable developers to build cool new solutions that have never been possible to build in the past.


No comments: