View Javadoc

1   /**
2    * Copyright 2004-2013 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.Date;
23  import java.util.Iterator;
24  import java.util.List;
25  
26  import org.apache.commons.lang.StringUtils;
27  import org.kuali.hr.job.Job;
28  import org.kuali.hr.time.department.Department;
29  import org.kuali.hr.time.roles.TkRole;
30  import org.kuali.hr.time.service.base.TkServiceLocator;
31  import org.kuali.hr.time.timeblock.TimeBlock;
32  import org.kuali.hr.time.timeblock.TimeHourDetail;
33  import org.kuali.hr.time.util.TKContext;
34  import org.kuali.hr.time.util.TKUtils;
35  import org.kuali.hr.time.util.TkConstants;
36  import org.kuali.rice.kns.lookup.HtmlData;
37  import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl;
38  import org.kuali.rice.krad.bo.BusinessObject;
39  
40  public class TimeBlockLookupableHelperServiceImpl extends KualiLookupableHelperServiceImpl {
41  
42  	/**
43  	 * 
44  	 */
45  	private static final long serialVersionUID = 1L;
46  	
47  	static final String DOC_ID = "documentId";
48  	static final String DOC_STATUS_ID = "timesheetDocumentHeader.documentStatus";
49  	static final String BEGIN_DATE_ID = "beginDate";
50      private static final String BEGIN_TIMESTAMP = "beginTimestamp";
51  		
52  	 @Override
53      public List<? extends BusinessObject> getSearchResults(java.util.Map<String, String> fieldValues) {
54  
55           if (fieldValues.containsKey(BEGIN_DATE_ID)) {
56               //beginDate = fieldValues.get(BEGIN_DATE);
57               fieldValues.put(BEGIN_TIMESTAMP, fieldValues.get(BEGIN_DATE_ID));
58               fieldValues.remove(BEGIN_DATE_ID);
59           }
60          
61          List<TimeBlock> objectList = (List<TimeBlock>) super.getSearchResults(fieldValues);
62        
63          if(!objectList.isEmpty()) {
64          	Iterator<? extends BusinessObject> itr = objectList.iterator();
65  			while(itr.hasNext()){
66  				TimeBlock tb = (TimeBlock)itr.next();
67  				List<TkRole> tkRoles = TkServiceLocator.getTkRoleService().getRoles(TKContext.getPrincipalId(), TKUtils.getCurrentDate());
68  				Job job = TkServiceLocator.getJobService().getJob(tb.getUserPrincipalId(), tb.getJobNumber(), TKUtils.getCurrentDate(), false);
69  				boolean valid = false;
70  				for (TkRole tkRole : tkRoles) {
71  					if (StringUtils.equals(tkRole.getRoleName(),
72  							TkConstants.ROLE_TK_SYS_ADMIN)
73  							|| (StringUtils.equals(tkRole.getRoleName(), TkConstants.ROLE_TK_GLOBAL_VO))
74  							|| (StringUtils.equals(tkRole.getRoleName(),
75  									TkConstants.ROLE_TK_APPROVER) && tb
76  									.getWorkArea().equals(tkRole.getWorkArea()))
77  							|| (StringUtils.equals(tkRole.getRoleName(),
78  									TkConstants.ROLE_TK_DEPT_ADMIN) && (job != null && (job
79  									.getDept().equals(tkRole.getDepartment()))))) {
80  						valid = true;
81  						break;
82  					}
83  					if(StringUtils.equals(tkRole.getRoleName(), TkConstants.ROLE_TK_LOCATION_ADMIN) && job != null && tkRole.getLocationObj()!=null){
84  						List<Department> departments = TkServiceLocator.getDepartmentService().getDepartmentByLocation(tkRole.getLocationObj().getLocation());
85  						for(Department department : departments){
86  							if(StringUtils.equals(job.getDept(), department.getDept())){
87  								valid = true;
88  								break;
89  							}
90  						}
91  						if(valid){
92  							break;
93  						}
94  					}
95  				}
96  				if (!valid) {
97  					itr.remove();
98  					continue;
99  				}
100 			}
101 			
102 			// Fetch list from time hour detail and convert it into TimeBlock
103 			if(!objectList.isEmpty()) {
104 				List<TimeBlock> timeBlocks = new ArrayList<TimeBlock>(objectList);
105 				for(TimeBlock tb: timeBlocks) {
106 					List<TimeHourDetail> timeHourDetails = tb.getTimeHourDetails();
107 					for(TimeHourDetail thd : timeHourDetails) {
108 					  if(!thd.getEarnCode().equalsIgnoreCase(tb.getEarnCode())) {
109 						  TimeBlock timeBlock = tb.copy();
110 						  timeBlock.setEarnCode(thd.getEarnCode());
111 						  timeBlock.setHours(thd.getHours());
112 						  timeBlock.setAmount(thd.getAmount());
113 						  objectList.add(timeBlock);
114 					  }
115 					} // inner for ends
116 				} // outer for ends
117 			} // if ends
118 			
119         }
120         
121      
122         return objectList;
123 	 }
124 	 
125 	 public boolean checkDate(TimeBlock tb, Date asOfDate, String dateString) {
126 		 if(tb.getTimesheetDocumentHeader() == null) {
127 				return false;
128 		 }
129 		try {
130 			DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
131 			Date dateFrom;
132 			Date dateTo;
133 			String subDateString;
134 			if(dateString.indexOf("..") == 10) {
135 				subDateString= dateString.substring(0, 10);
136 				dateFrom = df.parse(subDateString);
137 				subDateString= dateString.substring(12, dateString.length());
138 				dateTo = df.parse(subDateString);
139 				if(asOfDate != null) {
140 					if(!( (asOfDate.after(dateFrom) || asOfDate.equals(dateFrom))
141 							&& (asOfDate.before(dateTo) || asOfDate.equals(dateTo)))) {
142 						return false;
143 					}
144 				} else {
145 					return false;
146 				}
147 			} else{
148 				subDateString= dateString.substring(2, dateString.length());
149 				dateTo = df.parse(subDateString);
150 				if(asOfDate != null) {
151 					if( (dateString.startsWith(">=") && asOfDate.before(dateTo))
152 							|| (dateString.startsWith("<=") && asOfDate.after(dateTo))) {
153 						return false;
154 					}
155 				} else {
156 					return false;
157 				}
158 			}
159 		} catch (ParseException e) {
160 		}
161 	  return true;
162 	 }
163 	 
164 	@SuppressWarnings("unchecked")
165 	@Override
166 	public List<HtmlData> getCustomActionUrls(BusinessObject businessObject, List pkNames) {
167 		List<HtmlData> customActionUrls = super.getCustomActionUrls(businessObject, pkNames);
168 		List<HtmlData> overrideUrls = new ArrayList<HtmlData>();
169 		for(HtmlData actionUrl : customActionUrls){
170 			if(!StringUtils.equals(actionUrl.getMethodToCall(), "copy")){
171 				overrideUrls.add(actionUrl);
172 			}
173 		}
174 		return overrideUrls;
175 	}
176 }