Coverage Report - org.kuali.rice.kns.service.impl.InactivateableFromToServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
InactivateableFromToServiceImpl
0%
0/59
0%
0/18
1.833
 
 1  
 /*
 2  
  * Copyright 2007-2010 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.kns.service.impl;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.Collections;
 20  
 import java.util.Date;
 21  
 import java.util.List;
 22  
 import java.util.Map;
 23  
 
 24  
 import org.apache.commons.lang.StringUtils;
 25  
 import org.kuali.rice.core.api.datetime.DateTimeService;
 26  
 import org.kuali.rice.kns.bo.BusinessObject;
 27  
 import org.kuali.rice.kns.bo.InactivateableFromTo;
 28  
 import org.kuali.rice.kns.service.BusinessObjectDictionaryService;
 29  
 import org.kuali.rice.kns.service.InactivateableFromToService;
 30  
 import org.kuali.rice.kns.service.LookupService;
 31  
 import org.kuali.rice.kns.util.BeanPropertyComparator;
 32  
 import org.kuali.rice.kns.util.KNSPropertyConstants;
 33  
 import org.kuali.rice.kns.util.ObjectUtils;
 34  
 
 35  
 /**
 36  
  * Implementation of InactivateableFromToService that uses the lookup service for query implementation
 37  
  * 
 38  
  * @see org.kuali.rice.kns.service.InactivateableFromToService
 39  
  */
 40  0
 public class InactivateableFromToServiceImpl implements InactivateableFromToService {
 41  
 
 42  
         protected DateTimeService dateTimeService;
 43  
         protected BusinessObjectDictionaryService businessObjectDictionaryService;
 44  
         protected LookupService lookupService;
 45  
 
 46  
         /**
 47  
          * Uses lookup service which will convert the active criteria to active begin/to field criteria
 48  
          * 
 49  
          * @see org.kuali.rice.kns.service.InactivateableFromToService#findMatchingActive(java.lang.Class, java.util.Map)
 50  
          */
 51  
         public List<InactivateableFromTo> findMatchingActive(Class<? extends InactivateableFromTo> clazz, Map fieldValues) {
 52  0
                 fieldValues.put(KNSPropertyConstants.ACTIVE, "true");
 53  
 
 54  0
                 return (List<InactivateableFromTo>) lookupService.findCollectionBySearchUnbounded(clazz, fieldValues);
 55  
         }
 56  
 
 57  
         /**
 58  
          * Uses lookup service which will convert the active criteria to active begin/to field criteria
 59  
          * 
 60  
          * @see org.kuali.rice.kns.service.InactivateableFromToService#findMatchingActiveAsOfDate(java.lang.Class, java.util.Map,
 61  
          *      java.util.Date)
 62  
          */
 63  
         public List<InactivateableFromTo> findMatchingActiveAsOfDate(Class<? extends InactivateableFromTo> clazz,
 64  
                         Map fieldValues, Date activeAsOfDate) {
 65  0
                 fieldValues.put(KNSPropertyConstants.ACTIVE, "true");
 66  0
                 fieldValues.put(KNSPropertyConstants.ACTIVE_AS_OF_DATE, dateTimeService.toDateString(activeAsOfDate));
 67  
 
 68  0
                 return (List<InactivateableFromTo>) lookupService.findCollectionBySearchUnbounded(clazz, fieldValues);
 69  
         }
 70  
 
 71  
         /**
 72  
          * @see org.kuali.rice.kns.service.InactivateableFromToService#filterOutNonActive(java.util.List)
 73  
          */
 74  
         public List<InactivateableFromTo> filterOutNonActive(List<InactivateableFromTo> filterList) {
 75  0
                 return filterOutNonActive(filterList, dateTimeService.getCurrentDate());
 76  
         }
 77  
 
 78  
         /**
 79  
          * @see org.kuali.rice.kns.service.InactivateableFromToService#filterOutNonActive(java.util.List, java.util.Date)
 80  
          */
 81  
         public List<InactivateableFromTo> filterOutNonActive(List<InactivateableFromTo> filterList, Date activeAsOfDate) {
 82  0
                 List<InactivateableFromTo> filteredList = new ArrayList<InactivateableFromTo>();
 83  
 
 84  0
                 for (InactivateableFromTo inactivateable : filterList) {
 85  0
                         inactivateable.setActiveAsOfDate(new java.sql.Timestamp(activeAsOfDate.getTime()));
 86  0
                         if (inactivateable.isActive()) {
 87  0
                                 filteredList.add(inactivateable);
 88  
                         }
 89  
                 }
 90  
 
 91  0
                 return filteredList;
 92  
         }
 93  
 
 94  
         /**
 95  
          * Uses lookup service which will convert the active and current criteria to active begin/to field criteria
 96  
          * 
 97  
          * @see org.kuali.rice.kns.service.InactivateableFromToService#findMatchingCurrent(java.lang.Class, java.util.Map)
 98  
          */
 99  
         public List<InactivateableFromTo> findMatchingCurrent(Class<? extends InactivateableFromTo> clazz,
 100  
                         Map fieldValues) {
 101  0
                 fieldValues.put(KNSPropertyConstants.ACTIVE, "true");
 102  0
                 fieldValues.put(KNSPropertyConstants.CURRENT, "true");
 103  
 
 104  0
                 return (List<InactivateableFromTo>) lookupService.findCollectionBySearchUnbounded(clazz, fieldValues);
 105  
         }
 106  
 
 107  
         /**
 108  
          * Uses lookup service which will convert the active and current criteria to active begin/to field criteria
 109  
          * 
 110  
          * @see org.kuali.rice.kns.service.InactivateableFromToService#findMatchingCurrent(java.lang.Class, java.util.Map, java.util.Date)
 111  
          */
 112  
         public List<InactivateableFromTo> findMatchingCurrent(Class<? extends InactivateableFromTo> clazz,
 113  
                         Map fieldValues, Date currentAsOfDate) {
 114  0
                 fieldValues.put(KNSPropertyConstants.ACTIVE, "true");
 115  0
                 fieldValues.put(KNSPropertyConstants.CURRENT, "true");
 116  0
                 fieldValues.put(KNSPropertyConstants.ACTIVE_AS_OF_DATE, dateTimeService.toDateString(currentAsOfDate));
 117  
 
 118  0
                 return (List<InactivateableFromTo>) lookupService.findCollectionBySearchUnbounded(clazz, fieldValues);
 119  
         }
 120  
 
 121  
         /**
 122  
          * @see org.kuali.rice.kns.service.InactivateableFromToService#filterOutNonCurrent(java.util.List)
 123  
          */
 124  
         public List<InactivateableFromTo> filterOutNonCurrent(List<InactivateableFromTo> filterList) {
 125  0
                 return filterOutNonCurrent(filterList, dateTimeService.getCurrentDate());
 126  
         }
 127  
 
 128  
         /**
 129  
          * @see org.kuali.rice.kns.service.InactivateableFromToService#filterOutNonCurrent(java.util.List, java.util.Date)
 130  
          */
 131  
         public List<InactivateableFromTo> filterOutNonCurrent(List<InactivateableFromTo> filterList, Date currentAsOfDate) {
 132  0
                 List<InactivateableFromTo> activeList = filterOutNonActive(filterList, currentAsOfDate);
 133  
 
 134  0
                 if (activeList.isEmpty()) {
 135  0
                         return activeList;
 136  
                 }
 137  
 
 138  0
                 List<InactivateableFromTo> currentList = new ArrayList<InactivateableFromTo>();
 139  
 
 140  0
                 List<String> groupByList = businessObjectDictionaryService.getGroupByAttributesForEffectiveDating(activeList
 141  
                                 .get(0).getClass());
 142  0
                 if (groupByList != null) {
 143  0
                         List<String> sortByList = new ArrayList<String>(groupByList);
 144  0
                         sortByList.add(KNSPropertyConstants.ACTIVE_FROM_DATE);
 145  
 
 146  
                         // sort list so we get records together with same group by
 147  0
                         Collections.sort(activeList, new BeanPropertyComparator(sortByList, true));
 148  
 
 149  
                         // reverse so we get max active begin date first within the group by
 150  0
                         Collections.reverse(activeList);
 151  
 
 152  0
                         String previousGroupByString = "";
 153  0
                         Date previousActiveFromDate = null;
 154  0
                         for (InactivateableFromTo inactivateable : activeList) {
 155  0
                                 String groupByString = buildGroupByValueString((BusinessObject) inactivateable, groupByList);
 156  0
                                 if (!StringUtils.equals(groupByString, previousGroupByString)) {
 157  
                                         // always add first record of new group by since list is sorted by active begin date descending
 158  0
                                         currentList.add(inactivateable);
 159  
                                 }
 160  
                                 // active from date should not be null here since we are dealing with only active records
 161  0
                                 else if (inactivateable.getActiveFromDate().equals(previousActiveFromDate)) {
 162  
                                         // have more than one record for the group by key with same active begin date, so they both are current
 163  0
                                         currentList.add(inactivateable);
 164  
                                 }
 165  
 
 166  0
                                 previousGroupByString = groupByString;
 167  0
                                 previousActiveFromDate = inactivateable.getActiveFromDate();
 168  0
                         }
 169  0
                 } else {
 170  0
                         currentList = activeList;
 171  
                 }
 172  
 
 173  0
                 return currentList;
 174  
         }
 175  
 
 176  
         /**
 177  
          * Builds a string containing the values from the given business object for the fields in the given list, concatenated together using a
 178  
          * bar. Null values are treated as an empty string
 179  
          * 
 180  
          * @param businessObject
 181  
          *            - business object instance to get values from
 182  
          * @param groupByList
 183  
          *            - list of fields to get values for
 184  
          * @return String
 185  
          */
 186  
         protected String buildGroupByValueString(BusinessObject businessObject, List<String> groupByList) {
 187  0
                 String groupByValueString = "";
 188  
 
 189  0
                 for (String groupByField : groupByList) {
 190  0
                         Object fieldValue = ObjectUtils.getPropertyValue(businessObject, groupByField);
 191  0
                         groupByValueString += "|";
 192  0
                         if (fieldValue != null) {
 193  0
                                 groupByValueString += fieldValue;
 194  
                         }
 195  0
                 }
 196  
 
 197  0
                 return groupByValueString;
 198  
         }
 199  
 
 200  
         public void setDateTimeService(DateTimeService dateTimeService) {
 201  0
                 this.dateTimeService = dateTimeService;
 202  0
         }
 203  
 
 204  
         public void setBusinessObjectDictionaryService(BusinessObjectDictionaryService businessObjectDictionaryService) {
 205  0
                 this.businessObjectDictionaryService = businessObjectDictionaryService;
 206  0
         }
 207  
 
 208  
         public void setLookupService(LookupService lookupService) {
 209  0
                 this.lookupService = lookupService;
 210  0
         }
 211  
 
 212  
 }