Package account.addresses

Source Code of account.addresses.Controller$CreateAddressForm

/*
* Copyright 2004-2005 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 account.addresses;

import javax.servlet.http.HttpServletRequest;

import org.apache.beehive.netui.pageflow.Forward;
import org.apache.beehive.netui.pageflow.PageFlowController;
import org.apache.beehive.netui.pageflow.annotations.Jpf;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;

import org.apache.beehive.controls.api.bean.Control;
import org.apache.beehive.samples.petstore.controls.AddressControl;
import org.apache.beehive.samples.petstore.controls.exceptions.NoSuchAddressException;
import org.apache.beehive.samples.petstore.controls.exceptions.InvalidIdentifierException;
import org.apache.beehive.samples.petstore.forms.AddressForm;
import org.apache.beehive.samples.petstore.model.Address;

@Jpf.Controller(
    sharedFlowRefs = {
    @Jpf.SharedFlowRef(name = "rootSharedFlow", type = webappRoot.SharedFlow.class)
    },
    messageBundles = {@Jpf.MessageBundle(bundlePath = "org.apache.beehive.samples.petstore.resources.account")}
)
public class Controller
    extends PageFlowController {

    @Control()
    private AddressControl _addressControl;

    @Jpf.SharedFlowField(name = "rootSharedFlow")
    private webappRoot.SharedFlow _sharedFlow = null;

    private CreateAddressForm _createForm = null;

  @Jpf.Action(
        forwards = {
            @Jpf.Forward(name = "success", path = "listAddresses.jsp",
            actionOutputs = {
                @Jpf.ActionOutput(name = "addresses",
                                  type = Address[].class,
                                  required = true)
                })
        }
    )
    protected Forward begin() {
       
        _sharedFlow.ensureLogin();
        Forward forward = new Forward("success");
        forward.addActionOutput("addresses", _addressControl.getUserAddresses(_sharedFlow.getAccount().getUserId()));
        return forward;
    }
 
  @Jpf.Action(
        forwards = {
            @Jpf.Forward(name = "success", path = "createUpdate.jsp")
        }
    )
    protected Forward createUpdateAddress() throws InvalidIdentifierException
    {
        _sharedFlow.ensureLogin();
   
    // If an addressId is passed in, this is an edit of an existing address
    String addressId = getRequest().getParameter("addressId");
    Address address;
   
    if (addressId == null)
    {
      // New address
      address = new Address();
      address.setUserId(_sharedFlow.getAccount().getUserId());
    } else {
      // Existing address
      address = _addressControl.getAddress(Integer.parseInt(addressId));
    }

    _createForm = new CreateAddressForm(address);
        Forward forward = new Forward("success", _createForm);
        return forward;
    }

  @Jpf.Action(
       useFormBean = "_createForm",
       forwards = {
           @Jpf.Forward(name = "success", action = "begin")
       }, validationErrorForward = @Jpf.Forward(name = "failure", navigateTo = Jpf.NavigateTo.currentPage)
  )
    protected Forward updateAddress(CreateAddressForm form)
        throws InvalidIdentifierException, NoSuchAddressException
    {
        _sharedFlow.ensureLogin();

        Address address = AddressForm.getAddress(form);
   
    if (address.getAddressId() == -1)
    {
      // Add New Address
      _addressControl.insertAddress(address);
    } else {
      // Update Existing Address
      _addressControl.updateAddress(address);
    }

    return new Forward("success");
   

  @Jpf.Action(
        forwards = {
            @Jpf.Forward(name = "success", action = "begin")
        }
    )
    protected Forward deleteAddress() throws InvalidIdentifierException, NoSuchAddressException
    {
    _sharedFlow.ensureLogin();
 
    String addressId = getRequest().getParameter("addressId");
    if (addressId == null)
      throw new InvalidIdentifierException("AddressId not passed in");
     
    // Will only delete the address if the user owns it
    _addressControl.deleteAddress(Integer.parseInt(addressId), _sharedFlow.getAccount().getUserId());

        return new Forward("success");
    }

    public static class CreateAddressForm
        extends AddressForm {

        public CreateAddressForm() {
            super();
        }

        public CreateAddressForm(Address address) {
            super(address);
        }

        public void setUserId(String userId) {
            getAddress().setUserId(userId);
        }

        public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
            ActionErrors errors = new ActionErrors();
            Address address = getAddress();

            addErrorIfStringEmpty(errors, "userId", "address.error.userid.required", address.getUserId());

            baseValidate(errors, mapping, request);

            return errors;
        }
    }
}
TOP

Related Classes of account.addresses.Controller$CreateAddressForm

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.