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.document.dataaccess.impl;
017    
018    import java.util.Iterator;
019    
020    import org.apache.ojb.broker.query.Criteria;
021    import org.apache.ojb.broker.query.ReportQueryByCriteria;
022    import org.kuali.kfs.module.purap.PurapPropertyConstants;
023    import org.kuali.kfs.module.purap.document.RequisitionDocument;
024    import org.kuali.kfs.module.purap.document.dataaccess.RequisitionDao;
025    import org.kuali.kfs.sys.KFSPropertyConstants;
026    import org.kuali.rice.kns.dao.impl.PlatformAwareDaoBaseOjb;
027    import org.kuali.rice.kns.util.TransactionalServiceUtils;
028    
029    /**
030     * OJB implementation of RequisitionDao.
031     */
032    public class RequisitionDaoOjb extends PlatformAwareDaoBaseOjb implements RequisitionDao {
033        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RequisitionDaoOjb.class);
034    
035        /**
036         * @see org.kuali.kfs.module.purap.document.dataaccess.RequisitionDao#getDocumentNumberForRequisitionId(java.lang.Integer)
037         */
038        public String getDocumentNumberForRequisitionId(Integer id) {
039            Criteria criteria = new Criteria();
040            criteria.addEqualTo(PurapPropertyConstants.PURAP_DOC_ID, id);
041    
042            ReportQueryByCriteria rqbc = new ReportQueryByCriteria(RequisitionDocument.class, criteria);
043            rqbc.setAttributes(new String[] { KFSPropertyConstants.DOCUMENT_NUMBER });
044            rqbc.addOrderByAscending(KFSPropertyConstants.DOCUMENT_NUMBER);
045            Iterator iter = getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(rqbc);
046            if (iter.hasNext()) {
047                Object[] cols = (Object[]) iter.next();
048                if (iter.hasNext()) {
049                    // the iterator should have held only a single doc id of data but it holds 2 or more
050                    String errorMsg = "Expected single document number for given criteria but multiple (at least 2) were returned";
051                    LOG.error(errorMsg);
052                    TransactionalServiceUtils.exhaustIterator(iter);
053                    throw new RuntimeException();
054                }
055                // at this part of the code, we know there's no more elements in iterator
056                return (String) cols[0];
057            }
058            return null;
059        }
060    
061    }