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.document.web.struts;
017    
018    import java.util.Map;
019    
020    import javax.servlet.http.HttpServletRequest;
021    import javax.servlet.http.HttpServletResponse;
022    
023    import org.apache.commons.lang.StringUtils;
024    import org.apache.struts.action.ActionForm;
025    import org.apache.struts.action.ActionForward;
026    import org.apache.struts.action.ActionMapping;
027    import org.kuali.kfs.module.purap.PurapKeyConstants;
028    import org.kuali.kfs.module.purap.businessobject.DefaultPrincipalAddress;
029    import org.kuali.kfs.module.purap.businessobject.RequisitionItem;
030    import org.kuali.kfs.module.purap.document.RequisitionDocument;
031    import org.kuali.kfs.module.purap.document.service.PurapService;
032    import org.kuali.kfs.sys.KFSConstants;
033    import org.kuali.kfs.sys.context.SpringContext;
034    import org.kuali.rice.kew.exception.WorkflowException;
035    import org.kuali.rice.kns.service.BusinessObjectService;
036    import org.kuali.rice.kns.service.PersistenceService;
037    import org.kuali.rice.kns.util.GlobalVariables;
038    import org.kuali.rice.kns.util.ObjectUtils;
039    import org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase;
040    
041    /**
042     * Struts Action for Requisition document.
043     */
044    public class RequisitionAction extends PurchasingActionBase {
045        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RequisitionAction.class);
046    
047        /**
048         * Does initialization for a new requisition.
049         * 
050         * @see org.kuali.rice.kns.web.struts.action.KualiDocumentActionBase#createDocument(org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase)
051         */
052        @Override
053        protected void createDocument(KualiDocumentFormBase kualiDocumentFormBase) throws WorkflowException {
054            super.createDocument(kualiDocumentFormBase);
055            ((RequisitionDocument) kualiDocumentFormBase.getDocument()).initiateDocument();
056        }
057    
058        public ActionForward setAsDefaultBuilding(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
059            RequisitionDocument req = (RequisitionDocument) ((RequisitionForm) form).getDocument();
060            
061            if (ObjectUtils.isNotNull(req.getDeliveryCampusCode()) && ObjectUtils.isNotNull(req.getDeliveryBuildingCode())) {
062                DefaultPrincipalAddress defaultPrincipalAddress = new DefaultPrincipalAddress(GlobalVariables.getUserSession().getPerson().getPrincipalId());
063                Map addressKeys = SpringContext.getBean(PersistenceService.class).getPrimaryKeyFieldValues(defaultPrincipalAddress);
064                defaultPrincipalAddress = (DefaultPrincipalAddress) SpringContext.getBean(BusinessObjectService.class).findByPrimaryKey(DefaultPrincipalAddress.class, addressKeys);
065    
066                if (ObjectUtils.isNull(defaultPrincipalAddress)) {
067                    defaultPrincipalAddress = new DefaultPrincipalAddress(GlobalVariables.getUserSession().getPerson().getPrincipalId());
068                }
069                
070                defaultPrincipalAddress.setDefaultBuilding(req.getDeliveryCampusCode(), req.getDeliveryBuildingCode(), req.getDeliveryBuildingRoomNumber());
071                SpringContext.getBean(BusinessObjectService.class).save(defaultPrincipalAddress);
072                GlobalVariables.getMessageList().add(PurapKeyConstants.DEFAULT_BUILDING_SAVED);
073            }
074            
075            return mapping.findForward(KFSConstants.MAPPING_BASIC);
076        }
077    
078        /**
079         * @see org.kuali.rice.kns.web.struts.action.KualiAction#refresh(org.apache.struts.action.ActionMapping,
080         *      org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
081         */
082        @Override
083        public ActionForward refresh(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
084            ActionForward forward = super.refresh(mapping, form, request, response);
085            RequisitionForm rqForm = (RequisitionForm) form;
086            RequisitionDocument document = (RequisitionDocument) rqForm.getDocument();
087    
088            // super.refresh() must occur before this line to get the correct APO limit
089            document.setOrganizationAutomaticPurchaseOrderLimit(SpringContext.getBean(PurapService.class).getApoLimit(document.getVendorContractGeneratedIdentifier(), document.getChartOfAccountsCode(), document.getOrganizationCode()));
090    
091            return forward;
092        }
093        
094        /**
095         * Adds a PurchasingItemCapitalAsset (a container for the Capital Asset Number) to the selected 
096         * item's list.
097         * 
098         * @param mapping       An ActionMapping
099         * @param form          The Form
100         * @param request       An HttpServletRequest
101         * @param response      The HttpServletResponse
102         * @return      An ActionForward
103         * @throws Exception
104         */
105        public ActionForward addAsset(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
106            RequisitionForm rqForm = (RequisitionForm) form;
107            RequisitionDocument document = (RequisitionDocument) rqForm.getDocument();
108            RequisitionItem item = (RequisitionItem)document.getItemByLineNumber(getSelectedLine(request) + 1);
109            //TODO: Add a new way to add assets to the system.
110            //item.addAsset();
111            return mapping.findForward(KFSConstants.MAPPING_BASIC);
112        }
113    
114        public ActionForward displayB2BRequisition(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
115            RequisitionForm reqForm = (RequisitionForm) form;
116            reqForm.setDocId((String) request.getSession().getAttribute("docId"));
117            loadDocument(reqForm);
118            String multipleB2BReqs = (String) request.getSession().getAttribute("multipleB2BRequisitions");
119            if (StringUtils.isNotEmpty(multipleB2BReqs)) {
120                GlobalVariables.getMessageList().add(PurapKeyConstants.B2B_MULTIPLE_REQUISITIONS);
121            }
122            request.getSession().removeAttribute("docId");
123            request.getSession().removeAttribute("multipleB2BRequisitions");
124            return mapping.findForward(KFSConstants.MAPPING_BASIC);
125        }
126    
127        /**
128         * Clears the vendor selection from the Requisition.  NOTE, this functionality is only available on Requisition and not PO.
129         * 
130         * @param mapping An ActionMapping
131         * @param form An ActionForm
132         * @param request A HttpServletRequest
133         * @param response A HttpServletResponse
134         * @return An ActionForward
135         * @throws Exception
136         */
137        public ActionForward clearVendor(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
138            PurchasingFormBase baseForm = (PurchasingFormBase) form;
139            RequisitionDocument document = (RequisitionDocument) baseForm.getDocument();
140    
141            document.setVendorHeaderGeneratedIdentifier(null);
142            document.setVendorDetailAssignedIdentifier(null);
143            document.setVendorDetail(null);
144            document.setVendorName("");
145            document.setVendorLine1Address("");
146            document.setVendorLine2Address("");
147            document.setVendorAddressInternationalProvinceName("");
148            document.setVendorCityName("");
149            document.setVendorStateCode("");
150            document.setVendorPostalCode("");
151            document.setVendorCountryCode("");
152            document.setVendorContractGeneratedIdentifier(null);
153            document.setVendorContract(null);
154            document.setVendorFaxNumber("");
155            document.setVendorCustomerNumber("");
156            document.setVendorAttentionName("");
157            return mapping.findForward(KFSConstants.MAPPING_BASIC);
158        }
159    
160        /**
161         * Set up blanket approve indicator which will be used to decide if need to run accounting line validation at the time of
162         * blanket approve.
163         * 
164         * @see org.kuali.kfs.module.purap.document.web.struts.PurchasingActionBase#blanketApprove(org.apache.struts.action.ActionMapping,
165         *      org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
166         */
167        @Override
168        public ActionForward blanketApprove(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
169            RequisitionDocument document = (RequisitionDocument) ((PurchasingFormBase) form).getDocument();
170            document.setBlanketApproveRequest(true);
171            return super.blanketApprove(mapping, form, request, response);
172        }
173    }