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.kuali.ole.module.purap.PurapConstants;
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.PurchasingAccountsPayableDocument;
23  import org.kuali.ole.sys.document.validation.GenericValidation;
24  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
25  import org.kuali.rice.krad.util.GlobalVariables;
26  
27  public class PurchasingProcessTradeInValidation extends GenericValidation {
28  
29      /**
30       * Validates that the total cost must be greater or equal to zero.
31       *
32       * @param purDocument the purchasing document to be validated
33       * @return boolean false if the total cost is less than zero.
34       */
35      public boolean validate(AttributedDocumentEvent event) {
36          boolean isAssignedToTradeInItemFound = false;
37          PurchasingAccountsPayableDocument purapDocument = (PurchasingAccountsPayableDocument) event.getDocument();
38          for (PurApItem item : purapDocument.getItems()) {
39              // Refresh the item type for validation.
40              item.refreshReferenceObject(PurapPropertyConstants.ITEM_TYPE);
41  
42              if (item.getItemType().isLineItemIndicator()) {
43                  if (item.getItemAssignedToTradeInIndicator()) {
44                      isAssignedToTradeInItemFound = true;
45                      break;
46                  }
47              }
48          }
49          if (!isAssignedToTradeInItemFound) {
50              PurApItem tradeInItem = purapDocument.getTradeInItem();
51              if (tradeInItem != null && tradeInItem.getItemUnitPrice() != null) {
52                  GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, PurapKeyConstants.ERROR_ITEM_TRADE_IN_NEEDS_TO_BE_ASSIGNED);
53                  return false;
54              }
55          }
56          return true;
57      }
58  }