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