1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
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 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | 0 | public class InactivateableFromToServiceImpl implements InactivateableFromToService { |
41 | |
|
42 | |
protected DateTimeService dateTimeService; |
43 | |
protected BusinessObjectDictionaryService businessObjectDictionaryService; |
44 | |
protected LookupService lookupService; |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
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 | |
|
59 | |
|
60 | |
|
61 | |
|
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 | |
|
73 | |
|
74 | |
public List<InactivateableFromTo> filterOutNonActive(List<InactivateableFromTo> filterList) { |
75 | 0 | return filterOutNonActive(filterList, dateTimeService.getCurrentDate()); |
76 | |
} |
77 | |
|
78 | |
|
79 | |
|
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 | |
|
96 | |
|
97 | |
|
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 | |
|
109 | |
|
110 | |
|
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 | |
|
123 | |
|
124 | |
public List<InactivateableFromTo> filterOutNonCurrent(List<InactivateableFromTo> filterList) { |
125 | 0 | return filterOutNonCurrent(filterList, dateTimeService.getCurrentDate()); |
126 | |
} |
127 | |
|
128 | |
|
129 | |
|
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 | |
|
147 | 0 | Collections.sort(activeList, new BeanPropertyComparator(sortByList, true)); |
148 | |
|
149 | |
|
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 | |
|
158 | 0 | currentList.add(inactivateable); |
159 | |
} |
160 | |
|
161 | 0 | else if (inactivateable.getActiveFromDate().equals(previousActiveFromDate)) { |
162 | |
|
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 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
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 | |
} |