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.PurchasingItemBase;
22  import org.kuali.ole.module.purap.document.PurchasingDocument;
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  public class PurchasingProcessContainsAtLeastOneItemValidation extends GenericValidation {
30  
31      /**
32       * Validates that the document contains at least one item.
33       *
34       * @param purDocument the purchasing document to be validated
35       * @return boolean false if the document does not contain at least one item.
36       */
37      public boolean validate(AttributedDocumentEvent event) {
38          boolean valid = false;
39          PurchasingDocument purDocument = (PurchasingDocument) event.getDocument();
40          for (PurApItem item : purDocument.getItems()) {
41              if (!((PurchasingItemBase) item).isEmpty() && item.getItemType().isLineItemIndicator()) {
42  
43                  return true;
44              }
45          }
46          String documentType = getDocumentTypeLabel(purDocument.getDocumentHeader().getWorkflowDocument().getDocumentTypeName());
47  
48          if (!valid) {
49              GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, PurapKeyConstants.ERROR_ITEM_REQUIRED, documentType);
50          }
51  
52          return valid;
53      }
54  
55      protected String getDocumentTypeLabel(String documentTypeName) {
56          return SpringContext.getBean(DocumentTypeService.class).getDocumentTypeByName(documentTypeName).getLabel();
57      }
58  
59  }