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.Collection;
020    import java.util.Collections;
021    import java.util.Iterator;
022    import java.util.List;
023    import java.util.Map;
024    
025    import org.apache.ojb.broker.query.Criteria;
026    import org.apache.ojb.broker.query.QueryByCriteria;
027    import org.apache.ojb.broker.query.QueryFactory;
028    import org.apache.ojb.broker.query.ReportQueryByCriteria;
029    import org.kuali.kfs.module.bc.businessobject.BudgetConstructionOrganizationReports;
030    import org.kuali.kfs.module.bc.document.dataaccess.BudgetConstructionOrganizationReportsDao;
031    import org.kuali.kfs.sys.KFSPropertyConstants;
032    import org.kuali.rice.kns.dao.impl.PlatformAwareDaoBaseOjb;
033    import org.kuali.rice.kns.util.TransactionalServiceUtils;
034    
035    public class BudgetConstructionOrganizationReportsDaoOjb extends PlatformAwareDaoBaseOjb implements BudgetConstructionOrganizationReportsDao {
036    
037        /**
038         * @see BudgetConstructionOrganizationReportsDao#getByPrimaryId(java.lang.String, java.lang.String)
039         */
040        public BudgetConstructionOrganizationReports getByPrimaryId(String chartOfAccountsCode, String organizationCode) {
041            Criteria criteria = new Criteria();
042            criteria.addEqualTo("chartOfAccountsCode", chartOfAccountsCode);
043            criteria.addEqualTo("organizationCode", organizationCode);
044    
045            QueryByCriteria qbc = QueryFactory.newQuery(BudgetConstructionOrganizationReports.class, criteria);
046           
047            // Since using primaryId, getObject should be better than collection.
048            return (BudgetConstructionOrganizationReports) getPersistenceBrokerTemplate().getObjectByQuery(qbc);
049        }
050        
051        /**
052         * @see org.kuali.kfs.module.bc.document.dataaccess.BudgetConstructionOrganizationReportsDao#getBySearchCriteria(java.lang.Class, java.util.Map)
053         */
054        public Collection getBySearchCriteria(Class cls, Map searchCriteria) {
055            Criteria criteria = new Criteria();
056            for (Iterator iter = searchCriteria.keySet().iterator(); iter.hasNext();) {
057                String element = (String) iter.next();
058                criteria.addEqualTo(element, searchCriteria.get(element));
059            }
060    
061            QueryByCriteria qbc = QueryFactory.newQuery(cls, criteria);
062            return getPersistenceBrokerTemplate().getCollectionByQuery(qbc);
063        }
064        
065        /**
066         * @see org.kuali.kfs.module.bc.document.dataaccess.BudgetConstructionOrganizationReportsDao#getBySearchCriteriaWithOrderByList(java.lang.Class, java.util.Map, java.util.List)
067         */
068        public Collection getBySearchCriteriaWithOrderByList(Class cls, Map searchCriteria, List<String> list) {
069            Criteria criteria = new Criteria();
070            for (Iterator iter = searchCriteria.keySet().iterator(); iter.hasNext();) {
071                String element = (String) iter.next();
072                criteria.addEqualTo(element, searchCriteria.get(element));
073            }
074    
075            QueryByCriteria qbc = QueryFactory.newQuery(cls, criteria);
076            for (String orderAttribute : list) {
077                qbc.addOrderByAscending(orderAttribute);
078            }
079            return getPersistenceBrokerTemplate().getCollectionByQuery(qbc);
080        }
081    
082        /**
083         * @see org.kuali.kfs.module.bc.document.dataaccess.BudgetConstructionOrganizationReportsDao#getActiveChildOrgs(java.lang.String,
084         *      java.lang.String)
085         */
086        public List getActiveChildOrgs(String chartOfAccountsCode, String organizationCode) {
087            List orgs = new ArrayList();
088            Criteria cycleCheckCriteria = new Criteria();
089            cycleCheckCriteria.addEqualToField("chartOfAccountsCode", "reportsToChartOfAccountsCode");
090            cycleCheckCriteria.addEqualToField("organizationCode", "reportsToOrganizationCode");
091            cycleCheckCriteria.setEmbraced(true);
092            cycleCheckCriteria.setNegative(true);
093    
094            Criteria criteria = new Criteria();
095            criteria.addEqualTo("reportsToChartOfAccountsCode", chartOfAccountsCode);
096            criteria.addEqualTo("reportsToOrganizationCode", organizationCode);
097            criteria.addAndCriteria(cycleCheckCriteria);
098            criteria.addEqualTo("organization.active", Boolean.TRUE);
099            orgs = (List) getPersistenceBrokerTemplate().getCollectionByQuery(QueryFactory.newQuery(BudgetConstructionOrganizationReports.class, criteria));
100    
101            if (orgs.isEmpty() || orgs.size() == 0) {
102                return Collections.EMPTY_LIST;
103            }
104            return orgs;
105        }
106    
107        /**
108         * @see org.kuali.kfs.module.bc.document.dataaccess.BudgetConstructionOrganizationReportsDao#isLeafOrg(java.lang.String, java.lang.String)
109         */
110        public boolean isLeafOrg(String chartOfAccountsCode, String organizationCode) {
111    
112            Criteria childExistsCriteria = new Criteria();
113            childExistsCriteria.addEqualTo("reportsToChartOfAccountsCode", chartOfAccountsCode);
114            childExistsCriteria.addEqualTo("reportsToOrganizationCode", organizationCode);
115            childExistsCriteria.addEqualTo("organization.active", Boolean.TRUE);
116    
117            QueryByCriteria childExistsQuery = QueryFactory.newQuery(BudgetConstructionOrganizationReports.class, childExistsCriteria);
118    
119            Criteria criteria = new Criteria();
120            criteria.addEqualTo("chartOfAccountsCode", chartOfAccountsCode);
121            criteria.addEqualTo("organizationCode", organizationCode);
122    
123            criteria.addExists(childExistsQuery);
124    
125            String[] queryAttr = { KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE };
126    
127            ReportQueryByCriteria query = new ReportQueryByCriteria(BudgetConstructionOrganizationReports.class, queryAttr, criteria, true);
128            Iterator rowsReturned = getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(query);
129            if (rowsReturned.hasNext()) {
130                TransactionalServiceUtils.exhaustIterator(rowsReturned);
131                return false;
132            }
133            else {
134                return true;
135            }
136        }
137    }