001    /*
002     * Copyright 2011 The Kuali Foundation.
003     * 
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     * http://www.opensource.org/licenses/ecl2.php
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.kfs.module.purap.web.struts;
017    
018    import java.util.List;
019    
020    import javax.servlet.http.HttpServletRequest;
021    import javax.servlet.http.HttpServletResponse;
022    
023    import org.apache.struts.action.ActionForm;
024    import org.apache.struts.action.ActionForward;
025    import org.apache.struts.action.ActionMapping;
026    import org.kuali.kfs.module.purap.document.RequisitionDocument;
027    import org.kuali.kfs.module.purap.document.service.B2BShoppingService;
028    import org.kuali.kfs.module.purap.exception.B2BConnectionException;
029    import org.kuali.kfs.module.purap.exception.B2BShoppingException;
030    import org.kuali.kfs.module.purap.util.cxml.B2BShoppingCart;
031    import org.kuali.kfs.module.purap.util.cxml.B2BParserHelper;
032    import org.kuali.kfs.sys.KFSConstants;
033    import org.kuali.kfs.sys.context.SpringContext;
034    import org.kuali.rice.kns.util.GlobalVariables;
035    import org.kuali.rice.kns.util.ObjectUtils;
036    import org.kuali.rice.kns.web.struts.action.KualiAction;
037    
038    public class B2BAction extends KualiAction {
039        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(B2BAction.class);
040    
041        public ActionForward shopCatalogs(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
042            B2BForm b2bForm = (B2BForm) form;
043            String url = SpringContext.getBean(B2BShoppingService.class).getPunchOutUrl(GlobalVariables.getUserSession().getPerson());
044    
045            if (ObjectUtils.isNull(url)) {
046                throw new B2BConnectionException("Unable to connect to remote site for punchout.");
047            }
048    
049            b2bForm.setShopUrl(url);
050            return mapping.findForward(KFSConstants.MAPPING_BASIC);
051        }
052    
053        public ActionForward returnFromShopping(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
054            String cXml = request.getParameter("cxml-urlencoded");
055            LOG.info("executeLogic() cXML returned in PunchoutOrderMessage:\n" + cXml);
056    
057            B2BShoppingCart cart = B2BParserHelper.getInstance().parseShoppingCartXML(cXml);
058    
059            if (cart.isSuccess()) {
060                List requisitions = SpringContext.getBean(B2BShoppingService.class).createRequisitionsFromCxml(cart, GlobalVariables.getUserSession().getPerson());
061                if (requisitions.size() > 1) {
062                    request.getSession().setAttribute("multipleB2BRequisitions", "true");
063                }
064                request.setAttribute("forward", "/portal.do?channelTitle=Requisition&channelUrl=purapRequisition.do?methodToCall=displayB2BRequisition");
065                request.getSession().setAttribute("docId", ((RequisitionDocument) requisitions.get(0)).getDocumentNumber());
066            }
067            else {
068                LOG.debug("executeLogic() Retrieving shopping cart from cxml was unsuccessful. Error message:" + cart.getStatusText());
069                throw new B2BShoppingException("Retrieving shopping cart from cxml was unsuccessful. Error message:" + cart.getStatusText());
070            }
071    
072            return (mapping.findForward("removeframe"));
073        }
074    
075    }
076