Package org.strecks.holidaybooking.web.struts.form

Source Code of org.strecks.holidaybooking.web.struts.form.HolidayBookingForm

/*
* Copyright 2005-2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

package org.strecks.holidaybooking.web.struts.form;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.validator.GenericValidator;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.strecks.holidaybooking.domain.HolidayBooking;

/**
* @author Phil Zoio
*/
public class HolidayBookingForm extends ActionForm
{

  private static final long serialVersionUID = -4359715889805708158L;

  private String entryId;

  private String title;

  private String startDate;

  private String days;

  private boolean editMode;

  public String getDays()
  {
    return days;
  }

  public void setDays(String days)
  {
    this.days = days;
  }

  public String getEntryId()
  {
    return entryId;
  }

  public void setEntryId(String entryId)
  {
    this.entryId = entryId;
  }

  public String getStartDate()
  {
    return startDate;
  }

  public void setStartDate(String startDate)
  {
    this.startDate = startDate;
  }

  public String getTitle()
  {
    return title;
  }

  public void setTitle(String title)
  {
    this.title = title;
  }

  public void readFrom(HolidayBooking booking)
  {
    if (booking != null)
    {
      if (booking.getEntryId() != 0)
        this.entryId = String.valueOf(booking.getEntryId());
      if (booking.getEntryId() != 0)
        this.days = String.valueOf(booking.getDays());
      if (booking.getStartDate() != null)
        this.startDate = new java.sql.Date(booking.getStartDate().getTime()).toString();
      this.title = booking.getTitle();
    }
  }

  public void writeTo(HolidayBooking booking)
  {
    if (booking == null)
      throw new IllegalArgumentException("Booking cannot be null");

    if (this.days != null)
      booking.setDays(Integer.parseInt(this.days));
    if (this.entryId != null)
      booking.setEntryId(Long.parseLong(this.entryId));

    // don't accept empty Strings
    if (this.title != null && this.title.trim().length() > 0)
      booking.setTitle(title);

    // assume validation has been handled
    if (this.startDate != null && this.startDate.trim().length() > 0)
      booking.setStartDate(java.sql.Date.valueOf(startDate));

  }

  public ActionErrors validate(ActionMapping mapping, HttpServletRequest request)
  {

    ActionErrors errors = new ActionErrors();

    boolean hasError = false;

    if (title == null)
    {
      ActionMessage error = new ActionMessage("Title cannot be null", false);
      errors.add("title", error);
      hasError = true;
    }

    if (days == null)
    {
      ActionMessage error = new ActionMessage("Days cannot be null", false);
      errors.add("days", error);
      hasError = true;
    }
    else
    {
      if (!GenericValidator.isInt(days))
      {
        ActionMessage error = new ActionMessage("Days must be number", false);
        errors.add("days", error);
        hasError = true;
      }
    }

    if (startDate == null)
    {
      ActionMessage error = new ActionMessage("Start date cannot be null", false);
      errors.add("startDate", error);
      hasError = true;
    }
    else
    {

      if (!GenericValidator.isDate(startDate, "yyyy-MM-dd", false))
      {
        ActionMessage error = new ActionMessage("Start date must be a date in yyyy-MM-dd", false);
        errors.add("startDate", error);
        hasError = true;
      }
    }

    if (!hasError)
      return null;

    return errors;

  }

  public void setEditMode(boolean editMode)
  {
    this.editMode = editMode;
  }

  public boolean isEditMode()
  {
    return editMode;
  }

  public String getNextAction()
  {
    return (editMode ? "submit_edit_holiday_booking" : "submit_new_holiday_booking");
  }

}
TOP

Related Classes of org.strecks.holidaybooking.web.struts.form.HolidayBookingForm

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.