View Javadoc
1   /*
2    * Copyright 2008 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.kuali.ole.module.purap.PurapAuthorizationConstants;
19  import org.kuali.ole.module.purap.PurapConstants;
20  import org.kuali.ole.module.purap.PurapParameterConstants;
21  import org.kuali.ole.module.purap.businessobject.LineItemReceivingItem;
22  import org.kuali.ole.module.purap.document.LineItemReceivingDocument;
23  import org.kuali.ole.module.purap.document.service.ReceivingService;
24  import org.kuali.ole.sys.OLEConstants;
25  import org.kuali.ole.sys.context.SpringContext;
26  import org.kuali.rice.coreservice.framework.parameter.ParameterService;
27  import org.kuali.rice.kew.api.WorkflowDocument;
28  import org.kuali.rice.kim.api.identity.Person;
29  import org.kuali.rice.kns.document.authorization.DocumentAuthorizer;
30  import org.kuali.rice.kns.service.DocumentHelperService;
31  import org.kuali.rice.kns.web.ui.ExtraButton;
32  import org.kuali.rice.kns.web.ui.HeaderField;
33  import org.kuali.rice.krad.util.GlobalVariables;
34  import org.kuali.rice.krad.util.ObjectUtils;
35  
36  import java.util.HashMap;
37  import java.util.List;
38  import java.util.Map;
39  
40  public class LineItemReceivingForm extends ReceivingFormBase {
41  
42      protected Integer purchaseOrderId;
43      protected LineItemReceivingItem newLineItemReceivingItemLine;
44      protected boolean fromPurchaseOrder = false;
45      protected Boolean hideAddUnorderedItem = true;
46  
47      /**
48       * Constructs a LineItemReceivingForm instance and sets up the appropriately casted document.
49       */
50      public LineItemReceivingForm() {
51          super();
52  
53          this.setNewLineItemReceivingItemLine(setupNewLineItemReceivingItemLine());
54          newLineItemReceivingItemLine.setItemTypeCode(PurapConstants.ItemTypeCodes.ITEM_TYPE_UNORDERED_ITEM_CODE);
55      }
56  
57      @Override
58      protected String getDefaultDocumentTypeName() {
59          return "OLE_RCVL";
60      }
61  
62      public LineItemReceivingDocument getLineItemReceivingDocument() {
63          return (LineItemReceivingDocument) getDocument();
64      }
65  
66      public void setLineItemReceivingDocument(LineItemReceivingDocument lineItemReceivingDocument) {
67          setDocument(lineItemReceivingDocument);
68      }
69  
70      public Integer getPurchaseOrderId() {
71          return purchaseOrderId;
72      }
73  
74      public void setPurchaseOrderId(Integer purchaseOrderId) {
75          this.purchaseOrderId = purchaseOrderId;
76      }
77  
78      public LineItemReceivingItem setupNewLineItemReceivingItemLine() {
79          return new LineItemReceivingItem();
80      }
81  
82      public LineItemReceivingItem getNewLineItemReceivingItemLine() {
83          return newLineItemReceivingItemLine;
84      }
85  
86      public void setNewLineItemReceivingItemLine(LineItemReceivingItem newLineItemReceivingItemLine) {
87          this.newLineItemReceivingItemLine = newLineItemReceivingItemLine;
88      }
89  
90      @Override
91      public void populateHeaderFields(WorkflowDocument workflowDocument) {
92          super.populateHeaderFields(workflowDocument);
93  
94          //leave the first field blank to match the other PURAP docs
95          getDocInfo().add(new HeaderField());
96  
97          String applicationDocumentStatus = PurapConstants.PURAP_APPLICATION_DOCUMENT_STATUS_NOT_AVAILABLE;
98  
99          if (ObjectUtils.isNotNull(this.getLineItemReceivingDocument().getAppDocStatus())) {
100             applicationDocumentStatus = workflowDocument.getApplicationDocumentStatus();
101         }
102 
103         getDocInfo().add(new HeaderField("DataDictionary.LineItemReceivingDocument.attributes.applicationDocumentStatus", applicationDocumentStatus));
104     }
105 
106     /**
107      * Override the superclass method to add appropriate buttons for LineItemReceivingDocument.
108      *
109      * @see org.kuali.rice.kns.web.struts.form.KualiForm#getExtraButtons()
110      */
111     @Override
112     public List<ExtraButton> getExtraButtons() {
113         extraButtons.clear();
114         Map buttonsMap = createButtonsMap();
115 
116         String displayInitTab = (String) getEditingMode().get(PurapAuthorizationConstants.LineItemReceivingEditMode.DISPLAY_INIT_TAB);
117         if (ObjectUtils.isNotNull(displayInitTab) && displayInitTab.equalsIgnoreCase("true")) {
118             extraButtons.add((ExtraButton) buttonsMap.get("methodToCall.continueReceivingLine"));
119             extraButtons.add((ExtraButton) buttonsMap.get("methodToCall.clearInitFields"));
120         } else {
121             if (canCreateCorrection()) {
122                 extraButtons.add((ExtraButton) buttonsMap.get("methodToCall.createReceivingCorrection"));
123             }
124         }
125 
126         return extraButtons;
127     }
128 
129     protected boolean canCreateCorrection() {
130         Person user = GlobalVariables.getUserSession().getPerson();
131         DocumentAuthorizer documentAuthorizer = SpringContext.getBean(DocumentHelperService.class).getDocumentAuthorizer(OLEConstants.FinancialDocumentTypeCodes.CORRECTION_RECEIVING);
132         boolean isUserAuthorized = documentAuthorizer.canInitiate(OLEConstants.FinancialDocumentTypeCodes.CORRECTION_RECEIVING, user);
133         return SpringContext.getBean(ReceivingService.class).canCreateCorrectionReceivingDocument(getLineItemReceivingDocument()) && isUserAuthorized;
134     }
135 
136     /**
137      * Creates a MAP for all the buttons to appear on the Receiving Line Form, and sets the attributes of these buttons.
138      *
139      * @return the button map created.
140      */
141     protected Map<String, ExtraButton> createButtonsMap() {
142         HashMap<String, ExtraButton> result = new HashMap<String, ExtraButton>();
143 
144         // Continue button
145         ExtraButton continueButton = new ExtraButton();
146         continueButton.setExtraButtonProperty("methodToCall.continueReceivingLine");
147         continueButton.setExtraButtonSource("${" + OLEConstants.RICE_EXTERNALIZABLE_IMAGES_URL_KEY + "}buttonsmall_continue.gif");
148         continueButton.setExtraButtonAltText("Continue");
149         result.put(continueButton.getExtraButtonProperty(), continueButton);
150 
151         // Clear button
152         ExtraButton clearButton = new ExtraButton();
153         clearButton.setExtraButtonProperty("methodToCall.clearInitFields");
154         clearButton.setExtraButtonSource("${" + OLEConstants.RICE_EXTERNALIZABLE_IMAGES_URL_KEY + "}buttonsmall_clear.gif");
155         clearButton.setExtraButtonAltText("Clear");
156         result.put(clearButton.getExtraButtonProperty(), clearButton);
157 
158         // Correction button
159         ExtraButton correctionButton = new ExtraButton();
160         correctionButton.setExtraButtonProperty("methodToCall.createReceivingCorrection");
161         correctionButton.setExtraButtonSource("${" + OLEConstants.EXTERNALIZABLE_IMAGES_URL_KEY + "}buttonsmall_correction.gif");
162         correctionButton.setExtraButtonAltText("Correction");
163         result.put(correctionButton.getExtraButtonProperty(), correctionButton);
164 
165         return result;
166     }
167 
168     /**
169      * Returns the new Receiving Item Line and resets it to null.
170      *
171      * @return the new Receiving Item Line.
172      */
173     public LineItemReceivingItem getAndResetNewReceivingItemLine() {
174         LineItemReceivingItem receivingItemLine = getNewLineItemReceivingItemLine();
175         setNewLineItemReceivingItemLine(setupNewReceivingItemLine());
176         return receivingItemLine;
177     }
178 
179     /**
180      * This method should be overriden (or see accountingLines for an alternate way of doing this with newInstance)
181      */
182     public LineItemReceivingItem setupNewReceivingItemLine() {
183         LineItemReceivingItem lineItemReceivingItem = new LineItemReceivingItem((LineItemReceivingDocument) getDocument());
184         newLineItemReceivingItemLine.setItemTypeCode(PurapConstants.ItemTypeCodes.ITEM_TYPE_UNORDERED_ITEM_CODE);
185         return lineItemReceivingItem;
186     }
187 
188     /**
189      * Indicates if the clear and load quantity buttons can be shown, according to the
190      * value of a system parameter.
191      *
192      * @return
193      */
194     public boolean isAbleToShowClearAndLoadQtyButtons() {
195         return SpringContext.getBean(ParameterService.class).getParameterValueAsBoolean(LineItemReceivingDocument.class, PurapParameterConstants.SHOW_CLEAR_AND_LOAD_QTY_BUTTONS);
196     }
197 
198     /**
199      * Indicates if a warning should be given when users click "add unordered item" button, according to the system parameter.
200      *
201      * @return true if the parameter says YES; otherwise faluse.
202      */
203     public boolean shouldGiveAddUnorderedItemWarning() {
204         return SpringContext.getBean(ParameterService.class).getParameterValueAsBoolean(LineItemReceivingDocument.class, PurapParameterConstants.UNORDERED_ITEM_WARNING_IND);
205     }
206 
207     public boolean isFromPurchaseOrder() {
208         return fromPurchaseOrder;
209     }
210 
211     public void setFromPurchaseOrder(boolean fromPurchaseOrder) {
212         this.fromPurchaseOrder = fromPurchaseOrder;
213     }
214 
215     public Boolean getHideAddUnorderedItem() {
216         return hideAddUnorderedItem;
217     }
218 
219     public void setHideAddUnorderedItem(Boolean hideAddUnorderedItem) {
220         this.hideAddUnorderedItem = hideAddUnorderedItem;
221     }
222 
223 }