Using global variables in business logic

If your ZIETrans application uses global variables to store information, you can use these global variables in your business logic.

There are two types of global variables: local and shared. A local global variable is one that is created within a ZIETrans project and is only visible to the project. A shared global variable is one that is visible to and can be used by all the applications in an EAR file. There are also two lists of ZIETrans global variables, one for local global variables and one for shared global variables. Two global variables with the same name can coexist if one is local and the other is shared.

When you create your business logic class, use the Create Business Logic wizard and select the Create global variable helper methods check box. This creates methods in your business logic for getting, setting, and removing local and shared global variables.

The following methods are created:
////////////////////////////////////////////////////////////////////////////////
//  This sample is provided AS IS.
//  Permission to use, copy and modify this software for any purpose and
//  without fee is hereby granted. provided that the name of IBM not be used in
//  advertising or publicity pertaining to distribution of the software without
//  specific written permission.
//
//  IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SAMPLE, INCLUDING ALL
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL IBM
//  BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
//  DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
//  IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
//  OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SAMPLE.
////////////////////////////////////////////////////////////////////////////////
/**
	* Example method that sets a named global variable 
	* from the current session to a value
	* @param blInfo - IBusinessLogicInformation from current session
	* @param name - Name of the global variable
	* @param value - Value of the global variable
	*/
public static void setGlobalVariable(IBusinessLogicInformation blInfo,
					String name,Object value)
{
		IGlobalVariable gv = blInfo.getGlobalVariable(name);
		if ( gv == null )
			{
				gv = new GlobalVariable(name,value);
			}
		else
			{
				gv.set(value); 
			}
		blInfo.getGlobalVariables().put(name,gv);
}

/**
	* Example method that sets a named shared
	* global variable from the current session to a value
	* @param blInfo - IBusinessLogicInformation from current session
	* @param name - Name of the shared global variable
	* @param value - Value of the shared global variable
	*/ 
public static void setSharedGlobalVariable(IBusinessLogicInformation 
		blInfo,String name,Object value)
{
		IGlobalVariable gv = blInfo.getSharedGlobalVariable(name);
		if ( gv == null )
			{
				gv = new GlobalVariable(name,value);
			}
		else
			{
				gv.set(value); 
			}
		blInfo.getSharedGlobalVariables().put(name,gv);
}

/**
	* Example method that removes a named global variable from the current session
	* @param blInfo - IBusinessLogicInformation from current session
	* @param name - Name of the global variable
*/
public static void removeGlobalVariable(IBusinessLogicInformation blInfo, String  name)
{
		IGlobalVariable gv = blInfo.getGlobalVariable(name);
		if ( gv != null )
			{
				blInfo.getGlobalVariables().remove(name);
				gv.clear();
				gv = null;
			}
}
/**
	* Example method that removes a named shared global variable from the current session
	* @param blInfo - IBusinessLogicInformation from current session
	* @param name - Name of the shared global variable
*/
public static void removeSharedGlobalVariable(IBusinessLogicInformation blInfo, String name)
{
		IGlobalVariable gv = blInfo.getSharedGlobalVariable(name);
			if ( gv != null )
				{
					blInfo.getSharedGlobalVariables().remove(name);
					gv.clear();
					gv = null;
				}
}
/**
	* Example method that retrieves a named global variable from the current session
	* @param blInfo - IBusinessLogicInformation from current session
	* @param name - Name of the global variable
	* @return - an instance of the global variable, or null if not found.
	*/
public static IGlobalVariable getGlobalVariable(IBusinessLogicInformation 
		blInfo,String name)
{ 
		IGlobalVariable gv = blInfo.getGlobalVariable(name);
		return gv;
}

/**
	* Example method that retrieves a named shared 
	* global variable from the current session
	* @param blInfo - IBusinessLogicInformation from current session
	* @param name - Name of the shared global variable
	* @return - an instance of the global variable, or null if not found.
	*/
public static IGlobalVariable getSharedGlobalVariable(IBusinessLogicInformation 
		blInfo,String name)
{ 
		IGlobalVariable gv = blInfo.getSharedGlobalVariable(name);
		return gv;
}
Elsewhere in your code, when you need the value of a local global variable, you can call this method:
  GlobalVariable gv1 = getGlobalVariable(blInfo,"varname");
To get the value of a shared global variable, use the following method:
  GlobalVariable gv1 = getSharedGlobalVariable(blInfo,"varname");