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.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 	@Override
125 	public List<? extends BusinessObject> getSearchResults(Map<String, String> fieldValues) {
126 		String dept = fieldValues.get("dept");
127 		String workArea = fieldValues.get("workArea");
128 		String description = fieldValues.get("description");
129 		String fromEffdt = TKUtils.getFromDateString(fieldValues.get("effectiveDate"));
130 		String toEffdt = TKUtils.getToDateString(fieldValues.get("effectiveDate"));
131 		String active = fieldValues.get("active");
132 		String showHist = fieldValues.get("history");
133 		
134 		if (StringUtils.equals(workArea, "%")) {
135 			workArea = "";
136 		}
137 		
138 		return TkServiceLocator.getWorkAreaService().getWorkAreas(dept, workArea, description, TKUtils.formatDateString(fromEffdt), 
139 				TKUtils.formatDateString(toEffdt), active, showHist);
140 	}
141 
142 }