View Javadoc

1   /**
2    * Copyright 2004-2012 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.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.time.timeblock.TimeBlockHistory;
33  import org.kuali.hr.time.timeblock.TimeBlockHistoryDetail;
34  import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl;
35  import org.kuali.rice.krad.bo.BusinessObject;
36  
37  public class TimeBlockHistoryDetailLookupableHelperServiceImpl extends KualiLookupableHelperServiceImpl {
38  
39  	/**
40  	 * 
41  	 */
42  	private static final long serialVersionUID = 1L;
43  	
44  	static final String DOC_ID = "documentId";
45  	static final String DOC_STATUS_ID = "timesheetDocumentHeader.documentStatus";
46  	static final String BEGIN_DATE_ID = "beginDate";
47  
48      @SuppressWarnings("unchecked")
49  	@Override
50      public List<? extends BusinessObject> getSearchResults(java.util.Map<String, String> fieldValues) {
51  
52          String docStatus = "", beginDateString = "";
53  
54          if (fieldValues.containsKey(DOC_STATUS_ID)) {
55              docStatus = fieldValues.get(DOC_STATUS_ID);
56              fieldValues.remove(DOC_STATUS_ID);
57          }
58          if (fieldValues.containsKey(BEGIN_DATE_ID)) {
59              beginDateString = fieldValues.get(BEGIN_DATE_ID);
60              fieldValues.remove(BEGIN_DATE_ID);
61          }
62          List<TimeBlockHistoryDetail> objectList = (List<TimeBlockHistoryDetail>) super.getSearchResults(fieldValues);
63          Map<String,List<TimeBlockHistoryDetail>> timeBlockHistoryToDetailMap = new HashMap<String,List<TimeBlockHistoryDetail>>();
64          Map<String,List<TimeBlockHistoryDetail>> filteredTimeBlockHistoryToDetailMap = new HashMap<String,List<TimeBlockHistoryDetail>>();
65          
66          if (!objectList.isEmpty()) {
67          	for(TimeBlockHistoryDetail tbhd : objectList){
68          		if(!timeBlockHistoryToDetailMap.containsKey(tbhd.getTkTimeBlockHistoryId())){
69          			List<TimeBlockHistoryDetail> thdList = new ArrayList<TimeBlockHistoryDetail>();
70          			timeBlockHistoryToDetailMap.put(tbhd.getTkTimeBlockHistoryId(), thdList);
71          		}
72          		
73          		List<TimeBlockHistoryDetail> thdList = timeBlockHistoryToDetailMap.get(tbhd.getTkTimeBlockHistoryId());
74          		thdList.add(tbhd);
75          	}
76          	filteredTimeBlockHistoryToDetailMap.putAll(timeBlockHistoryToDetailMap);
77  
78          	Iterator<String> itr = timeBlockHistoryToDetailMap.keySet().iterator();
79          	while(itr.hasNext()){
80          		String timeHourDetailId = itr.next();
81          		List<TimeBlockHistoryDetail> tbhdList = timeBlockHistoryToDetailMap.get(timeHourDetailId);
82          		TimeBlockHistoryDetail tbhd = tbhdList.get(0);
83          		TimeBlockHistory tbh = tbhd.getTimeBlockHistory();
84          		
85          		if(StringUtils.isNotEmpty(docStatus)){
86          			if(tbh.getTimesheetDocumentHeader() == null){
87          				filteredTimeBlockHistoryToDetailMap.remove(timeHourDetailId);
88          				continue;
89          			} else {
90          				if (tbh.getTimesheetDocumentHeader().getDocumentStatus() != null) {
91          					if (!tbh.getTimesheetDocumentHeader().getDocumentStatus().equals(docStatus)) {
92          						filteredTimeBlockHistoryToDetailMap.remove(timeHourDetailId);
93          						continue;
94          					}
95          				} else {
96          					filteredTimeBlockHistoryToDetailMap.remove(timeHourDetailId);
97          					continue;
98          				}
99          			}
100         		}
101         		
102                 if(StringUtils.isNotEmpty(beginDateString)) {
103 					if(tbh.getBeginDate() != null) {
104 						if(!this.inDateRange(tbh.getBeginDate(), beginDateString)) {
105 							filteredTimeBlockHistoryToDetailMap.remove(timeHourDetailId);
106 							continue;
107 						} 
108 					} else {
109 						filteredTimeBlockHistoryToDetailMap.remove(timeHourDetailId);
110 						continue;
111 					}
112 				}
113         	}
114         }
115         
116         List<TimeBlockHistoryDetail> lstFinalList = new ArrayList<TimeBlockHistoryDetail>();
117         for(List<TimeBlockHistoryDetail> tbhdList : filteredTimeBlockHistoryToDetailMap.values()){
118         	lstFinalList.addAll(tbhdList);
119         }
120 
121         sortByTimeBlockId(lstFinalList);
122         return lstFinalList;
123     }
124 
125     private void sortByTimeBlockId(List<TimeBlockHistoryDetail> objectList) {
126         Collections.sort(objectList, new Comparator<TimeBlockHistoryDetail>() { // Sort the Time Blocks
127             @Override
128             public int compare(TimeBlockHistoryDetail timeBlockHistory, TimeBlockHistoryDetail timeBlockHistory1) {
129                 return timeBlockHistory.getTimeBlockHistory().getTkTimeBlockId().compareTo(timeBlockHistory1.getTimeBlockHistory().getTkTimeBlockId());
130             }
131         });
132     }
133     
134 	 public boolean inDateRange(Date asOfDate, String dateString) {
135 		try {
136 			DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
137 			Date dateFrom;
138 			Date dateTo;
139 			String subDateString;
140 			if(dateString.indexOf("..") == 10) {
141 				subDateString= dateString.substring(0, 10);
142 				dateFrom = df.parse(subDateString);
143 				subDateString= dateString.substring(12, dateString.length());
144 				dateTo = df.parse(subDateString);
145 				if(asOfDate != null) {
146 					Interval range = new Interval(dateFrom.getTime(),dateTo.getTime());
147 					return range.contains(asOfDate.getTime());
148 				} else {
149 					return false;
150 				}
151 			} else{
152 				subDateString= dateString.substring(2, dateString.length());
153 				dateTo = df.parse(subDateString);
154 				if(asOfDate != null) {
155 					if( (dateString.startsWith(">=") && asOfDate.before(dateTo))
156 							|| (dateString.startsWith("<=") && asOfDate.after(dateTo))) {
157 						return false;
158 					}
159 				} else {
160 					return false;
161 				}
162 			}
163 		} catch (ParseException e) {
164 		}
165 	  return true;
166 	 }
167 }