001    /**
002     * Copyright 2004-2012 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.util.ArrayList;
019    import java.util.Comparator;
020    import java.util.List;
021    import java.util.Properties;
022    
023    import org.apache.commons.lang.StringUtils;
024    import org.kuali.hr.time.authorization.DepartmentalRule;
025    import org.kuali.hr.time.authorization.DepartmentalRuleAuthorizer;
026    import org.kuali.hr.time.authorization.TkAuthorizedLookupableHelperBase;
027    import org.kuali.hr.time.clock.location.ClockLocationRule;
028    import org.kuali.hr.time.department.Department;
029    import org.kuali.hr.time.service.base.TkServiceLocator;
030    import org.kuali.hr.time.util.TKContext;
031    import org.kuali.hr.time.util.TKUtils;
032    import org.kuali.rice.kns.lookup.HtmlData;
033    import org.kuali.rice.kns.lookup.HtmlData.AnchorHtmlData;
034    import org.kuali.rice.krad.bo.BusinessObject;
035    import org.kuali.rice.krad.util.KRADConstants;
036    import org.kuali.rice.krad.util.UrlFactory;
037    
038    public class ClockLocationRuleLookupableHelper extends TkAuthorizedLookupableHelperBase {
039    
040            private static final long serialVersionUID = 7261054962204557586L;
041    
042            @Override
043        /**
044         * Implemented method to reduce the set of Business Objects that are shown
045         * to the user based on their current roles.
046         */
047        public boolean shouldShowBusinessObject(BusinessObject bo) {
048            return (bo instanceof DepartmentalRule) && DepartmentalRuleAuthorizer.hasAccessToRead((DepartmentalRule)bo);
049        }
050    
051        @Override
052            public List<HtmlData> getCustomActionUrls(BusinessObject businessObject, List pkNames) {
053            List<HtmlData> customActionUrls = new ArrayList<HtmlData>();
054                    
055                    List<HtmlData> defaultCustomActionUrls = super.getCustomActionUrls(businessObject, pkNames);
056                    
057                    ClockLocationRule clockLocationRule = (ClockLocationRule) businessObject;
058                    String tkClockLocationRuleId = clockLocationRule.getTkClockLocationRuleId();
059            Department dept = TkServiceLocator.getDepartmentService().getDepartment(clockLocationRule.getDept(), TKUtils.getCurrentDate());
060                    String location = dept == null ? null : dept.getLocation();
061            String department = clockLocationRule.getDept();
062            
063                    boolean systemAdmin = TKContext.getUser().isSystemAdmin();
064                    boolean locationAdmin = TKContext.getUser().getLocationAdminAreas().contains(location);
065                    boolean departmentAdmin = TKContext.getUser().getDepartmentAdminAreas().contains(department);
066                    
067                    for (HtmlData defaultCustomActionUrl : defaultCustomActionUrls){
068                            if (StringUtils.equals(defaultCustomActionUrl.getMethodToCall(), "edit")) {
069                                    if (systemAdmin || locationAdmin || departmentAdmin) {
070                                            customActionUrls.add(defaultCustomActionUrl);
071                                    }
072                            } else {
073                                    customActionUrls.add(defaultCustomActionUrl);
074                            }
075                    }
076                    
077                    Properties params = new Properties();
078                    params.put(KRADConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, getBusinessObjectClass().getName());
079                    params.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.MAINTENANCE_NEW_METHOD_TO_CALL);
080                    params.put("tkClockLocationRuleId", tkClockLocationRuleId);
081                    AnchorHtmlData viewUrl = new AnchorHtmlData(UrlFactory.parameterizeUrl(KRADConstants.INQUIRY_ACTION, params), "view");
082                    viewUrl.setDisplayText("view");
083                    viewUrl.setTarget(AnchorHtmlData.TARGET_BLANK);
084                    customActionUrls.add(viewUrl);
085                    
086                    return customActionUrls;
087            }
088    
089            @Override
090            protected void validateSearchParameterWildcardAndOperators(
091                            String attributeName, String attributeValue) {
092                    if (!StringUtils.equals(attributeValue, "%")) {
093                            super.validateSearchParameterWildcardAndOperators(attributeName,
094                                            attributeValue);
095                    }
096            }
097            
098            @SuppressWarnings("rawtypes")
099            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    }