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.pdp.service.impl;
017    
018    import java.sql.Date;
019    import java.sql.Timestamp;
020    import java.util.Arrays;
021    import java.util.HashMap;
022    import java.util.Iterator;
023    import java.util.List;
024    import java.util.Map;
025    import java.util.TreeMap;
026    
027    import org.apache.commons.lang.StringUtils;
028    import org.kuali.kfs.pdp.PdpKeyConstants;
029    import org.kuali.kfs.pdp.PdpPropertyConstants;
030    import org.kuali.kfs.pdp.businessobject.PaymentGroup;
031    import org.kuali.kfs.pdp.businessobject.PaymentProcess;
032    import org.kuali.kfs.pdp.dataaccess.PaymentGroupDao;
033    import org.kuali.kfs.pdp.service.PaymentGroupService;
034    import org.kuali.kfs.sys.DynamicCollectionComparator;
035    import org.kuali.kfs.sys.context.SpringContext;
036    import org.kuali.rice.kns.service.BusinessObjectService;
037    import org.kuali.rice.kns.service.DataDictionaryService;
038    import org.kuali.rice.kns.service.KualiConfigurationService;
039    import org.kuali.rice.kns.service.ParameterEvaluator;
040    import org.kuali.rice.kns.service.ParameterService;
041    import org.kuali.rice.kns.util.ObjectUtils;
042    import org.springframework.transaction.annotation.Transactional;
043    
044    @Transactional
045    public class PaymentGroupServiceImpl implements PaymentGroupService {
046        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(PaymentGroupServiceImpl.class);
047    
048        private PaymentGroupDao paymentGroupDao;
049        private ParameterService parameterService;          
050        private DataDictionaryService dataDictionaryService;            
051        private Map<Integer,ParameterEvaluator> sortGroupSelectionParameters;
052        private BusinessObjectService businessObjectService;
053        
054        public void setPaymentGroupDao(PaymentGroupDao c) {
055            paymentGroupDao = c;
056        }
057    
058        /**
059         * @see org.kuali.kfs.pdp.service.PaymentGroupService#getDisbursementNumbersByDisbursementType(java.lang.Integer, java.lang.String)
060         */
061        public List<Integer> getDisbursementNumbersByDisbursementType(Integer pid,String disbursementType) {
062            LOG.debug("getDisbursementNumbersByDisbursementType() started");
063    
064            return paymentGroupDao.getDisbursementNumbersByDisbursementType(pid, disbursementType);
065        }
066    
067        /**
068         * @see org.kuali.kfs.pdp.service.PaymentGroupService#getDisbursementNumbersByDisbursementTypeAndBankCode(java.lang.Integer, java.lang.String, java.lang.String)
069         */
070        public List<Integer> getDisbursementNumbersByDisbursementTypeAndBankCode(Integer pid, String disbursementType, String bankCode) {
071            return paymentGroupDao.getDisbursementNumbersByDisbursementTypeAndBankCode(pid, disbursementType, bankCode);
072        }
073    
074        /**
075         * @see org.kuali.kfs.pdp.service.PaymentGroupService#getDistinctBankCodesForProcessAndType(java.lang.Integer, java.lang.String)
076         */
077        public List<String> getDistinctBankCodesForProcessAndType(Integer pid, String disbursementType) {
078            return paymentGroupDao.getDistinctBankCodesForProcessAndType(pid, disbursementType);
079        }
080    
081        /**
082         * @see org.kuali.kfs.pdp.service.PaymentGroupService#getByDisbursementTypeStatusCode(java.lang.String, java.lang.String)
083         */
084        public Iterator getByDisbursementTypeStatusCode(String disbursementType, String paymentStatusCode) {
085            LOG.debug("getByDisbursementTypeStatusCode() started");
086            
087            Map fieldValues = new HashMap();
088            fieldValues.put(PdpPropertyConstants.PaymentGroup.PAYMENT_GROUP_DISBURSEMENT_TYPE_CODE, disbursementType);
089            fieldValues.put(PdpPropertyConstants.PaymentGroup.PAYMENT_GROUP_PAYMENT_STATUS_CODE, paymentStatusCode);
090            List paymentGroupList = (List) this.businessObjectService.findMatching(PaymentGroup.class, fieldValues);
091            DynamicCollectionComparator.sort(paymentGroupList, PdpPropertyConstants.PaymentGroup.PAYMENT_GROUP_DISBURSEMENT_NBR);
092            
093            return paymentGroupList.iterator();
094        }
095    
096        /**
097         * @see org.kuali.kfs.pdp.service.PaymentGroupService#getByProcess(org.kuali.kfs.pdp.businessobject.PaymentProcess)
098         */
099        public Iterator getByProcess(PaymentProcess p) {
100            LOG.debug("getByProcess() started");
101            
102            Map fieldValues = new HashMap();
103            fieldValues.put(PdpPropertyConstants.PaymentGroup.PAYMENT_GROUP_PROCESS_ID, p.getId());
104            List paymentGroupList = (List) this.businessObjectService.findMatching(PaymentGroup.class, fieldValues);
105            DynamicCollectionComparator.sort(paymentGroupList, PdpPropertyConstants.PaymentGroup.PAYMENT_GROUP_SORT_VALUE, PdpPropertyConstants.PaymentGroup.PAYMENT_GROUP_PAYEE_NAME, PdpPropertyConstants.PaymentGroup.PAYMENT_GROUP_LINE1_ADDRESS, PdpPropertyConstants.PaymentGroup.NOTES_LINES);
106            
107            return paymentGroupList.iterator();
108        }
109       
110        /**
111         * @see org.kuali.kfs.pdp.service.PaymentGroupService#get(java.lang.Integer)
112         */
113        public PaymentGroup get(Integer id) {
114            LOG.debug("get() started");
115            
116            Map primaryKeys = new HashMap();
117            primaryKeys.put(PdpPropertyConstants.PaymentGroup.PAYMENT_GROUP_ID, id);
118            return (PaymentGroup) this.businessObjectService.findByPrimaryKey(PaymentGroup.class, primaryKeys);
119        }
120    
121        /**
122         * @see org.kuali.kfs.pdp.service.PaymentGroupService#getByBatchId(java.lang.Integer)
123         */
124        public List getByBatchId(Integer batchId) {
125            LOG.debug("getByBatchId() started");
126            
127            Map fieldValues = new HashMap();
128            fieldValues.put(PdpPropertyConstants.PaymentGroup.PAYMENT_GROUP_BATCH_ID, batchId);
129            
130            return (List) this.businessObjectService.findMatching(PaymentGroup.class, fieldValues);
131        }
132    
133        /**
134         * @see org.kuali.kfs.pdp.service.PaymentGroupService#getByDisbursementNumber(java.lang.Integer)
135         */
136        public List getByDisbursementNumber(Integer disbursementNbr) {
137            LOG.debug("getByDisbursementNumber() started");
138    
139            Map fieldValues = new HashMap();
140            fieldValues.put(PdpPropertyConstants.PaymentGroup.PAYMENT_GROUP_DISBURSEMENT_NBR, disbursementNbr);
141            
142            return (List) this.businessObjectService.findMatching(PaymentGroup.class, fieldValues);
143        }
144    
145        /**
146         * @see org.kuali.kfs.pdp.service.PaymentGroupService#processPaidGroup(org.kuali.kfs.pdp.businessobject.PaymentGroup, java.sql.Date)
147         */
148        public void processPaidGroup(PaymentGroup group, Date processDate) {
149            LOG.debug("processPaidGroup() started");
150    
151            Timestamp ts = new Timestamp(processDate.getTime());
152            group.setEpicPaymentPaidExtractedDate(ts);
153            group.setLastUpdate(ts);
154            this.businessObjectService.save(group);
155        }
156    
157        /**
158         * @see org.kuali.kfs.pdp.service.PaymentGroupService#processCancelledGroup(org.kuali.kfs.pdp.businessobject.PaymentGroup,
159         *      java.sql.Date)
160         */
161        public void processCancelledGroup(PaymentGroup group, Date processDate) {
162            LOG.debug("processCancelledGroup() started");
163    
164            Timestamp ts = new Timestamp(processDate.getTime());
165            group.setEpicPaymentCancelledExtractedDate(ts);
166            group.setLastUpdate(ts);
167            this.businessObjectService.save(group);
168        }
169        
170        /**
171         * @see org.kuali.kfs.pdp.service.PaymentGroupService#setParameterService(org.kuali.kfs.sys.service.ParameterService)
172         */
173        public void setParameterService(ParameterService parameterService) {
174            this.parameterService = parameterService;
175        }
176        
177        /**
178         * @see org.kuali.kfs.pdp.service.PaymentGroupService#getSortGroupId(org.kuali.kfs.pdp.businessobject.PaymentGroup)
179         */
180        public int getSortGroupId(PaymentGroup paymentGroup) {      
181            String DEFAULT_SORT_GROUP_ID_PARAMETER = SpringContext.getBean(KualiConfigurationService.class).getPropertyString(PdpKeyConstants.DEFAULT_SORT_GROUP_ID_PARAMETER);
182    
183            for (Integer sortGroupId : getSortGroupSelectionParameters().keySet()) {         
184                List<String> parameterValues = Arrays.asList(StringUtils.substringAfter(getSortGroupSelectionParameters().get(sortGroupId).getValue(), "=").split(";"));         
185                String constrainedValue = String.valueOf(ObjectUtils.getPropertyValue(paymentGroup, StringUtils.substringBefore(getSortGroupSelectionParameters().get(sortGroupId).getValue(), "=")));           
186                if ((getSortGroupSelectionParameters().get(sortGroupId).constraintIsAllow() && parameterValues.contains(constrainedValue))           
187                    || (!getSortGroupSelectionParameters().get(sortGroupId).constraintIsAllow() && !parameterValues.contains(constrainedValue))) {           
188                        return sortGroupId;         
189                }           
190            }
191            
192            return new Integer(parameterService.getParameterValue(PaymentGroup.class, DEFAULT_SORT_GROUP_ID_PARAMETER));            
193        }       
194           
195        /**
196         * @see org.kuali.kfs.pdp.service.PaymentGroupService#getSortGroupName(int)
197         */
198        public String getSortGroupName(int sortGroupId) {      
199            String DEFAULT_SORT_GROUP_ID_PARAMETER = SpringContext.getBean(KualiConfigurationService.class).getPropertyString(PdpKeyConstants.DEFAULT_SORT_GROUP_ID_PARAMETER);
200    
201            if ((sortGroupId + "").equals(parameterService.getParameterValue(PaymentGroup.class, DEFAULT_SORT_GROUP_ID_PARAMETER))) {           
202                return SpringContext.getBean(KualiConfigurationService.class).getPropertyString(PdpKeyConstants.DEFAULT_GROUP_NAME_OTHER);         
203            }       
204            
205            return dataDictionaryService.getAttributeLabel(PaymentGroup.class, StringUtils.substringBefore(getSortGroupSelectionParameters().get(sortGroupId).getValue(), "="));         
206        } 
207        
208        /**
209         * @see org.kuali.kfs.pdp.service.PaymentGroupService#getAchPaymentsNeedingAdviceNotification()
210         */
211        public List<PaymentGroup> getAchPaymentsNeedingAdviceNotification() {
212            return this.paymentGroupDao.getAchPaymentsNeedingAdviceNotification();
213        }
214        
215        /**
216         * Gets the sort group parameters
217         * 
218         * @return
219         */
220        protected Map<Integer,ParameterEvaluator> getSortGroupSelectionParameters() {
221            String SORT_GROUP_SELECTION_PARAMETER_PREFIX = SpringContext.getBean(KualiConfigurationService.class).getPropertyString(PdpKeyConstants.SORT_GROUP_SELECTION_PARAMETER_PREFIX);
222            
223            if (sortGroupSelectionParameters == null) {         
224                sortGroupSelectionParameters = new TreeMap<Integer,ParameterEvaluator>();           
225                boolean moreParameters = true;          
226                int i = 1;          
227                while (moreParameters) {            
228                    if (parameterService.parameterExists(PaymentGroup.class, SORT_GROUP_SELECTION_PARAMETER_PREFIX + i)) {          
229                        sortGroupSelectionParameters.put(i, parameterService.getParameterEvaluator(PaymentGroup.class, SORT_GROUP_SELECTION_PARAMETER_PREFIX + i, null));           
230                        i++;            
231                    }           
232                    else {          
233                        moreParameters = false;         
234                    }           
235                }           
236            } 
237            
238            return sortGroupSelectionParameters;            
239        }
240    
241        public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
242            this.dataDictionaryService = dataDictionaryService;
243        }
244        
245        /**
246         * Sets the business object service
247         * 
248         * @param businessObjectService
249         */
250        public void setBusinessObjectService(BusinessObjectService businessObjectService) {
251            this.businessObjectService = businessObjectService;
252        }
253    }