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.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.department.Department;
28  import org.kuali.hr.time.service.base.TkServiceLocator;
29  import org.kuali.hr.time.util.TKUser;
30  import org.kuali.hr.time.util.TKUtils;
31  import org.kuali.hr.time.workarea.WorkArea;
32  import org.kuali.rice.kns.document.authorization.BusinessObjectRestrictions;
33  import org.kuali.rice.kns.lookup.HtmlData;
34  import org.kuali.rice.kns.lookup.HtmlData.AnchorHtmlData;
35  import org.kuali.rice.kns.web.struts.form.LookupForm;
36  import org.kuali.rice.krad.bo.BusinessObject;
37  import org.kuali.rice.krad.util.KRADConstants;
38  import org.kuali.rice.krad.util.UrlFactory;
39  
40  public class WorkAreaLookupableHelper extends TkAuthorizedLookupableHelperBase {
41  
42      /**
43       * Implemented method to reduce the set of Business Objects that are shown
44       * to the user based on their current roles.
45       */
46      public boolean shouldShowBusinessObject(BusinessObject bo) {
47          return (bo instanceof DepartmentalRule) && DepartmentalRuleAuthorizer.hasAccessToRead((DepartmentalRule)bo);
48      }
49  
50  	@Override
51  	public List<HtmlData> getCustomActionUrls(BusinessObject businessObject, List pkNames) {
52  		List<HtmlData> customActionUrls = new ArrayList<HtmlData>();
53  		
54  		List<HtmlData> defaultCustomActionUrls = super.getCustomActionUrls(businessObject, pkNames);
55  		
56  		WorkArea workArea = (WorkArea) businessObject;
57  		String tkWorkAreaId = workArea.getTkWorkAreaId();
58  		
59  		String department = workArea.getDept();
60  		Department departmentObj = TkServiceLocator.getDepartmentService().getDepartment(workArea.getDept(), TKUtils.getCurrentDate());
61  		String location = departmentObj != null ? departmentObj.getLocation() : null;
62  		
63  		boolean systemAdmin = TKUser.isSystemAdmin();
64  		boolean locationAdmin = TKUser.getLocationAdminAreas().contains(location);
65  		boolean departmentAdmin = TKUser.getDepartmentAdminAreas().contains(department);
66  		
67  		for (HtmlData defaultCustomActionUrl : defaultCustomActionUrls){
68  			if (StringUtils.equals(defaultCustomActionUrl.getMethodToCall(), "edit")) {
69  				if (systemAdmin || locationAdmin || departmentAdmin) {
70  					customActionUrls.add(defaultCustomActionUrl);
71  				}
72  			} else {
73  				customActionUrls.add(defaultCustomActionUrl);
74  			}
75  		}
76  		
77  		Properties params = new Properties();
78  		params.put(KRADConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, getBusinessObjectClass().getName());
79  		params.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.MAINTENANCE_NEW_METHOD_TO_CALL);
80  		params.put("tkWorkAreaId", tkWorkAreaId);
81  		AnchorHtmlData viewUrl = new AnchorHtmlData(UrlFactory.parameterizeUrl(KRADConstants.INQUIRY_ACTION, params), "view");
82  		viewUrl.setDisplayText("view");
83  		viewUrl.setTarget(AnchorHtmlData.TARGET_BLANK);
84  		customActionUrls.add(viewUrl);
85  		
86  		return customActionUrls;
87  	}
88  
89  	@Override
90  	public HtmlData getReturnUrl(BusinessObject businessObject,
91  			LookupForm lookupForm, List returnKeys,
92  			BusinessObjectRestrictions businessObjectRestrictions) {
93  		if (lookupForm.getFieldConversions().containsKey("effectiveDate")) {
94  			lookupForm.getFieldConversions().remove("effectiveDate");
95  		}
96  		if (returnKeys.contains("effectiveDate")) {
97  			returnKeys.remove("effectiveDate");
98  		}
99  		if (lookupForm.getFieldConversions().containsKey("dept")) {
100 			lookupForm.getFieldConversions().remove("dept");
101 		}
102 		if (returnKeys.contains("dept")) {
103 			returnKeys.remove("dept");
104 		}
105 		
106 		if(lookupForm.getFieldConversions().containsKey("tkWorkAreaId")){
107 			lookupForm.getFieldConversions().remove("tkWorkAreaId");
108 		}
109 		if(returnKeys.contains("tkWorkAreaId")){
110 			returnKeys.remove("tkWorkAreaId");
111 		}
112 		return super.getReturnUrl(businessObject, lookupForm, returnKeys,
113 				businessObjectRestrictions);
114 	}
115 	
116 
117 
118 	@Override
119 	protected void validateSearchParameterWildcardAndOperators(
120 			String attributeName, String attributeValue) {
121 		if (!StringUtils.equals(attributeValue, "%")) {
122 			super.validateSearchParameterWildcardAndOperators(attributeName,
123 					attributeValue);
124 		}
125 	}
126 	
127 	@Override
128 	public List<? extends BusinessObject> getSearchResults(Map<String, String> fieldValues) {
129 		String dept = fieldValues.get("dept");
130 		String workArea = fieldValues.get("workArea");
131 		String description = fieldValues.get("description");
132 		String fromEffdt = TKUtils.getFromDateString(fieldValues.get("effectiveDate"));
133 		String toEffdt = TKUtils.getToDateString(fieldValues.get("effectiveDate"));
134 		String active = fieldValues.get("active");
135 		String showHist = fieldValues.get("history");
136 		
137 		if (StringUtils.contains(workArea, "%")) {
138 			workArea = "";
139 		}
140 		
141 		return TkServiceLocator.getWorkAreaService().getWorkAreas(dept, workArea, description, TKUtils.formatDateString(fromEffdt), 
142 				TKUtils.formatDateString(toEffdt), active, showHist);
143 	}
144 
145 }