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