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.gl.businessobject.lookup;
017    
018    import java.util.Iterator;
019    import java.util.List;
020    import java.util.Map;
021    
022    import org.kuali.kfs.gl.Constant;
023    import org.kuali.kfs.gl.GeneralLedgerConstants;
024    import org.kuali.kfs.gl.businessobject.AccountBalance;
025    import org.kuali.kfs.gl.businessobject.TransientBalanceInquiryAttributes;
026    import org.kuali.kfs.gl.businessobject.inquiry.AccountBalanceByObjectInquirableImpl;
027    import org.kuali.kfs.gl.businessobject.inquiry.AccountBalanceInquirableImpl;
028    import org.kuali.kfs.gl.service.AccountBalanceService;
029    import org.kuali.kfs.sys.KFSConstants;
030    import org.kuali.kfs.sys.KFSPropertyConstants;
031    import org.kuali.rice.kns.bo.PersistableBusinessObject;
032    import org.kuali.rice.kns.lookup.CollectionIncomplete;
033    import org.kuali.rice.kns.lookup.HtmlData;
034    import org.kuali.rice.kns.lookup.KualiLookupableImpl;
035    
036    /**
037     * An extension of KualiLookupableImpl to support the account balance by object inquiry screen
038     */
039    public class AccountBalanceByObjectLookupableImpl extends KualiLookupableImpl {
040        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(AccountBalanceByObjectLookupableImpl.class);
041    
042        private AccountBalanceService accountBalanceService;
043    
044        public void setAccountBalanceService(AccountBalanceService abs) {
045            accountBalanceService = abs;
046        }
047    
048        /**
049         * Returns the inquiry url for a result field.
050         * 
051         * @param bo the business object instance to build the urls for
052         * @param propertyName the property which links to an inquirable
053         * @return String url to inquiry
054         */
055        public HtmlData getInquiryUrl(PersistableBusinessObject bo, String propertyName) {
056            if (GeneralLedgerConstants.DummyBusinessObject.LINK_BUTTON_OPTION.equals(propertyName)) {
057                return (new AccountBalanceByObjectInquirableImpl()).getInquiryUrl(bo, propertyName);
058            }
059            return (new AccountBalanceInquirableImpl()).getInquiryUrl(bo, propertyName);
060        }
061    
062        /**
063         * Uses Lookup Service to provide a basic search.
064         * 
065         * @param fieldValues - Map containing prop name keys and search values
066         * @return List found business objects
067         */
068        public List getSearchResults(Map fieldValues) {
069            LOG.debug("getSearchResults() started");
070    
071            setBackLocation((String) fieldValues.get(KFSConstants.BACK_LOCATION));
072            setDocFormKey((String) fieldValues.get(KFSConstants.DOC_FORM_KEY));
073    
074            BusinessObjectFieldConverter.escapeSingleQuote(fieldValues);
075    
076            String costShareOption = (String) fieldValues.get(GeneralLedgerConstants.DummyBusinessObject.COST_SHARE_OPTION);
077            String pendingEntryOption = (String) fieldValues.get(GeneralLedgerConstants.DummyBusinessObject.PENDING_ENTRY_OPTION);
078            String consolidationOption = (String) fieldValues.get(GeneralLedgerConstants.DummyBusinessObject.CONSOLIDATION_OPTION);
079            boolean isCostShareExcluded = Constant.COST_SHARE_EXCLUDE.equals(costShareOption);
080            int pendingEntryCode = AccountBalanceService.PENDING_NONE;
081            if (GeneralLedgerConstants.PendingEntryOptions.APPROVED.equals(pendingEntryOption)) {
082                pendingEntryCode = AccountBalanceService.PENDING_APPROVED;
083            }
084            else if (GeneralLedgerConstants.PendingEntryOptions.ALL.equals(pendingEntryOption)) {
085                pendingEntryCode = AccountBalanceService.PENDING_ALL;
086            }
087            boolean isConsolidated = Constant.CONSOLIDATION.equals(consolidationOption);
088    
089            String chartOfAccountsCode = (String) fieldValues.get(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE);
090            String accountNumber = (String) fieldValues.get(KFSPropertyConstants.ACCOUNT_NUMBER);
091            String subAccountNumber = (String) fieldValues.get(KFSPropertyConstants.SUB_ACCOUNT_NUMBER);
092            String financialObjectLevelCode = (String) fieldValues.get(GeneralLedgerConstants.BalanceInquiryDrillDowns.OBJECT_LEVEL_CODE);
093            String financialReportingSortCode = (String) fieldValues.get(GeneralLedgerConstants.BalanceInquiryDrillDowns.REPORTING_SORT_CODE);
094    
095            // Dashes means no sub account number
096            if (KFSConstants.getDashSubAccountNumber().equals(subAccountNumber)) {
097                subAccountNumber = "";
098            }
099    
100            String ufy = (String) fieldValues.get(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR);
101    
102            // TODO Deal with invalid numbers
103            Integer universityFiscalYear = new Integer(Integer.parseInt(ufy));
104    
105            List results = accountBalanceService.findAccountBalanceByObject(universityFiscalYear, chartOfAccountsCode, accountNumber, subAccountNumber, financialObjectLevelCode, financialReportingSortCode, isCostShareExcluded, isConsolidated, pendingEntryCode);
106    
107            // Put the search related stuff in the objects
108            for (Iterator iter = results.iterator(); iter.hasNext();) {
109                AccountBalance ab = (AccountBalance) iter.next();
110    
111                TransientBalanceInquiryAttributes dbo = ab.getDummyBusinessObject();
112                dbo.setConsolidationOption(consolidationOption);
113                dbo.setCostShareOption(costShareOption);
114                dbo.setPendingEntryOption(pendingEntryOption);
115                dbo.setLinkButtonOption(Constant.LOOKUP_BUTTON_VALUE);
116            }
117            return new CollectionIncomplete(results, new Long(results.size()));
118        }
119    }