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.module.bc.businessobject.lookup;
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.kfs.module.bc.BCConstants;
025    import org.kuali.kfs.module.bc.BCPropertyConstants;
026    import org.kuali.kfs.module.bc.businessobject.BudgetConstructionIntendedIncumbent;
027    import org.kuali.kfs.module.bc.util.BudgetParameterFinder;
028    import org.kuali.kfs.sys.KFSConstants;
029    import org.kuali.kfs.sys.KFSPropertyConstants;
030    import org.kuali.rice.kns.bo.BusinessObject;
031    import org.kuali.rice.kns.lookup.HtmlData;
032    import org.kuali.rice.kns.lookup.HtmlData.AnchorHtmlData;
033    import org.kuali.rice.kns.service.KualiConfigurationService;
034    import org.kuali.rice.kns.util.KNSConstants;
035    import org.kuali.rice.kns.util.UrlFactory;
036    import org.kuali.rice.kns.web.format.BooleanFormatter;
037    
038    /**
039     * Lookupable helper service implementation for the intended incumbent lookup..
040     */
041    public class IntendedIncumbentLookupableHelperServiceImpl extends SelectLookupableHelperServiceImpl {
042        public KualiConfigurationService kualiConfigurationService;
043    
044        /**
045         * @see org.kuali.rice.kns.lookup.LookupableHelperService#getCustomActionUrls(org.kuali.rice.kns.bo.BusinessObject, java.util.List, java.util.List pkNames)
046         */
047        @Override
048        public List<HtmlData> getCustomActionUrls(BusinessObject businessObject, List pkNames) {
049            Map requestParameters = super.getParameters();
050            if (requestParameters.containsKey(BCConstants.SHOW_SALARY_BY_INCUMBENT_ACTION)) {
051                String[] requestParm = (String[]) requestParameters.get(BCConstants.SHOW_SALARY_BY_INCUMBENT_ACTION);
052                Boolean showSalaryByIncumbent = (Boolean) (new BooleanFormatter()).convertFromPresentationFormat(requestParm[0]);
053                if (!showSalaryByIncumbent) {
054                    return getIncumbentCustomActionUrls(businessObject, pkNames);
055                }
056            }
057            else {
058                return getIncumbentCustomActionUrls(businessObject, pkNames);
059            }
060            return getSalarySettingByIncumbentUrls(businessObject);
061        }
062    
063        /***
064         * 
065         * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#getActionUrlHref(org.kuali.rice.kns.bo.BusinessObject, java.lang.String, java.util.List)
066         */
067        @Override
068        protected String getActionUrlHref(BusinessObject businessObject, String methodToCall, List pkNames){
069            String href = super.getActionUrlHref(businessObject, methodToCall, pkNames);
070            return StringUtils.replace(href, KFSConstants.MAINTENANCE_ACTION,
071                    KFSConstants.RICE_PATH_PREFIX + KFSConstants.MAINTENANCE_ACTION);
072        }
073    
074        /**
075         * Checks system parameter to determine if the incumbent table is maintained by an external system or internally. If internally
076         * they calls super to display the edit and copy links. If external then returns the refresh button source.
077         *
078         * @param businessObject business object for result row
079         * @return String holding the action column contents
080         */
081        private List<HtmlData> getIncumbentCustomActionUrls(BusinessObject businessObject, List pkNames) {
082            BudgetConstructionIntendedIncumbent intendedIncumbent = (BudgetConstructionIntendedIncumbent) businessObject;
083            boolean payrollIncumbentFeed = BudgetParameterFinder.getPayrollIncumbentFeedIndictor();
084            if (!payrollIncumbentFeed) {
085                return super.getCustomActionUrls(businessObject, pkNames);
086            }
087            return super.getEmptyActionUrls();
088        }
089    
090        /**
091         * Override to check system parameter for determining if the incumbent data is feed from Payroll or maintained in the KFS.
092         *
093         * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#allowsMaintenanceNewOrCopyAction()
094         */
095        @Override
096        public boolean allowsMaintenanceNewOrCopyAction() {
097            boolean payrollIncumbentFeed = BudgetParameterFinder.getPayrollIncumbentFeedIndictor();
098            if (payrollIncumbentFeed) {
099                return false;
100            }
101    
102            return true;
103        }
104    
105        protected Properties getSalarySettingByIncumbentParameters(BusinessObject businessObject){
106            BudgetConstructionIntendedIncumbent intendedIncumbent = (BudgetConstructionIntendedIncumbent) businessObject;
107    
108            Properties parameters = new Properties();
109            parameters.put(KFSConstants.DISPATCH_REQUEST_PARAMETER, BCConstants.INCUMBENT_SALARY_SETTING_METHOD);
110            parameters.put(KFSPropertyConstants.EMPLID, intendedIncumbent.getEmplid());
111    
112            Map requestParameters = super.getParameters();
113            boolean linkToNewWindow = true;
114            if (requestParameters.containsKey(BCPropertyConstants.ADD_LINE)) {
115                String[] requestParm = (String[]) requestParameters.get(BCPropertyConstants.ADD_LINE);
116                parameters.put(BCPropertyConstants.ADD_LINE, requestParm[0]);
117                Boolean addNewFunding = (Boolean) (new BooleanFormatter()).convertFromPresentationFormat(requestParm[0]);
118                if (addNewFunding) {
119                    linkToNewWindow = false;
120                }
121            }
122            else {
123                parameters.put(BCPropertyConstants.ADD_LINE, "false");
124            }
125    
126            String[] universityFiscalYear = (String[]) super.getParameters().get(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR);
127            parameters.put(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR, universityFiscalYear[0]);
128    
129            if (requestParameters.containsKey(KNSConstants.DOC_FORM_KEY)) {
130                String[] requestParm = (String[]) requestParameters.get(KNSConstants.DOC_FORM_KEY);
131                parameters.put(BCConstants.RETURN_FORM_KEY, requestParm[0]);
132            }
133            else  if (requestParameters.containsKey(KFSConstants.FORM_KEY)) {
134                String[] requestParm = (String[]) requestParameters.get(KFSConstants.FORM_KEY);
135                parameters.put(BCConstants.RETURN_FORM_KEY, requestParm[0]);
136            }
137    
138            if (requestParameters.containsKey(KFSConstants.BACK_LOCATION)) {
139                String[] requestParm = (String[]) requestParameters.get(KFSConstants.BACK_LOCATION);
140                parameters.put(KFSConstants.BACK_LOCATION, requestParm[0]);
141            }
142    
143            if (requestParameters.containsKey(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE)) {
144                String[] requestParm = (String[]) requestParameters.get(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE);
145                parameters.put(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE, requestParm[0]);
146            }
147    
148            if (requestParameters.containsKey(KFSPropertyConstants.ACCOUNT_NUMBER)) {
149                String[] requestParm = (String[]) requestParameters.get(KFSPropertyConstants.ACCOUNT_NUMBER);
150                parameters.put(KFSPropertyConstants.ACCOUNT_NUMBER, requestParm[0]);
151            }
152    
153            if (requestParameters.containsKey(KFSPropertyConstants.SUB_ACCOUNT_NUMBER)) {
154                String[] requestParm = (String[]) requestParameters.get(KFSPropertyConstants.SUB_ACCOUNT_NUMBER);
155                parameters.put(KFSPropertyConstants.SUB_ACCOUNT_NUMBER, requestParm[0]);
156            }
157    
158            if (requestParameters.containsKey(KFSPropertyConstants.FINANCIAL_OBJECT_CODE)) {
159                String[] requestParm = (String[]) requestParameters.get(KFSPropertyConstants.FINANCIAL_OBJECT_CODE);
160                parameters.put(KFSPropertyConstants.FINANCIAL_OBJECT_CODE, requestParm[0]);
161            }
162    
163            if (requestParameters.containsKey(KFSPropertyConstants.FINANCIAL_SUB_OBJECT_CODE)) {
164                String[] requestParm = (String[]) requestParameters.get(KFSPropertyConstants.FINANCIAL_SUB_OBJECT_CODE);
165                parameters.put(KFSPropertyConstants.FINANCIAL_SUB_OBJECT_CODE, requestParm[0]);
166            }
167            
168            if (requestParameters.containsKey(BCPropertyConstants.BUDGET_BY_ACCOUNT_MODE)) {
169                String[] requestParm = (String[]) requestParameters.get(BCPropertyConstants.BUDGET_BY_ACCOUNT_MODE);
170                parameters.put(BCPropertyConstants.BUDGET_BY_ACCOUNT_MODE, requestParm[0]);
171            }
172    
173            if (requestParameters.containsKey(BCPropertyConstants.MAIN_WINDOW)) {
174                String[] requestParm = (String[]) requestParameters.get(BCPropertyConstants.MAIN_WINDOW);
175                parameters.put(BCPropertyConstants.MAIN_WINDOW, requestParm[0]);
176            }
177    
178           if (requestParameters.containsKey(BCPropertyConstants.SINGLE_ACCOUNT_MODE)) {
179                String[] requestParm = (String[]) requestParameters.get(BCPropertyConstants.SINGLE_ACCOUNT_MODE);
180                parameters.put(BCPropertyConstants.SINGLE_ACCOUNT_MODE, requestParm[0]);
181            }
182            else {
183                parameters.put(BCPropertyConstants.SINGLE_ACCOUNT_MODE, "false");
184            }
185    
186            parameters.put(BCConstants.REFRESH_INCUMBENT_BEFORE_SALARY_SETTING, "false");
187            return parameters;
188        }
189    
190        /**
191         * Builds URL to salary setting by Incumbent setting parameters based on the caller request.
192         *
193         * @param businessObject business object for result row
194         * @return String holding the action column contents
195         */
196        public List<HtmlData> getSalarySettingByIncumbentUrls(BusinessObject businessObject) {
197            List<HtmlData> anchorHtmlDataList = new ArrayList<HtmlData>();
198            
199            Properties parameters = getSalarySettingByIncumbentParameters(businessObject);
200            String href = UrlFactory.parameterizeUrl(BCConstants.INCUMBENT_SALARY_SETTING_ACTION, parameters);
201            AnchorHtmlData urlData1 =
202                new AnchorHtmlData(href, BCConstants.INCUMBENT_SALARY_SETTING_METHOD, "Incmbnt Salset");
203            
204            Map requestParameters = super.getParameters();
205            
206            boolean linkToNewWindow = true;
207            if (requestParameters.containsKey(BCPropertyConstants.ADD_LINE)) {
208                String[] requestParm = (String[]) requestParameters.get(BCPropertyConstants.ADD_LINE);
209                Boolean addNewFunding = (Boolean) (new BooleanFormatter()).convertFromPresentationFormat(requestParm[0]);
210                if (addNewFunding) {
211                    linkToNewWindow = false;
212                }
213            }
214            if (linkToNewWindow) {
215                urlData1.setTarget(KFSConstants.NEW_WINDOW_URL_TARGET);
216            }
217            
218            anchorHtmlDataList.add(urlData1);
219    
220            // now add refresh url if feed from payroll is on
221            boolean payrollIncumbentFeed = BudgetParameterFinder.getPayrollIncumbentFeedIndictor();
222            if (payrollIncumbentFeed) {
223                parameters.put(BCConstants.REFRESH_INCUMBENT_BEFORE_SALARY_SETTING, "true");
224                href = UrlFactory.parameterizeUrl(BCConstants.INCUMBENT_SALARY_SETTING_ACTION, parameters);
225                AnchorHtmlData urlData2 =
226                    new AnchorHtmlData(href, BCConstants.INCUMBENT_SALARY_SETTING_METHOD, "Incmbnt Salset w/sync");
227    
228                if (linkToNewWindow) {
229                    urlData2.setTarget(KFSConstants.NEW_WINDOW_URL_TARGET);
230                }
231                anchorHtmlDataList.add(urlData2);
232                anchorHtmlDataList.get(anchorHtmlDataList.lastIndexOf(urlData2)).setPrependDisplayText("<br />");
233            }
234            
235            return anchorHtmlDataList;
236        }
237    
238        /**
239         * Sets the kualiConfigurationService attribute value.
240         *
241         * @param kualiConfigurationService The kualiConfigurationService to set.
242         */
243        public void setKualiConfigurationService(KualiConfigurationService kualiConfigurationService) {
244            this.kualiConfigurationService = kualiConfigurationService;
245        }
246    
247    }