July 19, 2013

Custom Java Action Class Example

This entry is part of the Maximo Java Development series.

In a previous post I have described how create a custom action class using Java.

Here is a full example that can help to understand how to navigate objects and set values.

package cust.psdi.app.workorder;

import java.rmi.RemoteException;

import psdi.common.action.ActionCustomClass;
import psdi.mbo.MboConstants;
import psdi.mbo.MboRemote;
import psdi.mbo.MboSetRemote;
import psdi.util.MXException;

/**
 * Sets the Workorder description with a new one obtained concatenating the
 * following related descriptions:
 * - :JOBPLAN.DESCRIPTION
 * - :PM.LOCATIONS.DESCRIPTION
 * - :PM.ASSET.DESCRIPTION
 * 
 * @author Bruno Portaluri
 */
public class ActionSetDescriptionFromPm implements ActionCustomClass
{
  public ActionSetDescriptionFromPm()
  {
    super();
  }

  public void applyCustomAction(MboRemote mbo, Object[] params) throws MXException, RemoteException
  {
    String desc = "";
    
    // Get the description of the Job Plan
    MboSetRemote jp = mbo.getMboSet("WO_JOBPLAN");
    if (jp.getMbo(0)!=null)
    {
      desc = desc + jp.getMbo(0).getString("DESCRIPTION");
    }

    
    // Get the description of the Job Plan
    MboSetRemote pm = mbo.getMboSet("WO_PM");
    
    if (pm.getMbo(0)!=null)
    {
      // Get the description of the PM's Location
      MboSetRemote locations = pm.getMbo(0).getMboSet("LOCATIONS");
      if (locations.getMbo(0)!=null)
      {
        desc = desc + " - " + locations.getMbo(0).getString("DESCRIPTION");
      }
      
      // Get the description of the PM's Asset
      MboSetRemote asset = pm.getMbo(0).getMboSet("ASSET");
      if (asset.getMbo(0)!=null)
      {
        desc = desc + " - " + asset.getMbo(0).getString("DESCRIPTION");
      }
    }

    // Truncates to fit the field length
    desc = desc.substring (0, 100);
    
    System.out.println("Setting WO's " + mbo.getString("WONUM") + " description:" + desc);
    
    mbo.setValue("DESCRIPTION", desc, MboConstants.NOACCESSCHECK|MboConstants.NOVALIDATION_AND_NOACTION);
  }
}


6 comments:

  1. Very Useful post Bruno. Thank you very much for sharing your knowledge with us :)

    ReplyDelete
  2. Very good example.

    I would replace if (xxx.getMbo(0) != null) for if (!xxx.isEmpty()), it is clearer to understand and that makes the source code a little more legible. I know it is not exactly the same, but for practical purposes these two "ways" can be used interchangeably.

    Also, I tend to use variable names ending with "set" when working with MboSets. Another alternative is using "plural" for sets and "singular" for Mbos when naming variables. Typically these differences come handy when you want to iterate a set and assign the "current" Mbo inside the loop to a local variable.

    I do tend to think these conventions also make the code a little more easy to understand (even by yourself in the future) ;-)

    ReplyDelete
  3. This comment has been removed by a blog administrator.

    ReplyDelete
  4. Very nice post here and thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great content of different kinds of the valuable information's. 
    best rpa training in bangalore
    rpa training in bangalore | rpa course in bangalore
    RPA training in bangalore
    rpa training in chennai
    rpa online training

    ReplyDelete
  5. Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
    python Course in Pune
    python Course institute in Chennai
    python Training institute in Bangalore

    ReplyDelete
  6. This is a nice post in an interesting line of content.Thanks for sharing this article, great way of bring this topic to discussion.
    AWS Training in Bangalore

    AWS Training in pune

    ReplyDelete