db-torque-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jeff Rasmussen <jrasmus...@lnxi.com>
Subject Abstraction of doSelect() methods
Date Fri, 09 Jan 2004 01:12:59 GMT
I have been using torque and Struts on a large scale project and am
trying to set up a helper class to minimize code.

I would like to dynamically call actions (.doSelect(criteria) , .save(),
deDelete(criteria) methods) by passing the criteria to another class.

I have gotten the inserts and deletes to work but I am having problems
with the selects?? This is weird because it does not correctly build the
criteria for the select but it will for the delete.

I would love suggestions on how to better implement this.

Here is my current code (I have commented what is going on):


package com.lnxi.mfg.utils;

import org.apache.struts.action.*;
import org.apache.struts.validator.*;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.util.*;
import com.lnxi.mfg.utils.*;
import com.lnxi.mfg.om.*;
import org.apache.torque.om.BaseObject;
import org.apache.torque.util.BasePeer;
import org.apache.torque.Torque;
import org.apache.torque.util.Criteria;

public abstract class LNXIAction extends Action{
  private BaseObject localObject = null;
  private BasePeer localPeerObject = null;
  private Criteria selectCrit = new Criteria();
  private Criteria deleteCrit = new Criteria();
  
  // getters and setters for LocalObject
  // localObject is the torque generated classes
  // Ex. User.java
  public BaseObject getLocalObject(){ return localObject;}
  public void setLocalObject(BaseObject o) { this.localObject = o; }
  public void clearLocalObject() { this.localObject = null; }
  
  // getters and setters for LocalPeerObject
  // localPeerObject is the torque generated peer classes
  // Ex. UserPeer.java
  public BasePeer getLocalPeerObject(){ return localPeerObject;}
  public void setLocalPeerObject(BasePeer peer) { this.localPeerObject =
peer; }
  public void clearLocalPeerObject() { this.localPeerObject = null; }
  
  // some setters for different criteria
  public void clearSelectCriteria() { this.selectCrit = new Criteria();}
  public Criteria getSelectCriteria(){ return this.selectCrit; }
  public void setSelectCriteria(Criteria selectCrit) {
    this.clearSelectCriteria();
    this.selectCrit = selectCrit;
  }
  
  public void clearDeleteCriteria() { this.deleteCrit = new Criteria();}
  public Criteria getDeleteCriteria(){ return this.deleteCrit; }
  public void setDeleteCriteria(Criteria deleteCrit) { 
    this.clearDeleteCriteria();
    this.deleteCrit = deleteCrit;
  }
  
  // actionForward used by Struts
  public ActionForward lnxiExecute(ActionMapping mapping, ActionForm
form, HttpServletRequest request, HttpServletResponse response) throws
IOException, ServletException{  
    ActionErrors errors = new ActionErrors();
    
    //Application Constatnts and simplified dropdown Object (select all
from table)
    Constants constants = new Constants();
    DropDownObjects ddo = new DropDownObjects();
    
    Criteria crit = new Criteria();
    HttpSession session = request.getSession();
    
    // reads the action from the submitted form
    // defaults to dropdown
    String action = "dropdown";
    
    // if the form was submitted then set action from the form
    if(null != form){
      action = (String) ((DynaValidatorForm) form).get("action");
    }
    
    // if action = null redirect to error
    if((action==null) || (action.equalsIgnoreCase(""))){
      errors.add(ActionErrors.GLOBAL_ERROR, new
ActionError("error.missing.action"));
      saveErrors(request, errors);
      return (new ActionForward(mapping.getInput()));
    }            
     
    // Handle insert actions
    else if(action.equalsIgnoreCase("insert")){
       try{
        // make sure torque has been initialized
        constants.initTorque();
        
        // set the BaseObject
        // this is a pre populated object that is set with
setLocalObject
        // This code Works
        
        //     Torque inheritance model
        
        //      BaseObject -->>   Base<TableName> -->> <TableName>
        //      BasePeer -->> Base<TableName>Peer -->> <TableName>Peer
        
        BaseObject nu = this.getLocalObject();
        nu.save();

        request.setAttribute("results","inserted");
      }catch(Exception e){
        e.printStackTrace();
        session.setAttribute("error",e.getMessage());
        return mapping.findForward("error");
      }
      return mapping.findForward("insertSuccess");
    }
    
    // handle Edits
    // takes the id and pre-populates the edit jsp form
    else if(action.equalsIgnoreCase("edit")){
      
       try{
        constants.initTorque();
        // This does not work
        // The criteria leaves out the column names but everey thing
else is correct
        // Ex. SELECT  FROM TABLE WHERE TABLE.TABLE_ID=1
        // SHOULD BE 
        // SELECT TABLE.TABLE_ID FROM TABLE WHERE TABLE.TABLE_ID=1
        
        // because the doSelect() method uses the generated classes I
had to add
        // an abstract method doSelect(BaseObject) in BasePeer. It is
defined in
        // the generated method. and casts the Base Object to the
generated class type
        // Ex. 
        //  public void doSelect(BaseObject obj){
        //    this.doSelect((<TABLE CLASS>) obj);
        //  }
        List tmp = (List)
(this.localPeerObject).doSelect(this.getLocalObject());
        BaseObject updateObject = (BaseObject) tmp.get(0);
        
        session.setAttribute("updateObject",updateObject);
        request.setAttribute("results","updated");
      }catch(Exception e){ 
        e.printStackTrace();
        session.setAttribute("error",e.getMessage());
        return mapping.findForward("error"); 
      }
      
      return mapping.findForward("editSuccess");
    }
    
    // actually update data in the db
    // have not tested this but I think it should work :-)
    else if(action.equalsIgnoreCase("update")){
      try{
        constants.initTorque();

        BaseObject nu = this.getLocalObject();
        (this.localPeerObject).doUpdate(nu);

        request.setAttribute("results","edited");
      }catch(Exception e){ 
        e.printStackTrace();
        session.setAttribute("error", e.getMessage());
        return mapping.findForward("error"); 
      }
      return mapping.findForward("updateSuccess");
    }
    
    // handle deletes
    // This works great
    else if(action.equalsIgnoreCase("delete")){
      try{
        constants.initTorque();
        
        (this.getLocalPeerObject()).doDelete(this.getDeleteCriteria());
        
        request.setAttribute("results","deleted");
      }catch(Exception e){ 
        session.setAttribute("error",e.getMessage());
        return mapping.findForward("error"); 
      }
      return mapping.findForward("deleteSuccess");
    }
    
    // handle viewAll
    // have not tested
    else if(action.equalsIgnoreCase("viewAll")){
      /*
      try{
        constants.initTorque();
        BiosConfig retObject = BiosConfigPeer.retrieveByPK(((Integer)
dynaForm.get("biosConfigId")).intValue());
        Vector viewAllResults = new Vector();
        viewAllResults.add(retObject);
        session.setAttribute("viewAllResults", viewAllResults);
      }catch(Exception e){ 
        session.setAttribute("error",e.getMessage());
        return mapping.findForward("error"); 
      }
      */
      return mapping.findForward("viewAllSuccess");
    }
    
    // return error if the action was not found
    session.setAttribute("error","The action "+action+" was not handled
in "+this.getClass());
    return mapping.findForward("error");
  }

}

Mime
View raw message