View Javadoc

1   /*
2    * Copyright 2008-2009 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.document.InvoiceDocument;
21  import org.kuali.ole.sys.document.validation.GenericValidation;
22  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
23  import org.kuali.rice.kew.api.WorkflowDocument;
24  import org.kuali.rice.krad.bo.Note;
25  import org.kuali.rice.krad.util.GlobalVariables;
26  import org.kuali.rice.krad.util.KRADConstants;
27  import org.kuali.rice.krad.util.ObjectUtils;
28  
29  import java.util.List;
30  import java.util.Set;
31  
32  public class InvoiceImageAttachmentValidation extends GenericValidation {
33  
34      public boolean validate(AttributedDocumentEvent event) {
35          boolean valid = true;
36          InvoiceDocument document = (InvoiceDocument) event.getDocument();
37          GlobalVariables.getMessageMap().clearErrorPath();
38  
39          if (isDocumentStoppedInRouteNode(document, "ImageAttachment")) {
40              //assume false if we're in the correct node
41              valid = false;
42  
43              //loop through notes looking for a invoice image
44              List boNotes = document.getNotes();
45              if (ObjectUtils.isNotNull(boNotes)) {
46                  for (Object obj : boNotes) {
47                      Note note = (Note) obj;
48  
49                      note.refreshReferenceObject("attachment");
50                      if (ObjectUtils.isNotNull(note.getAttachment()) && PurapConstants.AttachmentTypeCodes.ATTACHMENT_TYPE_INVOICE_IMAGE.equals(note.getAttachment().getAttachmentTypeCode())) {
51                          valid = true;
52                          break;
53                      }
54                  }
55              }
56  
57              if (valid == false) {
58                  GlobalVariables.getMessageMap().putError(KRADConstants.NEW_DOCUMENT_NOTE_PROPERTY_NAME, PurapKeyConstants.ERROR_PAYMENT_REQUEST_INVOICE_REQUIRED);
59              }
60          }
61  
62          GlobalVariables.getMessageMap().clearErrorPath();
63  
64          return valid;
65      }
66  
67      /**
68       * @param document
69       * @param nodeName
70       * @return
71       */
72      protected boolean isDocumentStoppedInRouteNode(InvoiceDocument document, String nodeName) {
73          WorkflowDocument workflowDoc = document.getDocumentHeader().getWorkflowDocument();
74          Set<String> currentRouteLevels = workflowDoc.getCurrentNodeNames();
75          if (currentRouteLevels.contains(nodeName) && workflowDoc.isApprovalRequested()) {
76              return true;
77          }
78          return false;
79      }
80  }