001: /*
002: * Copyright 2005-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005: * in compliance with the License. You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software distributed under the License
010: * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011: * or implied. See the License for the specific language governing permissions and limitations under
012: * the License.
013: */
014:
015: package org.strecks.holidaybooking.web.struts.form;
016:
017: import javax.servlet.http.HttpServletRequest;
018:
019: import org.apache.commons.validator.GenericValidator;
020: import org.apache.struts.action.ActionErrors;
021: import org.apache.struts.action.ActionForm;
022: import org.apache.struts.action.ActionMapping;
023: import org.apache.struts.action.ActionMessage;
024: import org.strecks.holidaybooking.domain.HolidayBooking;
025:
026: /**
027: * @author Phil Zoio
028: */
029: public class HolidayBookingForm extends ActionForm {
030:
031: private static final long serialVersionUID = -4359715889805708158L;
032:
033: private String entryId;
034:
035: private String title;
036:
037: private String startDate;
038:
039: private String days;
040:
041: private boolean editMode;
042:
043: public String getDays() {
044: return days;
045: }
046:
047: public void setDays(String days) {
048: this .days = days;
049: }
050:
051: public String getEntryId() {
052: return entryId;
053: }
054:
055: public void setEntryId(String entryId) {
056: this .entryId = entryId;
057: }
058:
059: public String getStartDate() {
060: return startDate;
061: }
062:
063: public void setStartDate(String startDate) {
064: this .startDate = startDate;
065: }
066:
067: public String getTitle() {
068: return title;
069: }
070:
071: public void setTitle(String title) {
072: this .title = title;
073: }
074:
075: public void readFrom(HolidayBooking booking) {
076: if (booking != null) {
077: if (booking.getEntryId() != 0)
078: this .entryId = String.valueOf(booking.getEntryId());
079: if (booking.getEntryId() != 0)
080: this .days = String.valueOf(booking.getDays());
081: if (booking.getStartDate() != null)
082: this .startDate = new java.sql.Date(booking
083: .getStartDate().getTime()).toString();
084: this .title = booking.getTitle();
085: }
086: }
087:
088: public void writeTo(HolidayBooking booking) {
089: if (booking == null)
090: throw new IllegalArgumentException("Booking cannot be null");
091:
092: if (this .days != null)
093: booking.setDays(Integer.parseInt(this .days));
094: if (this .entryId != null)
095: booking.setEntryId(Long.parseLong(this .entryId));
096:
097: // don't accept empty Strings
098: if (this .title != null && this .title.trim().length() > 0)
099: booking.setTitle(title);
100:
101: // assume validation has been handled
102: if (this .startDate != null
103: && this .startDate.trim().length() > 0)
104: booking.setStartDate(java.sql.Date.valueOf(startDate));
105:
106: }
107:
108: public ActionErrors validate(ActionMapping mapping,
109: HttpServletRequest request) {
110:
111: ActionErrors errors = new ActionErrors();
112:
113: boolean hasError = false;
114:
115: if (title == null) {
116: ActionMessage error = new ActionMessage(
117: "Title cannot be null", false);
118: errors.add("title", error);
119: hasError = true;
120: }
121:
122: if (days == null) {
123: ActionMessage error = new ActionMessage(
124: "Days cannot be null", false);
125: errors.add("days", error);
126: hasError = true;
127: } else {
128: if (!GenericValidator.isInt(days)) {
129: ActionMessage error = new ActionMessage(
130: "Days must be number", false);
131: errors.add("days", error);
132: hasError = true;
133: }
134: }
135:
136: if (startDate == null) {
137: ActionMessage error = new ActionMessage(
138: "Start date cannot be null", false);
139: errors.add("startDate", error);
140: hasError = true;
141: } else {
142:
143: if (!GenericValidator
144: .isDate(startDate, "yyyy-MM-dd", false)) {
145: ActionMessage error = new ActionMessage(
146: "Start date must be a date in yyyy-MM-dd",
147: false);
148: errors.add("startDate", error);
149: hasError = true;
150: }
151: }
152:
153: if (!hasError)
154: return null;
155:
156: return errors;
157:
158: }
159:
160: public void setEditMode(boolean editMode) {
161: this .editMode = editMode;
162: }
163:
164: public boolean isEditMode() {
165: return editMode;
166: }
167:
168: public String getNextAction() {
169: return (editMode ? "submit_edit_holiday_booking"
170: : "submit_new_holiday_booking");
171: }
172:
173: }
|