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