Reduce Redundant Code with Custom Base Classes
Platform: Sales and Service | 0 Comments 04.20.2009 Jason Balliet
Overview
Over the course of an implementation, you may need to create several different versions of similar extension types to solve different problems. Within each of those extensions, there are usually a handful of preparation steps that are performed in each version. In most cases, you would simply rewrite each step every time. But rewriting code each time increases the potential for errors and for inconsistencies in handling of those steps.
The Custom Base Class is the Answer
The answer to creating consistency and mitigating potential errors is to create custom base class for each of the primary extension types, allowing the redundant code to be placed in a single place. As an example, let’s use an Action extension. Just like any other base class, we simply create a class that is an extension of the standard E.piphany base class. For the cases of this example, we will use an underscore (_) as the prefix to a variable name to represent a global variable defined at the parent class.
package com.sample.extension.epny.extension.base;
// Standard EPNY Class References
import com.epiphany.shr.ui.action.ActionExtensionBase;
import com.epiphany.shr.ui.action.ActionContext;
import com.epiphany.shr.ui.action.ActionResult;
import com.epiphany.shr.ui.state.StateInterface;
import com.epiphany.shr.ui.model.data.UnitOfWorkBean;
import com.epiphany.shr.ui.model.data.DataBean;
import com.epiphany.shr.ui.view.RuntimeFormInterface;
import com.epiphany.shr.util.exceptions.EpiException;
import com.epiphany.shr.sf.util.EpnyUserContext;
import com.epiphany.shr.data.bio.UnitOfWork;
public class SampleActionExtensionBase extends ActionExtensionBase
{
// Declare Scope Level Objects
protected StateInterface _state = null;
protected UnitOfWorkBean _uowb = null;
protected UnitOfWork _uow = null;
protected RuntimeFormInterface _targetForm = null;
protected DataBean _beanFocus = null;
protected EpnyUserContext _userContext = null;
protected int execute(ActionContext actionContext, ActionResult actionResult) throws EpiException
{
// Manufacture the EPNYUserContext
_userContext = actionContext.getServiceManager().getUserContext();
// Get State and Unit of Work Bean
_state = actionContext.getState();
_uowb = _state.getDefaultUnitOfWork();
_uow = _uowb.getUOW();
// Determine Current Runtime Form
RuntimeFormInterface form = _state.getCurrentRuntimeForm();
if (form.getParentForm(_state) != null)
{
_targetForm = form.getParentForm(_state);
}
else
{
_targetForm = form;
}
// Get Current BEAN focus from Runtime Form
_beanFocus = _targetForm.getFocus();
// Execute Parent Class Action
return super.execute(actionContext, actionResult);
}
In our example above, we preset some of the most commonly used objects:
* _userContext — The user’s context variable derived from the current servicer manager
* _state — The standard StateInterface
* _uowb — The UnitOfWorkBean for user interface methods
* _ouw — The standard UnitOfWork for BIO interface methods
* _targetForm — The parent form of the action widget
* _beanFocus — The databean associated with the parent form
Inherit from the New Base Class
Now that you have your custom base class, you can use it for each an every Action extension instance.
package com.sample.extension.opportunity.ui;
// Import the new Custom Base Class
import com.sample.extension.epny.extension.base.SampleActionExtensionBase;
// Declare the Class with the new Custom Base Class
public class SampleButtonAction extends SampleActionExtensionBase
{
protected static ILoggerCategory _log = LoggerFactory.getInstance(SampleButtonAction.class);
protected static String CLASS_NAME = "SampleButtonAction";
protected int execute(ActionContext actionContext, ActionResult actionResult) throws EpiDataException, EpiException
{
// Local Variables
String METHOD_NAME = "execute";
// Execute Parent Class to Initalize
super.execute(actionContext, actionResult);
// You can use the following objects that are pre-built
/*****************************
* --> _state
* --> _beanFocus
* --> _targetForm
* --> _uowb
* --> _uow
*****************************/
debug(METHOD_NAME, _targetForm.getName());
debug(METHOD_NAME, _beanFocus.getDataType());
// Custom Processing Steps, your Code goes here
:
:
}
As the extension above shows, the new version of the extension can simplify each instance and allow the base class to share the common processing across all instances. The use of the custom base class removes the complexity of each instance and adds consistency to every instance.








