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.dept.lunch.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.dept.lunch.DeptLunchRule;
29  import org.kuali.hr.time.service.base.TkServiceLocator;
30  import org.kuali.hr.time.util.TKContext;
31  import org.kuali.hr.time.util.TKUtils;
32  import org.kuali.rice.kns.lookup.HtmlData;
33  import org.kuali.rice.kns.lookup.HtmlData.AnchorHtmlData;
34  import org.kuali.rice.krad.bo.BusinessObject;
35  import org.kuali.rice.krad.util.KRADConstants;
36  import org.kuali.rice.krad.util.UrlFactory;
37  
38  public class DepartmentLunchRuleLookupableHelper extends TkAuthorizedLookupableHelperBase {
39  	
40  	private static final long serialVersionUID = -6171434403261481651L;
41  
42  	@Override
43      /**
44       * Implemented method to reduce the set of Business Objects that are shown
45       * to the user based on their current roles.
46       */
47      public boolean shouldShowBusinessObject(BusinessObject bo) {
48          return (bo instanceof DepartmentalRule) && DepartmentalRuleAuthorizer.hasAccessToRead((DepartmentalRule)bo);
49      }
50  
51  	@Override
52  	public List<HtmlData> getCustomActionUrls(BusinessObject businessObject, List pkNames) {
53  		List<HtmlData> customActionUrls = new ArrayList<HtmlData>();
54  		
55  		List<HtmlData> defaultCustomActionUrls = super.getCustomActionUrls(businessObject, pkNames);
56  		
57  		DeptLunchRule deptLunchRule = (DeptLunchRule) businessObject;
58  		String tkDeptLunchRuleId = deptLunchRule.getTkDeptLunchRuleId();
59  		String department = deptLunchRule.getDept();
60  		String workArea = String.valueOf(deptLunchRule.getWorkArea());
61          Department dept = TkServiceLocator.getDepartmentService().getDepartment(deptLunchRule.getDept(), TKUtils.getCurrentDate());
62          String location = dept == null ? null : dept.getLocation();
63  		
64  		boolean systemAdmin = TKContext.getUser().isSystemAdmin();
65  		boolean locationAdmin = TKContext.getUser().getLocationAdminAreas().contains(location);
66  		boolean departmentAdmin = TKContext.getUser().getDepartmentAdminAreas().contains(department);
67  		
68  		for (HtmlData defaultCustomActionUrl : defaultCustomActionUrls){
69  			if (StringUtils.equals(defaultCustomActionUrl.getMethodToCall(), "edit")) {
70  				if (systemAdmin || locationAdmin || departmentAdmin) {
71  					customActionUrls.add(defaultCustomActionUrl);
72  				}
73  			} else {
74  				customActionUrls.add(defaultCustomActionUrl);
75  			}
76  		}
77  		
78  		Properties params = new Properties();
79  		params.put(KRADConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, getBusinessObjectClass().getName());
80  		params.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.MAINTENANCE_NEW_METHOD_TO_CALL);
81  		params.put("tkDeptLunchRuleId", tkDeptLunchRuleId);
82  		params.put("dept", department);
83  		params.put("workArea", workArea);
84  		AnchorHtmlData viewUrl = new AnchorHtmlData(UrlFactory.parameterizeUrl(KRADConstants.INQUIRY_ACTION, params), "view");
85  		viewUrl.setDisplayText("view");
86  		viewUrl.setTarget(AnchorHtmlData.TARGET_BLANK);
87  		customActionUrls.add(viewUrl);
88  		
89  		return customActionUrls;
90  	}
91  
92  	@Override
93  	protected void validateSearchParameterWildcardAndOperators(
94  			String attributeName, String attributeValue) {
95  		if (!StringUtils.equals(attributeValue, "%")) {
96  			super.validateSearchParameterWildcardAndOperators(attributeName,
97  					attributeValue);
98  		}
99  	}
100 
101     @Override
102     public List<? extends BusinessObject> getSearchResults(Map<String, String> fieldValues) {
103         String principalId = fieldValues.get("principalId");
104         String jobNumber = fieldValues.get("jobNumber");
105         String dept = fieldValues.get("dept");
106         String workArea = fieldValues.get("workArea");
107         String active = fieldValues.get("active");
108 
109         if (StringUtils.equals(workArea,"%") || StringUtils.equals(workArea,"*")){
110             workArea = "";
111         }
112         List<DeptLunchRule> departmentLunchRules = TkServiceLocator.getDepartmentLunchRuleService().getDepartmentLunchRules(dept,
113                 workArea, principalId, jobNumber, active);
114         return departmentLunchRules;
115     }
116 }