Hi

Just some thoughts. You could use the delegate pattern instead of
inheritence. This will help decouple your FormBeans from your DaoBeans.

Keith



-----Original Message-----
From: Struts Newsgroup [mailto:@[EMAIL PROTECTED]]
Sent: Thursday, 24 January 2002 9:10 a.m.
To: [EMAIL PROTECTED]
Subject: Re: Model Persistence Survey


Subject: Re: Model Persistence Survey
From: Vic Cekvenich <[EMAIL PROTECTED]>
 ===


Here is a sample DAO Extended Form Bean code :

public class NameZoomFrm extends daoFormBean {

private String fstName;
private String lstName;


public String getFstName() {   //get it from _crs RowSet
         CachedRowSet cr = getCrs();
        try{   this.fstName = cr.getString("first_name");  } // try
         catch (Exception e)   {System.out.println(e);} // catch
         return (this.fstName);  }// get
public String getLstName() {  //get it from _crs RowSet
         CachedRowSet cr = getCrs();
        try{  this.lstName = cr.getString("last_name");    } // try
         catch (Exception e)   {System.out.println(e);} // catch
         return (this.lstName);
     }// get
  public void setFstName(String aFstName) {   //get it from _crs RowSet
        CachedRowSet cr = getCrs();
        try{ cr.updateString("first_name",aFstName);
        this.fstName = aFstName;
        } // try
        catch (Exception e)   {System.out.println(e);} // catch
        setCrs(cr);
} //setFst
public void setLstName(String aLstName) {   //get it from _crs RowSet
        CachedRowSet cr = getCrs();
        try{ cr.updateString("last_name",aLstName);
        this.lstName=aLstName;
         } // try
        catch (Exception e)   {System.out.println(e);} // catch
        setCrs(cr);
} //setFst


public int retrieve(BigDecimal aBD) {
try{
     CachedRowSet cr = getCrs();
     cr = new CachedRowSet(); // easier then resultset
     // you must test this sql string manualy for a SQL IDE
     cr.setCommand("select * from contact where id = ?");
     // driver issues fix:
     cr.setTableName("contact");
      // to limit the number of rows coming back, we only need a page at
a time
      cr.setBigDecimal(1,aBD);
       exec(cr);
} // try
catch (Exception e)
{ System.out.println(e);
   e.printStackTrace();} // catch
return 0;
}// retrieve

public void blank()
{  try { CachedRowSet cr = getCrs();
     cr.updateNull("first_name");
     cr.updateNull("last_name");
     cr.updateNull("id");
    }  catch (Exception e)   { procE(this,e);} // catch
}// blank
}//class


Here is the DAO implementation ... using RowSet, but could use JDO, such
as Castor.

import com.wintecinc.struts.action.ValidatorForm;
public class daoFormBean extends ValidatorForm  {


     private Connection con=null;
     private Connection upCon=null;
     private DataSource ds;
     private CachedRowSet _crs;
     private String sqlString;
     private BigDecimal PK;


     public Connection getCon() {return con;}
     public CachedRowSet getCrs() {return this._crs;}
     public void setCrs(CachedRowSet aCRS) {this._crs=aCRS;}



         public Hashtable getIterateMap() { //is there a better way?
like use crs.getCollection?
             CachedRowSet cr = getCrs();
             int rc=cr.size();
             Hashtable iterateMap = new Hashtable();
             // count out the number of rows with dummy map
             for (int i=1; i <= rc; i++) { iterateMap.put(new
Integer(i),new Integer(i));}
             return iterateMap;}


             public BigDecimal getPK() {  //get it from _crs RowSet
                 try{  this.PK = _crs.getBigDecimal("id");    } // try
                 catch (Exception e)   {System.out.println(e);} // catch
                 return (this.PK);
             }// get

             public void dbCon() {  // connect to db
                 try{  ds = PoolMan.findDataSource("jndiSample");
                 //we should in production give it a password in code
not in file pool.xml
                 con=ds.getConnection();}
                 catch (Exception e)  {
                     debug(this, "We did not connect");
                     procE(this,e);} // catch
             }// con

             public void dbDisCon() {
                 try{if (con!=null) con.close();
                 ds=null;
                 }
                 catch (Exception e) {System.out.println(e);  }//catch
             } //disCon

             public int getRowNext() {  // get the next row
                 try{
                     CachedRowSet cr = getCrs();
                     cr.next();
                 }
                 catch (Exception e)  { debug(this,"what row");
                 System.out.println(e);  e.printStackTrace();} // catch
                 return 0; } // rowNext()

                 public long update(){
                     try{
                         // update should be using a different con pool
to same db
                         dbCon();
                         _crs.updateRow();
                         _crs.acceptChanges(getCon());
                         dbDisCon();
                     } catch (Exception e)  { procE(this,e);} // catch
                     return 0; } // update() should return new PK

                     public void setDelete() { // mark this row deleted
                         CachedRowSet cr = getCrs();
                         try{
                             cr.updateString("DEL_FLG","X");
                             // send to DB
                             update();
                         } // try
                         catch (Exception e)   {System.out.println(e);}
// catch
                     } //del

                     public void newRow()
                     { try {
                           debug(this," Insert begin");
                           dbCon();
                           BigDecimal tmp = new BigDecimal(1);
                           retreive(tmp); // descemdat SQL Row signature
                           CachedRowSet cr = getCrs();
                           cr.moveToInsertRow();
                           blank();
                           debug(this," before InsertRow");
                           cr.insertRow();
                           cr.moveToCurrentRow();
                           cr.next();
                           debug(this," we HAVE set up an insert");
                       } catch (Exception e)  {
                           debug(this, "oh no new row, check driver");
                           e.printStackTrace();
                           procE(this,e);} // catch
                     } // newRow()

                     public void blank() { } // stub

                public int retrieve(BigDecimal aBD) { System.out.println("should
never be called"); return 0;} // stub
                     // implement in descendent

                     public void setSqlString(String aSQL)
                     { sqlString = aSQL;}

     public daoFormBean() { // cons
         super();
         try{
             _crs = new CachedRowSet();
         } // try
         catch (Exception e)  {
             System.out.println(e);
             e.printStackTrace();}
     } //cons

     public int exec(CachedRowSet pCR) {
         int c=0;
         try{
             pCR.execute(getCon());
             pCR.first();
             c= pCR.size();
             //debug(this,c+" rc "+pCR.getCommand());
             setCrs(pCR);
         }// try
         catch (Exception e)   {procE(this,e);
         } // catch
         return c;
     } // exec


}//class

And to call above in perform:

public ActionForward perform( ActionMapping mapping, ActionForm form,
                                HttpServletRequest request,
HttpServletResponse response)
                                  throws IOException, ServletException
{

NameZoomFrm frm  = (NameZoomFrm) form;
if (frm == null) {frm = new NameZoomFrm();}
String PK = request.getParameter("PK");
String parameter = mapping.getParameter();

if (parameter == null) parameter ="X";

if (parameter.equals("del"))
    {  BigDecimal pkBD = new BigDecimal (PK);
       frm.setDelete();
       return(mapping.findForward("searchNam"));
     } else
if (PK != null) { // do we have an argument PK passed? Display!
     BigDecimal pkBD = new BigDecimal (PK);
     frm.dbCon();
     frm.retrieve(pkBD);
     frm.dbDisCon();
    } else
if (parameter.equals("new"))
    {
        frm.newRow();

     }
else { // Save
     ActionErrors errors = frm.validate(mapping,request);
     if (!errors.empty()) {
     saveErrors(request, errors);
     return (new ActionForward(mapping.getInput()));}
     frm.update(); // save the cached row to DB
     return(mapping.findForward("searchNam"));
} // else

return(mapping.findForward("nameZoomPg"));
// go to search pg.
}// perform Act
} // class



Ted Husted wrote:

> If anyone has any brief code examples or other notes to share regarding
> how they handle the interface between ActionForm beans and their model
> beans, I'd be happy to compile something for the Users Guide.
>
> -- Ted Husted, Husted dot Com, Fairport NY USA.
> -- Java Web Development with Struts.
> -- Tel +1 585 737-3463.
> -- Web http://www.husted.com/struts/
>
> --
> To unsubscribe, e-mail:
<mailto:[EMAIL PROTECTED]>
> For additional commands, e-mail:
<mailto:[EMAIL PROTECTED]>
>


--
To unsubscribe, e-mail:
<mailto:[EMAIL PROTECTED]>
For additional commands, e-mail:
<mailto:[EMAIL PROTECTED]>


--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to