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.apache.commons.lang.StringUtils;
019import org.kuali.ole.coa.businessobject.Account;
020import org.kuali.ole.coa.businessobject.SubObjectCode;
021import org.kuali.rice.kns.document.MaintenanceDocument;
022import org.kuali.rice.krad.util.ObjectUtils;
023
024/**
025 * PreRules checks for the {@link SubObjCd} that needs to occur while still in the Struts processing. This includes defaults, confirmations,
026 * etc.
027 */
028public class SubObjectPreRules extends MaintenancePreRulesBase {
029    protected SubObjectCode newSubObjectCode;
030
031
032    public SubObjectPreRules() {
033
034    }
035    
036    /**
037     * Executes the following pre rules
038     * <ul>
039     * <li>{@link SubObjectPreRules#checkForContinuationAccounts()}</li>
040     * </ul>
041     * This does not fail on rule failures
042     * @see org.kuali.ole.coa.document.validation.impl.MaintenancePreRulesBase#doCustomPreRules(org.kuali.rice.kns.document.MaintenanceDocument)
043     */
044    protected boolean doCustomPreRules(MaintenanceDocument document) {
045
046        setupConvenienceObjects(document);
047        checkForContinuationAccounts(); // run this first to avoid side effects
048
049        LOG.debug("done with continuation account, proceeeding with remaining pre rules");
050
051
052        return true;
053    }
054
055    /**
056     * This method checks for continuation accounts and presents the user with a question regarding their use on this account.
057     * 
058     */
059    protected void checkForContinuationAccounts() {
060        LOG.debug("entering checkForContinuationAccounts()");
061
062        if (StringUtils.isNotBlank(newSubObjectCode.getAccountNumber())) {
063            Account account = checkForContinuationAccount("Account Number", newSubObjectCode.getChartOfAccountsCode(), newSubObjectCode.getAccountNumber(), "");
064            if (ObjectUtils.isNotNull(account)) { // override old user inputs
065                newSubObjectCode.setAccountNumber(account.getAccountNumber());
066                newSubObjectCode.setChartOfAccountsCode(account.getChartOfAccountsCode());
067            }
068        }
069    }
070
071    /**
072     * This method sets the convenience objects like newSubObjectCode and copySubObjectCode, so you have short and easy handles to the new and
073     * old objects contained in the maintenance document. It also calls the BusinessObjectBase.refresh(), which will attempt to load
074     * all sub-objects from the DB by their primary keys, if available.
075     * @param document
076     */
077    protected void setupConvenienceObjects(MaintenanceDocument document) {
078
079        // setup newAccount convenience objects, make sure all possible sub-objects are populated
080        newSubObjectCode = (SubObjectCode) document.getNewMaintainableObject().getBusinessObject();
081    }
082}