views:

5

answers:

0

Hi

I've been having a problem for a while that I am not able to understand. The situation is like this.. I have a page in the wizard that I use for adding business locations (BusinessLocation.java entity) of a service provider (ServiceProvider.java entity) but the thing is that I dont get a consistent result when I see the next page in which I display all that business locations that I just added in the previous page of the wizard. Sometimes I see all the locations displayed but sometimes just the last one added and even the order in which they are displayed is not consistent and I always get a blank location displayed in the list of added locations. I added a blank businessLocation in the postProcessPage(..) method in my wizard controller for populating a blank form after adding a location and also for incrementing the counter by one for moving on to next index in a list (businessLocations[${locationIndex}]).

Here is the code for my controller and jsp:

RegisterServiceProviderController.java

public class RegisterServiceProviderController extends AbstractWizardFormController{

 protected Object formBackingObject(HttpServletRequest request) throws Exception {
  ExtendedServiceProvider extendedServiceProvider = (ExtendedServiceProvider) super.formBackingObject(request);

  ServiceProvider serviceProvider = new ServiceProvider();

  Set<BusinessLocation> businessLocations = new HashSet<BusinessLocation>();
  BusinessLocation businessLocation = new BusinessLocation();
  businessLocation.setState(new State());
  businessLocations.add(businessLocation);
  serviceProvider.setBusinessLocations(businessLocations);

  extendedServiceProvider.setServiceProvider(serviceProvider);  

  return extendedServiceProvider;
 }
 protected Map referenceData(HttpServletRequest request, Object command, Errors errors, int page) throws Exception {
  ExtendedServiceProvider extendedServiceProvider = (ExtendedServiceProvider) command;
  Map refData = new HashMap();
  refData.put("currentLocationNumber", extendedServiceProvider.getServiceProvider().getBusinessLocations().size());
  refData.put("locationIndex", extendedServiceProvider.getServiceProvider().getBusinessLocations().size() - 1);
  refData.put("stateData", hibernateTemplate.find("from StateData"));
  return refData;
 }

 protected void postProcessPage(HttpServletRequest request, Object command, Errors errors, int page) throws Exception {
  ExtendedServiceProvider extendedServiceProvider = (ExtendedServiceProvider) command;  
  if(page == 1 && request.getParameter("_target1") != null) {
   BusinessLocation businessLocation = new BusinessLocation();
   businessLocation.setState(new State());
   extendedServiceProvider.getServiceProvider().getBusinessLocations().add(businessLocation);  
  }
 }

 protected ModelAndView processFinish(HttpServletRequest request,
   HttpServletResponse response, Object command, BindException errors)
   throws Exception {

  ExtendedServiceProvider extendedServiceProvider = (ExtendedServiceProvider) command;

  //the last business location is always blank... remove it
  extendedServiceProvider.getServiceProvider().getBusinessLocations().remove(extendedServiceProvider.getServiceProvider().getBusinessLocations().size() - 1);
  service.addServiceProvider(extendedServiceProvider.getServiceProvider());

  return new ModelAndView(getSuccussView(), "serviceProvider", extendedServiceProvider.getServiceProvider());
 } 

 private String getSuccussView() {
  return getPages()[getPages().length - 1];
 }

 private ServiceProviderService service;
 public void setServiceProviderService(ServiceProviderService serviceProviderService) {
  this.service = serviceProviderService;
 }

 private HibernateTemplate hibernateTemplate;
 public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
  this.hibernateTemplate = hibernateTemplate;
 }

}

locationDetailForm.jsp

<form:form commandName="extendedServiceProvider" method="POST" action="register.htm">
 <input type="hidden" name="page" value="1" /><br>
 <tr>
  <td>locationCount: <c:out value="${extendedServiceProvider.locationCount}" /></td>
 </tr><br></br>
 <tr>
  <td>Address: </td>
  <td><form:input path="serviceProvider.businessLocations[${locationIndex}].address"/></td>
 </tr><br></br>
 <tr>
  <td>State: </td>
  <td>
   <form:select path="serviceProvider.businessLocations[${locationIndex}].state.abbreviatedName">
    <form:option value="0" label="--Have your pick--" />
    <form:options items="${stateData}" itemValue="abbreviatedName" itemLabel="name" />
   </form:select>
  </td>
 </tr><br></br>
    <input type="submit" name="_target0" value="Back">&nbsp;
    <input type="submit" name="_target1" value="Add Business Location">&nbsp;
    <input type="submit" name="_target2" value="Next">
</form:form>

Could someone help me understand that why am I not getting all the added locations displayed consistently in a consistent order I am adding them?

Thanks