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.validation.impl;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.ole.integration.cab.CapitalAssetBuilderModuleService;
20  import org.kuali.ole.module.purap.PurapConstants;
21  import org.kuali.ole.module.purap.PurapKeyConstants;
22  import org.kuali.ole.module.purap.PurapPropertyConstants;
23  import org.kuali.ole.module.purap.businessobject.PurApItem;
24  import org.kuali.ole.module.purap.businessobject.PurchasingItemBase;
25  import org.kuali.ole.module.purap.document.PurchasingDocument;
26  import org.kuali.ole.sys.OLEKeyConstants;
27  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
28  import org.kuali.ole.vnd.businessobject.CommodityCode;
29  import org.kuali.rice.krad.service.BusinessObjectService;
30  import org.kuali.rice.krad.util.GlobalVariables;
31  
32  import java.util.HashMap;
33  import java.util.Map;
34  
35  public class PurchasingNewIndividualItemValidation extends PurchasingAccountsPayableNewIndividualItemValidation {
36  
37      private BusinessObjectService businessObjectService;
38      private CapitalAssetBuilderModuleService capitalAssetBuilderModuleService;
39      private PurchasingUnitOfMeasureValidation unitOfMeasureValidation;
40      private PurchasingItemUnitPriceValidation itemUnitPriceValidation;
41      private PurchasingItemDescriptionValidation itemDescriptionValidation;
42      private PurchasingItemQuantityValidation itemQuantityValidation;
43      private PurchasingBelowTheLineItemNoUnitCostValidation belowTheLineItemNoUnitCostValidation;
44  
45      public boolean validate(AttributedDocumentEvent event) {
46          boolean valid = true;
47          valid &= super.validate(event);
48          String recurringPaymentTypeCode = ((PurchasingDocument) event.getDocument()).getRecurringPaymentTypeCode();
49          //Capital asset validations are only done on line items (not additional charge items).
50          if (!getItemForValidation().getItemType().isAdditionalChargeIndicator()) {
51              valid &= capitalAssetBuilderModuleService.validateItemCapitalAssetWithErrors(recurringPaymentTypeCode, getItemForValidation(), false);
52          }
53          unitOfMeasureValidation.setItemForValidation(getItemForValidation());
54          valid &= unitOfMeasureValidation.validate(event);
55  
56          if (getItemForValidation().getItemType().isLineItemIndicator()) {
57              itemUnitPriceValidation.setItemForValidation(getItemForValidation());
58              valid &= itemUnitPriceValidation.validate(event);
59  
60              itemDescriptionValidation.setItemForValidation(getItemForValidation());
61              valid &= itemDescriptionValidation.validate(event);
62  
63              itemQuantityValidation.setItemForValidation(getItemForValidation());
64              valid &= itemQuantityValidation.validate(event);
65  
66              valid &= validateCommodityCodes(getItemForValidation(), commodityCodeIsRequired());
67          } else {
68              // No accounts can be entered on below-the-line items that have no unit cost.
69              belowTheLineItemNoUnitCostValidation.setItemForValidation(getItemForValidation());
70              valid &= belowTheLineItemNoUnitCostValidation.validate(event);
71          }
72          return valid;
73      }
74  
75      protected boolean commodityCodeIsRequired() {
76          return false;
77      }
78  
79      /**
80       * Validates that the document contains at least one item.
81       *
82       * @param purDocument the purchasing document to be validated
83       * @return boolean false if the document does not contain at least one item.
84       */
85      public boolean validateContainsAtLeastOneItem(PurchasingDocument purDocument) {
86          boolean valid = false;
87          for (PurApItem item : purDocument.getItems()) {
88              if (!((PurchasingItemBase) item).isEmpty() && item.getItemType().isLineItemIndicator()) {
89  
90                  return true;
91              }
92          }
93          String documentType = getDocumentTypeLabel(purDocument.getDocumentHeader().getWorkflowDocument().getDocumentTypeName());
94  
95          if (!valid) {
96              GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, PurapKeyConstants.ERROR_ITEM_REQUIRED, documentType);
97          }
98  
99          return valid;
100     }
101 
102     /**
103      * Validates whether the commodity code existed on the item, and if existed, whether the
104      * commodity code on the item existed in the database, and if so, whether the commodity
105      * code is active. Display error if any of these 3 conditions are not met.
106      *
107      * @param item The PurApItem containing the commodity code to be validated.
108      * @return boolean false if the validation fails and true otherwise.
109      */
110     protected boolean validateCommodityCodes(PurApItem item, boolean commodityCodeRequired) {
111         boolean valid = true;
112         String identifierString = item.getItemIdentifierString();
113         PurchasingItemBase purItem = (PurchasingItemBase) item;
114 
115         //This validation is only needed if the commodityCodeRequired system parameter is true
116         if (commodityCodeRequired && StringUtils.isBlank(purItem.getPurchasingCommodityCode())) {
117             //This is the case where the commodity code is required but the item does not currently contain the commodity code.
118             valid = false;
119             String attributeLabel = getDataDictionaryService().getDataDictionary().getBusinessObjectEntry(CommodityCode.class.getName()).
120                     getAttributeDefinition(PurapPropertyConstants.ITEM_COMMODITY_CODE).getLabel();
121             GlobalVariables.getMessageMap().putError(PurapPropertyConstants.ITEM_COMMODITY_CODE, OLEKeyConstants.ERROR_REQUIRED, attributeLabel + " in " + identifierString);
122         } else if (StringUtils.isNotBlank(purItem.getPurchasingCommodityCode())) {
123             //Find out whether the commodity code has existed in the database
124             Map<String, String> fieldValues = new HashMap<String, String>();
125             fieldValues.put(PurapPropertyConstants.ITEM_COMMODITY_CODE, purItem.getPurchasingCommodityCode());
126             if (businessObjectService.countMatching(CommodityCode.class, fieldValues) != 1) {
127                 //This is the case where the commodity code on the item does not exist in the database.
128                 valid = false;
129                 GlobalVariables.getMessageMap().putError(PurapPropertyConstants.ITEM_COMMODITY_CODE, PurapKeyConstants.PUR_COMMODITY_CODE_INVALID, " in " + identifierString);
130             } else {
131                 valid &= validateThatCommodityCodeIsActive(item);
132             }
133         }
134 
135         return valid;
136     }
137 
138     protected boolean validateThatCommodityCodeIsActive(PurApItem item) {
139         item.refreshReferenceObject(PurapPropertyConstants.COMMODITY_CODE);
140         if (!((PurchasingItemBase) item).getCommodityCode().isActive()) {
141             //This is the case where the commodity code on the item is not active.
142             GlobalVariables.getMessageMap().putError(PurapPropertyConstants.ITEM_COMMODITY_CODE, PurapKeyConstants.PUR_COMMODITY_CODE_INACTIVE, " in " + item.getItemIdentifierString());
143             return false;
144         }
145         return true;
146     }
147 
148     public BusinessObjectService getBusinessObjectService() {
149         return businessObjectService;
150     }
151 
152     public void setBusinessObjectService(BusinessObjectService businessObjectService) {
153         this.businessObjectService = businessObjectService;
154     }
155 
156     public CapitalAssetBuilderModuleService getCapitalAssetBuilderModuleService() {
157         return capitalAssetBuilderModuleService;
158     }
159 
160     public void setCapitalAssetBuilderModuleService(CapitalAssetBuilderModuleService capitalAssetBuilderModuleService) {
161         this.capitalAssetBuilderModuleService = capitalAssetBuilderModuleService;
162     }
163 
164     public PurchasingUnitOfMeasureValidation getUnitOfMeasureValidation() {
165         return unitOfMeasureValidation;
166     }
167 
168     public void setUnitOfMeasureValidation(PurchasingUnitOfMeasureValidation unitOfMeasureValidation) {
169         this.unitOfMeasureValidation = unitOfMeasureValidation;
170     }
171 
172     public PurchasingItemUnitPriceValidation getItemUnitPriceValidation() {
173         return itemUnitPriceValidation;
174     }
175 
176     public void setItemUnitPriceValidation(PurchasingItemUnitPriceValidation itemUnitPriceValidation) {
177         this.itemUnitPriceValidation = itemUnitPriceValidation;
178     }
179 
180     public PurchasingItemDescriptionValidation getItemDescriptionValidation() {
181         return itemDescriptionValidation;
182     }
183 
184     public void setItemDescriptionValidation(PurchasingItemDescriptionValidation itemDescriptionValidation) {
185         this.itemDescriptionValidation = itemDescriptionValidation;
186     }
187 
188     public PurchasingItemQuantityValidation getItemQuantityValidation() {
189         return itemQuantityValidation;
190     }
191 
192     public void setItemQuantityValidation(PurchasingItemQuantityValidation itemQuantityValidation) {
193         this.itemQuantityValidation = itemQuantityValidation;
194     }
195 
196     public PurchasingBelowTheLineItemNoUnitCostValidation getBelowTheLineItemNoUnitCostValidation() {
197         return belowTheLineItemNoUnitCostValidation;
198     }
199 
200     public void setBelowTheLineItemNoUnitCostValidation(PurchasingBelowTheLineItemNoUnitCostValidation belowTheLineItemNoUnitCostValidation) {
201         this.belowTheLineItemNoUnitCostValidation = belowTheLineItemNoUnitCostValidation;
202     }
203 
204 
205 }