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.dept.lunch.service;
017    
018    import java.util.ArrayList;
019    import java.util.List;
020    import java.util.Map;
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.department.Department;
028    import org.kuali.hr.time.dept.lunch.DeptLunchRule;
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 DepartmentLunchRuleLookupableHelper extends TkAuthorizedLookupableHelperBase {
039            
040            private static final long serialVersionUID = -6171434403261481651L;
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                    DeptLunchRule deptLunchRule = (DeptLunchRule) businessObject;
058                    String tkDeptLunchRuleId = deptLunchRule.getTkDeptLunchRuleId();
059                    String department = deptLunchRule.getDept();
060                    String workArea = String.valueOf(deptLunchRule.getWorkArea());
061            Department dept = TkServiceLocator.getDepartmentService().getDepartment(deptLunchRule.getDept(), TKUtils.getCurrentDate());
062            String location = dept == null ? null : dept.getLocation();
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("tkDeptLunchRuleId", tkDeptLunchRuleId);
082                    params.put("dept", department);
083                    params.put("workArea", workArea);
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        @Override
102        public List<? extends BusinessObject> getSearchResults(Map<String, String> fieldValues) {
103            String principalId = fieldValues.get("principalId");
104            String jobNumber = fieldValues.get("jobNumber");
105            String dept = fieldValues.get("dept");
106            String workArea = fieldValues.get("workArea");
107            String active = fieldValues.get("active");
108    
109            if (StringUtils.equals(workArea,"%") || StringUtils.equals(workArea,"*")){
110                workArea = "";
111            }
112            List<DeptLunchRule> departmentLunchRules = TkServiceLocator.getDepartmentLunchRuleService().getDepartmentLunchRules(dept,
113                    workArea, principalId, jobNumber, active);
114            return departmentLunchRules;
115        }
116    }