1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.hr.time.timeblock.service;
17
18 import java.text.DateFormat;
19 import java.text.ParseException;
20 import java.text.SimpleDateFormat;
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.Comparator;
24 import java.util.Date;
25 import java.util.HashMap;
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.Map;
29
30 import org.apache.commons.lang.StringUtils;
31 import org.joda.time.Interval;
32 import org.kuali.hr.job.Job;
33 import org.kuali.hr.time.department.Department;
34 import org.kuali.hr.time.roles.TkRole;
35 import org.kuali.hr.time.service.base.TkServiceLocator;
36 import org.kuali.hr.time.timeblock.TimeBlockHistory;
37 import org.kuali.hr.time.timeblock.TimeBlockHistoryDetail;
38 import org.kuali.hr.time.util.TKContext;
39 import org.kuali.hr.time.util.TKUtils;
40 import org.kuali.hr.time.util.TkConstants;
41 import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl;
42 import org.kuali.rice.krad.bo.BusinessObject;
43
44 public class TimeBlockHistoryDetailLookupableHelperServiceImpl extends KualiLookupableHelperServiceImpl {
45
46
47
48
49 private static final long serialVersionUID = 1L;
50
51 static final String DOC_ID = "documentId";
52 static final String DOC_STATUS_ID = "timeBlockHistory.timesheetDocumentHeader.documentStatus";
53 static final String BEGIN_DATE_ID = "beginDate";
54
55 @SuppressWarnings("unchecked")
56 @Override
57 public List<? extends BusinessObject> getSearchResults(java.util.Map<String, String> fieldValues) {
58
59 String docStatus = "", beginDateString = "";
60
61 if (fieldValues.containsKey(DOC_STATUS_ID)) {
62 docStatus = fieldValues.get(DOC_STATUS_ID);
63 fieldValues.remove(DOC_STATUS_ID);
64 }
65 if (fieldValues.containsKey(BEGIN_DATE_ID)) {
66 beginDateString = fieldValues.get(BEGIN_DATE_ID);
67 fieldValues.remove(BEGIN_DATE_ID);
68 }
69 List<TimeBlockHistoryDetail> objectList = (List<TimeBlockHistoryDetail>) super.getSearchResults(fieldValues);
70 Map<String,List<TimeBlockHistoryDetail>> timeBlockHistoryToDetailMap = new HashMap<String,List<TimeBlockHistoryDetail>>();
71 Map<String,List<TimeBlockHistoryDetail>> filteredTimeBlockHistoryToDetailMap = new HashMap<String,List<TimeBlockHistoryDetail>>();
72
73 if (!objectList.isEmpty()) {
74 this.filterWithSecurity(objectList);
75
76 for(TimeBlockHistoryDetail tbhd : objectList){
77 if(!timeBlockHistoryToDetailMap.containsKey(tbhd.getTkTimeBlockHistoryId())){
78 List<TimeBlockHistoryDetail> thdList = new ArrayList<TimeBlockHistoryDetail>();
79 timeBlockHistoryToDetailMap.put(tbhd.getTkTimeBlockHistoryId(), thdList);
80 }
81
82 List<TimeBlockHistoryDetail> thdList = timeBlockHistoryToDetailMap.get(tbhd.getTkTimeBlockHistoryId());
83 thdList.add(tbhd);
84 }
85 filteredTimeBlockHistoryToDetailMap.putAll(timeBlockHistoryToDetailMap);
86
87 Iterator<String> itr = timeBlockHistoryToDetailMap.keySet().iterator();
88 while(itr.hasNext()){
89 String timeHourDetailId = itr.next();
90 List<TimeBlockHistoryDetail> tbhdList = timeBlockHistoryToDetailMap.get(timeHourDetailId);
91 TimeBlockHistoryDetail tbhd = tbhdList.get(0);
92 TimeBlockHistory tbh = tbhd.getTimeBlockHistory();
93
94 if(StringUtils.isNotEmpty(docStatus)){
95 if(tbh.getTimesheetDocumentHeader() == null){
96 filteredTimeBlockHistoryToDetailMap.remove(timeHourDetailId);
97 continue;
98 } else {
99 if (tbh.getTimesheetDocumentHeader().getDocumentStatus() != null) {
100 if (!tbh.getTimesheetDocumentHeader().getDocumentStatus().equals(docStatus)) {
101 filteredTimeBlockHistoryToDetailMap.remove(timeHourDetailId);
102 continue;
103 }
104 } else {
105 filteredTimeBlockHistoryToDetailMap.remove(timeHourDetailId);
106 continue;
107 }
108 }
109 }
110
111 if(StringUtils.isNotEmpty(beginDateString)) {
112 if(tbh.getBeginDate() != null) {
113 if(!this.inDateRange(tbh.getBeginDate(), beginDateString)) {
114 filteredTimeBlockHistoryToDetailMap.remove(timeHourDetailId);
115 continue;
116 }
117 } else {
118 filteredTimeBlockHistoryToDetailMap.remove(timeHourDetailId);
119 continue;
120 }
121 }
122 }
123 }
124
125 List<TimeBlockHistoryDetail> lstFinalList = new ArrayList<TimeBlockHistoryDetail>();
126 for(List<TimeBlockHistoryDetail> tbhdList : filteredTimeBlockHistoryToDetailMap.values()){
127 lstFinalList.addAll(tbhdList);
128 }
129
130 sortByTimeBlockId(lstFinalList);
131 return lstFinalList;
132 }
133
134 private void filterWithSecurity(List<TimeBlockHistoryDetail> objectList) {
135 Iterator<? extends BusinessObject> itr = objectList.iterator();
136 List<TkRole> tkRoles = TkServiceLocator.getTkRoleService().getRoles(TKContext.getPrincipalId(), TKUtils.getCurrentDate());
137 while(itr.hasNext()){
138 TimeBlockHistoryDetail tbhd = (TimeBlockHistoryDetail)itr.next();
139 Job job = TkServiceLocator.getJobService().getJob(tbhd.getTimeBlockHistory().getPrincipalId(), tbhd.getTimeBlockHistory().getJobNumber(), TKUtils.getCurrentDate(), false);
140 boolean valid = false;
141 for (TkRole tkRole : tkRoles) {
142 if (StringUtils.equals(tkRole.getRoleName(),
143 TkConstants.ROLE_TK_SYS_ADMIN)
144 || (StringUtils.equals(tkRole.getRoleName(), TkConstants.ROLE_TK_GLOBAL_VO))
145 || (StringUtils.equals(tkRole.getRoleName(),
146 TkConstants.ROLE_TK_APPROVER) && tbhd.getTimeBlockHistory().getWorkArea().equals(tkRole.getWorkArea()))
147 || (StringUtils.equals(tkRole.getRoleName(),
148 TkConstants.ROLE_TK_DEPT_ADMIN) && (job != null && (job.getDept().equals(tkRole.getDepartment()))))) {
149 valid = true;
150 break;
151 }
152 if(StringUtils.equals(tkRole.getRoleName(), TkConstants.ROLE_TK_LOCATION_ADMIN) && job != null && tkRole.getLocationObj()!=null){
153 List<Department> departments = TkServiceLocator.getDepartmentService().getDepartmentByLocation(tkRole.getLocationObj().getLocation());
154 for(Department department : departments){
155 if(StringUtils.equals(job.getDept(), department.getDept())){
156 valid = true;
157 break;
158 }
159 }
160 if(valid){
161 break;
162 }
163 }
164 }
165 if (!valid) {
166 itr.remove();
167 continue;
168 }
169 }
170 }
171
172 private void sortByTimeBlockId(List<TimeBlockHistoryDetail> objectList) {
173 Collections.sort(objectList, new Comparator<TimeBlockHistoryDetail>() {
174 @Override
175 public int compare(TimeBlockHistoryDetail timeBlockHistory, TimeBlockHistoryDetail timeBlockHistory1) {
176 return timeBlockHistory.getTimeBlockHistory().getTkTimeBlockId().compareTo(timeBlockHistory1.getTimeBlockHistory().getTkTimeBlockId());
177 }
178 });
179 }
180
181 public boolean inDateRange(Date asOfDate, String dateString) {
182 try {
183 DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
184 java.util.Date dateFrom = df.parse(TKUtils.getFromDateString(dateString));
185 java.util.Date dateTo = df.parse(TKUtils.getToDateString(dateString));;
186
187 if(dateString.indexOf("..") == 10) {
188 if(asOfDate != null) {
189 Interval range = new Interval(dateFrom.getTime(),dateTo.getTime());
190 return range.contains(asOfDate.getTime());
191 } else {
192 return false;
193 }
194 } else{
195 if(asOfDate != null) {
196 if( (dateString.startsWith(">=") && asOfDate.before(dateTo))
197 || (dateString.startsWith("<=") && asOfDate.after(dateTo))) {
198 return false;
199 }
200 } else {
201 return false;
202 }
203 }
204 } catch (ParseException e) {
205 }
206 return true;
207 }
208 }