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.PurapKeyConstants;
19  import org.kuali.ole.module.purap.PurapPropertyConstants;
20  import org.kuali.ole.module.purap.document.PaymentRequestDocument;
21  import org.kuali.ole.module.purap.document.service.PurapService;
22  import org.kuali.ole.sys.OLEPropertyConstants;
23  import org.kuali.ole.sys.document.validation.GenericValidation;
24  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
25  import org.kuali.rice.kew.api.WorkflowDocument;
26  import org.kuali.rice.krad.service.BusinessObjectService;
27  import org.kuali.rice.krad.service.PersistenceService;
28  import org.kuali.rice.krad.util.GlobalVariables;
29  import org.kuali.rice.krad.util.ObjectUtils;
30  
31  import java.util.Map;
32  
33  public class PaymentRequestPayDateNotPastValidation extends GenericValidation {
34  
35      private PurapService purapService;
36      private PersistenceService persistenceService;
37      private BusinessObjectService businessObjectService;
38  
39      /**
40       * Validates that the payment request date does not occur in the past.
41       */
42      public boolean validate(AttributedDocumentEvent event) {
43          boolean valid = true;
44          PaymentRequestDocument document = (PaymentRequestDocument) event.getDocument();
45          GlobalVariables.getMessageMap().clearErrorPath();
46          GlobalVariables.getMessageMap().addToErrorPath(OLEPropertyConstants.DOCUMENT);
47  
48          java.sql.Date paymentRequestPayDate = document.getPaymentRequestPayDate();
49          if (ObjectUtils.isNotNull(paymentRequestPayDate) && purapService.isDateInPast(paymentRequestPayDate)) {
50              // the pay date is in the past, now we need to check whether given the state of the document to determine whether a past pay date is allowed
51              WorkflowDocument workflowDocument = document.getDocumentHeader().getWorkflowDocument();
52              if (workflowDocument.isInitiated() || workflowDocument.isSaved()) {
53                  // past pay dates are not allowed if the document has never been routed (i.e. in saved or initiated state)
54                  // (note that this block will be run when a document is being routed, or re-saved after being routed
55                  valid &= false;
56                  GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PAYMENT_REQUEST_PAY_DATE, PurapKeyConstants.ERROR_INVALID_PAY_DATE);
57              } else {
58                  // otherwise, this document has already been routed
59                  // it's an error if the pay date has been changed from the pay date in the database and the new pay date is in the past
60                  // retrieve doc from DB, and compare the dates
61                  PaymentRequestDocument paymentRequestDocumentFromDatabase = retrievePaymentRequestDocumentFromDatabase(document);
62  
63                  if (ObjectUtils.isNull(paymentRequestDocumentFromDatabase)) {
64                      // this definitely should not happen
65                      throw new NullPointerException("Unable to find payment request document " + document.getDocumentNumber() + " from database");
66                  }
67  
68                  java.sql.Date paymentRequestPayDateFromDatabase = paymentRequestDocumentFromDatabase.getPaymentRequestPayDate();
69                  if (ObjectUtils.isNull(paymentRequestPayDateFromDatabase) || !paymentRequestPayDateFromDatabase.equals(paymentRequestPayDate)) {
70                      valid &= false;
71                      GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PAYMENT_REQUEST_PAY_DATE, PurapKeyConstants.ERROR_INVALID_PAY_DATE);
72                  }
73              }
74          }
75  
76          GlobalVariables.getMessageMap().clearErrorPath();
77  
78          return valid;
79      }
80  
81      /**
82       * Retrieves the payment request document from the database.  Note that the instance returned
83       *
84       * @param document the document to look in the database for
85       * @return an instance representing what's stored in the database for this instance
86       */
87      protected PaymentRequestDocument retrievePaymentRequestDocumentFromDatabase(PaymentRequestDocument document) {
88          Map primaryKeyValues = persistenceService.getPrimaryKeyFieldValues(document);
89          return (PaymentRequestDocument) businessObjectService.findByPrimaryKey(document.getClass(), primaryKeyValues);
90      }
91  
92      public PurapService getPurapService() {
93          return purapService;
94      }
95  
96      public void setPurapService(PurapService purapService) {
97          this.purapService = purapService;
98      }
99  
100     public PersistenceService getPersistenceService() {
101         return persistenceService;
102     }
103 
104     public void setPersistenceService(PersistenceService persistenceService) {
105         this.persistenceService = persistenceService;
106     }
107 
108     public BusinessObjectService getBusinessObjectService() {
109         return businessObjectService;
110     }
111 
112     public void setBusinessObjectService(BusinessObjectService businessObjectService) {
113         this.businessObjectService = businessObjectService;
114     }
115 
116 }