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.timecollection.rule.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.authorization.DepartmentalRule;
025    import org.kuali.hr.time.authorization.DepartmentalRuleAuthorizer;
026    import org.kuali.hr.time.authorization.TkAuthorizedLookupableHelperBase;
027    import org.kuali.hr.time.collection.rule.TimeCollectionRule;
028    import org.kuali.hr.time.department.Department;
029    import org.kuali.hr.time.service.base.TkServiceLocator;
030    import org.kuali.hr.time.util.TKContext;
031    import org.kuali.hr.time.util.TKUtils;
032    import org.kuali.rice.kns.lookup.HtmlData;
033    import org.kuali.rice.kns.lookup.HtmlData.AnchorHtmlData;
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 TimeCollectionRuleLookupableHelper extends TkAuthorizedLookupableHelperBase {
039    
040            private static final long serialVersionUID = -1690980961895784168L;
041    
042            @Override
043        /**
044         * Implemented method to reduce the set of Business Objects that are shown
045         * to the user based on their current roles.
046         */
047        public boolean shouldShowBusinessObject(BusinessObject bo) {
048            return (bo instanceof DepartmentalRule) && DepartmentalRuleAuthorizer.hasAccessToRead((DepartmentalRule)bo);
049        }
050    
051            @Override
052            public List<HtmlData> getCustomActionUrls(BusinessObject businessObject, List pkNames) {
053                    List<HtmlData> customActionUrls = new ArrayList<HtmlData>();
054                    
055                    List<HtmlData> defaultCustomActionUrls = super.getCustomActionUrls(businessObject, pkNames);
056                    
057                    TimeCollectionRule timeCollectionRule = (TimeCollectionRule) businessObject;
058                    String tkTimeCollectionRuleId = timeCollectionRule.getTkTimeCollectionRuleId();
059                    String department = timeCollectionRule.getDept();
060                    String workArea = String.valueOf(timeCollectionRule.getWorkArea());
061            String location = null;
062            if (timeCollectionRule.getDept() != null) {
063                Department dept = TkServiceLocator.getDepartmentService().getDepartment(timeCollectionRule.getDept(), TKUtils.getCurrentDate());
064                        location = dept == null ? null : dept.getLocation();
065            }
066                    boolean systemAdmin = TKContext.getUser().isSystemAdmin();
067                    boolean locationAdmin = TKContext.getUser().getLocationAdminAreas().contains(location);
068                    boolean departmentAdmin = TKContext.getUser().getDepartmentAdminAreas().contains(department);
069                    
070                    for (HtmlData defaultCustomActionUrl : defaultCustomActionUrls){
071                            if (StringUtils.equals(defaultCustomActionUrl.getMethodToCall(), "edit")) {
072                                    if (systemAdmin || locationAdmin || departmentAdmin) {
073                                            customActionUrls.add(defaultCustomActionUrl);
074                                    }
075                            } else {
076                                    customActionUrls.add(defaultCustomActionUrl);
077                            }
078                    }
079                    
080                    Properties params = new Properties();
081                    params.put(KRADConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, getBusinessObjectClass().getName());
082                    params.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.MAINTENANCE_NEW_METHOD_TO_CALL);
083                    params.put("tkTimeCollectionRuleId", tkTimeCollectionRuleId);
084                    params.put("dept", department);
085                    params.put("workArea", workArea);
086                    AnchorHtmlData viewUrl = new AnchorHtmlData(UrlFactory.parameterizeUrl(KRADConstants.INQUIRY_ACTION, params), "view");
087                    viewUrl.setDisplayText("view");
088                    viewUrl.setTarget(AnchorHtmlData.TARGET_BLANK);
089                    customActionUrls.add(viewUrl);
090                    
091                    return customActionUrls;
092            }
093    
094            @Override
095            public List<? extends BusinessObject> getSearchResults( Map<String, String> fieldValues) {
096            String workArea = fieldValues.get("workArea");
097            String dept = fieldValues.get("dept");
098            String payType = fieldValues.get("payType");
099            String active = fieldValues.get("active");
100    
101            List<TimeCollectionRule> timeCollectionRules = TkServiceLocator.getTimeCollectionRuleService().getTimeCollectionRules(dept, workArea, payType, active);
102                    return timeCollectionRules;
103            }
104    
105            @Override
106            protected void validateSearchParameterWildcardAndOperators(
107                            String attributeName, String attributeValue) {
108                    if (!StringUtils.equals(attributeValue, "%")) {
109                            super.validateSearchParameterWildcardAndOperators(attributeName,
110                                            attributeValue);
111                    }
112            }
113    }