001/*
002 * Copyright 2006 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 */
016package org.kuali.ole.module.purap.document.dataaccess.impl;
017
018import org.apache.ojb.broker.query.Criteria;
019import org.apache.ojb.broker.query.ReportQueryByCriteria;
020import org.kuali.ole.module.purap.PurapPropertyConstants;
021import org.kuali.ole.module.purap.document.RequisitionDocument;
022import org.kuali.ole.module.purap.document.dataaccess.RequisitionDao;
023import org.kuali.ole.sys.OLEPropertyConstants;
024import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
025import org.springframework.transaction.annotation.Transactional;
026
027import java.util.List;
028
029/**
030 * OJB implementation of RequisitionDao.
031 */
032@Transactional
033public class RequisitionDaoOjb extends PlatformAwareDaoBaseOjb implements RequisitionDao {
034    private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RequisitionDaoOjb.class);
035
036    /**
037     * @see org.kuali.ole.module.purap.document.dataaccess.RequisitionDao#getDocumentNumberForRequisitionId(java.lang.Integer)
038     */
039    public String getDocumentNumberForRequisitionId(Integer id) {
040        Criteria criteria = new Criteria();
041        criteria.addEqualTo(PurapPropertyConstants.PURAP_DOC_ID, id);
042
043        ReportQueryByCriteria rqbc = new ReportQueryByCriteria(RequisitionDocument.class, criteria);
044        rqbc.addOrderByAscending(OLEPropertyConstants.DOCUMENT_NUMBER);
045
046        List<RequisitionDocument> reqs = (List<RequisitionDocument>) getPersistenceBrokerTemplate().getCollectionByQuery(rqbc);
047
048        if (reqs.isEmpty()) {
049            return null;
050        }
051        if (reqs.size() > 1) {
052            String errorMsg = "Expected single document number for given criteria but multiple (at least 2) were returned";
053            LOG.error(errorMsg);
054            throw new RuntimeException();
055        } else {
056            RequisitionDocument req = reqs.get(0);
057            return req.getDocumentNumber();
058        }
059    }
060}