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.clock.location.service;
17  
18  import java.util.ArrayList;
19  import java.util.Comparator;
20  import java.util.List;
21  import java.util.Map;
22  import java.util.Properties;
23  
24  import org.apache.commons.lang.StringUtils;
25  import org.kuali.hr.time.authorization.DepartmentalRule;
26  import org.kuali.hr.time.authorization.DepartmentalRuleAuthorizer;
27  import org.kuali.hr.time.authorization.TkAuthorizedLookupableHelperBase;
28  import org.kuali.hr.time.clock.location.ClockLocationRule;
29  import org.kuali.hr.time.department.Department;
30  import org.kuali.hr.time.service.base.TkServiceLocator;
31  import org.kuali.hr.time.util.TKContext;
32  import org.kuali.hr.time.util.TKUtils;
33  import org.kuali.rice.kns.lookup.HtmlData;
34  import org.kuali.rice.kns.lookup.HtmlData.AnchorHtmlData;
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 ClockLocationRuleLookupableHelper extends TkAuthorizedLookupableHelperBase {
40  
41  	private static final long serialVersionUID = 7261054962204557586L;
42  
43  	@Override
44      /**
45       * Implemented method to reduce the set of Business Objects that are shown
46       * to the user based on their current roles.
47       */
48      public boolean shouldShowBusinessObject(BusinessObject bo) {
49          return (bo instanceof DepartmentalRule) && DepartmentalRuleAuthorizer.hasAccessToRead((DepartmentalRule)bo);
50      }
51  
52      @Override
53  	public List<HtmlData> getCustomActionUrls(BusinessObject businessObject, List pkNames) {
54      	List<HtmlData> customActionUrls = new ArrayList<HtmlData>();
55  		
56  		List<HtmlData> defaultCustomActionUrls = super.getCustomActionUrls(businessObject, pkNames);
57  		
58  		ClockLocationRule clockLocationRule = (ClockLocationRule) businessObject;
59  		String tkClockLocationRuleId = clockLocationRule.getTkClockLocationRuleId();
60          Department dept = TkServiceLocator.getDepartmentService().getDepartment(clockLocationRule.getDept(), TKUtils.getCurrentDate());
61  		String location = dept == null ? null : dept.getLocation();
62          String department = clockLocationRule.getDept();
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("tkClockLocationRuleId", tkClockLocationRuleId);
82  		AnchorHtmlData viewUrl = new AnchorHtmlData(UrlFactory.parameterizeUrl(KRADConstants.INQUIRY_ACTION, params), "view");
83  		viewUrl.setDisplayText("view");
84  		viewUrl.setTarget(AnchorHtmlData.TARGET_BLANK);
85  		customActionUrls.add(viewUrl);
86  		
87  		return customActionUrls;
88  	}
89  
90  	@Override
91  	protected void validateSearchParameterWildcardAndOperators(
92  			String attributeName, String attributeValue) {
93  		if (!StringUtils.equals(attributeValue, "%")) {
94  			super.validateSearchParameterWildcardAndOperators(attributeName,
95  					attributeValue);
96  		}
97  	}
98  	
99  	@SuppressWarnings("rawtypes")
100 	public class EffectiveDateTimestampCompare implements Comparator{
101 
102 		@Override
103 		public int compare(Object arg0, Object arg1) {
104 			ClockLocationRule clockLocationRule = (ClockLocationRule)arg0;
105 			ClockLocationRule clockLocationRule2 = (ClockLocationRule)arg1;
106 			int result = clockLocationRule.getEffectiveDate().compareTo(clockLocationRule2.getEffectiveDate());
107 			if(result==0){
108 				return clockLocationRule.getTimestamp().compareTo(clockLocationRule2.getTimestamp());
109 			}
110 			return result;
111 		}
112 		
113 	}
114 
115     public List<? extends BusinessObject> getSearchResults(Map<String, String> fieldValues) {
116         String fromEffdt = TKUtils.getFromDateString(fieldValues.get("effectiveDate"));
117         String toEffdt = TKUtils.getToDateString(fieldValues.get("effectiveDate"));
118         String principalId = fieldValues.get("principalId");
119         String jobNumber = fieldValues.get("jobNumber");
120         String dept = fieldValues.get("dept");
121         String workArea = fieldValues.get("workArea");
122         String active = fieldValues.get("active");
123         String showHist = fieldValues.get("history");
124 
125         return TkServiceLocator.getClockLocationRuleService().getClockLocationRules(TKUtils.formatDateString(fromEffdt), TKUtils.formatDateString(toEffdt), 
126         		principalId, jobNumber, dept, workArea, active, showHist);
127     }
128     
129 }