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.endow.batch.service.impl;
017    
018    import java.sql.Date;
019    import java.util.ArrayList;
020    import java.util.Collection;
021    import java.util.HashMap;
022    import java.util.Map;
023    
024    import org.apache.ojb.broker.query.Criteria;
025    import org.apache.ojb.broker.query.QueryByCriteria;
026    import org.apache.ojb.broker.query.QueryFactory;
027    import org.kuali.kfs.module.endow.EndowParameterKeyConstants;
028    import org.kuali.kfs.module.endow.EndowPropertyConstants;
029    import org.kuali.kfs.module.endow.businessobject.FeeMethod;
030    import org.kuali.kfs.module.endow.businessobject.KemidFee;
031    import org.kuali.kfs.module.endow.document.service.KEMService;
032    import org.kuali.kfs.module.endow.batch.service.KemidCorpusValueService;
033    import org.kuali.kfs.module.endow.batch.service.KemidFeeService;
034    import org.kuali.kfs.sys.service.impl.KfsParameterConstants;
035    import org.kuali.rice.kns.service.BusinessObjectService;
036    import org.kuali.rice.kns.service.ParameterService;
037    import org.kuali.rice.kns.util.KualiDecimal;
038    import org.kuali.rice.kns.util.ObjectUtils;
039    
040    /**
041     * This class is the service implementation for the KemidFeeService. This is the default, Kuali provided implementation.
042     */
043    public class KemidFeeServiceImpl implements KemidFeeService {
044        protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(KemidFeeServiceImpl.class);
045    
046        private BusinessObjectService businessObjectService;
047        protected KEMService kemService;
048        protected ParameterService parameterService;
049        protected KemidCorpusValueService kemidCorpusValueService;
050    
051        /**
052         * @see org.kuali.kfs.module.endow.batch.service.KemidFeeService#getAllKemIdFee()
053         */
054        public Collection <KemidFee> getAllKemIdFee() {
055            Collection <KemidFee> kemIdFee = new ArrayList();
056            
057            kemIdFee = businessObjectService.findAll(KemidFee.class);
058            
059            return kemIdFee;
060        }
061        
062        /**
063         * 
064         */
065        public boolean saveKemidFee(KemidFee kemidFee) {
066            boolean saved = true;
067            
068            try {
069                businessObjectService.save(kemidFee);
070                return saved;
071            } 
072            catch (IllegalArgumentException iae) {
073                return false;
074            }
075        }
076        
077        /**
078         * @see org.kuali.kfs.module.endow.batch.KemidFeeService#KemidFeeService(String)
079         */
080        public Collection<KemidFee> getAllKemidForFeeMethodCode(String feeMethodCode) {
081           Collection<KemidFee> kemId = new ArrayList(); 
082           
083           Map fieldValues = new HashMap();
084           
085           fieldValues.put(EndowPropertyConstants.KEMID_FEE_MTHD_CD, feeMethodCode);
086           
087           kemId = businessObjectService.findMatching(KemidFee.class, fieldValues);
088           
089           return kemId;
090        }
091        
092        /**
093         * @see org.kuali.kfs.module.endow.batch.KemidFeeService#ChargeFeeToKemid(KemidFee, boolean)
094         */
095        public boolean chargeFeeToKemid(FeeMethod feeMethod, KemidFee kemidFee) {
096            boolean chargeFee = true;
097            boolean corpusPercentToleranceGreaterThanZero = (feeMethod.getCorpusPctTolerance().isGreaterThan(new KualiDecimal("0")) ? true : false );
098            
099            if (kemidFee.getFeeStartDate().compareTo(kemService.getCurrentDate()) <= 0) {
100                if (ObjectUtils.isNull(kemidFee.getFeeEndDate()) || 
101                   (kemidFee.getFeeEndDate().compareTo(kemService.getCurrentDate()) > 0)) {
102                    chargeFee = kemidCorpusValueService.canFeeBeChargedToKemid(kemidFee.getKemid(), feeMethod.getCorpusPctTolerance());
103                    return chargeFee;
104                }
105            }
106            
107            return chargeFee;
108        }
109        
110        /**
111         * Updates the END_KEMID_FEE_T table records by setting the amounts in WAIVE_FEE_YTD to zero
112         * if current date is the first day of the institution's fiscal year
113         * @see org.kuali.kfs.module.endow.document.service.KemidFeeService#updateWaiverFeeYearToDateTotals()
114         */
115        public boolean updateWaiverFeeYearToDateTotals() {
116           LOG.info("updateWaiverFeeYearToDateTotals() process started");
117           
118           boolean updated = true;
119           
120           if (!systemParametersForUpdateWaiverFeeAmounts()) {
121               return false;
122           }
123           
124           try {
125               java.sql.Date firstDayAfterFiscalYear = kemService.getFirstDayAfterFiscalYearEndDayAndMonth();
126               
127               if (firstDayAfterFiscalYear.equals(kemService.getCurrentDate())) {
128                   //update END_KEMID_FEE_T:
129                   Collection <KemidFee> kemidFeeRecords = getAllKemIdFee();
130                      
131                   for (KemidFee kemidFee : kemidFeeRecords) {
132                        kemidFee.setTotalWaivedFees(KualiDecimal.ZERO);
133                        businessObjectService.save(kemidFee);
134                   }
135               }
136               
137               return updated;
138           }
139           catch (Exception exception) {
140               return false;
141           }
142        }
143        
144        /**
145         * This method checks if the System parameters have been set up for this batch job.
146         * @result return true if the system parameters exist, else false
147         */
148        protected boolean systemParametersForUpdateWaiverFeeAmounts() {
149            LOG.info("systemParametersForUpdateWaiverFeeAmounts() started.");
150            
151            boolean systemParameterExists = true;
152            
153            // check to make sure the system parameter has been setup...
154            if (!getParameterService().parameterExists(KfsParameterConstants.ENDOWMENT_BATCH.class, EndowParameterKeyConstants.FISCAL_YEAR_END_MONTH_AND_DAY)) {
155              LOG.warn("FISCAL_YEAR_END_MONTH_AND_DAY System parameter does not exist in the parameters list.  The job can not continue without this parameter");
156              return false;
157            }
158            
159            return systemParameterExists;
160        }
161        
162        /**
163         * This method gets the businessObjectService.
164         * 
165         * @return businessObjectService
166         */
167        public BusinessObjectService getBusinessObjectService() {
168            return businessObjectService;
169        }
170    
171        /**
172         * This method sets the businessObjectService
173         * 
174         * @param businessObjectService
175         */
176        public void setBusinessObjectService(BusinessObjectService businessObjectService) {
177            this.businessObjectService = businessObjectService;
178        }
179    
180        /**
181         * Gets the kemService.
182         * @return kemService
183         */
184        protected KEMService getKemService() {
185            return kemService;
186        }
187    
188        /**
189         * Sets the kemService.
190         * @param kemService
191         */
192        public void setKemService(KEMService kemService) {
193            this.kemService = kemService;
194        }
195    
196        /**
197         * Gets the parameterService attribute.
198         * @return Returns the parameterService.
199         */    
200        protected ParameterService getParameterService() {
201            return parameterService;
202        }
203    
204        /**
205         * Sets the parameterService attribute value.
206         * @param parameterService The parameterService to set.
207         */    
208        public void setParameterService(ParameterService parameterService) {
209            this.parameterService = parameterService;
210        }
211        
212        /**
213         * Gets the kemidCorpusValueService attribute. 
214         * @return Returns the kemidCorpusValueService.
215         */
216        public KemidCorpusValueService getKemidCorpusValueService() {
217            return kemidCorpusValueService;
218        }
219    
220        /**
221         * Sets the kemidCorpusValueService attribute value.
222         * @param kemidCorpusValueService The kemidCorpusValueService to set.
223         */
224        public void setKemidCorpusValueService(KemidCorpusValueService kemidCorpusValueService) {
225            this.kemidCorpusValueService = kemidCorpusValueService;
226        }
227    }