001    /*
002     * Copyright 2011 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     */
016    package org.kuali.kfs.module.purap.document.validation.impl;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.kuali.kfs.module.purap.PurapKeyConstants;
020    import org.kuali.kfs.module.purap.PurapPropertyConstants;
021    import org.kuali.kfs.module.purap.document.VendorCreditMemoDocument;
022    import org.kuali.kfs.module.purap.document.service.PaymentRequestService;
023    import org.kuali.kfs.sys.KFSKeyConstants;
024    import org.kuali.kfs.sys.context.SpringContext;
025    import org.kuali.kfs.sys.document.validation.GenericValidation;
026    import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
027    import org.kuali.rice.kns.bo.BusinessObject;
028    import org.kuali.rice.kns.service.DataDictionaryService;
029    import org.kuali.rice.kns.util.GlobalVariables;
030    import org.kuali.rice.kns.util.ObjectUtils;
031    
032    public class VendorCreditMemoInitTabRequiredFieldsValidation extends GenericValidation {
033        
034        private DataDictionaryService dataDictionaryService;
035        private PaymentRequestService paymentRequestService;
036        
037        /**
038         * Validates the necessary fields on the init tab were given and credit memo date is valid. (NOTE: formats for cm date and
039         * number already performed by pojo conversion)
040         */
041        public boolean validate(AttributedDocumentEvent event) {
042            VendorCreditMemoDocument cmDocument = (VendorCreditMemoDocument) event.getDocument();
043            
044            boolean valid = true;
045    
046            valid = validateRequiredField(cmDocument, PurapPropertyConstants.CREDIT_MEMO_NUMBER);
047            valid = valid && validateRequiredField(cmDocument, PurapPropertyConstants.CREDIT_MEMO_AMOUNT);
048            boolean creditMemoDateExist = validateRequiredField(cmDocument, PurapPropertyConstants.CREDIT_MEMO_DATE);
049    
050            if (creditMemoDateExist) {
051                if (paymentRequestService.isInvoiceDateAfterToday(cmDocument.getCreditMemoDate())) {
052                    String label = dataDictionaryService.getAttributeErrorLabel(VendorCreditMemoDocument.class, PurapPropertyConstants.CREDIT_MEMO_DATE);
053                    GlobalVariables.getMessageMap().putError(PurapPropertyConstants.CREDIT_MEMO_DATE, PurapKeyConstants.ERROR_INVALID_INVOICE_DATE, label);
054                    valid = false;
055                }
056            }
057    
058            return valid;
059    
060        }
061    
062        /**
063         * Helper method to perform required field checks add error messages if the validation fails. Adds an error required to
064         * GlobalVariables.errorMap using the given fieldName as the error key and retrieving the error label from the data dictionary
065         * for the error required message param.
066         * 
067         * @param businessObject - Business object to check for value
068         * @param fieldName - Name of the property in the business object
069         */
070        protected boolean validateRequiredField(BusinessObject businessObject, String fieldName) {
071            boolean valid = true;
072    
073            Object fieldValue = ObjectUtils.getPropertyValue(businessObject, fieldName);
074            if (fieldValue == null || (fieldValue instanceof String && StringUtils.isBlank(fieldName))) {
075                String label = dataDictionaryService.getAttributeErrorLabel(businessObject.getClass(), fieldName);
076                GlobalVariables.getMessageMap().putError(fieldName, KFSKeyConstants.ERROR_REQUIRED, label);
077                valid = false;
078            }
079    
080            return valid;
081        }
082    
083        public DataDictionaryService getDataDictionaryService() {
084            return dataDictionaryService;
085        }
086    
087        public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
088            this.dataDictionaryService = dataDictionaryService;
089        }
090    
091        public PaymentRequestService getPaymentRequestService() {
092            return paymentRequestService;
093        }
094    
095        public void setPaymentRequestService(PaymentRequestService paymentRequestService) {
096            this.paymentRequestService = paymentRequestService;
097        }
098    
099    }