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.purap.dataaccess.impl;
017    
018    import java.util.ArrayList;
019    import java.util.Collection;
020    import java.util.HashMap;
021    import java.util.List;
022    import java.util.Map;
023    
024    import org.apache.ojb.broker.query.Criteria;
025    import org.apache.ojb.broker.query.QueryByCriteria;
026    import org.apache.ojb.broker.query.QueryFactory;
027    import org.kuali.kfs.module.purap.PurapPropertyConstants;
028    import org.kuali.kfs.module.purap.businessobject.AccountsPayableSummaryAccount;
029    import org.kuali.kfs.module.purap.businessobject.PurApItem;
030    import org.kuali.kfs.module.purap.businessobject.PurchaseOrderItem;
031    import org.kuali.kfs.module.purap.dataaccess.PurApAccountingDao;
032    import org.kuali.rice.kns.dao.impl.PlatformAwareDaoBaseOjb;
033    import org.kuali.rice.kns.service.BusinessObjectService;
034    
035    /**
036     * OJB Implementation of PurApAccountingDao.
037     */
038    public class PurApAccountingDaoOjb extends PlatformAwareDaoBaseOjb implements PurApAccountingDao {
039    
040        private BusinessObjectService businessObjectService;
041        
042        public void setBusinessObjectService(BusinessObjectService businessObjectService) {
043            this.businessObjectService = businessObjectService;
044        }
045    
046        /**
047         * @see org.kuali.kfs.module.purap.dataaccess.PurApAccountingDao#getAccountingLinesForItem(org.kuali.kfs.module.purap.businessobject.PurApItem)
048         */
049        public List getAccountingLinesForItem(PurApItem item) {
050            Class clazz = item.getAccountingLineClass();
051            Criteria criteria = new Criteria();
052            criteria.addEqualTo("itemIdentifier", item.getItemIdentifier());
053            
054            // if it's a purchaseOrderItem, we need to make sure we're getting for the right doc number
055            if (item instanceof PurchaseOrderItem) {
056                criteria.addEqualTo("documentNumber", ((PurchaseOrderItem) item).getDocumentNumber());
057            }
058    
059            QueryByCriteria query = QueryFactory.newQuery(clazz, criteria);
060            Collection lines = getPersistenceBrokerTemplate().getCollectionByQuery(query);
061    
062            return new ArrayList(lines);
063    
064        }
065    
066        /**
067         * @see org.kuali.kfs.module.purap.dataaccess.PurApAccountingDao#deleteSummaryAccountsbyPaymentRequestIdentifier(java.lang.Integer)
068         */
069        public void deleteSummaryAccountsbyPaymentRequestIdentifier(Integer paymentRequestIdentifier) {
070            if (paymentRequestIdentifier != null) {
071                Criteria criteria = new Criteria();
072                criteria.addEqualTo(PurapPropertyConstants.PAYMENT_REQUEST_ID, paymentRequestIdentifier);
073    
074                getPersistenceBrokerTemplate().deleteByQuery(QueryFactory.newQuery(AccountsPayableSummaryAccount.class, criteria));
075                getPersistenceBrokerTemplate().clearCache();
076            }
077        }
078        
079        /**
080         * @see org.kuali.kfs.module.purap.dataaccess.PurApAccountingDao#deleteSummaryAccountsbyCreditMemoIdentifier(java.lang.Integer)
081         */
082        public void deleteSummaryAccountsbyCreditMemoIdentifier(Integer creditMemoIdentifier) {
083            if (creditMemoIdentifier != null) {
084                Criteria criteria = new Criteria();
085                criteria.addEqualTo(PurapPropertyConstants.CREDIT_MEMO_ID, creditMemoIdentifier);
086    
087                getPersistenceBrokerTemplate().deleteByQuery(QueryFactory.newQuery(AccountsPayableSummaryAccount.class, criteria));
088                getPersistenceBrokerTemplate().clearCache();
089            }
090        }
091        
092        /**
093         * @see org.kuali.kfs.module.purap.dataaccess.PurApAccountingDao#getSummaryAccountsbyPaymentRequestIdentifier(java.lang.Integer)
094         */
095        public List getSummaryAccountsbyPaymentRequestIdentifier(Integer paymentRequestIdentifier) {
096            if (paymentRequestIdentifier != null) {
097                Map fieldValues = new HashMap();
098                fieldValues.put(PurapPropertyConstants.PAYMENT_REQUEST_ID, paymentRequestIdentifier);
099                return new ArrayList(businessObjectService.findMatching(AccountsPayableSummaryAccount.class, fieldValues));
100            }
101            return null;
102        }
103        
104        /**
105         * @see org.kuali.kfs.module.purap.dataaccess.PurApAccountingDao#getSummaryAccountsbyCreditMemoIdentifier(java.lang.Integer)
106         */
107        public List getSummaryAccountsbyCreditMemoIdentifier(Integer creditMemoIdentifier) {
108            if (creditMemoIdentifier != null) {
109                Map fieldValues = new HashMap();
110                fieldValues.put(PurapPropertyConstants.CREDIT_MEMO_ID, creditMemoIdentifier);
111                return new ArrayList(businessObjectService.findMatching(AccountsPayableSummaryAccount.class, fieldValues));
112            }
113            return null;
114        }
115        
116    }