package faqapp.struts.actions;

import java.rmi.RemoteException;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionError;

import faqapp.AppSessionFacade;
import faqapp.AreaTO;
import faqapp.struts.forms.AreaForm;

/**
 * Implementation of AreaAction. This FAQApp specific action implementation implements
 * the business logic execution methods defined and invoked by FAQAbstractAction,
 * which are validate, create, update, saveCreate, saveUpdate, and query.
 */
public final class AreaAction extends FAQAbstractAction {
    public ActionErrors validate(ActionForm form, HttpServletRequest request,
                                 AppSessionFacade asf) throws RemoteException {
        /* Validate from data and return validation errors if necessary */
        AreaForm af = (AreaForm)form;
        ActionErrors errors = new ActionErrors();
        if ((af.getAreaDesc() == null) || (af.getAreaDesc().length() < 1))
                errors.add("areaDesc", new ActionError("errors.areaDesc.required"));
        return errors;
    }

    public String create(ActionForm form, HttpServletRequest request,
                         AppSessionFacade asf) {
        /* No specific business logic necessary */
        return SUCCESS;
    }

    public String update(ActionForm form, HttpServletRequest request,
                         AppSessionFacade asf) throws RemoteException {
        /**
         * Read Area based on AreaForm.id and store it in AreaForm
         * for further usage in view (Area.jsp).
         */
        AreaForm af = (AreaForm)form;
        AreaTO area = asf.findAreaByPK(af.getId());
        af.setId(area.getAreaID());
        af.setAreaDesc(area.getAreaDesc());
        return SUCCESS;
    }

    public String saveCreate(ActionForm form, HttpServletRequest request,
                             AppSessionFacade asf) throws RemoteException{
        /**
         * Invoke createArea(...) method on AppSessionFacade
         * with corresponding data from AreaForm
         */
        AreaForm af = (AreaForm)form;
        af.setId(asf.createArea(af.getAreaDesc()));
        return SUCCESS;
    }

    public String saveUpdate(ActionForm form, HttpServletRequest request,
                             AppSessionFacade asf) throws RemoteException{
        /**
         * Invoke updateArea(...) method on AppSessionFacade
         * with corresponding data from AreaForm
         */
        AreaForm af = (AreaForm)form;
        asf.updateArea(af.getId(), af.getAreaDesc());
        return SUCCESS;
    }

    public String query(ActionForm form, HttpServletRequest request,
                        AppSessionFacade asf) throws RemoteException {
        /**
         * Read all Areas and store them in HTTP request for
         * further usage in view (AreaQuery.jsp).
         */
        AreaTO[] areas = asf.findAreaAll();
        request.setAttribute("areas", areas);
        return SUCCESS;
    }
}

