Hi there... I'm a Java/Struts newbie, but a web developer oldie (Perl, ASP,
Cold Fusion, blah blah blah). This is my first attempt. If someone could
look at this design and give me some comments, I'd appreciate it.
Excuse the pseudo-code or comments-instead-of-code in some places.
Here are some specific questions (also look for "QUERY:" in the code [there
are lots of them])
First, the purpose of this application is to manage a real estate office's
rental and sales listings. So, for every class here, there will be an
analogous Sales class, since a sales listing is described much differently
than a rental listing. Any opinions on that would be helpful too.
1) Is the relationship between RentalAdd (action), RentalBean (the logic
bean), and DataAccess (a data access object) correct? Specifically, does
it adhere to MVC conventions, and is it a good design if I want to be able
to switch databases or user EJBs in the future?
2) Do I need the constructor in RentalBean? What is the easiest way to do
what Im trying to do there?
3) Should the entire application go in the same package?
4) If my DataAccess/Rental Bean model is correct, then should all database
specific stuff for the whole application go into my DataAccess class or
should I have a RentalDataAccess and a SalesDataAccess, etc.?
ANY HELP IS GREATLY APPRECIATED! IF ANYONE REALLY KNOWS THIS STUFF AND
WANTS TO HELP ME ON A CONSULTATIVE BASIS, EMAIL ME!
--Naf
package com.nafster.realestate;
public class RentalForm extends ActionForm{
private String city=null;
private int rooms=null;
//setters and getters for member vars go here
// validate domain of form data
public ActionErrors validate(ActionMapping mapping, HttpServletRequest
request) {
ActionErrors errors = new ActionErrors();
if (city == null)
errors.add("","");
if (rooms == null)
errors.add("","");
if (rooms < 0)
errors.add("","");
return errors;
}
public void reset(ActionMapping mapping, HttpServletRequest request){
city=null;
rooms=null;
}
}
class BaseAction extends Action{ // QUERY: This was a suggestion by Ted
Husted. Is this logical?
protected String action=null;
public abstract ActionForward performAction(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException;
public ActionForward perform(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
//Find out what we are doing (Create,Edit,Save,Reset, Delete, etc.)
action=request.getParameter("action"); // This is accessed by
subclasses primariliy
if (action == null)
action=mapping.getParameter();
return(performAction(mapping, form, request, response));
}
}
public class RentalAdd extends BaseAction{
/*
This Action is responsible for Creating a new Rental Record or Editting
an Existing one
Possible parameters (from Struts) are:
* Create: Generate a blank form
* Edit: Generate a form with existing values
* Save: INSERT or UPDATE the database
*/
public ActionForward performAction(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
// "action" comes from BaseAction
if (action.equals("create")){
// TODO: Forward to a blank form
}
else if (action.equals("edit")){ // QUERY: Am I thinking too
much like a procedural developer?
// Get the ID of the one we want to edit
int id=request.getParameter("id");
// Create a new RentalBean, populate it
RentalBean rentalBean=new RentalBean();
rentalBean.get(id); // QUERY: Make sense to abstract
like this?
//TODO: Populate RentalForm with Contents of RentalBean
RentalForm rentalForm= (RentalForm) form;
}
else if (action.equals("save")){
// Pull stuff out of the form
RentalForm rentalForm = (RentalForm) form;
RentalBean rentalBean = new
RentalBean(rentalForm); //QUERY: Can I do this? How else?
//Make sure all of the values adhere to business rules
rentalBean.validate();
// Store the data to the database, or wherever else we
might store it
rentalBean.store(); //QUERY: Should store() do the
validate?
}
// Save error messages keys into HTTP request for use by
<struts:errors> tag
if (!errors.empty){
saveErrors(request,errors);
//return to the original form
return (new ActionForward(mapping.getInput()));
}
// forward control to the specified "success" URI defined in
Action.xml
return(mapping.findForward("success"));
}
}
public class RentalBean{
private int id;
private String address;
private String city;
private float rent;
private int rooms;
ActionErrors errors=new ActionErrors();
DataAccess dao=new DataAccess();
public RentalBean(RentalForm rf){
//TODO: Set all this.<property> = rf.<property>
}
public ActionErrors validateLogic(){ //QUERY: This is the type of logic
that should be validated here, right?
// TODO: Biz Rules
if (!city.equals("Boston") && rooms > 5)
errors.add("",new ActionError("No place in Boston has 5 rooms!"));
}
public void store(){
dao.rentalInsert(this);
}
public ResultSet get(int id){
this.id=id;
return dao.rentalGet(this);
}
}
public class DataAccess {
private Connection conn;
// Other DB specific stuff
public void rentalInsert(RentalBean){
// Create sql statement based on values in the bean
// Execute the sql statement
}
public RecordSet rentalGet(RentalBean){
// Create sql statement to SELECT record from database WHERE
ID=RentalBean.id
// Execute the sql statement
// Return a "RecordSet" or null
}
}
public class BaseActionServlet extends ActionServlet{
// TODO: Check logon state, reroute as appropriate
// TODO: (if necc.) accessing, modifying,, resetting application-specific
objects
// TODO: log exceptions, save under a common request attribute
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>