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.TKUser;
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 DepartmentLunchRuleLookupableHelper extends TkAuthorizedLookupableHelperBase {
040            
041            private static final long serialVersionUID = -6171434403261481651L;
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                    DeptLunchRule deptLunchRule = (DeptLunchRule) businessObject;
059                    String tkDeptLunchRuleId = deptLunchRule.getTkDeptLunchRuleId();
060                    String department = deptLunchRule.getDept();
061                    String workArea = String.valueOf(deptLunchRule.getWorkArea());
062            Department dept = TkServiceLocator.getDepartmentService().getDepartment(deptLunchRule.getDept(), TKUtils.getCurrentDate());
063            String location = dept == null ? null : dept.getLocation();
064                    
065                    boolean systemAdmin = TKUser.isSystemAdmin();
066                    boolean locationAdmin = TKUser.getLocationAdminAreas().contains(location);
067                    boolean departmentAdmin = TKUser.getDepartmentAdminAreas().contains(department);
068                    
069                    for (HtmlData defaultCustomActionUrl : defaultCustomActionUrls){
070                            if (StringUtils.equals(defaultCustomActionUrl.getMethodToCall(), "edit")) {
071                                    if (systemAdmin || locationAdmin || departmentAdmin) {
072                                            customActionUrls.add(defaultCustomActionUrl);
073                                    }
074                            } else {
075                                    customActionUrls.add(defaultCustomActionUrl);
076                            }
077                    }
078                    
079                    Properties params = new Properties();
080                    params.put(KRADConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, getBusinessObjectClass().getName());
081                    params.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.MAINTENANCE_NEW_METHOD_TO_CALL);
082                    params.put("tkDeptLunchRuleId", tkDeptLunchRuleId);
083                    params.put("dept", department);
084                    params.put("workArea", workArea);
085                    AnchorHtmlData viewUrl = new AnchorHtmlData(UrlFactory.parameterizeUrl(KRADConstants.INQUIRY_ACTION, params), "view");
086                    viewUrl.setDisplayText("view");
087                    viewUrl.setTarget(AnchorHtmlData.TARGET_BLANK);
088                    customActionUrls.add(viewUrl);
089                    
090                    return customActionUrls;
091            }
092    
093            @Override
094            protected void validateSearchParameterWildcardAndOperators(
095                            String attributeName, String attributeValue) {
096                    if (!StringUtils.equals(attributeValue, "%")) {
097                            super.validateSearchParameterWildcardAndOperators(attributeName,
098                                            attributeValue);
099                    }
100            }
101    
102        @Override
103        public List<? extends BusinessObject> getSearchResults(Map<String, String> fieldValues) {
104            String principalId = fieldValues.get("principalId");
105            String jobNumber = fieldValues.get("jobNumber");
106            String dept = fieldValues.get("dept");
107            String workArea = fieldValues.get("workArea");
108            String active = fieldValues.get("active");
109            String history = fieldValues.get("history");
110            String fromEffdt = TKUtils.getFromDateString(fieldValues.get("effectiveDate"));
111            String toEffdt = TKUtils.getToDateString(fieldValues.get("effectiveDate"));
112    
113            if (StringUtils.equals(workArea,"%") || StringUtils.equals(workArea,"*")){
114                workArea = "";
115            }
116            return TkServiceLocator.getDepartmentLunchRuleService().getDepartmentLunchRules(dept,
117                    workArea, principalId, jobNumber, TKUtils.formatDateString(fromEffdt), TKUtils.formatDateString(toEffdt), active, history);
118        }
119    }