001    /*
002     * Copyright 2011 The Kuali Foundation.
003     * 
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     * http://www.opensource.org/licenses/ecl2.php
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.kfs.module.purap.document.validation.impl;
017    
018    import java.util.Map;
019    
020    import org.kuali.kfs.module.purap.PurapKeyConstants;
021    import org.kuali.kfs.module.purap.PurapPropertyConstants;
022    import org.kuali.kfs.module.purap.document.PaymentRequestDocument;
023    import org.kuali.kfs.module.purap.document.service.PurapService;
024    import org.kuali.kfs.sys.KFSPropertyConstants;
025    import org.kuali.kfs.sys.context.SpringContext;
026    import org.kuali.kfs.sys.document.validation.GenericValidation;
027    import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
028    import org.kuali.rice.kns.service.BusinessObjectService;
029    import org.kuali.rice.kns.service.PersistenceService;
030    import org.kuali.rice.kns.util.GlobalVariables;
031    import org.kuali.rice.kns.util.ObjectUtils;
032    import org.kuali.rice.kns.workflow.service.KualiWorkflowDocument;
033    
034    public class PaymentRequestPayDateNotPastValidation extends GenericValidation {
035    
036        private PurapService purapService;
037        private PersistenceService persistenceService;
038        private BusinessObjectService businessObjectService;
039        
040        /**
041         * Validates that the payment request date does not occur in the past.
042         *
043         */
044        public boolean validate(AttributedDocumentEvent event) {
045            boolean valid = true;
046            PaymentRequestDocument document = (PaymentRequestDocument)event.getDocument();
047            GlobalVariables.getMessageMap().clearErrorPath();
048            GlobalVariables.getMessageMap().addToErrorPath(KFSPropertyConstants.DOCUMENT);
049    
050            java.sql.Date paymentRequestPayDate = document.getPaymentRequestPayDate();
051            if (ObjectUtils.isNotNull(paymentRequestPayDate) && purapService.isDateInPast(paymentRequestPayDate)) {
052                // 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
053                KualiWorkflowDocument workflowDocument = document.getDocumentHeader().getWorkflowDocument(); 
054                if (workflowDocument.stateIsInitiated() || workflowDocument.stateIsSaved()) {
055                    // past pay dates are not allowed if the document has never been routed (i.e. in saved or initiated state)
056                    // (note that this block will be run when a document is being routed, or re-saved after being routed
057                    valid &= false;
058                    GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PAYMENT_REQUEST_PAY_DATE, PurapKeyConstants.ERROR_INVALID_PAY_DATE);
059                } else {
060                    // otherwise, this document has already been routed
061                    // 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
062                    // retrieve doc from DB, and compare the dates
063                    PaymentRequestDocument paymentRequestDocumentFromDatabase = retrievePaymentRequestDocumentFromDatabase(document);
064                    
065                    if (ObjectUtils.isNull(paymentRequestDocumentFromDatabase)) {
066                        // this definitely should not happen
067                        throw new NullPointerException("Unable to find payment request document " + document.getDocumentNumber() + " from database");
068                    }
069                    
070                    java.sql.Date paymentRequestPayDateFromDatabase = paymentRequestDocumentFromDatabase.getPaymentRequestPayDate();
071                    if (ObjectUtils.isNull(paymentRequestPayDateFromDatabase) || !paymentRequestPayDateFromDatabase.equals(paymentRequestPayDate)) {
072                        valid &= false;
073                        GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PAYMENT_REQUEST_PAY_DATE, PurapKeyConstants.ERROR_INVALID_PAY_DATE);
074                    }
075                }
076            }
077            
078            GlobalVariables.getMessageMap().clearErrorPath();
079            
080            return valid;
081        }
082    
083        /**
084         * Retrieves the payment request document from the database.  Note that the instance returned 
085         * @param document the document to look in the database for
086         * @return an instance representing what's stored in the database for this instance
087         */
088        protected PaymentRequestDocument retrievePaymentRequestDocumentFromDatabase(PaymentRequestDocument document) {
089            Map primaryKeyValues = persistenceService.getPrimaryKeyFieldValues(document);
090            return (PaymentRequestDocument) businessObjectService.findByPrimaryKey(document.getClass(), primaryKeyValues);
091        }
092    
093        public PurapService getPurapService() {
094            return purapService;
095        }
096    
097        public void setPurapService(PurapService purapService) {
098            this.purapService = purapService;
099        }
100    
101        public PersistenceService getPersistenceService() {
102            return persistenceService;
103        }
104    
105        public void setPersistenceService(PersistenceService persistenceService) {
106            this.persistenceService = persistenceService;
107        }
108    
109        public BusinessObjectService getBusinessObjectService() {
110            return businessObjectService;
111        }
112    
113        public void setBusinessObjectService(BusinessObjectService businessObjectService) {
114            this.businessObjectService = businessObjectService;
115        }
116    
117    }