001 /**
002 * Copyright 2004-2012 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.hr.time.timeblock.service;
017
018 import java.text.DateFormat;
019 import java.text.ParseException;
020 import java.text.SimpleDateFormat;
021 import java.util.ArrayList;
022 import java.util.Collections;
023 import java.util.Comparator;
024 import java.util.Date;
025 import java.util.HashMap;
026 import java.util.Iterator;
027 import java.util.List;
028 import java.util.Map;
029
030 import org.apache.commons.lang.StringUtils;
031 import org.joda.time.Interval;
032 import org.kuali.hr.time.timeblock.TimeBlockHistory;
033 import org.kuali.hr.time.timeblock.TimeBlockHistoryDetail;
034 import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl;
035 import org.kuali.rice.krad.bo.BusinessObject;
036
037 public class TimeBlockHistoryDetailLookupableHelperServiceImpl extends KualiLookupableHelperServiceImpl {
038
039 /**
040 *
041 */
042 private static final long serialVersionUID = 1L;
043
044 static final String DOC_ID = "documentId";
045 static final String DOC_STATUS_ID = "timesheetDocumentHeader.documentStatus";
046 static final String BEGIN_DATE_ID = "beginDate";
047
048 @SuppressWarnings("unchecked")
049 @Override
050 public List<? extends BusinessObject> getSearchResults(java.util.Map<String, String> fieldValues) {
051
052 String docStatus = "", beginDateString = "";
053
054 if (fieldValues.containsKey(DOC_STATUS_ID)) {
055 docStatus = fieldValues.get(DOC_STATUS_ID);
056 fieldValues.remove(DOC_STATUS_ID);
057 }
058 if (fieldValues.containsKey(BEGIN_DATE_ID)) {
059 beginDateString = fieldValues.get(BEGIN_DATE_ID);
060 fieldValues.remove(BEGIN_DATE_ID);
061 }
062 List<TimeBlockHistoryDetail> objectList = (List<TimeBlockHistoryDetail>) super.getSearchResults(fieldValues);
063 Map<String,List<TimeBlockHistoryDetail>> timeBlockHistoryToDetailMap = new HashMap<String,List<TimeBlockHistoryDetail>>();
064 Map<String,List<TimeBlockHistoryDetail>> filteredTimeBlockHistoryToDetailMap = new HashMap<String,List<TimeBlockHistoryDetail>>();
065
066 if (!objectList.isEmpty()) {
067 for(TimeBlockHistoryDetail tbhd : objectList){
068 if(!timeBlockHistoryToDetailMap.containsKey(tbhd.getTkTimeBlockHistoryId())){
069 List<TimeBlockHistoryDetail> thdList = new ArrayList<TimeBlockHistoryDetail>();
070 timeBlockHistoryToDetailMap.put(tbhd.getTkTimeBlockHistoryId(), thdList);
071 }
072
073 List<TimeBlockHistoryDetail> thdList = timeBlockHistoryToDetailMap.get(tbhd.getTkTimeBlockHistoryId());
074 thdList.add(tbhd);
075 }
076 filteredTimeBlockHistoryToDetailMap.putAll(timeBlockHistoryToDetailMap);
077
078 Iterator<String> itr = timeBlockHistoryToDetailMap.keySet().iterator();
079 while(itr.hasNext()){
080 String timeHourDetailId = itr.next();
081 List<TimeBlockHistoryDetail> tbhdList = timeBlockHistoryToDetailMap.get(timeHourDetailId);
082 TimeBlockHistoryDetail tbhd = tbhdList.get(0);
083 TimeBlockHistory tbh = tbhd.getTimeBlockHistory();
084
085 if(StringUtils.isNotEmpty(docStatus)){
086 if(tbh.getTimesheetDocumentHeader() == null){
087 filteredTimeBlockHistoryToDetailMap.remove(timeHourDetailId);
088 continue;
089 } else {
090 if (tbh.getTimesheetDocumentHeader().getDocumentStatus() != null) {
091 if (!tbh.getTimesheetDocumentHeader().getDocumentStatus().equals(docStatus)) {
092 filteredTimeBlockHistoryToDetailMap.remove(timeHourDetailId);
093 continue;
094 }
095 } else {
096 filteredTimeBlockHistoryToDetailMap.remove(timeHourDetailId);
097 continue;
098 }
099 }
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 }