001    /*
002     * Copyright 2011 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.kfs.coa.businessobject.lookup;
017    
018    import java.util.ArrayList;
019    import java.util.List;
020    import java.util.Map;
021    
022    import org.kuali.kfs.coa.businessobject.Account;
023    import org.kuali.kfs.sys.KFSConstants;
024    import org.kuali.kfs.sys.KFSPropertyConstants;
025    import org.kuali.kfs.sys.context.SpringContext;
026    import org.kuali.rice.kim.bo.Person;
027    import org.kuali.rice.kim.service.IdentityManagementService;
028    import org.kuali.rice.kns.bo.BusinessObject;
029    import org.kuali.rice.kns.lookup.HtmlData;
030    import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl;
031    import org.kuali.rice.kns.lookup.HtmlData.AnchorHtmlData;
032    import org.kuali.rice.kns.util.GlobalVariables;
033    import org.kuali.rice.kns.util.KNSConstants;
034    
035    /**
036     * This class overrids the base getActionUrls method
037     */
038    public class KualiAccountLookupableHelperServiceImpl extends KualiLookupableHelperServiceImpl {
039    
040        ThreadLocal<Person> currentUser = new ThreadLocal<Person>();
041    
042        /**
043         * If the account is not closed or the user is an Administrator the "edit" link is added The "copy" link is added for Accounts
044         * 
045         * @returns links to edit and copy maintenance action for the current maintenance record.
046         * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#getCustomActionUrls(org.kuali.rice.kns.bo.BusinessObject,
047         *      java.util.List)
048         */
049        @Override
050        public List<HtmlData> getCustomActionUrls(BusinessObject businessObject, List pkNames) {
051            StringBuffer actions = new StringBuffer();
052            Account theAccount = (Account) businessObject;
053            List<HtmlData> anchorHtmlDataList = new ArrayList<HtmlData>();
054            Person user = currentUser.get();
055            if (user == null) {
056                user = GlobalVariables.getUserSession().getPerson();
057                currentUser.set(user);
058            }
059            AnchorHtmlData urlDataCopy = getUrlData(businessObject, KNSConstants.MAINTENANCE_COPY_METHOD_TO_CALL, pkNames);
060    
061            if (theAccount.isActive()) {
062                anchorHtmlDataList.add(getUrlData(businessObject, KNSConstants.MAINTENANCE_EDIT_METHOD_TO_CALL, pkNames));
063            }
064            else {
065                String principalId = user.getPrincipalId();
066                String namespaceCode = KFSConstants.ParameterNamespaces.CHART;
067                String permissionName = KFSConstants.PermissionNames.EDIT_INACTIVE_ACCOUNT;
068    
069                IdentityManagementService identityManagementService = SpringContext.getBean(IdentityManagementService.class);
070                Boolean isAuthorized = identityManagementService.hasPermission(principalId, namespaceCode, permissionName, null);
071    
072                if (isAuthorized) {
073                    anchorHtmlDataList.add(getUrlData(businessObject, KNSConstants.MAINTENANCE_EDIT_METHOD_TO_CALL, pkNames));
074                }
075                else {
076                    urlDataCopy.setPrependDisplayText("&nbsp;&nbsp;&nbsp;&nbsp;");
077                }
078            }
079            anchorHtmlDataList.add(urlDataCopy);
080    
081            return anchorHtmlDataList;
082        }
083    
084        /**
085         * Overridden to changed the "closed" parameter to an "active" parameter
086         * 
087         * @see org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl#getSearchResults(java.util.Map)
088         */
089        @Override
090        public List<? extends BusinessObject> getSearchResults(Map<String, String> parameters) {
091            if (parameters.containsKey(KFSPropertyConstants.CLOSED)) {
092                final String closedValue = parameters.get(KFSPropertyConstants.CLOSED);
093    
094                if (closedValue != null && closedValue.length() != 0) {
095                    if ("Y1T".indexOf(closedValue) > -1) {
096                        parameters.put(KFSPropertyConstants.ACTIVE, "N");
097                    }
098                    else if ("N0F".indexOf(closedValue) > -1) {
099                        parameters.put(KFSPropertyConstants.ACTIVE, "Y");
100                    }
101                }
102    
103                parameters.remove(KFSPropertyConstants.CLOSED);
104            }
105            return super.getSearchResults(parameters);
106        }
107    
108    
109    }