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.workarea.service;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  import java.util.Map;
21  import java.util.Properties;
22  
23  import org.apache.commons.lang.StringUtils;
24  import org.kuali.hr.time.authorization.DepartmentalRule;
25  import org.kuali.hr.time.authorization.DepartmentalRuleAuthorizer;
26  import org.kuali.hr.time.authorization.TkAuthorizedLookupableHelperBase;
27  import org.kuali.hr.time.service.base.TkServiceLocator;
28  import org.kuali.hr.time.util.TKContext;
29  import org.kuali.hr.time.util.TKUtils;
30  import org.kuali.hr.time.workarea.WorkArea;
31  import org.kuali.rice.kns.document.authorization.BusinessObjectRestrictions;
32  import org.kuali.rice.kns.lookup.HtmlData;
33  import org.kuali.rice.kns.lookup.HtmlData.AnchorHtmlData;
34  import org.kuali.rice.kns.web.struts.form.LookupForm;
35  import org.kuali.rice.krad.bo.BusinessObject;
36  import org.kuali.rice.krad.util.KRADConstants;
37  import org.kuali.rice.krad.util.UrlFactory;
38  
39  public class WorkAreaLookupableHelper extends TkAuthorizedLookupableHelperBase {
40  
41      /**
42       * Implemented method to reduce the set of Business Objects that are shown
43       * to the user based on their current roles.
44       */
45      public boolean shouldShowBusinessObject(BusinessObject bo) {
46          return (bo instanceof DepartmentalRule) && DepartmentalRuleAuthorizer.hasAccessToRead((DepartmentalRule)bo);
47      }
48  
49  	@Override
50  	public List<HtmlData> getCustomActionUrls(BusinessObject businessObject, List pkNames) {
51  		List<HtmlData> customActionUrls = new ArrayList<HtmlData>();
52  		
53  		List<HtmlData> defaultCustomActionUrls = super.getCustomActionUrls(businessObject, pkNames);
54  		
55  		WorkArea workArea = (WorkArea) businessObject;
56  		String tkWorkAreaId = workArea.getTkWorkAreaId();
57  		String location = TkServiceLocator.getDepartmentService().getDepartment(workArea.getDept(), TKUtils.getCurrentDate()).getLocation();
58  		String department = workArea.getDept();
59  		
60  		boolean systemAdmin = TKContext.getUser().isSystemAdmin();
61  		boolean locationAdmin = TKContext.getUser().getLocationAdminAreas().contains(location);
62  		boolean departmentAdmin = TKContext.getUser().getDepartmentAdminAreas().contains(department);
63  		
64  		for (HtmlData defaultCustomActionUrl : defaultCustomActionUrls){
65  			if (StringUtils.equals(defaultCustomActionUrl.getMethodToCall(), "edit")) {
66  				if (systemAdmin || locationAdmin || departmentAdmin) {
67  					customActionUrls.add(defaultCustomActionUrl);
68  				}
69  			} else {
70  				customActionUrls.add(defaultCustomActionUrl);
71  			}
72  		}
73  		
74  		Properties params = new Properties();
75  		params.put(KRADConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, getBusinessObjectClass().getName());
76  		params.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.MAINTENANCE_NEW_METHOD_TO_CALL);
77  		params.put("tkWorkAreaId", tkWorkAreaId);
78  		AnchorHtmlData viewUrl = new AnchorHtmlData(UrlFactory.parameterizeUrl(KRADConstants.INQUIRY_ACTION, params), "view");
79  		viewUrl.setDisplayText("view");
80  		viewUrl.setTarget(AnchorHtmlData.TARGET_BLANK);
81  		customActionUrls.add(viewUrl);
82  		
83  		return customActionUrls;
84  	}
85  
86  	@Override
87  	public HtmlData getReturnUrl(BusinessObject businessObject,
88  			LookupForm lookupForm, List returnKeys,
89  			BusinessObjectRestrictions businessObjectRestrictions) {
90  		if (lookupForm.getFieldConversions().containsKey("effectiveDate")) {
91  			lookupForm.getFieldConversions().remove("effectiveDate");
92  		}
93  		if (returnKeys.contains("effectiveDate")) {
94  			returnKeys.remove("effectiveDate");
95  		}
96  		if (lookupForm.getFieldConversions().containsKey("dept")) {
97  			lookupForm.getFieldConversions().remove("dept");
98  		}
99  		if (returnKeys.contains("dept")) {
100 			returnKeys.remove("dept");
101 		}
102 		
103 		if(lookupForm.getFieldConversions().containsKey("tkWorkAreaId")){
104 			lookupForm.getFieldConversions().remove("tkWorkAreaId");
105 		}
106 		if(returnKeys.contains("tkWorkAreaId")){
107 			returnKeys.remove("tkWorkAreaId");
108 		}
109 		return super.getReturnUrl(businessObject, lookupForm, returnKeys,
110 				businessObjectRestrictions);
111 	}
112 	
113 
114 
115 	@Override
116 	protected void validateSearchParameterWildcardAndOperators(
117 			String attributeName, String attributeValue) {
118 		if (!StringUtils.equals(attributeValue, "%")) {
119 			super.validateSearchParameterWildcardAndOperators(attributeName,
120 					attributeValue);
121 		}
122 	}
123 	
124 	
125 	@SuppressWarnings("unchecked")
126 	@Override
127 	/**
128 	 * Custom work area search logic
129 	 * 
130 	 */
131 	public List<? extends BusinessObject> getSearchResults(
132 			Map<String, String> fieldValues) {
133 		
134 		String dept = fieldValues.get("dept");
135 		String workArea = fieldValues.get("workArea");
136 		String descr = fieldValues.get("description");
137 		String fromEffdt = fieldValues.get("rangeLowerBoundKeyPrefix_effectiveDate");
138 		String toEffdt = StringUtils.isNotBlank(fieldValues.get("effectiveDate")) ? fieldValues.get("effectiveDate").replace("<=", "") : "";
139 		String active = fieldValues.get("active");
140 		String showHist = fieldValues.get("history");
141 		
142 		
143 		return TkServiceLocator.getWorkAreaService().getWorkAreas(dept, workArea, descr, TKUtils.formatDateString(fromEffdt), 
144 													TKUtils.formatDateString(toEffdt), active, showHist);
145 		
146 //		if (fieldValues.containsKey("jobNumber")
147 //				&& StringUtils.equals(fieldValues.get("jobNumber"), "%")) {
148 //			fieldValues.put("jobNumber", "");
149 //		}
150 //		
151 //		if (fieldValues.containsKey("workArea")
152 //				&& StringUtils.equals(fieldValues.get("workArea"), "%")) {
153 //			fieldValues.put("workArea", "");
154 //		}
155 //		if (fieldValues.containsKey("principalId")
156 //				&& StringUtils.equals(fieldValues.get("principalId"), "%")) {
157 //			fieldValues.put("principalId", "");
158 //		}		
159 //		String showHistory = "Y";
160 //		if (fieldValues.containsKey("history")) {
161 //			showHistory = fieldValues.get("history");
162 //			fieldValues.remove("history");
163 //		}
164 //		String active = "";
165 //		if(fieldValues.containsKey("active")){
166 //			active = fieldValues.get("active");
167 //			fieldValues.put("active", "");
168 //		}
169 //		
170 //		List<HrBusinessObject> hrObjList = (List<HrBusinessObject>)super.getSearchResults(fieldValues);
171 //		//Create a principalId+jobNumber map as this is the unique key for results
172 //		Map<String,List<HrBusinessObject>> hrBusinessMap = new HashMap<String,List<HrBusinessObject>>();
173 //		
174 //		for(HrBusinessObject hrObj : hrObjList){
175 //			String key = hrObj.getUniqueKey();
176 //			
177 //			//If no key exists for this business object return full collection
178 //			if(StringUtils.isEmpty(key)){
179 //				return hrObjList;
180 //			}
181 //			if(hrBusinessMap.get(key)!= null){
182 //				List<HrBusinessObject> lstHrBusinessList = hrBusinessMap.get(key);
183 //				lstHrBusinessList.add(hrObj);
184 //			} else {
185 //				List<HrBusinessObject> lstHrBusinessObj = new ArrayList<HrBusinessObject>();
186 //				lstHrBusinessObj.add(hrObj);
187 //				hrBusinessMap.put(key, lstHrBusinessObj);
188 //			}
189 //		}
190 //		
191 //		List<BusinessObject> finalBusinessObjectList = new ArrayList<BusinessObject>();
192 //		
193 //		for(List<HrBusinessObject> lstHrBusinessObj: hrBusinessMap.values()){
194 //			Collections.sort(lstHrBusinessObj, new EffectiveDateTimestampCompare());
195 //			Collections.reverse(lstHrBusinessObj);
196 //		}
197 //		
198 //		
199 //		Date currDate = TKUtils.getCurrentDate();
200 //		//Active = Both and Show History = Yes
201 //		//return all results
202 //		if(StringUtils.isEmpty(active) && StringUtils.equals("Y", showHistory)){
203 //			return hrObjList;
204 //		} 
205 //		//Active = Both and show history = No
206 //		//return the most effective results from today and any future rows
207 //		else if(StringUtils.isEmpty(active) && StringUtils.equals("N", showHistory)){
208 //			for(List<HrBusinessObject> lstHrBusiness : hrBusinessMap.values()){
209 //				for(HrBusinessObject hrBus : lstHrBusiness){
210 //					if(hrBus.getEffectiveDate().before(currDate)){
211 //						finalBusinessObjectList.add(hrBus);
212 //						break;
213 //					} else {
214 //						finalBusinessObjectList.add(hrBus);
215 //					}
216 //				}
217 //			}
218 //		}
219 //		//Active = Yes and Show History = No
220 //		//return all active records as of today and any active future rows
221 //		//if there is an inactive record before the active one then do not show the results as this record is inactive
222 //		else if(StringUtils.equals(active, "Y") && StringUtils.equals("N", showHistory)){
223 //			for(List<HrBusinessObject> lstHrBus : hrBusinessMap.values()){
224 //				for(HrBusinessObject hrBusinessObject : lstHrBus){
225 //					if(!hrBusinessObject.isActive() && hrBusinessObject.getEffectiveDate().before(currDate)){
226 //						break;
227 //					}
228 //					else {
229 //						if(hrBusinessObject.getEffectiveDate().before(currDate)){
230 //							finalBusinessObjectList.add(hrBusinessObject);
231 //							break;
232 //						} else {
233 //							if(hrBusinessObject.isActive()){
234 //								finalBusinessObjectList.add(hrBusinessObject);
235 //							}
236 //						}
237 //					}
238 //				}
239 //			}			
240 //		}
241 //		//Active = Yes and Show History = Yes
242 //		//return all active records from database
243 //		//if there is an inactive record before the active one then do not show the results as this record is inactive
244 //		else if(StringUtils.equals(active, "Y") && StringUtils.equals("Y", showHistory)){
245 //			for(List<HrBusinessObject> lstHrBus : hrBusinessMap.values()){
246 //				for(HrBusinessObject hrBus : lstHrBus){
247 //					if(!hrBus.isActive() && hrBus.getEffectiveDate().before(currDate)){
248 //						break;
249 //					}
250 //					else if(hrBus.isActive()){
251 //						finalBusinessObjectList.add(hrBus);			
252 //					}
253 //				}
254 //			}
255 //		}
256 //		//Active = No and Show History = Yes
257 //		//return all inactive records in the database
258 //		else if(StringUtils.equals(active, "N") && StringUtils.equals(showHistory, "Y")){
259 //			for(List<HrBusinessObject> lstHrBus : hrBusinessMap.values()){
260 //				for(HrBusinessObject hrBus : lstHrBus){
261 //					if(!hrBus.isActive()){
262 //						finalBusinessObjectList.add(hrBus);	
263 //					}
264 //				}
265 //			}
266 //		}
267 //		//Active = No and Show History = No
268 //		//return the most effective inactive rows if there are no active rows <= the curr date
269 //		else if(StringUtils.equals(active, "N") && StringUtils.equals(showHistory, "N")){
270 //			for(List<HrBusinessObject> lstHrBusiness : hrBusinessMap.values()){
271 //				for(HrBusinessObject hrBus : lstHrBusiness){
272 //					if(hrBus.getEffectiveDate().before(currDate)){
273 //						if(!hrBus.isActive()){
274 //							finalBusinessObjectList.add(hrBus);
275 //						} 
276 //						break;
277 //					} else {
278 //						if(!hrBus.isActive()){
279 //							finalBusinessObjectList.add(hrBus);
280 //						}
281 //					}
282 //				}
283 //			}
284 //		}
285 //		
286 //		Integer searchResultsLimit = LookupUtils.getSearchResultsLimit(businessObjectClass);
287 //
288 //		Long matchingResultsCount = Long.valueOf(finalBusinessObjectList.size());
289 //
290 //		if (matchingResultsCount.intValue() <= searchResultsLimit.intValue()) {
291 //
292 //		matchingResultsCount = new Long(0);
293 //
294 //		}
295 //
296 //		return new CollectionIncomplete(finalBusinessObjectList, matchingResultsCount);
297 
298 	}
299 
300 
301 
302 	
303 }