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.time.assignment.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.assignment.Assignment;
026    import org.kuali.hr.time.department.Department;
027    import org.kuali.hr.time.service.base.TkServiceLocator;
028    import org.kuali.hr.time.util.TKContext;
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 AssignmentLookupableHelper extends HrEffectiveDateActiveLookupableHelper {
037    
038            private static final long serialVersionUID = 774015772672806415L;
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                    Assignment assignment = (Assignment) businessObject;
047            String tkAssignmentId = assignment.getTkAssignmentId();
048            Department dept = TkServiceLocator.getDepartmentService().getDepartment(assignment.getDept(), TKUtils.getCurrentDate());
049            String location = dept == null ? null : dept.getLocation();
050            String department = assignment.getDept();
051            
052            boolean systemAdmin = TKContext.getUser().isSystemAdmin();
053                    boolean locationAdmin = TKContext.getUser().getLocationAdminAreas().contains(location);
054                    boolean departmentAdmin = TKContext.getUser().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("tkAssignmentId", tkAssignmentId);
070                    AnchorHtmlData viewUrl = new AnchorHtmlData(UrlFactory.parameterizeUrl(KRADConstants.INQUIRY_ACTION, params), "view");
071                    viewUrl.setDisplayText("view");
072                    viewUrl.setTarget(AnchorHtmlData.TARGET_BLANK);
073                    customActionUrls.add(viewUrl);
074                    
075                    return customActionUrls;
076        }
077    
078        @SuppressWarnings({"unchecked"})
079        @Override
080        public List<? extends BusinessObject> getSearchResults(Map<String, String> fieldValues) {
081            String fromEffdt = fieldValues.get("rangeLowerBoundKeyPrefix_effectiveDate");
082            String toEffdt = StringUtils.isNotBlank(fieldValues.get("effectiveDate")) ? fieldValues.get("effectiveDate").replace("<=", "") : "";
083            String principalId = fieldValues.get("principalId");
084            String jobNumber = fieldValues.get("jobNumber");
085            String dept = fieldValues.get("dept");
086            String workArea = fieldValues.get("workArea");
087            String active = fieldValues.get("active");
088            String showHist = fieldValues.get("history");
089    
090            List<Assignment> assignments = TkServiceLocator.getAssignmentService().searchAssignments(TKUtils.formatDateString(fromEffdt), TKUtils.formatDateString(toEffdt), principalId,
091                    jobNumber, dept, workArea, active, showHist);
092    
093    //              String deptName = "";
094    //              if(fieldValues.containsKey("dept") && !fieldValues.get("dept").isEmpty()) {
095    //                      deptName = fieldValues.get("dept");
096    //                      fieldValues.remove("dept");
097    //              }
098    //              List aList = super.getSearchResults(fieldValues);
099    //              if(deptName.isEmpty()) {
100    //                      return aList;
101    //              }
102    //              List finalList = new ArrayList<Assignment>();
103    //              for(int i = 0; i< aList.size(); i++) {
104    //                      Assignment anAssign = (Assignment) aList.get(i);
105    //                      if(anAssign.getPrincipalId() != null && anAssign.getEffectiveDate() != null) {
106    //                              List<Job> jobList = TkServiceLocator.getJobService().getJobs(anAssign.getPrincipalId(), anAssign.getEffectiveDate());
107    //                              for(Job aJob : jobList) {
108    //                                      if(!aJob.getJobNumber().equals(anAssign.getJobNumber())){
109    //                                              continue;
110    //                                      }
111    //                                      if( aJob.getDept().equals(deptName)) {
112    //                                              finalList.add(anAssign);
113    //                                              break;
114    //                                      }
115    //                              }
116    //                      }
117    //              }
118            return assignments;
119        }
120    
121    
122    }