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.document.PurchasingDocument;
22  import org.kuali.ole.sys.document.validation.GenericValidation;
23  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
24  import org.kuali.ole.sys.service.UniversityDateService;
25  import org.kuali.rice.core.api.datetime.DateTimeService;
26  import org.kuali.rice.krad.util.GlobalVariables;
27  import org.kuali.rice.krad.util.ObjectUtils;
28  
29  import java.util.Date;
30  
31  public class PurchasingPaymentInfoValidation extends GenericValidation {
32  
33      DateTimeService dateTimeService;
34      UniversityDateService universityDateService;
35  
36      public boolean validate(AttributedDocumentEvent event) {
37          boolean valid = true;
38          PurchasingDocument purDocument = (PurchasingDocument) event.getDocument();
39  
40          GlobalVariables.getMessageMap().addToErrorPath(PurapConstants.PAYMENT_INFO_ERRORS);
41          valid &= checkBeginDateBeforeEndDate(purDocument);
42          if (valid && (ObjectUtils.isNotNull(purDocument.getPurchaseOrderBeginDate()) || ObjectUtils.isNotNull(purDocument.getPurchaseOrderEndDate()))) {
43              if (ObjectUtils.isNotNull(purDocument.getPurchaseOrderBeginDate()) && ObjectUtils.isNull(purDocument.getPurchaseOrderEndDate())) {
44                  GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_END_DATE, PurapKeyConstants.ERROR_PURCHASE_ORDER_BEGIN_DATE_NO_END_DATE);
45                  valid &= false;
46              } else {
47                  if (ObjectUtils.isNull(purDocument.getPurchaseOrderBeginDate()) && ObjectUtils.isNotNull(purDocument.getPurchaseOrderEndDate())) {
48                      GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_BEGIN_DATE, PurapKeyConstants.ERROR_PURCHASE_ORDER_END_DATE_NO_BEGIN_DATE);
49                      valid &= false;
50                  }
51              }
52          }
53          if (valid && ObjectUtils.isNotNull(purDocument.getPurchaseOrderBeginDate()) && ObjectUtils.isNotNull(purDocument.getPurchaseOrderEndDate())) {
54              if (ObjectUtils.isNull(purDocument.getRecurringPaymentTypeCode())) {
55                  GlobalVariables.getMessageMap().putError(PurapPropertyConstants.RECURRING_PAYMENT_TYPE_CODE, PurapKeyConstants.ERROR_RECURRING_DATE_NO_TYPE);
56  
57                  valid &= false;
58              }
59          } else if (valid && ObjectUtils.isNotNull(purDocument.getRecurringPaymentTypeCode())) {
60              GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_BEGIN_DATE, PurapKeyConstants.ERROR_RECURRING_TYPE_NO_DATE);
61              valid &= false;
62          }
63  
64          //checks for when FY is set to next FY
65          if (purDocument.isPostingYearNext()) {
66              Integer currentFY = universityDateService.getCurrentFiscalYear();
67              Date closingDate = universityDateService.getLastDateOfFiscalYear(currentFY);
68  
69              //if recurring payment begin dates entered, begin date must be > closing date
70              if (ObjectUtils.isNotNull(purDocument.getPurchaseOrderBeginDate()) &&
71                      (purDocument.getPurchaseOrderBeginDate().before(closingDate) ||
72                              purDocument.getPurchaseOrderBeginDate().equals(closingDate))) {
73                  GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_BEGIN_DATE, PurapKeyConstants.ERROR_NEXT_FY_BEGIN_DATE_INVALID);
74                  valid &= false;
75              }
76          }
77  
78          GlobalVariables.getMessageMap().removeFromErrorPath(PurapConstants.PAYMENT_INFO_ERRORS);
79          return valid;
80      }
81  
82      /**
83       * Implementation of the rule that if a document has a recurring payment begin date and end date, the begin date should come
84       * before the end date. We needed to play around with this order if the fiscal year is the next fiscal year, since we
85       * were dealing just with month and day, but we don't need to do that here; we're dealing with the whole Date object.
86       *
87       * @param purDocument the purchasing document to be validated
88       * @return boolean false if the begin date is not before the end date.
89       */
90      protected boolean checkBeginDateBeforeEndDate(PurchasingDocument purDocument) {
91          boolean valid = true;
92  
93          Date beginDate = purDocument.getPurchaseOrderBeginDate();
94          Date endDate = purDocument.getPurchaseOrderEndDate();
95          if (ObjectUtils.isNotNull(beginDate) && ObjectUtils.isNotNull(endDate)) {
96              if (dateTimeService.dateDiff(beginDate, endDate, false) <= 0) {
97                  valid &= false;
98                  GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_END_DATE, PurapKeyConstants.ERROR_PURCHASE_ORDER_BEGIN_DATE_AFTER_END);
99              }
100         }
101 
102         return valid;
103     }
104 
105     public DateTimeService getDateTimeService() {
106         return dateTimeService;
107     }
108 
109     public void setDateTimeService(DateTimeService dateTimeService) {
110         this.dateTimeService = dateTimeService;
111     }
112 
113     public UniversityDateService getUniversityDateService() {
114         return universityDateService;
115     }
116 
117     public void setUniversityDateService(UniversityDateService universityDateService) {
118         this.universityDateService = universityDateService;
119     }
120 
121 }