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.job.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.job.Job;
025    import org.kuali.hr.time.HrEffectiveDateActiveLookupableHelper;
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.document.authorization.BusinessObjectRestrictions;
031    import org.kuali.rice.kns.lookup.HtmlData;
032    import org.kuali.rice.kns.lookup.HtmlData.AnchorHtmlData;
033    import org.kuali.rice.kns.web.struts.form.LookupForm;
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 JobLookupableHelper extends HrEffectiveDateActiveLookupableHelper {
039    
040            private static final long serialVersionUID = 3233495722838070429L;
041    
042            @Override
043        public List<HtmlData> getCustomActionUrls(BusinessObject businessObject, List pkNames) {
044            List<HtmlData> customActionUrls = new ArrayList<HtmlData>();
045                    
046                    List<HtmlData> defaultCustomActionUrls = super.getCustomActionUrls(businessObject, pkNames);
047            
048                    Job job = (Job) businessObject;
049            String hrJobId = job.getHrJobId();
050            String jobNumber = String.valueOf(job.getJobNumber());
051            String principalId = job.getPrincipalId();
052            String location = job.getLocation();
053            String department = job.getDept();
054            
055            boolean systemAdmin = TKUser.isSystemAdmin();
056                    boolean locationAdmin = TKUser.getLocationAdminAreas().contains(location);
057                    boolean departmentAdmin = TKUser.getDepartmentAdminAreas().contains(department);
058                    
059                    for (HtmlData defaultCustomActionUrl : defaultCustomActionUrls){
060                            if (StringUtils.equals(defaultCustomActionUrl.getMethodToCall(), "edit")) {
061                                    if (systemAdmin || locationAdmin || departmentAdmin) {
062                                            customActionUrls.add(defaultCustomActionUrl);
063                                    }
064                            } else {
065                                    customActionUrls.add(defaultCustomActionUrl);
066                            }
067                    }
068                    
069                    Properties params = new Properties();
070                    params.put(KRADConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, getBusinessObjectClass().getName());
071                    params.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.MAINTENANCE_NEW_METHOD_TO_CALL);
072                    params.put("hrJobId", hrJobId);
073                    params.put("jobNumber", jobNumber);
074                    params.put("principalId", principalId);
075                    AnchorHtmlData viewUrl = new AnchorHtmlData(UrlFactory.parameterizeUrl(KRADConstants.INQUIRY_ACTION, params), "view");
076                    viewUrl.setDisplayText("view");
077                    viewUrl.setTarget(AnchorHtmlData.TARGET_BLANK);
078                    customActionUrls.add(viewUrl);
079                    
080                    return customActionUrls;
081        }
082    
083        @Override
084        public List<? extends BusinessObject> getSearchResults(Map<String, String> fieldValues) {
085            String principalId = fieldValues.get("principalId");
086            String firstName = fieldValues.get("firstName");
087            String lastName = fieldValues.get("lastName");
088            String jobNumber = fieldValues.get("jobNumber");
089            String dept = fieldValues.get("dept");
090            String positionNumber = fieldValues.get("positionNumber");
091            String hrPayType = fieldValues.get("hrPayType");
092            String fromEffdt = TKUtils.getFromDateString(fieldValues.get("effectiveDate"));
093            String toEffdt = TKUtils.getToDateString(fieldValues.get("effectiveDate"));
094            String active = fieldValues.get("active");
095            String showHist = fieldValues.get("history");
096    
097            return TkServiceLocator.getJobService().getJobs(principalId, firstName, lastName, jobNumber, dept, positionNumber, hrPayType, 
098                            TKUtils.formatDateString(fromEffdt), TKUtils.formatDateString(toEffdt), active, showHist);
099        }
100    
101        @SuppressWarnings("rawtypes")
102        @Override
103        public HtmlData getReturnUrl(BusinessObject businessObject,
104                                     LookupForm lookupForm, List returnKeys,
105                                     BusinessObjectRestrictions businessObjectRestrictions) {
106            if (lookupForm.getFieldConversions().containsKey("effectiveDate")) {
107                lookupForm.getFieldConversions().remove("effectiveDate");
108            }
109            if (returnKeys.contains("effectiveDate")) {
110                returnKeys.remove("effectiveDate");
111            }
112            return super.getReturnUrl(businessObject, lookupForm, returnKeys,
113                    businessObjectRestrictions);
114        }
115    }