View Javadoc
1   /*
2    * Copyright 2006 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.module.purap.document.web.struts;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.struts.action.ActionForm;
20  import org.apache.struts.action.ActionForward;
21  import org.apache.struts.action.ActionMapping;
22  import org.kuali.ole.module.purap.PurapKeyConstants;
23  import org.kuali.ole.module.purap.businessobject.DefaultPrincipalAddress;
24  import org.kuali.ole.module.purap.businessobject.RequisitionItem;
25  import org.kuali.ole.module.purap.document.PurchasingAccountsPayableDocument;
26  import org.kuali.ole.module.purap.document.RequisitionDocument;
27  import org.kuali.ole.module.purap.document.service.PurapService;
28  import org.kuali.ole.select.document.OleRequisitionDocument;
29  import org.kuali.ole.sys.OLEConstants;
30  import org.kuali.ole.sys.context.SpringContext;
31  import org.kuali.rice.core.api.util.RiceConstants;
32  import org.kuali.rice.kew.api.exception.WorkflowException;
33  import org.kuali.rice.kns.util.KNSGlobalVariables;
34  import org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase;
35  import org.kuali.rice.krad.service.BusinessObjectService;
36  import org.kuali.rice.krad.service.PersistenceService;
37  import org.kuali.rice.krad.util.GlobalVariables;
38  import org.kuali.rice.krad.util.ObjectUtils;
39  
40  import javax.servlet.http.HttpServletRequest;
41  import javax.servlet.http.HttpServletResponse;
42  import java.util.Map;
43  
44  /**
45   * Struts Action for Requisition document.
46   */
47  public class RequisitionAction extends PurchasingActionBase {
48      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RequisitionAction.class);
49  
50      /**
51       * save the document without any validations.....
52       *
53       * @see org.kuali.ole.sys.web.struts.KualiAccountingDocumentActionBase#save(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
54       */
55      @Override
56      public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
57          //call the super save to save the document without validations...
58  
59          ActionForward actionForward = super.save(mapping, form, request, response);
60  
61          //we need to make "calculated" to false so that the "below lines"
62          //can be edited until calculated button is clicked.
63          KualiDocumentFormBase kualiDocumentFormBase = (KualiDocumentFormBase) form;
64          PurchasingFormBase baseForm = (PurchasingFormBase) form;
65          PurchasingAccountsPayableDocument purapDocument = (PurchasingAccountsPayableDocument) kualiDocumentFormBase.getDocument();
66  
67          baseForm.setCalculated(false);
68          purapDocument.setCalculated(false);
69  
70          return mapping.findForward(RiceConstants.MAPPING_BASIC);
71      }
72  
73      /**
74       * Does initialization for a new requisition.
75       *
76       * @see org.kuali.rice.kns.web.struts.action.KualiDocumentActionBase#createDocument(org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase)
77       */
78      @Override
79      protected void createDocument(KualiDocumentFormBase kualiDocumentFormBase) throws WorkflowException {
80          super.createDocument(kualiDocumentFormBase);
81          ((RequisitionDocument) kualiDocumentFormBase.getDocument()).initiateDocument();
82      }
83  
84      public ActionForward setAsDefaultBuilding(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
85          RequisitionDocument req = (RequisitionDocument) ((RequisitionForm) form).getDocument();
86  
87          if (ObjectUtils.isNotNull(req.getDeliveryCampusCode()) && ObjectUtils.isNotNull(req.getDeliveryBuildingCode())) {
88              DefaultPrincipalAddress defaultPrincipalAddress = new DefaultPrincipalAddress(GlobalVariables.getUserSession().getPerson().getPrincipalId());
89              Map addressKeys = SpringContext.getBean(PersistenceService.class).getPrimaryKeyFieldValues(defaultPrincipalAddress);
90              defaultPrincipalAddress = (DefaultPrincipalAddress) SpringContext.getBean(BusinessObjectService.class).findByPrimaryKey(DefaultPrincipalAddress.class, addressKeys);
91  
92              if (ObjectUtils.isNull(defaultPrincipalAddress)) {
93                  defaultPrincipalAddress = new DefaultPrincipalAddress(GlobalVariables.getUserSession().getPerson().getPrincipalId());
94              }
95  
96              defaultPrincipalAddress.setDefaultBuilding(req.getDeliveryCampusCode(), req.getDeliveryBuildingCode(), req.getDeliveryBuildingRoomNumber());
97              SpringContext.getBean(BusinessObjectService.class).save(defaultPrincipalAddress);
98              KNSGlobalVariables.getMessageList().add(PurapKeyConstants.DEFAULT_BUILDING_SAVED);
99          }
100 
101         return mapping.findForward(OLEConstants.MAPPING_BASIC);
102     }
103 
104     /**
105      * @see org.kuali.rice.kns.web.struts.action.KualiAction#refresh(org.apache.struts.action.ActionMapping,
106      *      org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
107      */
108     @Override
109     public ActionForward refresh(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
110         ActionForward forward = super.refresh(mapping, form, request, response);
111         RequisitionForm rqForm = (RequisitionForm) form;
112         RequisitionDocument document = (RequisitionDocument) rqForm.getDocument();
113 
114         // super.refresh() must occur before this line to get the correct APO limit
115         document.setOrganizationAutomaticPurchaseOrderLimit(SpringContext.getBean(PurapService.class).getApoLimit(document.getVendorContractGeneratedIdentifier(), document.getChartOfAccountsCode(), document.getOrganizationCode()));
116 
117         return forward;
118     }
119 
120     /**
121      * Adds a PurchasingItemCapitalAsset (a container for the Capital Asset Number) to the selected
122      * item's list.
123      *
124      * @param mapping  An ActionMapping
125      * @param form     The Form
126      * @param request  An HttpServletRequest
127      * @param response The HttpServletResponse
128      * @return An ActionForward
129      * @throws Exception
130      */
131     public ActionForward addAsset(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
132         RequisitionForm rqForm = (RequisitionForm) form;
133         RequisitionDocument document = (RequisitionDocument) rqForm.getDocument();
134         RequisitionItem item = (RequisitionItem) document.getItemByLineNumber(getSelectedLine(request) + 1);
135         //TODO: Add a new way to add assets to the system.
136         //item.addAsset();
137         return mapping.findForward(OLEConstants.MAPPING_BASIC);
138     }
139 
140     public ActionForward displayB2BRequisition(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
141         RequisitionForm reqForm = (RequisitionForm) form;
142         reqForm.setDocId((String) request.getSession().getAttribute("docId"));
143         loadDocument(reqForm);
144         String multipleB2BReqs = (String) request.getSession().getAttribute("multipleB2BRequisitions");
145         if (StringUtils.isNotEmpty(multipleB2BReqs)) {
146             KNSGlobalVariables.getMessageList().add(PurapKeyConstants.B2B_MULTIPLE_REQUISITIONS);
147         }
148         request.getSession().removeAttribute("docId");
149         request.getSession().removeAttribute("multipleB2BRequisitions");
150         return mapping.findForward(OLEConstants.MAPPING_BASIC);
151     }
152 
153     /**
154      * Clears the vendor selection from the Requisition.  NOTE, this functionality is only available on Requisition and not PO.
155      *
156      * @param mapping  An ActionMapping
157      * @param form     An ActionForm
158      * @param request  A HttpServletRequest
159      * @param response A HttpServletResponse
160      * @return An ActionForward
161      * @throws Exception
162      */
163     public ActionForward clearVendor(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
164         PurchasingFormBase baseForm = (PurchasingFormBase) form;
165         OleRequisitionDocument document = (OleRequisitionDocument) baseForm.getDocument();
166 
167         document.setVendorHeaderGeneratedIdentifier(null);
168         document.setVendorDetailAssignedIdentifier(null);
169         document.setVendorDetail(null);
170         document.setVendorName("");
171         document.setVendorLine1Address("");
172         document.setVendorLine2Address("");
173         document.setVendorAddressInternationalProvinceName("");
174         document.setVendorCityName("");
175         document.setVendorStateCode("");
176         document.setVendorPostalCode("");
177         document.setVendorCountryCode("");
178         document.setVendorContractGeneratedIdentifier(null);
179         document.setVendorContract(null);
180         document.setVendorFaxNumber("");
181         document.setVendorCustomerNumber("");
182         document.setVendorAttentionName("");
183         document.setVendorAliasName("");
184         return mapping.findForward(OLEConstants.MAPPING_BASIC);
185     }
186 
187     /**
188      * Set up blanket approve indicator which will be used to decide if need to run accounting line validation at the time of
189      * blanket approve.
190      *
191      * @see org.kuali.ole.module.purap.document.web.struts.PurchasingActionBase#blanketApprove(org.apache.struts.action.ActionMapping,
192      *      org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
193      */
194     @Override
195     public ActionForward blanketApprove(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
196         RequisitionDocument document = (RequisitionDocument) ((PurchasingFormBase) form).getDocument();
197         document.setBlanketApproveRequest(true);
198         return super.blanketApprove(mapping, form, request, response);
199     }
200 }