/*
* Copyright 2004 The Apache Software Foundation.
*
* 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.
*
* $Header:$
*/
package org.apache.beehive.samples.petstore.forms;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.beehive.samples.petstore.model.Account;
import org.apache.beehive.samples.petstore.model.Address;
import org.apache.beehive.samples.petstore.model.Order;
import javax.servlet.http.HttpServletRequest;
public class CheckoutForm
extends OrderForm {
private boolean _shippingAddressRequired;
private boolean _isCheckOut;
public void setShippingAddressRequired(boolean shippingAddressRequired) {
_shippingAddressRequired = shippingAddressRequired;
}
public boolean isShippingAddressRequired() {
return _shippingAddressRequired;
}
public boolean isCheckOut() {
return _isCheckOut;
}
public void setCheckOut(boolean checkOut) {
_isCheckOut = checkOut;
}
// todo: format validation for e-mail, zip, credit card, date
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
Order order = getOrder();
ActionErrors errors = new ActionErrors();
Account account = order.getAccount();
addErrorIfStringEmpty(errors, "creditCart", "order.error.creditCard.required", order.getCreditCard());
addErrorIfStringEmpty(errors, "expirationDate", "order.error.expirationDate.required", order.getExprDate());
addErrorIfStringEmpty(errors, "cardType", "order.error.cardType.required", order.getCardType());
Address billing = order.getBillingAddress();
addErrorIfStringEmpty(errors, "billToFirstName", "order.error.billToFirstName.required", billing.getFirstName());
addErrorIfStringEmpty(errors, "billToLastName", "order.error.billToLastName.required", billing.getLastName());
addErrorIfStringEmpty(errors, "billToAddress1", "order.error.billToAddress1.required", billing.getAddr1());
addErrorIfStringEmpty(errors, "billToCity", "order.error.billToCity.required", billing.getCity());
addErrorIfStringEmpty(errors, "billToState", "order.error.billToState.required", billing.getState());
addErrorIfStringEmpty(errors, "billToZip", "order.error.billToZip.required", billing.getZip());
addErrorIfStringEmpty(errors, "billToCountry", "order.error.billToCountry.required", billing.getCountry());
if(isShippingAddressRequired()) {
Address shipping = order.getShippingAddress();
addErrorIfStringEmpty(errors, "shipToFirstName", "order.error.shipToFirstName.required", shipping.getFirstName());
addErrorIfStringEmpty(errors, "shipToLastName", "order.error.shipToLastName.required", shipping.getLastName());
addErrorIfStringEmpty(errors, "shipToAddress1", "order.error.shipToAddress1.required", shipping.getAddr1());
addErrorIfStringEmpty(errors, "shipToCity", "order.error.shipToCity.required", shipping.getCity());
addErrorIfStringEmpty(errors, "shipToState", "order.error.shipToState.required", shipping.getState());
addErrorIfStringEmpty(errors, "shipToZip", "order.error.shipToZip.required", shipping.getZip());
addErrorIfStringEmpty(errors, "shipToCountry", "order.error.shipToCountry.required", shipping.getCountry());
}
// sets the ship address the same as the bill-to address
if(!isShippingAddressRequired() && errors.size() > 0) {
order.setShippingAddressIsBillingAddress(true);
}
return errors;
}
}