001/*
002 * Created on Mar 2, 2006
003 *
004 */
005package org.kuali.ole.module.purap.dataaccess.impl;
006
007import org.apache.ojb.broker.query.Criteria;
008import org.apache.ojb.broker.query.QueryByCriteria;
009import org.kuali.ole.module.purap.PurapConstants;
010import org.kuali.ole.module.purap.businessobject.ElectronicInvoiceItemMapping;
011import org.kuali.ole.module.purap.businessobject.ElectronicInvoiceLoadSummary;
012import org.kuali.ole.module.purap.dataaccess.ElectronicInvoicingDao;
013import org.kuali.ole.module.purap.document.PaymentRequestDocument;
014import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
015
016import java.util.HashMap;
017import java.util.Iterator;
018import java.util.List;
019import java.util.Map;
020
021public class ElectronicInvoicingDaoOjb extends PlatformAwareDaoBaseOjb implements ElectronicInvoicingDao {
022
023    private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ElectronicInvoicingDaoOjb.class);
024
025    public ElectronicInvoicingDaoOjb() {
026        super();
027    }
028
029    public ElectronicInvoiceLoadSummary getElectronicInvoiceLoadSummary(Integer loadId, String vendorDunsNumber) {
030        LOG.debug("getElectronicInvoiceLoadSummary() started");
031
032        Criteria criteria = new Criteria();
033        criteria.addEqualTo("id", loadId);
034        criteria.addEqualTo("vendorDunsNumber", vendorDunsNumber);
035
036        return (ElectronicInvoiceLoadSummary) getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(ElectronicInvoiceLoadSummary.class, criteria));
037    }
038
039    public List getPendingElectronicInvoices() {
040        LOG.debug("getPendingElectronicInvoices() started");
041
042        Criteria criteria = new Criteria();
043        criteria.addEqualTo("status.code", PurapConstants.PaymentRequestStatuses.APPDOC_PENDING_E_INVOICE);
044        criteria.addEqualTo("isElectronicInvoice", Boolean.TRUE);
045        List invoices = (List) getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(PaymentRequestDocument.class, criteria));
046        for (Iterator iter = invoices.iterator(); iter.hasNext(); ) {
047            PaymentRequestDocument p = (PaymentRequestDocument) iter.next();
048        }
049
050        return invoices;
051    }
052
053    public Map getDefaultItemMappingMap() {
054        LOG.debug("getDefaultItemMappingMap() started");
055        Criteria criteria = new Criteria();
056        criteria.addIsNull("vendorHeaderGeneratedIdentifier");
057        criteria.addIsNull("vendorDetailAssignedIdentifier");
058        criteria.addEqualTo("active", true);
059        return this.getItemMappingMap(criteria);
060    }
061
062    public Map getItemMappingMap(Integer vendorHeaderId, Integer vendorDetailId) {
063        if (LOG.isDebugEnabled()) {
064            LOG.debug("getItemMappingMap() started for vendor id " + vendorHeaderId + "-" + vendorDetailId);
065        }
066        Criteria criteria = new Criteria();
067        criteria.addEqualTo("vendorHeaderGeneratedIdentifier", vendorHeaderId);
068        criteria.addEqualTo("vendorDetailAssignedIdentifier", vendorDetailId);
069        criteria.addEqualTo("active", true);
070        return this.getItemMappingMap(criteria);
071    }
072
073    protected Map getItemMappingMap(Criteria criteria) {
074        Map hm = new HashMap();
075        List itemMappings = (List) getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ElectronicInvoiceItemMapping.class, criteria));
076
077        for (Iterator iter = itemMappings.iterator(); iter.hasNext(); ) {
078            ElectronicInvoiceItemMapping mapping = (ElectronicInvoiceItemMapping) iter.next();
079            hm.put(mapping.getInvoiceItemTypeCode(), mapping);
080        }
081        return hm;
082    }
083}