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.clock.location.service;
17  
18  import java.util.ArrayList;
19  import java.util.Comparator;
20  import java.util.List;
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.clock.location.ClockLocationRule;
28  import org.kuali.hr.time.department.Department;
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 ClockLocationRuleLookupableHelper extends TkAuthorizedLookupableHelperBase {
39  
40  	private static final long serialVersionUID = 7261054962204557586L;
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  		ClockLocationRule clockLocationRule = (ClockLocationRule) businessObject;
58  		String tkClockLocationRuleId = clockLocationRule.getTkClockLocationRuleId();
59          Department dept = TkServiceLocator.getDepartmentService().getDepartment(clockLocationRule.getDept(), TKUtils.getCurrentDate());
60  		String location = dept == null ? null : dept.getLocation();
61          String department = clockLocationRule.getDept();
62          
63  		boolean systemAdmin = TKContext.getUser().isSystemAdmin();
64  		boolean locationAdmin = TKContext.getUser().getLocationAdminAreas().contains(location);
65  		boolean departmentAdmin = TKContext.getUser().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("tkClockLocationRuleId", tkClockLocationRuleId);
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  	protected void validateSearchParameterWildcardAndOperators(
91  			String attributeName, String attributeValue) {
92  		if (!StringUtils.equals(attributeValue, "%")) {
93  			super.validateSearchParameterWildcardAndOperators(attributeName,
94  					attributeValue);
95  		}
96  	}
97  	
98  	@SuppressWarnings("rawtypes")
99  	public class EffectiveDateTimestampCompare implements Comparator{
100 
101 		@Override
102 		public int compare(Object arg0, Object arg1) {
103 			ClockLocationRule clockLocationRule = (ClockLocationRule)arg0;
104 			ClockLocationRule clockLocationRule2 = (ClockLocationRule)arg1;
105 			int result = clockLocationRule.getEffectiveDate().compareTo(clockLocationRule2.getEffectiveDate());
106 			if(result==0){
107 				return clockLocationRule.getTimestamp().compareTo(clockLocationRule2.getTimestamp());
108 			}
109 			return result;
110 		}
111 		
112 	}
113 }