001    /**
002     * Copyright 2004-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.hr.time.clock.location.service;
017    
018    import java.io.Serializable;
019    import java.util.ArrayList;
020    import java.util.Comparator;
021    import java.util.List;
022    import java.util.Map;
023    import java.util.Properties;
024    
025    import org.apache.commons.lang.StringUtils;
026    import org.kuali.hr.time.authorization.DepartmentalRule;
027    import org.kuali.hr.time.authorization.DepartmentalRuleAuthorizer;
028    import org.kuali.hr.time.authorization.TkAuthorizedLookupableHelperBase;
029    import org.kuali.hr.time.clock.location.ClockLocationRule;
030    import org.kuali.hr.time.department.Department;
031    import org.kuali.hr.time.service.base.TkServiceLocator;
032    import org.kuali.hr.time.util.TKContext;
033    import org.kuali.hr.time.util.TKUser;
034    import org.kuali.hr.time.util.TKUtils;
035    import org.kuali.rice.kns.lookup.HtmlData;
036    import org.kuali.rice.kns.lookup.HtmlData.AnchorHtmlData;
037    import org.kuali.rice.krad.bo.BusinessObject;
038    import org.kuali.rice.krad.util.KRADConstants;
039    import org.kuali.rice.krad.util.UrlFactory;
040    
041    public class ClockLocationRuleLookupableHelper extends TkAuthorizedLookupableHelperBase {
042    
043            private static final long serialVersionUID = 7261054962204557586L;
044    
045            @Override
046        /**
047         * Implemented method to reduce the set of Business Objects that are shown
048         * to the user based on their current roles.
049         */
050        public boolean shouldShowBusinessObject(BusinessObject bo) {
051            return (bo instanceof DepartmentalRule) && DepartmentalRuleAuthorizer.hasAccessToRead((DepartmentalRule)bo);
052        }
053    
054        @Override
055            public List<HtmlData> getCustomActionUrls(BusinessObject businessObject, List pkNames) {
056            List<HtmlData> customActionUrls = new ArrayList<HtmlData>();
057                    
058                    List<HtmlData> defaultCustomActionUrls = super.getCustomActionUrls(businessObject, pkNames);
059                    
060                    ClockLocationRule clockLocationRule = (ClockLocationRule) businessObject;
061                    String tkClockLocationRuleId = clockLocationRule.getTkClockLocationRuleId();
062            Department dept = TkServiceLocator.getDepartmentService().getDepartment(clockLocationRule.getDept(), TKUtils.getCurrentDate());
063                    String location = dept == null ? null : dept.getLocation();
064            String department = clockLocationRule.getDept();
065            
066                    boolean systemAdmin = TKUser.isSystemAdmin();
067                    boolean locationAdmin = TKUser.getLocationAdminAreas().contains(location);
068                    boolean departmentAdmin = TKUser.getDepartmentAdminAreas().contains(department);
069                    
070                    for (HtmlData defaultCustomActionUrl : defaultCustomActionUrls){
071                            if (StringUtils.equals(defaultCustomActionUrl.getMethodToCall(), "edit")) {
072                                    if (systemAdmin || locationAdmin || departmentAdmin) {
073                                            customActionUrls.add(defaultCustomActionUrl);
074                                    }
075                            } else {
076                                    customActionUrls.add(defaultCustomActionUrl);
077                            }
078                    }
079                    
080                    Properties params = new Properties();
081                    params.put(KRADConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, getBusinessObjectClass().getName());
082                    params.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.MAINTENANCE_NEW_METHOD_TO_CALL);
083                    params.put("tkClockLocationRuleId", tkClockLocationRuleId);
084                    AnchorHtmlData viewUrl = new AnchorHtmlData(UrlFactory.parameterizeUrl(KRADConstants.INQUIRY_ACTION, params), "view");
085                    viewUrl.setDisplayText("view");
086                    viewUrl.setTarget(AnchorHtmlData.TARGET_BLANK);
087                    customActionUrls.add(viewUrl);
088                    
089                    return customActionUrls;
090            }
091    
092            @Override
093            protected void validateSearchParameterWildcardAndOperators(
094                            String attributeName, String attributeValue) {
095                    if (!StringUtils.equals(attributeValue, "%")) {
096                            super.validateSearchParameterWildcardAndOperators(attributeName,
097                                            attributeValue);
098                    }
099            }
100            
101            @SuppressWarnings("rawtypes")
102            public static class EffectiveDateTimestampCompare implements Comparator, Serializable {
103    
104                    @Override
105                    public int compare(Object arg0, Object arg1) {
106                            ClockLocationRule clockLocationRule = (ClockLocationRule)arg0;
107                            ClockLocationRule clockLocationRule2 = (ClockLocationRule)arg1;
108                            int result = clockLocationRule.getEffectiveDate().compareTo(clockLocationRule2.getEffectiveDate());
109                            if(result==0){
110                                    return clockLocationRule.getTimestamp().compareTo(clockLocationRule2.getTimestamp());
111                            }
112                            return result;
113                    }
114                    
115            }
116    
117        public List<? extends BusinessObject> getSearchResults(Map<String, String> fieldValues) {
118            String fromEffdt = TKUtils.getFromDateString(fieldValues.get("effectiveDate"));
119            String toEffdt = TKUtils.getToDateString(fieldValues.get("effectiveDate"));
120            String principalId = fieldValues.get("principalId");
121            String jobNumber = fieldValues.get("jobNumber");
122            String dept = fieldValues.get("dept");
123            String workArea = fieldValues.get("workArea");
124            String active = fieldValues.get("active");
125            String showHist = fieldValues.get("history");
126    
127            return TkServiceLocator.getClockLocationRuleService().getClockLocationRules(TKUtils.formatDateString(fromEffdt), TKUtils.formatDateString(toEffdt), 
128                            principalId, jobNumber, dept, workArea, active, showHist);
129        }
130        
131    }