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.pdp.document.validation.impl;
017
018import org.kuali.ole.pdp.PdpConstants;
019import org.kuali.ole.pdp.businessobject.ACHBank;
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 AchBankRule extends MaintenanceDocumentRuleBase {
026
027    protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ACHBank.class);
028
029    protected ACHBank oldAchBank;
030    protected ACHBank newAchBank;
031
032    /**
033     * This method sets the convenience objects like newAccount and oldAccount, so you have short and easy handles to the new and
034     * old objects contained in the maintenance document. It also calls the BusinessObjectBase.refresh(), which will attempt to load
035     * all sub-objects from the DB by their primary keys, if available.
036     * 
037     * @param document - the maintenanceDocument being evaluated
038     */
039    public void setupConvenienceObjects() {
040
041        LOG.info("setupConvenienceObjects called");
042
043        // setup oldAchBank convenience objects, make sure all possible sub-objects are populated
044        oldAchBank = (ACHBank) super.getOldBo();
045
046        // setup newAchBank convenience objects, make sure all possible sub-objects are populated
047        newAchBank = (ACHBank) super.getNewBo();
048    }
049
050    protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
051
052        LOG.info("processCustomSaveDocumentBusinessRules called");
053        // call the route rules to report all of the messages, but ignore the result
054        processCustomRouteDocumentBusinessRules(document);
055        
056        // Save always succeeds, even if there are business rule failures
057        return true;
058    }
059
060    protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
061
062        boolean validEntry = true;
063
064        LOG.info("processCustomRouteDocumentBusinessRules called");
065        setupConvenienceObjects();
066
067        String officeCode = newAchBank.getBankOfficeCode();
068        if ((ObjectUtils.isNotNull(officeCode)) && !officeCode.equals(PdpConstants.AchBankOfficeCodes.AchBankOfficeCode_O) && !officeCode.equals(PdpConstants.AchBankOfficeCodes.AchBankOfficeCode_B)) {
069            putFieldError("bankOfficeCode", OLEKeyConstants.ERROR_DOCUMENT_ACHBANKMAINT_INVALID_OFFICE_CODE);
070            validEntry = false;
071        }
072
073        String typeCode = newAchBank.getBankTypeCode();
074        if ((typeCode != null) && !typeCode.equals(PdpConstants.AchBankTypeCodes.AchBankTypeCode_0) && !typeCode.equals(PdpConstants.AchBankTypeCodes.AchBankTypeCode_1) && !typeCode.equals(PdpConstants.AchBankTypeCodes.AchBankTypeCode_2)) {
075            putFieldError("bankTypeCode", OLEKeyConstants.ERROR_DOCUMENT_ACHBANKMAINT_INVALID_TYPE_CODE);
076            validEntry = false;
077        }
078
079        String bankInstitutionStatusCode = newAchBank.getBankInstitutionStatusCode();
080        if ((ObjectUtils.isNotNull(bankInstitutionStatusCode) ) && !bankInstitutionStatusCode.equals(PdpConstants.ACH_BANK_INSTITUTION_CODE_DEFAULT)) {
081            putFieldError("bankInstitutionStatusCode", OLEKeyConstants.ERROR_DOCUMENT_ACHBANKMAINT_INVALID_INST_STATUS_CODE);
082            validEntry = false;
083        }
084
085        String bankDataViewCode = newAchBank.getBankDataViewCode();
086        if ((ObjectUtils.isNotNull(bankDataViewCode) ) && !bankDataViewCode.equals(PdpConstants.ACH_BANK_DATA_VIEW_CODE_DEFAULT)) {
087            putFieldError("bankDataViewCode", OLEKeyConstants.ERROR_DOCUMENT_ACHBANKMAINT_INVALID_DATA_VIEW_CODE);
088            validEntry = false;
089        }
090
091        return validEntry;
092    }
093   
094}