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