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.sys.dataaccess.impl;
017    
018    import java.util.ArrayList;
019    import java.util.Collection;
020    
021    import org.apache.log4j.Logger;
022    import org.apache.ojb.broker.metadata.MetadataManager;
023    import org.apache.ojb.broker.query.Criteria;
024    import org.apache.ojb.broker.query.Query;
025    import org.apache.ojb.broker.query.QueryByCriteria;
026    import org.apache.ojb.broker.query.QueryFactory;
027    import org.kuali.kfs.coa.dataaccess.impl.ChartDaoOjb;
028    import org.kuali.kfs.sys.KFSConstants;
029    import org.kuali.kfs.sys.businessobject.AccountingLine;
030    import org.kuali.kfs.sys.businessobject.SourceAccountingLine;
031    import org.kuali.kfs.sys.businessobject.TargetAccountingLine;
032    import org.kuali.kfs.sys.dataaccess.AccountingLineDao;
033    import org.kuali.rice.kns.dao.impl.PlatformAwareDaoBaseOjb;
034    import org.springframework.dao.DataAccessException;
035    
036    /**
037     * This class is the OJB implementation of the AccountingLineDao interface.
038     */
039    
040    public class AccountingLineDaoOjb extends PlatformAwareDaoBaseOjb implements AccountingLineDao {
041        private static Logger LOG = Logger.getLogger(ChartDaoOjb.class);
042    
043        /**
044         * Default constructor.
045         */
046        public AccountingLineDaoOjb() {
047            super();
048        }
049    
050        /**
051         * Saves an accounting line to the DB using OJB.
052         * 
053         * @param line
054         */
055        public void save(AccountingLine line) throws DataAccessException {
056            getPersistenceBrokerTemplate().store(line);
057        }
058    
059        /**
060         * Deletes an accounting line from the DB using OJB.
061         */
062        public void deleteAccountingLine(AccountingLine line) throws DataAccessException {
063            getPersistenceBrokerTemplate().delete(line);
064        }
065    
066        /**
067         * Retrieves accounting lines associate with a given document header ID using OJB.
068         * 
069         * @param classname
070         * @param id
071         * @return
072         */
073        public ArrayList findByDocumentHeaderId(Class clazz, String documentHeaderId) throws DataAccessException {
074            Criteria criteria = new Criteria();
075            criteria.addEqualTo("FDOC_NBR", documentHeaderId);
076            if (MetadataManager.getInstance().getRepository().getDescriptorFor(clazz).getFieldDescriptorByName("financialDocumentLineTypeCode") != null) {
077                if (SourceAccountingLine.class.isAssignableFrom(clazz)) {
078                    criteria.addEqualTo("FDOC_LN_TYP_CD", KFSConstants.SOURCE_ACCT_LINE_TYPE_CODE);
079                }
080                else if (TargetAccountingLine.class.isAssignableFrom(clazz)) {
081                    criteria.addEqualTo("FDOC_LN_TYP_CD", KFSConstants.TARGET_ACCT_LINE_TYPE_CODE);
082                }
083            }
084    
085            QueryByCriteria query = QueryFactory.newQuery(clazz, criteria);
086            Collection lines = findCollection(query);
087    
088            return new ArrayList(lines);
089        }
090    
091        /**
092         * Retrieve a Collection of Document instances found by a query.
093         * 
094         * @param query
095         * @return
096         */
097        protected Collection findCollection(Query query) throws DataAccessException {
098            return getPersistenceBrokerTemplate().getCollectionByQuery(query);
099        }
100    }