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.ld.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.ld.businessobject.LaborGeneralLedgerEntry;
025    import org.kuali.kfs.module.ld.dataaccess.LaborGeneralLedgerEntryDao;
026    import org.kuali.kfs.sys.KFSPropertyConstants;
027    import org.kuali.rice.kns.dao.impl.PlatformAwareDaoBaseOjb;
028    import org.kuali.rice.kns.util.TransactionalServiceUtils;
029    
030    /**
031     * This is the data access object for labor general ledger entry
032     * 
033     * @see org.kuali.kfs.module.ld.businessobject.LaborGeneralLedgerEntry
034     */
035    public class LaborGeneralLedgerEntryDaoOjb extends PlatformAwareDaoBaseOjb implements LaborGeneralLedgerEntryDao {
036    
037        /**
038         * @see org.kuali.kfs.module.ld.dataaccess.LaborGeneralLedgerEntryDao#getMaxSequenceNumber(org.kuali.kfs.module.ld.businessobject.LaborGeneralLedgerEntry)
039         */
040        public Integer getMaxSequenceNumber(LaborGeneralLedgerEntry laborGeneralLedgerEntry) {
041            Criteria criteria = new Criteria();
042    
043            criteria.addEqualTo(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR, laborGeneralLedgerEntry.getUniversityFiscalYear());
044            criteria.addEqualTo(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE, laborGeneralLedgerEntry.getChartOfAccountsCode());
045            criteria.addEqualTo(KFSPropertyConstants.ACCOUNT_NUMBER, laborGeneralLedgerEntry.getAccountNumber());
046            criteria.addEqualTo(KFSPropertyConstants.SUB_ACCOUNT_NUMBER, laborGeneralLedgerEntry.getSubAccountNumber());
047            criteria.addEqualTo(KFSPropertyConstants.FINANCIAL_OBJECT_CODE, laborGeneralLedgerEntry.getFinancialObjectCode());
048            criteria.addEqualTo(KFSPropertyConstants.FINANCIAL_SUB_OBJECT_CODE, laborGeneralLedgerEntry.getFinancialSubObjectCode());
049            criteria.addEqualTo(KFSPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE, laborGeneralLedgerEntry.getFinancialBalanceTypeCode());
050            criteria.addEqualTo(KFSPropertyConstants.FINANCIAL_OBJECT_TYPE_CODE, laborGeneralLedgerEntry.getFinancialObjectTypeCode());
051            criteria.addEqualTo(KFSPropertyConstants.UNIVERSITY_FISCAL_PERIOD_CODE, laborGeneralLedgerEntry.getUniversityFiscalPeriodCode());
052            criteria.addEqualTo(KFSPropertyConstants.FINANCIAL_DOCUMENT_TYPE_CODE, laborGeneralLedgerEntry.getFinancialDocumentTypeCode());
053            criteria.addEqualTo(KFSPropertyConstants.FINANCIAL_SYSTEM_ORIGINATION_CODE, laborGeneralLedgerEntry.getFinancialSystemOriginationCode());
054            criteria.addEqualTo(KFSPropertyConstants.DOCUMENT_NUMBER, laborGeneralLedgerEntry.getDocumentNumber());
055    
056            ReportQueryByCriteria query = QueryFactory.newReportQuery(LaborGeneralLedgerEntry.class, criteria);
057            query.setAttributes(new String[] { "max(" + KFSPropertyConstants.TRANSACTION_ENTRY_SEQUENCE_NUMBER + ")" });
058    
059            Iterator iterator = getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(query);
060            Integer maxSequenceNumber = Integer.valueOf(0);
061    
062            if (iterator.hasNext()) {
063                Object[] data = (Object[]) TransactionalServiceUtils.retrieveFirstAndExhaustIterator(iterator);
064                if (data[0] != null) {
065                    maxSequenceNumber = ((BigDecimal) data[0]).intValue();
066                }
067            }
068            return maxSequenceNumber;
069        }
070    
071        /**
072         * @see org.kuali.kfs.module.ld.dataaccess.LaborGeneralLedgerEntryDao#save(org.kuali.kfs.module.ld.businessobject.LaborGeneralLedgerEntry)
073         */
074        public void save(LaborGeneralLedgerEntry laborGeneralLedgerEntry) {
075            getPersistenceBrokerTemplate().store(laborGeneralLedgerEntry);
076        }
077    }