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.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.TKUser;
030    import org.kuali.hr.time.util.TKUtils;
031    import org.kuali.rice.kns.lookup.HtmlData;
032    import org.kuali.rice.kns.lookup.HtmlData.AnchorHtmlData;
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 AssignmentLookupableHelper extends HrEffectiveDateActiveLookupableHelper {
038    
039            private static final long serialVersionUID = 774015772672806415L;
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                    Assignment assignment = (Assignment) businessObject;
048            String tkAssignmentId = assignment.getTkAssignmentId();
049            Department dept = TkServiceLocator.getDepartmentService().getDepartment(assignment.getDept(), TKUtils.getCurrentDate());
050            String location = dept == null ? null : dept.getLocation();
051            String department = assignment.getDept();
052            
053            boolean systemAdmin = TKUser.isSystemAdmin();
054                    boolean locationAdmin = TKUser.getLocationAdminAreas().contains(location);
055                    boolean departmentAdmin = TKUser.getDepartmentAdminAreas().contains(department);
056                    
057                    for (HtmlData defaultCustomActionUrl : defaultCustomActionUrls){
058                            if (StringUtils.equals(defaultCustomActionUrl.getMethodToCall(), "edit")) {
059                                    if (systemAdmin || locationAdmin || departmentAdmin) {
060                                            customActionUrls.add(defaultCustomActionUrl);
061                                    }
062                            } else {
063                                    customActionUrls.add(defaultCustomActionUrl);
064                            }
065                    }
066                    
067                    Properties params = new Properties();
068                    params.put(KRADConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, getBusinessObjectClass().getName());
069                    params.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.MAINTENANCE_NEW_METHOD_TO_CALL);
070                    params.put("tkAssignmentId", tkAssignmentId);
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        @Override
080        public List<? extends BusinessObject> getSearchResults(Map<String, String> fieldValues) {
081            String fromEffdt = TKUtils.getFromDateString(fieldValues.get("effectiveDate"));
082            String toEffdt = TKUtils.getToDateString(fieldValues.get("effectiveDate"));
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            return TkServiceLocator.getAssignmentService().searchAssignments(TKUtils.formatDateString(fromEffdt), TKUtils.formatDateString(toEffdt), principalId, 
091                            jobNumber, dept, workArea, active, showHist);
092        }
093    
094    }