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.document.dataaccess.impl;
017    
018    import java.util.ArrayList;
019    import java.util.HashMap;
020    import java.util.List;
021    import java.util.Map;
022    
023    import org.apache.ojb.broker.PersistenceBroker;
024    import org.apache.ojb.broker.accesslayer.QueryCustomizer;
025    import org.apache.ojb.broker.metadata.CollectionDescriptor;
026    import org.apache.ojb.broker.query.Query;
027    import org.apache.ojb.broker.query.QueryByCriteria;
028    import org.kuali.kfs.module.bc.util.BudgetParameterFinder;
029    import org.kuali.kfs.sys.KFSPropertyConstants;
030    
031    /**
032     * This customizer constrains the relationship to PendingBudgetConstructionGeneralLedger so as to fetch expenditure or revenue lines
033     * based on a set of object code types
034     */
035    public class OjbPBGLQueryCustomizer implements QueryCustomizer {
036    
037        private Map attributes = new HashMap();
038        private static final String revenueAttributeName = "REVENUE";
039        private static final String objectCodeTypeField = KFSPropertyConstants.FINANCIAL_OBJECT_TYPE_CODE;
040    
041        /**
042         * @see org.apache.ojb.broker.metadata.AttributeContainer#addAttribute(java.lang.String, java.lang.String)
043         */
044        public void addAttribute(String arg0, String arg1) {
045            attributes.put(arg0, arg1);
046        }
047    
048        /**
049         * @see org.apache.ojb.broker.metadata.AttributeContainer#getAttribute(java.lang.String, java.lang.String)
050         */
051        public String getAttribute(String arg0, String arg1) {
052            return (String) attributes.get(arg0);
053        }
054    
055        /**
056         * @see org.apache.ojb.broker.metadata.AttributeContainer#getAttribute(java.lang.String)
057         */
058        public String getAttribute(String arg0) {
059            return (String) attributes.get(arg0);
060        }
061    
062        /**
063         * @see org.apache.ojb.broker.accesslayer.QueryCustomizer#customizeQuery(java.lang.Object,
064         *      org.apache.ojb.broker.PersistenceBroker, org.apache.ojb.broker.metadata.CollectionDescriptor,
065         *      org.apache.ojb.broker.query.QueryByCriteria)
066         */
067        public Query customizeQuery(Object arg0, PersistenceBroker arg1, CollectionDescriptor arg2, QueryByCriteria arg3) {
068            List paramValues;
069    
070            // these parameter service calls will throw an IllegalArgumentException exception if the parameter doesn't exist
071            if ("TRUE".equals(getAttribute(revenueAttributeName))) {
072                paramValues = BudgetParameterFinder.getRevenueObjectTypes();
073            }
074            else {
075                paramValues = BudgetParameterFinder.getExpenditureObjectTypes();
076            }
077            
078            ArrayList<String> revObjs = new ArrayList<String>();
079            revObjs.addAll(paramValues);
080            arg3.getCriteria().addIn(objectCodeTypeField, revObjs);
081            
082            return arg3;
083        }
084    
085    }