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.cg.document.validation.impl;
017    
018    import java.sql.Date;
019    
020    import org.kuali.kfs.module.cg.document.ProposalAwardCloseDocument;
021    import org.kuali.kfs.sys.KFSKeyConstants;
022    import org.kuali.kfs.sys.KFSPropertyConstants;
023    import org.kuali.kfs.sys.context.SpringContext;
024    import org.kuali.rice.kns.document.Document;
025    import org.kuali.rice.kns.rules.TransactionalDocumentRuleBase;
026    import org.kuali.rice.kns.service.DataDictionaryService;
027    import org.kuali.rice.kns.service.DateTimeService;
028    import org.kuali.rice.kns.util.GlobalVariables;
029    
030    /**
031     * Rules for handling Closes.
032     */
033    public class CloseDocumentRule extends TransactionalDocumentRuleBase {
034    
035        @Override
036        public boolean processCustomRouteDocumentBusinessRules(Document document) {
037            ProposalAwardCloseDocument closeDocument = (ProposalAwardCloseDocument) document;
038            Date today = SpringContext.getBean(DateTimeService.class).getCurrentSqlDateMidnight();
039            Date closeDate = closeDocument.getUserInitiatedCloseDate();
040            Date closeOnOrBeforeDate = closeDocument.getCloseOnOrBeforeDate();
041            DataDictionaryService dataDictionaryService = SpringContext.getBean(DataDictionaryService.class);
042            
043            boolean isValid = true;
044            boolean closeDateIsNotNull = true;
045            boolean closeDateIsTodayOrLater = true;
046            boolean closeOnOrBeforeDateIsNotNull = true;
047            boolean closeOnOrBeforeDateIsTodayOrLater = true;
048            boolean closeOnOrBeforeDateIsCloseDateOrEarlier = true;
049    
050            String closeDateLabel = dataDictionaryService.getAttributeLabel(ProposalAwardCloseDocument.class, "userInitiatedCloseDate");
051            String closeOnOrBeforeDateLabel = dataDictionaryService.getAttributeLabel(ProposalAwardCloseDocument.class, "closeOnOrBeforeDate");
052    
053            GlobalVariables.getMessageMap().addToErrorPath(KFSPropertyConstants.DOCUMENT);
054            if (closeDate == null) {
055                closeDateIsNotNull = false;
056                String label = dataDictionaryService.getAttributeLabel(ProposalAwardCloseDocument.class, "userInitiatedCloseDate");
057            } else if (today.getTime() > closeDate.getTime()) {
058                closeDateIsTodayOrLater = false;
059                GlobalVariables.getMessageMap().putError("userInitiatedCloseDate", KFSKeyConstants.ContractsAndGrants.USER_INITIATED_DATE_TOO_EARLY, closeDateLabel);
060            }
061            if (closeOnOrBeforeDate == null) {
062                closeOnOrBeforeDateIsNotNull = false;
063            } else if (today.getTime() > closeOnOrBeforeDate.getTime()) {
064                closeOnOrBeforeDateIsTodayOrLater = false;
065                GlobalVariables.getMessageMap().putError("closeOnOrBeforeDate", KFSKeyConstants.ContractsAndGrants.CLOSE_ON_OR_BEFORE_DATE_TOO_EARLY, closeOnOrBeforeDateLabel);
066            } else if (closeOnOrBeforeDate.getTime() > closeDate.getTime()) {
067                closeOnOrBeforeDateIsCloseDateOrEarlier = false;
068                GlobalVariables.getMessageMap().putError("closeOnOrBeforeDate", KFSKeyConstants.ContractsAndGrants.CLOSE_ON_OR_BEFORE_DATE_TOO_LATE, closeOnOrBeforeDateLabel, closeDateLabel);
069            }
070            GlobalVariables.getMessageMap().removeFromErrorPath(KFSPropertyConstants.DOCUMENT);
071            
072            isValid = closeDateIsNotNull && closeDateIsTodayOrLater && closeOnOrBeforeDateIsNotNull && closeOnOrBeforeDateIsTodayOrLater && closeOnOrBeforeDateIsCloseDateOrEarlier;
073            return isValid;
074        }
075    
076    }