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.ar.document.validation.impl;
017    
018    import static org.kuali.kfs.sys.document.validation.impl.AccountingDocumentRuleBaseConstants.ERROR_PATH.DOCUMENT_ERROR_PREFIX;
019    
020    import java.util.List;
021    
022    import org.apache.commons.lang.StringUtils;
023    import org.kuali.kfs.module.ar.ArConstants;
024    import org.kuali.kfs.module.ar.ArKeyConstants;
025    import org.kuali.kfs.module.ar.ArPropertyConstants;
026    import org.kuali.kfs.module.ar.businessobject.InvoiceRecurrence;
027    import org.kuali.kfs.module.ar.document.CustomerInvoiceDocument;
028    import org.kuali.kfs.sys.context.SpringContext;
029    import org.kuali.kfs.sys.document.validation.GenericValidation;
030    import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
031    import org.kuali.rice.kns.service.ParameterService;
032    import org.kuali.rice.kns.util.GlobalVariables;
033    import org.kuali.rice.kns.util.ObjectUtils;
034    
035    public class CustomerInvoiceMaximumNumberOfRecurrencesValidation extends GenericValidation {
036        
037        private CustomerInvoiceDocument customerInvoiceDocument;
038    
039        public boolean validate(AttributedDocumentEvent event) {
040            
041            // short circuit if no recurrence object at all
042            if (ObjectUtils.isNull(customerInvoiceDocument.getCustomerInvoiceRecurrenceDetails())) {
043                return true;
044            }
045            
046            if (ObjectUtils.isNull(customerInvoiceDocument.getCustomerInvoiceRecurrenceDetails().getDocumentRecurrenceIntervalCode()) ||
047                ObjectUtils.isNull(customerInvoiceDocument.getCustomerInvoiceRecurrenceDetails().getDocumentTotalRecurrenceNumber())) {
048                return true;
049            }
050    
051            boolean success = true;
052            Integer maximumRecurrencesByInterval;
053            if (ObjectUtils.isNotNull(customerInvoiceDocument.getCustomerInvoiceRecurrenceDetails().getDocumentRecurrenceIntervalCode())) {
054                List<String> maximumRecurrences = SpringContext.getBean(ParameterService.class).getParameterValues(InvoiceRecurrence.class, ArConstants.MAXIMUM_RECURRENCES_BY_INTERVAL, customerInvoiceDocument.getCustomerInvoiceRecurrenceDetails().getDocumentRecurrenceIntervalCode());
055                if (maximumRecurrences.size() > 0 && StringUtils.isNotBlank(maximumRecurrences.get(0))) {
056                    maximumRecurrencesByInterval = Integer.valueOf(maximumRecurrences.get(0));
057                    if (customerInvoiceDocument.getCustomerInvoiceRecurrenceDetails().getDocumentTotalRecurrenceNumber() > maximumRecurrencesByInterval) {
058                        GlobalVariables.getMessageMap().putError(DOCUMENT_ERROR_PREFIX + ArPropertyConstants.CustomerInvoiceDocumentFields.INVOICE_DOCUMENT_RECURRENCE_TOTAL_RECURRENCE_NUMBER, ArKeyConstants.ERROR_TOTAL_NUMBER_OF_RECURRENCES_GREATER_THAN_ALLOWED, maximumRecurrences.get(0));
059                        return false;
060                    }
061                }
062            }
063            return true;
064        }
065    
066        public CustomerInvoiceDocument getCustomerInvoiceDocument() {
067            return customerInvoiceDocument;
068        }
069    
070        public void setCustomerInvoiceDocument(CustomerInvoiceDocument customerInvoiceDocument) {
071            this.customerInvoiceDocument = customerInvoiceDocument;
072        }
073    }