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.businessobject.PurApItem;
21  import org.kuali.ole.module.purap.businessobject.PurchaseOrderItem;
22  import org.kuali.ole.module.purap.document.PurchasingAccountsPayableDocument;
23  import org.kuali.ole.sys.context.SpringContext;
24  import org.kuali.ole.sys.document.validation.GenericValidation;
25  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
26  import org.kuali.rice.kew.api.doctype.DocumentTypeService;
27  import org.kuali.rice.krad.util.GlobalVariables;
28  
29  import java.util.List;
30  
31  public class PurchaseOrderAmendmentContainsAtLeastOneActiveItemValidation extends GenericValidation {
32  
33      /**
34       * Validates that the given document contains at least one active item.
35       *
36       * @param purapDocument A PurchasingAccountsPayableDocument. (Should contain PurchaseOrderItems.)
37       * @return True if the document contains at least one active item
38       */
39      public boolean validate(AttributedDocumentEvent event) {
40          PurchasingAccountsPayableDocument purapDocument = (PurchasingAccountsPayableDocument) event.getDocument();
41          List<PurApItem> items = purapDocument.getItems();
42          for (PurApItem item : items) {
43              if (((PurchaseOrderItem) item).isItemActiveIndicator() && (!((PurchaseOrderItem) item).isEmpty() && item.getItemType().isLineItemIndicator())) {
44                  return true;
45              }
46          }
47          String documentType = getDocumentTypeLabel(purapDocument.getDocumentHeader().getWorkflowDocument().getDocumentTypeName());
48          GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, PurapKeyConstants.ERROR_ITEM_REQUIRED, documentType);
49  
50          return false;
51      }
52  
53      protected String getDocumentTypeLabel(String documentTypeName) {
54          return SpringContext.getBean(DocumentTypeService.class).getDocumentTypeByName(documentTypeName).getLabel();
55  
56      }
57  
58  }