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.Date;
023 import java.util.Iterator;
024 import java.util.List;
025
026 import org.apache.commons.lang.StringUtils;
027 import org.kuali.hr.job.Job;
028 import org.kuali.hr.time.department.Department;
029 import org.kuali.hr.time.roles.TkRole;
030 import org.kuali.hr.time.service.base.TkServiceLocator;
031 import org.kuali.hr.time.timeblock.TimeBlock;
032 import org.kuali.hr.time.util.TKContext;
033 import org.kuali.hr.time.util.TKUtils;
034 import org.kuali.hr.time.util.TkConstants;
035 import org.kuali.rice.kns.lookup.HtmlData;
036 import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl;
037 import org.kuali.rice.krad.bo.BusinessObject;
038
039 public class TimeBlockLookupableHelperServiceImpl extends KualiLookupableHelperServiceImpl {
040
041 /**
042 *
043 */
044 private static final long serialVersionUID = 1L;
045
046 static final String DOC_ID = "documentId";
047 static final String DOC_STATUS_ID = "timesheetDocumentHeader.documentStatus";
048 static final String BEGIN_DATE_ID = "beginDate";
049
050 @Override
051 public List<? extends BusinessObject> getSearchResults(java.util.Map<String, String> fieldValues) {
052
053 String docStatus = "", beginDateString="";
054
055 if(fieldValues.containsKey(DOC_STATUS_ID)){
056 docStatus = fieldValues.get(DOC_STATUS_ID);
057 fieldValues.remove(DOC_STATUS_ID);
058 }
059 if(fieldValues.containsKey(BEGIN_DATE_ID)){
060 beginDateString = fieldValues.get(BEGIN_DATE_ID);
061 fieldValues.remove(BEGIN_DATE_ID);
062 }
063
064 List<? extends BusinessObject> objectList = super.getSearchResults(fieldValues);
065
066 if(!objectList.isEmpty()) {
067 Iterator<? extends BusinessObject> itr = objectList.iterator();
068 while(itr.hasNext()){
069 TimeBlock tb = (TimeBlock)itr.next();
070 List<TkRole> tkRoles = TkServiceLocator.getTkRoleService().getRoles(TKContext.getPrincipalId(), TKUtils.getCurrentDate());
071 Job job = TkServiceLocator.getJobService().getJob(tb.getUserPrincipalId(), tb.getJobNumber(), TKUtils.getCurrentDate(), false);
072 boolean valid = false;
073 for (TkRole tkRole : tkRoles) {
074 if (StringUtils.equals(tkRole.getRoleName(),
075 TkConstants.ROLE_TK_SYS_ADMIN)
076 || (StringUtils.equals(tkRole.getRoleName(), TkConstants.ROLE_TK_GLOBAL_VO))
077 || (StringUtils.equals(tkRole.getRoleName(),
078 TkConstants.ROLE_TK_APPROVER) && tb
079 .getWorkArea().equals(tkRole.getWorkArea()))
080 || (StringUtils.equals(tkRole.getRoleName(),
081 TkConstants.ROLE_TK_DEPT_ADMIN) && (job != null && (job
082 .getDept().equals(tkRole.getDepartment()))))) {
083 valid = true;
084 break;
085 }
086 if(StringUtils.equals(tkRole.getRoleName(), TkConstants.ROLE_TK_LOCATION_ADMIN) && job != null && tkRole.getLocationObj()!=null){
087 List<Department> departments = TkServiceLocator.getDepartmentService().getDepartmentByLocation(tkRole.getLocationObj().getLocation());
088 for(Department department : departments){
089 if(StringUtils.equals(job.getDept(), department.getDept())){
090 valid = true;
091 break;
092 }
093 }
094 if(valid){
095 break;
096 }
097 }
098 }
099 if (!valid) {
100 itr.remove();
101 continue;
102 }
103 if(StringUtils.isNotEmpty(docStatus)) {
104 if(tb.getTimesheetDocumentHeader() == null) {
105 itr.remove();
106 continue;
107 } else {
108 if(tb.getTimesheetDocumentHeader().getDocumentStatus() != null) {
109 if(!tb.getTimesheetDocumentHeader().getDocumentStatus().equals(docStatus)){
110 itr.remove();
111 continue;
112 }
113 } else {
114 itr.remove();
115 continue;
116 }
117 }
118 }
119
120 if(StringUtils.isNotEmpty(beginDateString)) {
121 if(tb.getBeginDate() != null) {
122 if(!this.checkDate(tb, tb.getBeginDate(), beginDateString)) {
123 itr.remove();
124 continue;
125 }
126 } else {
127 itr.remove();
128 continue;
129 }
130 }
131 }
132 }
133 return objectList;
134 }
135
136 public boolean checkDate(TimeBlock tb, Date asOfDate, String dateString) {
137 if(tb.getTimesheetDocumentHeader() == null) {
138 return false;
139 }
140 try {
141 DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
142 Date dateFrom;
143 Date dateTo;
144 String subDateString;
145 if(dateString.indexOf("..") == 10) {
146 subDateString= dateString.substring(0, 10);
147 dateFrom = df.parse(subDateString);
148 subDateString= dateString.substring(12, dateString.length());
149 dateTo = df.parse(subDateString);
150 if(asOfDate != null) {
151 if(!( (asOfDate.after(dateFrom) || asOfDate.equals(dateFrom))
152 && (asOfDate.before(dateTo) || asOfDate.equals(dateTo)))) {
153 return false;
154 }
155 } else {
156 return false;
157 }
158 } else{
159 subDateString= dateString.substring(2, dateString.length());
160 dateTo = df.parse(subDateString);
161 if(asOfDate != null) {
162 if( (dateString.startsWith(">=") && asOfDate.before(dateTo))
163 || (dateString.startsWith("<=") && asOfDate.after(dateTo))) {
164 return false;
165 }
166 } else {
167 return false;
168 }
169 }
170 } catch (ParseException e) {
171 }
172 return true;
173 }
174
175 @SuppressWarnings("unchecked")
176 @Override
177 public List<HtmlData> getCustomActionUrls(BusinessObject businessObject, List pkNames) {
178 List<HtmlData> customActionUrls = super.getCustomActionUrls(businessObject, pkNames);
179 List<HtmlData> overrideUrls = new ArrayList<HtmlData>();
180 for(HtmlData actionUrl : customActionUrls){
181 if(!StringUtils.equals(actionUrl.getMethodToCall(), "copy")){
182 overrideUrls.add(actionUrl);
183 }
184 }
185 return overrideUrls;
186 }
187 }