Sunday, November 17, 2013

Websphere Commerce: Command Caching and DynaCache

Java objects and the command cache

DynaCache provides support for caching the returned results from Java™ object calls. DynaCache provides a very straightforward mechanism that allows you to cache the results from these Java command objects; it is called command caching.

Commands written to the WebSphere® command architecture access databases, file systems, and connectors to perform business logic, and often run remotely on another server. Significant performance gains can be achieved by caching command results and avoiding their repeated invocation.

To take advantage of command caching, applications must be written to the WebSphere command framework API. This API is based on a common programming model, and uses a series of getters(), setters() and execute() methods to retrieve its results. To use a command, the client creates an instance of the command and calls the execute() method for the command. Depending on the command, calling other methods could be necessary. The specifics will vary with the application.

With Command cache, the data is cached before it is transformed into HTML. In order to take advantage of command caching, you must make simple code changes to your Java objects so that DynaCache can call them to retrieve the data. Command objects inherit from com.ibm.websphere.command.cachableCommandImpl and must provide certain methods to function properly.

Executed command objects can be stored in the cache so that subsequent instances of that command object's execute method can be served by copying output properties of the cached command to the current command for retrieval by its get methods. DynaCache supports caching of command objects for later reuse by servlets, JSPs, EJBs, or other business logic programming.


Interface (Example):

public interface CustomerRepsRetrieveCmd extends TargetableCommand{
    // set default command implement class
    static final String defaultCommandClassName =  "com.abccompany.commerce.commands.CustomerRepsRetrieveCmdImpl";
    /* property getters and setters go here */
    public String getCustomerId();
    public void setCustomerId(String customerId);  
    public List getCustomerRepsList();
}


Implementation Class (Example):

public class CustomerRepsRetrieveCmdImpl extends CacheableCommandImpl implements CustomerRepsRetrieveCmd {
    
     private String customerId; 
     private List customerRepsList;
 
     // called to validate that command input parameters have been set
     public boolean isReadyToCallExecute() {
          return (getCustomerId() != null && !"".equals(getCustomerId()));
     }
    
     // called by a cache-hit to copy output properties to this object
     public void setOutputProperties(TargetableCommand cmd) {
          CustomerRepsRetrieveCmd repsRetrieveCmd = (CustomerRepsRetrieveCmd) cmd;
          setCUstomerRepsList(repsRetrieveCmd.getCustomerRepsList());
     }
 
     public void performExecute() throws Exception {          
          /* business logic code goes here */
  
          ABCSession abcSession = Utilities.getABCSession();
          if(getCustomerId() != null && !"".equals(getCustomerId())) {
             setCustomerRepsList(abcSession.getCustomerReps(getCustomerId()));
          } else {
             setCustomerRepsList(new ArrayList());
          }
     }
 
     /* property getters and setters go here */
 
     public List getCustomerRepsList() {
          return customerRepsList;
     }
 
     protected void setCustomerRepsList(List repsList) {
          this.customerRepsList = repsList;
     }
 
     public String getCustomerId() {
          return customerId;
     }
 
     public void setCustomerId(String customerId) {
          this.customerId = customerId;
     }
}

Cacheable objects are also defined in the cachespec.xml file found inside the Web application archive (WAR) , Web module WEB-INF directory, or enterprise bean WEB-INF directory. A global cachespec.xml file can also be placed in the application server properties directory, but the recommended method is to place the cache configuration file with the deployment module.



Info pulled from the IBM documentation:

Command cache

CacheableCommandImpl

CacheableCommand

Configuring the dynamic cache service in cachespec.xml






Saturday, November 16, 2013

Websphere Commerce: Setting the view in a ControllerCommand

In the performExecute() method of the controller command, set the view in the response properties like:

     TypedProperty responseProperties = getResponseProperties();
     responseProperties.put(ECConstants.EC_VIEWTASKNAME, "MyNewView"); 
     setResponseProperties(responseProperties);


     OR

     TypedProperty responseProperties = getResponseProperties();
     responseProperties.put(“viewTaskName”, "MyNewView"); 
     setResponseProperties(responseProperties);



 When redirecting the view to the same jsp page do:

         responseProperties.put("redirecturl", "CustomerAccountForm");
         responseProperties.put("viewTaskName", "RedirectView");
         setResponseProperties(responseProperties);


where: CustomerAccountForm is the URL mapped to the same jsp page, i.e. CustomerAccount.jsp


For Commerce built- in commands (blackbox):

See IBM infocenter’s documentation for the command’s implementation class (cmdimpl).

Example -  RequisitionListDisplayCmdImpl:

IBM Documentation states that  for the View:
  • If requisitionListId is specified, it sets RequisitionListDetailView.
  • If requisitionListId is not specified, it sets RequisitionListView.

So, in order to return a view for this command, you need to map either the RequisitionListView or RequisitionListDetailView url to a jsp file in your configuration file (such as struts-config.xml file).

DOJO: Destroy then create a ContentPane

There may be instances where you may need to destroy an existing dojo ContentPane then replace it with a new ContentPane and place it in the same container div element.

Since all of this may also occur after the DOM (web page) has been rendered, you will also need to parse the widget to execute any javascipt code you may have in the new ContentPane. An example scenario is when your new ContentPane invokes a JSP page with a javascript code for an ajax call (nested ajax calls).

Here is a simple javascript function to destroy then create a ContentPane.

function destroyCreateContentPane() {
      
      // destroy the existing content pane 
      dijit.byId("cpId").destroyRecursive();
 
      // get the containing div element
      var containerDiv = dojo.byId("divContId");
 
      // create the new content pane (widget) and set the values for it's id and class properties
      var cpWidget = new dojox.layout.ContentPane({
                  id:"cpId",
                  class:"cpClass",
      });
 
      // use the contentpane's 'domNode' property to get the top level DOM node of the new contentpane widget (the visible representation of the widget)  
      var cpDiv = cpWidget.domNode;
 
      // put the new contentpane div as the last node of the containing div 
      dojo.place(cpDiv, containerDiv, 'last');
      cpDiv.id = "cpId";
 
      // optional: hide the new empty div
      cpDiv.style.display = "none";
 
      // parse the new contentpane widget
      dojo.parser.parse(dojo.byId('cpId'));
}

Sunday, November 3, 2013

Javascript: onfocus event and focus() method

onfocus in HTML

The onfocus event occurs when an element gets focus.

onfocus is often used with <input>, <select>, and <a>.

Check out the onfocus event here.

HTML Example:

<input type="text" onfocus="yourFunction()">


You may also pass the 'input' element as a parameter to your onfocus handler's function using the keyword 'this'.

<input type="text" onfocus="yourFunction(this)">


JavaScript Example:

HTMLElementObject.onfocus = function(){ your javascript code here }


The onfocus attribute can be used within ALL HTML elements, EXCEPT: <base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, and <title>.

So in javascript, you can also use the focus() method on any HTML element except on what’s listed above.


focus() Method

The focus() method is used to give focus to a HTML element.

Syntax:

HTMLElementObject.focus()

Example:

<script type="text/javascript">
  document.getElementById("targetElementId").focus();
</script>

Check out the focus() method here.

CSS: Showing the horizontal and vertical scrollbars

It is pretty easy to show the horizontal and vertical scrollbars for block DOM elements such as a DIV.

You just need to set the following CSS properties for your div:
  • width
  • height
  • overflow
You may also use the overflow-x property to show or hide the horizontal scrollbar and the overflow-y property to show or hide the vertical scrollbar.

Just use the following class with the style rules to show the horizontal and vertical scrollbars.

.scrollableContainer {
  background-color: #FCFCDD !important; 
  font-family: Consolas, Menlo, Monaco, Lucida Console, Liberation Mono, DejaVu Sans Mono, Bitstream Vera Sans Mono, Courier New, monospace, serif; 
  font-size:.9em; 
  padding: 10px; 
  width: auto;  
  height: auto;
  max-height: 500px; 
  overflow: auto; 
  word-wrap: normal;
  white-space: nowrap; 
}


Or you may apply it inline like this:

<div style="background-color: #FCFCDD; font-family: courier; font-size:.9em; padding: 10px; width: auto; height: auto; max-height: 500px; overflow: auto; word-wrap: normal; white-space: nowrap;" >
</div>