View Javadoc
1   /*
2    * Copyright 2010 The Kuali Foundation.
3    * 
4    * Licensed under the Educational Community License, Version 1.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/ecl1.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.module.purap.PurapKeyConstants;
20  import org.kuali.ole.module.purap.PurapPropertyConstants;
21  import org.kuali.ole.module.purap.businessobject.PurApItem;
22  import org.kuali.ole.module.purap.document.PurchasingAccountsPayableDocumentBase;
23  import org.kuali.ole.sys.document.validation.GenericValidation;
24  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
25  import org.kuali.rice.kns.service.DataDictionaryService;
26  import org.kuali.rice.krad.util.GlobalVariables;
27  import org.kuali.rice.krad.util.ObjectUtils;
28  
29  import java.util.List;
30  
31  public class RequisitionAssignToTradeInValidation extends GenericValidation {
32      private DataDictionaryService dataDictionaryService;
33  
34      public boolean validate(AttributedDocumentEvent event) {
35  
36          // Initialize the valid and true, and get the requisition document.
37          boolean foundTradeIn = false;
38          boolean valid = true;
39  
40          PurchasingAccountsPayableDocumentBase purapDoc =
41                  (PurchasingAccountsPayableDocumentBase) event.getDocument();
42  
43          // First, get all the items from the requisition document.  For each of
44          // the items, look for the ones that are assigned to a trade-in value.
45          // For these trade-in items, validate that the trade-in line has a valid
46          // description and amount.
47          List<PurApItem> items = (List<PurApItem>) purapDoc.getItems();
48          for (PurApItem item : items) {
49              item.refreshReferenceObject(PurapPropertyConstants.ITEM_TYPE);
50              if (item.getItemAssignedToTradeInIndicator()) {
51                  foundTradeIn = true;
52                  break;
53              }
54          }
55  
56          // Was a trade-in found for any of the above items?
57          if (foundTradeIn) {
58  
59              // Get the trade-in item.
60              PurApItem tradeInItem = purapDoc.getTradeInItem();
61              if (tradeInItem != null) {
62                  if (StringUtils.isEmpty(tradeInItem.getItemDescription())) {
63  
64                      String attributeLabel = dataDictionaryService.getDataDictionary().getBusinessObjectEntry(tradeInItem.getClass().getName()).getAttributeDefinition(PurapPropertyConstants.ITEM_DESCRIPTION).getLabel();
65                      tradeInItem.getItemLineNumber();
66                      GlobalVariables.getMessageMap().putError(
67                              "document.item[" + (items.size() - 1) + "]." + PurapPropertyConstants.ITEM_DESCRIPTION,
68                              PurapKeyConstants.ERROR_ITEM_BELOW_THE_LINE,
69                              "The item description of " + tradeInItem.getItemType().getItemTypeDescription(),
70                              "empty");
71  
72                      valid = false;
73                  } else if (ObjectUtils.isNull(tradeInItem.getItemUnitPrice())) {
74  
75                      String attributeLabel = dataDictionaryService.getDataDictionary().getBusinessObjectEntry(tradeInItem.getClass().getName()).getAttributeDefinition(PurapPropertyConstants.ITEM_UNIT_PRICE).getLabel();
76                      GlobalVariables.getMessageMap().putError(
77                              "document.item[" + (items.size() - 1) + "]." + PurapPropertyConstants.ITEM_UNIT_PRICE,
78                              PurapKeyConstants.ERROR_ITEM_BELOW_THE_LINE,
79                              tradeInItem.getItemType().getItemTypeDescription(),
80                              "zero");
81  
82                      valid = false;
83                  }
84              }
85          }
86  
87          return valid;
88      }
89  
90      /**
91       * Sets the dataDictionaryService attribute value.
92       *
93       * @param dataDictionaryService The dataDictionaryService to set.
94       */
95      public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
96          this.dataDictionaryService = dataDictionaryService;
97      }
98  
99  }