001/*
002 * Copyright 2007 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 */
016package org.kuali.ole.module.cg.document.validation.impl;
017
018import java.sql.Date;
019
020import org.kuali.ole.module.cg.document.ProposalAwardCloseDocument;
021import org.kuali.ole.sys.OLEKeyConstants;
022import org.kuali.ole.sys.OLEPropertyConstants;
023import org.kuali.ole.sys.context.SpringContext;
024import org.kuali.rice.core.api.datetime.DateTimeService;
025import org.kuali.rice.kns.rules.TransactionalDocumentRuleBase;
026import org.kuali.rice.kns.service.DataDictionaryService;
027import org.kuali.rice.krad.document.Document;
028import org.kuali.rice.krad.util.GlobalVariables;
029
030/**
031 * Rules for handling Closes.
032 */
033public 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(OLEPropertyConstants.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", OLEKeyConstants.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", OLEKeyConstants.ContractsAndGrants.CLOSE_ON_OR_BEFORE_DATE_TOO_EARLY, closeOnOrBeforeDateLabel);
066        } else if (closeOnOrBeforeDate.getTime() > closeDate.getTime()) {
067            closeOnOrBeforeDateIsCloseDateOrEarlier = false;
068            GlobalVariables.getMessageMap().putError("closeOnOrBeforeDate", OLEKeyConstants.ContractsAndGrants.CLOSE_ON_OR_BEFORE_DATE_TOO_LATE, closeOnOrBeforeDateLabel, closeDateLabel);
069        }
070        GlobalVariables.getMessageMap().removeFromErrorPath(OLEPropertyConstants.DOCUMENT);
071
072        isValid = closeDateIsNotNull && closeDateIsTodayOrLater && closeOnOrBeforeDateIsNotNull && closeOnOrBeforeDateIsTodayOrLater && closeOnOrBeforeDateIsCloseDateOrEarlier;
073        return isValid;
074    }
075
076}