001    /**
002     * Copyright 2004-2012 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.TKUtils;
029    import org.kuali.rice.kns.document.authorization.BusinessObjectRestrictions;
030    import org.kuali.rice.kns.lookup.HtmlData;
031    import org.kuali.rice.kns.lookup.HtmlData.AnchorHtmlData;
032    import org.kuali.rice.kns.web.struts.form.LookupForm;
033    import org.kuali.rice.krad.bo.BusinessObject;
034    import org.kuali.rice.krad.util.KRADConstants;
035    import org.kuali.rice.krad.util.UrlFactory;
036    
037    public class JobLookupableHelper extends HrEffectiveDateActiveLookupableHelper {
038    
039            private static final long serialVersionUID = 3233495722838070429L;
040    
041            @Override
042        public List<HtmlData> getCustomActionUrls(BusinessObject businessObject, List pkNames) {
043            List<HtmlData> customActionUrls = new ArrayList<HtmlData>();
044                    
045                    List<HtmlData> defaultCustomActionUrls = super.getCustomActionUrls(businessObject, pkNames);
046            
047                    Job job = (Job) businessObject;
048            String hrJobId = job.getHrJobId();
049            String jobNumber = String.valueOf(job.getJobNumber());
050            String principalId = job.getPrincipalId();
051            String location = job.getLocation();
052            String department = job.getDept();
053            
054            boolean systemAdmin = TKContext.getUser().isSystemAdmin();
055                    boolean locationAdmin = TKContext.getUser().getLocationAdminAreas().contains(location);
056                    boolean departmentAdmin = TKContext.getUser().getDepartmentAdminAreas().contains(department);
057                    
058                    for (HtmlData defaultCustomActionUrl : defaultCustomActionUrls){
059                            if (StringUtils.equals(defaultCustomActionUrl.getMethodToCall(), "edit")) {
060                                    if (systemAdmin || locationAdmin || departmentAdmin) {
061                                            customActionUrls.add(defaultCustomActionUrl);
062                                    }
063                            } else {
064                                    customActionUrls.add(defaultCustomActionUrl);
065                            }
066                    }
067                    
068                    Properties params = new Properties();
069                    params.put(KRADConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, getBusinessObjectClass().getName());
070                    params.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.MAINTENANCE_NEW_METHOD_TO_CALL);
071                    params.put("hrJobId", hrJobId);
072                    params.put("jobNumber", jobNumber);
073                    params.put("principalId", principalId);
074                    AnchorHtmlData viewUrl = new AnchorHtmlData(UrlFactory.parameterizeUrl(KRADConstants.INQUIRY_ACTION, params), "view");
075                    viewUrl.setDisplayText("view");
076                    viewUrl.setTarget(AnchorHtmlData.TARGET_BLANK);
077                    customActionUrls.add(viewUrl);
078                    
079                    return customActionUrls;
080        }
081    
082        @SuppressWarnings({"unchecked"})
083        @Override
084        public List<? extends BusinessObject> getSearchResults(
085                Map<String, String> fieldValues) {
086    
087            String principalId = fieldValues.get("principalId");
088            String firstName = fieldValues.get("firstName");
089            String lastName = fieldValues.get("lastName");
090            String jobNumber = fieldValues.get("jobNumber");
091            String dept = fieldValues.get("dept");
092            String positionNumber = fieldValues.get("positionNumber");
093            String hrPayType = fieldValues.get("hrPayType");
094            String fromEffdt = fieldValues.get("rangeLowerBoundKeyPrefix_effectiveDate");
095            String toEffdt = StringUtils.isNotBlank(fieldValues.get("effectiveDate")) ? fieldValues.get("effectiveDate").replace("<=", "") : "";
096            String active = fieldValues.get("active");
097            String showHist = fieldValues.get("history");
098    
099            List<Job> jobs = TkServiceLocator.getJobService().getJobs(principalId, firstName, lastName, jobNumber,
100                    dept, positionNumber, hrPayType,
101                    TKUtils.formatDateString(fromEffdt), TKUtils.formatDateString(toEffdt),
102                    active,  showHist);
103    
104            return jobs;
105    
106    
107    //              String firstName = null;
108    //              String lastName = null;
109    //              if (fieldValues.containsKey("firstName")) {
110    //                      firstName = fieldValues.get("firstName");
111    //                      fieldValues.remove("firstName");
112    //              }
113    //              if (fieldValues.containsKey("lastName")) {
114    //                      lastName = fieldValues.get("lastName");
115    //                      fieldValues.remove("lastName");
116    //              }
117    
118    
119    //              if(StringUtils.isNotEmpty(firstName) || StringUtils.isNotEmpty(lastName)){
120    //                      Map<String, String> fields = new HashMap<String, String>();
121    //                      fields.put("firstName", firstName);
122    //                      fields.put("lastName", lastName);
123    //                      List<Person> listPerson = KIMServiceLocator.getPersonService()
124    //                                      .findPeople(fields);
125    //                      List<Job> objectList = new ArrayList<Job>();
126    //                      for(Person p: listPerson){
127    //                              fieldValues.put("principalId", p.getPrincipalId());
128    //                              objectList.addAll((Collection<? extends Job>) super.getSearchResults(fieldValues));
129    //                      }
130    //                      //trim if greater than 300 items
131    //                      if(objectList.size() > 300){
132    //                              objectList = objectList.subList(0, 299);
133    //                      }
134    //                      return objectList;
135    //              }
136    
137    //        return super.getSearchResults(fieldValues);
138        }
139    
140        @SuppressWarnings("rawtypes")
141        @Override
142        public HtmlData getReturnUrl(BusinessObject businessObject,
143                                     LookupForm lookupForm, List returnKeys,
144                                     BusinessObjectRestrictions businessObjectRestrictions) {
145            if (lookupForm.getFieldConversions().containsKey("effectiveDate")) {
146                lookupForm.getFieldConversions().remove("effectiveDate");
147            }
148            if (returnKeys.contains("effectiveDate")) {
149                returnKeys.remove("effectiveDate");
150            }
151            return super.getReturnUrl(businessObject, lookupForm, returnKeys,
152                    businessObjectRestrictions);
153        }
154    }