Nature of Aspire: CollectionToBooleanConverter

satya - Tuesday, February 14, 2012 10:23:54 AM

Source code


package com.ai.filters;

import com.ai.application.interfaces.*;
import com.ai.common.UnexpectedTypeException;
import com.ai.data.*;
import java.util.*;
import com.ai.application.utils.*;

/**
 * A filter to convert a collection to a boolean result
 * If the set is empty decide to return true/false
 *
 * Config format:
 *    request.name.classname=EmptySetValidator
 *    request.name.onEmpty=[true|false:true]
 *
 * inputs: IDataCollection
 * outputs: true if the set is empty
 *          exception if not
 *
 * multiplicity: Singleton
 * No instance variables are allowerd
 * 
 * Based On
 * ********
 * NonEmptySetValidator
 * 
 * See
 * **********
 * com.ai.filter package
 * 
 */
public class CollectionToBooleanFilter implements ICreator
{
    public Object executeRequest(String requestName, Object args)
      throws RequestExecutionException
   {
      // The argument is an IDataCollection
      if (!(args instanceof IDataCollection ))
      {
         AppObjects.warn("Wrong type of object passed in : %1\n%2" 
                 ,args.getClass().getName()
                 ,"Expecting a class of type : com.ai.data.IDataCollection"
                 );
         throw new RequestExecutionException(
                 "Unexpected data type. Expecting a data collection");
      }
      IDataCollection col = null;
      try
      {
         boolean onEmpty = this.getOnEmpty(requestName);
         col = (IDataCollection)args;
         IIterator itr = col.getIIterator();
         itr.moveToFirst();
         if (!(itr.isAtTheEnd()))
         {
            // non empty data set
            // rows exist, it is OK
            return new Boolean(onEmpty);
         }
         //  not an empty data set
         //  No rows exist
         return new Boolean(!onEmpty);
      }
      catch(com.ai.data.DataException x)
      {
         AppObjects.log(x);
         throw new RequestExecutionException("Data Exception",x);
      }
      catch(com.ai.common.UnexpectedTypeException x)
      {
         AppObjects.log(x);
         throw new RequestExecutionException(
                 "Trying to convert a boolean string to boolean",x);
      }
      finally
      {
         try {col.closeCollection();}
         catch(DataException x){ AppObjects.log(x); }
      }
   }
   boolean getOnEmpty(String requestName) 
   throws UnexpectedTypeException
   {
       String message = 
           AppObjects.getIConfig().getValue(requestName + ".onEmpty",
                   "false");
       return FilterUtils.convertToBoolean(message);
   }
}//eof-class

satya - Tuesday, February 14, 2012 10:40:27 AM

How do you indicate your filter


request.requestName.classname=x
request.requestName.filterName=filterNameY
...
...

request.filterNameY.classname=somepackage.filterYClassname
request.filterNameY.arg1=aaa
request.filterNameY.arg2=bbb

satya - Tuesday, February 14, 2012 10:42:01 AM

Here is an example


#validate uniqueness
request.insertProjectvalidateUniqueness.className=com.ai.db.DBRequestExecutor1
request.insertProjectvalidateUniqueness.stmt= \
   SELECT project_name \
   FROM   projects \
   WHERE    project_name = {prj_name.quote}
request.insertProjectvalidateUniqueness.filterName=insertProjectAssertEmptySet

request.insertProjectAssertEmptySet.className=com.ai.filters.EmptySetValidator
request.insertProjectAssertEmptySet.exception_message=\
   duplicateproject:Project already exists