001/*
002 * The Kuali Financial System, a comprehensive financial management system for higher education.
003 * 
004 * Copyright 2005-2014 The Kuali Foundation
005 * 
006 * This program is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU Affero General Public License as
008 * published by the Free Software Foundation, either version 3 of the
009 * License, or (at your option) any later version.
010 * 
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU Affero General Public License for more details.
015 * 
016 * You should have received a copy of the GNU Affero General Public License
017 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
018 */
019package org.kuali.kfs.module.cab.batch.dataaccess.impl;
020
021import java.math.BigDecimal;
022import java.util.Iterator;
023
024import org.apache.ojb.broker.query.Criteria;
025import org.apache.ojb.broker.query.QueryFactory;
026import org.apache.ojb.broker.query.ReportQueryByCriteria;
027import org.kuali.kfs.module.cab.CabPropertyConstants;
028import org.kuali.kfs.module.cab.batch.dataaccess.PurchasingAccountsPayableItemAssetDao;
029import org.kuali.kfs.module.cab.businessobject.PurchasingAccountsPayableItemAsset;
030import org.kuali.kfs.sys.util.TransactionalServiceUtils;
031import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
032
033public class PurchasingAccountsPayableItemAssetDaoOjb extends PlatformAwareDaoBaseOjb implements PurchasingAccountsPayableItemAssetDao {
034
035    public Integer findMaxCabLineNumber(String documentNumber, Integer accountsPayableLineItemIdentifier) {
036        Criteria criteria = new Criteria();
037        criteria.addEqualTo(CabPropertyConstants.PurchasingAccountsPayableItemAsset.DOCUMENT_NUMBER, documentNumber);
038        criteria.addEqualTo(CabPropertyConstants.PurchasingAccountsPayableItemAsset.ACCOUNTS_PAYABLE_LINE_ITEM_IDENTIFIER, accountsPayableLineItemIdentifier);
039
040        ReportQueryByCriteria query = QueryFactory.newReportQuery(PurchasingAccountsPayableItemAsset.class, criteria);
041
042        query.setAttributes(new String[] { "max(" + CabPropertyConstants.PurchasingAccountsPayableItemAsset.CAPITAL_ASSET_BUILDER_LINE_NUMBER + ")" });
043        Iterator<?> iterator = getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(query);
044        Integer maxSequenceNumber = Integer.valueOf(0);
045
046        if (iterator.hasNext()) {
047            Object[] data = (Object[]) TransactionalServiceUtils.retrieveFirstAndExhaustIterator(iterator);
048            if (data[0] != null) {
049                maxSequenceNumber = ((BigDecimal) data[0]).intValue();
050            }
051        }
052        return maxSequenceNumber;
053    }
054}