| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.rice.krad.service.impl; |
| 17 | |
|
| 18 | |
import org.apache.commons.lang.StringUtils; |
| 19 | |
import org.kuali.rice.core.api.datetime.DateTimeService; |
| 20 | |
import org.kuali.rice.krad.bo.BusinessObject; |
| 21 | |
import org.kuali.rice.krad.bo.InactivatableFromTo; |
| 22 | |
import org.kuali.rice.krad.service.BusinessObjectDictionaryService; |
| 23 | |
import org.kuali.rice.krad.service.DataDictionaryService; |
| 24 | |
import org.kuali.rice.krad.service.InactivateableFromToService; |
| 25 | |
import org.kuali.rice.krad.service.KRADServiceLocatorWeb; |
| 26 | |
import org.kuali.rice.krad.service.LookupService; |
| 27 | |
import org.kuali.rice.krad.util.BeanPropertyComparator; |
| 28 | |
import org.kuali.rice.krad.util.KRADPropertyConstants; |
| 29 | |
import org.kuali.rice.krad.util.ObjectUtils; |
| 30 | |
|
| 31 | |
import java.util.ArrayList; |
| 32 | |
import java.util.Collections; |
| 33 | |
import java.util.Date; |
| 34 | |
import java.util.List; |
| 35 | |
import java.util.Map; |
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | 0 | public class InactivateableFromToServiceImpl implements InactivateableFromToService { |
| 43 | |
|
| 44 | |
protected DateTimeService dateTimeService; |
| 45 | |
protected LookupService lookupService; |
| 46 | |
protected DataDictionaryService dataDictionaryService; |
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
public List<InactivatableFromTo> findMatchingActive(Class<? extends InactivatableFromTo> clazz, Map fieldValues) { |
| 54 | 0 | fieldValues.put(KRADPropertyConstants.ACTIVE, "true"); |
| 55 | |
|
| 56 | 0 | return (List<InactivatableFromTo>) lookupService.findCollectionBySearchUnbounded(clazz, fieldValues); |
| 57 | |
} |
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
public List<InactivatableFromTo> findMatchingActiveAsOfDate(Class<? extends InactivatableFromTo> clazz, |
| 66 | |
Map fieldValues, Date activeAsOfDate) { |
| 67 | 0 | fieldValues.put(KRADPropertyConstants.ACTIVE, "true"); |
| 68 | 0 | fieldValues.put(KRADPropertyConstants.ACTIVE_AS_OF_DATE, dateTimeService.toDateString(activeAsOfDate)); |
| 69 | |
|
| 70 | 0 | return (List<InactivatableFromTo>) lookupService.findCollectionBySearchUnbounded(clazz, fieldValues); |
| 71 | |
} |
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
public List<InactivatableFromTo> filterOutNonActive(List<InactivatableFromTo> filterList) { |
| 77 | 0 | return filterOutNonActive(filterList, dateTimeService.getCurrentDate()); |
| 78 | |
} |
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
public List<InactivatableFromTo> filterOutNonActive(List<InactivatableFromTo> filterList, Date activeAsOfDate) { |
| 84 | 0 | List<InactivatableFromTo> filteredList = new ArrayList<InactivatableFromTo>(); |
| 85 | |
|
| 86 | 0 | for (InactivatableFromTo inactivateable : filterList) { |
| 87 | 0 | inactivateable.setActiveAsOfDate(new java.sql.Timestamp(activeAsOfDate.getTime())); |
| 88 | 0 | if (inactivateable.isActive()) { |
| 89 | 0 | filteredList.add(inactivateable); |
| 90 | |
} |
| 91 | |
} |
| 92 | |
|
| 93 | 0 | return filteredList; |
| 94 | |
} |
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
public List<InactivatableFromTo> findMatchingCurrent(Class<? extends InactivatableFromTo> clazz, |
| 102 | |
Map fieldValues) { |
| 103 | 0 | fieldValues.put(KRADPropertyConstants.ACTIVE, "true"); |
| 104 | 0 | fieldValues.put(KRADPropertyConstants.CURRENT, "true"); |
| 105 | |
|
| 106 | 0 | return (List<InactivatableFromTo>) lookupService.findCollectionBySearchUnbounded(clazz, fieldValues); |
| 107 | |
} |
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
public List<InactivatableFromTo> findMatchingCurrent(Class<? extends InactivatableFromTo> clazz, |
| 115 | |
Map fieldValues, Date currentAsOfDate) { |
| 116 | 0 | fieldValues.put(KRADPropertyConstants.ACTIVE, "true"); |
| 117 | 0 | fieldValues.put(KRADPropertyConstants.CURRENT, "true"); |
| 118 | 0 | fieldValues.put(KRADPropertyConstants.ACTIVE_AS_OF_DATE, dateTimeService.toDateString(currentAsOfDate)); |
| 119 | |
|
| 120 | 0 | return (List<InactivatableFromTo>) lookupService.findCollectionBySearchUnbounded(clazz, fieldValues); |
| 121 | |
} |
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
public List<InactivatableFromTo> filterOutNonCurrent(List<InactivatableFromTo> filterList) { |
| 127 | 0 | return filterOutNonCurrent(filterList, dateTimeService.getCurrentDate()); |
| 128 | |
} |
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
public List<InactivatableFromTo> filterOutNonCurrent(List<InactivatableFromTo> filterList, Date currentAsOfDate) { |
| 134 | 0 | List<InactivatableFromTo> activeList = filterOutNonActive(filterList, currentAsOfDate); |
| 135 | |
|
| 136 | 0 | if (activeList.isEmpty()) { |
| 137 | 0 | return activeList; |
| 138 | |
} |
| 139 | |
|
| 140 | 0 | List<InactivatableFromTo> currentList = new ArrayList<InactivatableFromTo>(); |
| 141 | |
|
| 142 | 0 | List<String> groupByList = getDataDictionaryService().getGroupByAttributesForEffectiveDating( |
| 143 | |
activeList.get(0).getClass()); |
| 144 | 0 | if (groupByList != null) { |
| 145 | 0 | List<String> sortByList = new ArrayList<String>(groupByList); |
| 146 | 0 | sortByList.add(KRADPropertyConstants.ACTIVE_FROM_DATE); |
| 147 | |
|
| 148 | |
|
| 149 | 0 | Collections.sort(activeList, new BeanPropertyComparator(sortByList, true)); |
| 150 | |
|
| 151 | |
|
| 152 | 0 | Collections.reverse(activeList); |
| 153 | |
|
| 154 | 0 | String previousGroupByString = ""; |
| 155 | 0 | Date previousActiveFromDate = null; |
| 156 | 0 | for (InactivatableFromTo inactivateable : activeList) { |
| 157 | 0 | String groupByString = buildGroupByValueString((BusinessObject) inactivateable, groupByList); |
| 158 | 0 | if (!StringUtils.equals(groupByString, previousGroupByString)) { |
| 159 | |
|
| 160 | 0 | currentList.add(inactivateable); |
| 161 | |
} |
| 162 | |
|
| 163 | 0 | else if (inactivateable.getActiveFromDate().equals(previousActiveFromDate)) { |
| 164 | |
|
| 165 | 0 | currentList.add(inactivateable); |
| 166 | |
} |
| 167 | |
|
| 168 | 0 | previousGroupByString = groupByString; |
| 169 | 0 | previousActiveFromDate = inactivateable.getActiveFromDate(); |
| 170 | 0 | } |
| 171 | 0 | } else { |
| 172 | 0 | currentList = activeList; |
| 173 | |
} |
| 174 | |
|
| 175 | 0 | return currentList; |
| 176 | |
} |
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
protected String buildGroupByValueString(BusinessObject businessObject, List<String> groupByList) { |
| 189 | 0 | String groupByValueString = ""; |
| 190 | |
|
| 191 | 0 | for (String groupByField : groupByList) { |
| 192 | 0 | Object fieldValue = ObjectUtils.getPropertyValue(businessObject, groupByField); |
| 193 | 0 | groupByValueString += "|"; |
| 194 | 0 | if (fieldValue != null) { |
| 195 | 0 | groupByValueString += fieldValue; |
| 196 | |
} |
| 197 | 0 | } |
| 198 | |
|
| 199 | 0 | return groupByValueString; |
| 200 | |
} |
| 201 | |
|
| 202 | |
public void setDateTimeService(DateTimeService dateTimeService) { |
| 203 | 0 | this.dateTimeService = dateTimeService; |
| 204 | 0 | } |
| 205 | |
|
| 206 | |
public void setLookupService(LookupService lookupService) { |
| 207 | 0 | this.lookupService = lookupService; |
| 208 | 0 | } |
| 209 | |
|
| 210 | |
protected DataDictionaryService getDataDictionaryService() { |
| 211 | 0 | if (dataDictionaryService == null) { |
| 212 | 0 | this.dataDictionaryService = KRADServiceLocatorWeb.getDataDictionaryService(); |
| 213 | |
} |
| 214 | 0 | return dataDictionaryService; |
| 215 | |
} |
| 216 | |
|
| 217 | |
public void setDataDictionaryService(DataDictionaryService dataDictionaryService) { |
| 218 | 0 | this.dataDictionaryService = dataDictionaryService; |
| 219 | 0 | } |
| 220 | |
} |