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.department.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.HrEffectiveDateActiveLookupableHelper;
025    import org.kuali.hr.time.department.Department;
026    import org.kuali.hr.time.service.base.TkServiceLocator;
027    import org.kuali.hr.time.util.TKContext;
028    import org.kuali.hr.time.util.TKUser;
029    import org.kuali.hr.time.util.TKUtils;
030    import org.kuali.rice.kns.lookup.HtmlData;
031    import org.kuali.rice.kns.lookup.HtmlData.AnchorHtmlData;
032    import org.kuali.rice.krad.bo.BusinessObject;
033    import org.kuali.rice.krad.util.KRADConstants;
034    import org.kuali.rice.krad.util.UrlFactory;
035    
036    public class DepartmentLookupableHelper extends HrEffectiveDateActiveLookupableHelper {
037            
038            private static final long serialVersionUID = 566277764259830850L;
039    
040            @Override
041            public List<HtmlData> getCustomActionUrls(BusinessObject businessObject, List pkNames) {
042                    List<HtmlData> customActionUrls = new ArrayList<HtmlData>();
043                    
044                    List<HtmlData> defaultCustomActionUrls = super.getCustomActionUrls(businessObject, pkNames);
045                    
046                    Department departmentObj = (Department) businessObject;
047                    String hrDeptId = departmentObj.getHrDeptId();
048            Department dept = TkServiceLocator.getDepartmentService().getDepartment(departmentObj.getDept(), TKUtils.getCurrentDate());
049                    String location = dept == null ? null : dept.getLocation();
050                    String department = departmentObj.getDept();
051                    
052                    boolean systemAdmin = TKUser.isSystemAdmin();
053                    boolean locationAdmin = TKUser.getLocationAdminAreas().contains(location);
054                    boolean departmentAdmin = TKUser.getDepartmentAdminAreas().contains(department);
055                    
056                    for (HtmlData defaultCustomActionUrl : defaultCustomActionUrls){
057                            if (StringUtils.equals(defaultCustomActionUrl.getMethodToCall(), "edit")) {
058                                    if (systemAdmin || locationAdmin || departmentAdmin) {
059                                            customActionUrls.add(defaultCustomActionUrl);
060                                    }
061                            } else {
062                                    customActionUrls.add(defaultCustomActionUrl);
063                            }
064                    }
065    
066                    Properties params = new Properties();
067                    params.put(KRADConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, getBusinessObjectClass().getName());
068                    params.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.MAINTENANCE_NEW_METHOD_TO_CALL);
069                    params.put("hrDeptId", hrDeptId);
070            params.put("dept", department);
071                    AnchorHtmlData viewUrl = new AnchorHtmlData(UrlFactory.parameterizeUrl(KRADConstants.INQUIRY_ACTION, params), "view");
072                    viewUrl.setDisplayText("view");
073                    viewUrl.setTarget(AnchorHtmlData.TARGET_BLANK);
074                    customActionUrls.add(viewUrl);
075                    
076                    return customActionUrls;
077            }
078    
079        @SuppressWarnings({"unchecked"})
080        @Override
081        public List<? extends BusinessObject> getSearchResults(Map<String, String> fieldValues) {
082            String dept = fieldValues.get("dept");
083            String location = fieldValues.get("location");
084            String descr = fieldValues.get("description");
085            String active = fieldValues.get("active");
086            String showHist = fieldValues.get("history");
087    
088            List<Department> departments =  TkServiceLocator.getDepartmentService().getDepartments(dept, location, descr, active, showHist);
089    
090            return departments;
091        }
092    }