001/*
002 * Copyright 2007-2009 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.purap.document.validation.impl;
017
018import org.kuali.ole.module.purap.PurapPropertyConstants;
019import org.kuali.ole.module.purap.businessobject.BillingAddress;
020import org.kuali.ole.sys.OLEPropertyConstants;
021import org.kuali.ole.sys.context.SpringContext;
022import org.kuali.ole.sys.service.PostalCodeValidationService;
023import org.kuali.rice.kns.document.MaintenanceDocument;
024import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
025import org.kuali.rice.krad.util.GlobalVariables;
026
027public class BillingAddressRule extends MaintenanceDocumentRuleBase {
028
029    protected boolean processCustomApproveDocumentBusinessRules(MaintenanceDocument document) {
030        LOG.info("processCustomApproveDocumentBusinessRules called");
031        this.setupConvenienceObjects();
032        boolean success = this.validateAddress(document);
033        return success && super.processCustomApproveDocumentBusinessRules(document);
034    }
035
036    protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
037        LOG.info("processCustomRouteDocumentBusinessRules called");
038        this.setupConvenienceObjects();
039        boolean success = this.validateAddress(document);
040        return success && super.processCustomRouteDocumentBusinessRules(document);
041    }
042
043    protected boolean validateAddress(MaintenanceDocument document) {
044        BillingAddress newBillingAddress = (BillingAddress) document.getNewMaintainableObject().getBusinessObject();
045        GlobalVariables.getMessageMap().clearErrorPath();
046        GlobalVariables.getMessageMap().addToErrorPath(OLEPropertyConstants.DOCUMENT + "." + OLEPropertyConstants.NEW_MAINTAINABLE_OBJECT);
047        boolean valid = SpringContext.getBean(PostalCodeValidationService.class).validateAddress(newBillingAddress.getBillingCountryCode(), newBillingAddress.getBillingStateCode(), newBillingAddress.getBillingPostalCode(), PurapPropertyConstants.BILLING_ADDRESS_STATE, PurapPropertyConstants.BILLING_ADDRESS_POSTAL_CODE);
048        GlobalVariables.getMessageMap().clearErrorPath();
049        return valid;
050    }
051
052}