package ordering.gui.actions;

 

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpSession;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;

import org.apache.struts.action.ActionError;

import org.apache.struts.action.ActionErrors;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionForward;

import org.apache.struts.action.ActionMapping;

import org.apache.struts.action.ActionServlet;

 

import ordering.gui.forms.*;

import ordering.common.model.*;

import ordering.common.vo.*;

import java.util.Properties;

import ordering.custom.Constants;

import java.util.Collection;

 

public final class PoViewAction extends Action {

 

   public ActionForward perform(ActionMapping mapping,

                                ActionForm form,

                                HttpServletRequest request,

                                HttpServletResponse response)

          throws IOException, ServletException

   {

       int poId = ((PoViewForm) form).getPoId();

 

       try {

           OrderingModel ordSrv = OrderingModelFactory.create();

           PurchaseOrderVO po = ordSrv.getPurchaseOrder(poId);

           if (po == null) {

             ActionErrors errors = new ActionErrors();

             errors.add(ActionErrors.GLOBAL_ERROR,new ActionError("error.po.noexist"));

             saveErrors(request,errors);

             return (mapping.findForward("fail"));

           }

           String action = request.getParameter("submit");

           if (action.equals("view")) {

               //HttpSession session = request.getSession();

               request.setAttribute("PO", po);

               request.setAttribute("LineItems", po.getLineItems());

               request.setAttribute("ShipTo", po.getShipTo());

               request.setAttribute("BillTo", po.getBillTo());

               return (new ActionForward(mapping.getInput()));

           }

           if (action.equals("accept")) {

              ordSrv.acceptPurchaseOrder(poId);

           }

           else if (action.equals("reject")) {

              ordSrv.rejectPurchaseOrder(poId);

           }

           return (mapping.findForward("success"));

       } catch (Exception e) {

           e.printStackTrace();

           return (mapping.findForward("fail"));

       }

    }

 

}