001/*
002 * Copyright 2008 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.apache.commons.lang.StringUtils;
019import org.kuali.ole.module.purap.PurapConstants;
020import org.kuali.ole.module.purap.PurapKeyConstants;
021import org.kuali.ole.module.purap.PurapPropertyConstants;
022import org.kuali.ole.module.purap.document.PurchasingDocument;
023import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
024import org.kuali.ole.sys.service.PostalCodeValidationService;
025import org.kuali.ole.vnd.businessobject.CampusParameter;
026import org.kuali.rice.core.api.datetime.DateTimeService;
027import org.kuali.rice.krad.service.BusinessObjectService;
028import org.kuali.rice.krad.util.GlobalVariables;
029
030import java.util.Collections;
031
032public class PurchasingDeliveryValidation extends PurchasingProcessRequestorPhoneAndEmailAddressValidation {
033
034    protected DateTimeService dateTimeService;
035    protected BusinessObjectService businessObjectService;
036    protected PostalCodeValidationService postalCodeValidationService;
037
038    @Override
039    public boolean validate(AttributedDocumentEvent event) {
040        boolean valid = true;
041        PurchasingDocument purDocument = (PurchasingDocument) event.getDocument();
042
043        GlobalVariables.getMessageMap().addToErrorPath(PurapConstants.DELIVERY_TAB_ERRORS);        
044        /*//perform the validation against phone Number
045        if (StringUtils.isNotBlank(purDocument.getRequestorPersonPhoneNumber())) {
046            if (!validatePhoneNumber(purDocument.getRequestorPersonPhoneNumber())) {
047                valid &= false;
048                GlobalVariables.getMessageMap().putError(PurapPropertyConstants.REQUESTOR_PERSON_PHONE_NUMBER, PurapKeyConstants.ERROR_INVALID_PH0NE_NUMBER);
049            }
050        }*/
051
052        //perform the validation against email address
053        if (StringUtils.isNotBlank(purDocument.getRequestorPersonEmailAddress())) {
054            if (!validateEmailAddress(purDocument.getRequestorPersonEmailAddress())) {
055                valid &= false;
056                GlobalVariables.getMessageMap().putError(PurapPropertyConstants.REQUESTOR_PERSON_EMAIL_ADDRESS, PurapKeyConstants.ERROR_INVALID_EMAIL_ADDRESS);
057            }
058        }
059
060        postalCodeValidationService.validateAddress(purDocument.getDeliveryCountryCode(), purDocument.getDeliveryStateCode(), purDocument.getDeliveryPostalCode(), PurapPropertyConstants.DELIVERY_STATE_CODE, PurapPropertyConstants.DELIVERY_POSTAL_CODE);
061
062        int match = businessObjectService.countMatching(CampusParameter.class, Collections.singletonMap("campusCode", purDocument.getDeliveryCampusCode()));
063        if (match < 1) {
064            valid = false;
065            GlobalVariables.getMessageMap().putError(PurapPropertyConstants.DELIVERY_CAMPUS_CODE, PurapKeyConstants.ERROR_DELIVERY_CAMPUS_INVALID);
066        }
067
068        GlobalVariables.getMessageMap().clearErrorPath();
069        return valid;
070    }
071
072    public void setDateTimeService(DateTimeService dateTimeService) {
073        this.dateTimeService = dateTimeService;
074    }
075
076    public void setBusinessObjectService(BusinessObjectService businessObjectService) {
077        this.businessObjectService = businessObjectService;
078    }
079
080    public void setPostalCodeValidationService(PostalCodeValidationService postalCodeValidationService) {
081        this.postalCodeValidationService = postalCodeValidationService;
082    }
083}