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.cab.batch.dataaccess.impl;
017    
018    import java.math.BigDecimal;
019    import java.util.Iterator;
020    
021    import org.apache.ojb.broker.query.Criteria;
022    import org.apache.ojb.broker.query.QueryFactory;
023    import org.apache.ojb.broker.query.ReportQueryByCriteria;
024    import org.kuali.kfs.module.cab.CabPropertyConstants;
025    import org.kuali.kfs.module.cab.batch.dataaccess.PurchasingAccountsPayableItemAssetDao;
026    import org.kuali.kfs.module.cab.businessobject.PurchasingAccountsPayableItemAsset;
027    import org.kuali.rice.kns.dao.impl.PlatformAwareDaoBaseOjb;
028    import org.kuali.rice.kns.util.TransactionalServiceUtils;
029    
030    public class PurchasingAccountsPayableItemAssetDaoOjb extends PlatformAwareDaoBaseOjb implements PurchasingAccountsPayableItemAssetDao {
031    
032        public Integer findMaxCabLineNumber(String documentNumber, Integer accountsPayableLineItemIdentifier) {
033            Criteria criteria = new Criteria();
034            criteria.addEqualTo(CabPropertyConstants.PurchasingAccountsPayableItemAsset.DOCUMENT_NUMBER, documentNumber);
035            criteria.addEqualTo(CabPropertyConstants.PurchasingAccountsPayableItemAsset.ACCOUNTS_PAYABLE_LINE_ITEM_IDENTIFIER, accountsPayableLineItemIdentifier);
036    
037            ReportQueryByCriteria query = QueryFactory.newReportQuery(PurchasingAccountsPayableItemAsset.class, criteria);
038    
039            query.setAttributes(new String[] { "max(" + CabPropertyConstants.PurchasingAccountsPayableItemAsset.CAPITAL_ASSET_BUILDER_LINE_NUMBER + ")" });
040            Iterator<?> iterator = getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(query);
041            Integer maxSequenceNumber = Integer.valueOf(0);
042    
043            if (iterator.hasNext()) {
044                Object[] data = (Object[]) TransactionalServiceUtils.retrieveFirstAndExhaustIterator(iterator);
045                if (data[0] != null) {
046                    maxSequenceNumber = ((BigDecimal) data[0]).intValue();
047                }
048            }
049            return maxSequenceNumber;
050        }
051    }