14-Apr-04 (Created: 14-Apr-04) | More in 'Howto-Advanced'

Writing a factory part: a sample

Original code

See how the following code is better done in the next section


package com.indent;

import com.ai.htmlgen.*;
import com.ai.application.utils.*;
import com.ai.application.interfaces.*;
import com.ai.data.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.*;
import java.lang.*;

public class loopEvents extends AFactoryPart
{
  public loopEvents() {
  }
  protected  Object executeRequestForPart(String requestName, Map inArgs) throws RequestExecutionException
  {
     String schid = "";
     try
      {
        String request1 = AppObjects.getValue(requestName + ".actualRequest");
         Object obj = AppObjects.getIFactory().getObject(request1, inArgs);

         IDataCollection myRows = (IDataCollection)obj;

         IIterator myItr = myRows.getIIterator();

         for (myItr.moveToFirst(); !myItr.isAtTheEnd();myItr.moveToNext())
         {
           Hashtable args = new Hashtable();
           IDataRow rowData = (IDataRow)myItr.getCurrentElement();
            String row = (String)rowData.getValue(0);
            schid=row;
            args.put("schid",schid);

        RequestExecutorResponse r =(RequestExecutorResponse)AppObjects.getIFactory().getObject("sendschemail",args);
          }
      }
      catch(RequestExecutionException x)
      {
         AppObjects.log("Could not execute a factory service");
         AppObjects.log(x);
      }
      catch(DataException x)
      {
         AppObjects.log("Some kind of data related error occured");
         AppObjects.log(x);
      }
      catch(com.ai.application.interfaces.ConfigException cex)
      {
         AppObjects.log("Could not read requests.");
      }
     return schid;
}
}

Better handling of exceptions and closing resources


package com.indent;

import com.ai.htmlgen.*;
import com.ai.application.utils.*;
import com.ai.application.interfaces.*;
import com.ai.data.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.*;
import java.lang.*;

public class loopEvents extends AFactoryPart
{
  public loopEvents() {
  }
  protected  Object executeRequestForPart(String requestName, Map inArgs) throws RequestExecutionException
  {
     String schid = "";
     IDataCollection myRows = null;
     try
      {
        String request1 = AppObjects.getValue(requestName + ".actualRequest");
         Object obj = AppObjects.getIFactory().getObject(request1, inArgs);

         myRows = (IDataCollection)obj;
         IIterator myItr = myRows.getIIterator();

         for (myItr.moveToFirst(); !myItr.isAtTheEnd();myItr.moveToNext())
         {
           Hashtable args = new Hashtable();
           IDataRow rowData = (IDataRow)myItr.getCurrentElement();
            String row = (String)rowData.getValue(0);
            schid=row;
            args.put("schid",schid);

	    RequestExecutorResponse r =(RequestExecutorResponse)AppObjects.getIFactory().getObject("sendschemail",args);
          }
       return schid;
      }
     catch(DataException x)
      {
      	throw new RequestExecutionException("Data exception",x);
      }
      catch(com.ai.application.interfaces.ConfigException cex)
      {
      	throw new RequestExecutionException("Config problem",cex);
      }
      finally
      {	
      	if (myRows != null) myRows.close();
      }
}
}