001/*
002 * Copyright 2006 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.coa.document.validation.impl;
017
018import org.kuali.ole.coa.businessobject.Account;
019import org.kuali.ole.coa.businessobject.SubObjectCode;
020import org.kuali.ole.sys.OLEKeyConstants;
021import org.kuali.rice.kns.document.MaintenanceDocument;
022import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
023import org.kuali.rice.krad.util.ObjectUtils;
024
025public class SubObjCdRule extends MaintenanceDocumentRuleBase {
026
027    protected static final String ACCOUNT_ORG_RULE_KEY = "SubObjectCode.AccountOrgsAllowingClosedAccounts";
028
029    protected SubObjectCode oldSubObjectCode;
030    protected SubObjectCode newSubObjectCode;
031
032    public SubObjCdRule() {
033        super();
034    }
035
036    /**
037     * This performs rules checks on document approve
038     * <ul>
039     * <li>{@link SubObjCdRule#checkExistenceAndActive()}</li>
040     * </ul>
041     * This rule fails on business rule failures
042     * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomApproveDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
043     */
044    protected boolean processCustomApproveDocumentBusinessRules(MaintenanceDocument document) {
045
046        LOG.debug("Entering processCustomApproveDocumentBusinessRules()");
047
048        // check that all sub-objects whose keys are specified have matching objects in the db
049        checkExistenceAndActive();
050
051        return true;
052    }
053
054    /**
055     * This performs rules checks on document route
056     * <ul>
057     * <li>{@link SubObjCdRule#checkExistenceAndActive()}</li>
058     * </ul>
059     * This rule fails on business rule failures
060     * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomRouteDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
061     */
062    protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
063
064        boolean success = true;
065
066        LOG.debug("Entering processCustomRouteDocumentBusinessRules()");
067
068        // check that all sub-objects whose keys are specified have matching objects in the db
069        success &= checkExistenceAndActive();
070
071        return success;
072    }
073
074    /**
075     * This performs rules checks on document save
076     * <ul>
077     * <li>{@link SubObjCdRule#checkExistenceAndActive()}</li>
078     * </ul>
079     * This rule does not fail on business rule failures
080     * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomSaveDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
081     */
082    protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
083
084        boolean success = true;
085
086        LOG.debug("Entering processCustomSaveDocumentBusinessRules()");
087
088        // check that all sub-objects whose keys are specified have matching objects in the db
089        success &= checkExistenceAndActive();
090
091        return success;
092    }
093
094    /**
095     * This method sets the convenience objects like newSubObjectCode and oldSubObjectCode, so you have short and easy handles to the new and
096     * old objects contained in the maintenance document. It also calls the BusinessObjectBase.refresh(), which will attempt to load
097     * all sub-objects from the DB by their primary keys, if available.
098     * 
099     * @param document - the maintenanceDocument being evaluated
100     */
101    public void setupConvenienceObjects() {
102
103        // setup oldAccount convenience objects, make sure all possible sub-objects are populated
104        oldSubObjectCode = (SubObjectCode) super.getOldBo();
105
106        // setup newAccount convenience objects, make sure all possible sub-objects are populated
107        newSubObjectCode = (SubObjectCode) super.getNewBo();
108    }
109
110    /**
111     * 
112     * This checks that the account on the sub object code is not closed
113     * @return false if the account is closed
114     */
115    protected boolean checkExistenceAndActive() {
116
117        LOG.debug("Entering checkExistenceAndActive()");
118        boolean success = true;
119
120        // disallow closed accounts unless in certain orgs
121        if (ObjectUtils.isNotNull(newSubObjectCode.getAccount())) {
122            Account account = newSubObjectCode.getAccount();
123
124            // if the account is closed
125            if (!account.isActive()) {
126                putFieldError("accountNumber", OLEKeyConstants.ERROR_DOCUMENT_SUBOBJECTMAINT_ACCOUNT_MAY_NOT_BE_CLOSED);
127                success &= false;
128            }
129        }
130        return success;
131    }
132
133}