JHeadstart(10.1.2) – Passing defaults for your bind Parameters

Sometimes you want the query of your view object to be dynamic: you want to be able to
pass an argument (a bind parameter) to the where clause of the query. JHeadstart
includes runtime support for passing such query bind parameters.
But wouldn’t it be nice if you could also pass default values for these query bind Parameters!
In this article, I will discuss two different ways to achieve this.

Solution 1
Use the existing “bindParam” property in your struts-config.xml to pass the default values and change the method applyIterBindParams in the JhsDataAction class to retrieve and set the defaults.

The advantage of this approach/solution is that you only need to change the JhsDataAction class and even more important if you re-generate your application your changes won’t be lost.

Steps to take

1] Specify default values for the bind parameters

You can either do this by modifing your “Query Bind parameters” in your Application Structure file or directly in you struts-config. The first one is of course recommended because you keep your changes after each generation.

Define the bind Param with the default it should get. Specify the default value by appending the colon character at the end of your bind parameter name followed by the value. See below. In your application structure file editor:

AppStructureRob

This is how your bindParam property in your Struts-config.xml looks after generation:

<set -property property="bindParams" value="SkillsMedewerkersIterator=${param.FindSkillsMedewerkersId:-1}" />

2] Change the method applyIterBindParams in the JhsDataAction class

These changes are necessary for retrieving and setting de default values.
code sample:

 protected void applyIterBindParams(DataActionContext daContext)
  {
    ActionMapping mapping = daContext.getActionMapping();

    if (mapping instanceof JhsDataActionMapping)
    {
      String iterBindParams = ((JhsDataActionMapping)mapping).getBindParams();

      if (iterBindParams!=null)
      {
        String[] paramsPerIterBinding = StringUtils.stringToStringArray(iterBindParams,";");

        for (int i = 0; i < paramsPerIterBinding.length; i++)
        {
          String[] keyValuePair 	= StringUtils.stringToStringArray(paramsPerIterBinding[i],"=");
          // could be that keyValuePair[ 0 ] or keyValuePair[ 1 ] do not exist, in that case we skip this part
          if (keyValuePair.length >= 2)
          {
            String iterBinding = keyValuePair[ 0 ];
            String[] bindParams = StringUtils.stringToStringArray(keyValuePair[ 1 ],",");
            //for storing default bind param values
            String[] bindParamValues = new String[bindParams.length];

            JUIteratorBinding ib = getBindingUtils().findIterBinding(daContext.getBindingContainer(), iterBinding);
            //
            if (ib!=null)
            {
              Evaluator evaluator = Evaluator.getEvaluator(daContext);
              ViewObject vo = ib.getRowSetIterator().getRowSet().getViewObject();
              Object[] oldParams = vo.getWhereClauseParams();
              boolean paramsChanged = false;
              //
              for (int j = 0; j < bindParams.length; j++)
              {

                //====>>>> new Default bindParamValue default exists?
                if (bindParams[j].indexOf(":") != -1)
                {
                  //first extract the default bind parameter value and store it
                  bindParamValues[j]= bindParams[j].substring(bindParams[j].indexOf(":")+1,bindParams[j].indexOf("}"));
                  bindParams[j] = bindParams[j].substring(0,bindParams[j].indexOf(":"))+"}";
                }
                //====>>>> end new
                Object value = String.valueOf(evaluator.getValue(bindParams[j]));
                Object oldValue = null;

                //====>>>> new only set value if bind param is empty and the default for this bind is defined
                if(    ( String.valueOf(value).equals("null")
                         ||  String.valueOf(value).trim().length() == 0
                       )
                       && bindParamValues[j] != null
                  )
                {
                  value = String.valueOf(bindParamValues[j]);
                }
                //====>>>> end new

                if ( oldParams!=null && oldParams.length > j )
                {
                   oldValue = oldParams[j];
                  if ((value!=null || oldValue!=null)
                      &&((value==null && oldValue!=null)
                         ||(value!=null && oldValue==null)
                         || !(String.valueOf(value).equals(String.valueOf(oldValue)))
                         )
                      )
                  {
                    vo.setWhereClauseParam(j, String.valueOf(value));
                    paramsChanged = true;
                  }
                }
                else
                {
                  // old param does no exist yet, always set the wehere clause param
                    sLog.debug("ViewObject "+ vo.getName()+": value of bind param "+j+" set to "+value);
                    vo.setWhereClauseParam(j, String.valueOf(value));
                    paramsChanged = true;
                }
              }
              if (!ib.isFindMode())
              {
                if (paramsChanged)
                {
                  ib.executeQuery();
                }
                else
                {
                  sLog.debug("ViewObject "+ vo.getName()+": bind parameter values have not changed");
                }
              }
            }
          }
        }
        daContext.getBindingContainer().refreshControl();
      }
    }

  }

Note: I am using the : sign as separator because = is already used by the the Iterator binding

Solution 2

Create a new ActionMapping property for example ‘bindDefaults’ and use this property in your struts-config for defining your defaults. Furthermore also change the method applyIterBindParams in the JhsDataAction to retrieve and set the defaults.

Using this approach your’re not passing the default values as part of bindParam property value but as a separate variable.

Steps to take

1] Before can use this new property you need to add a new member variable to the JhsDataActionMapping class
and add or generate getters and setters methods for this variable.

	public class JhsDataActionMapping extends DataActionMapping
	{
	  private String bindParams;
	  private String bindDefaults;//====>>>> new

	  //====>>>>  new
 	  public void setBindDefaults(String bindDefaults)
	  {
	    this.bindDefaults = bindDefaults;
	  }
	  //====>>>> new
	  public String getBindDefaults()
	  {
	    return bindDefaults;
	  }
	  .....

Now you can use this property in your struts-config like this :


<set -property property="bindDefaults" value="23,56,67" />


2] Change the method applyIterBindParams in the JhsDataAction class.

You can retrieve these default values in the applyIterBindParams method in the JhsDataAction class in the following
manner

      String      iterBindDefs      = ((JhsDataActionMapping)mapping).getBindDefaults();
      String[]    bindParamDefaults = StringUtils.stringToStringArray(iterBindDefs,",");

I don’t have a working sample for solution 2, but it shouldn’t be to hard to Map the values with the corresponding bind Parameters and set the default values.

This solution is not generator-safe which means that you lose your custom property when you re-generate
your application.