View Javadoc
1   /*
2    * Copyright 2006 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.module.purap.document.dataaccess.impl;
17  
18  import org.apache.ojb.broker.query.Criteria;
19  import org.apache.ojb.broker.query.ReportQueryByCriteria;
20  import org.kuali.ole.module.purap.PurapPropertyConstants;
21  import org.kuali.ole.module.purap.document.RequisitionDocument;
22  import org.kuali.ole.module.purap.document.dataaccess.RequisitionDao;
23  import org.kuali.ole.sys.OLEPropertyConstants;
24  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
25  import org.springframework.transaction.annotation.Transactional;
26  
27  import java.util.List;
28  
29  /**
30   * OJB implementation of RequisitionDao.
31   */
32  @Transactional
33  public class RequisitionDaoOjb extends PlatformAwareDaoBaseOjb implements RequisitionDao {
34      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RequisitionDaoOjb.class);
35  
36      /**
37       * @see org.kuali.ole.module.purap.document.dataaccess.RequisitionDao#getDocumentNumberForRequisitionId(java.lang.Integer)
38       */
39      public String getDocumentNumberForRequisitionId(Integer id) {
40          Criteria criteria = new Criteria();
41          criteria.addEqualTo(PurapPropertyConstants.PURAP_DOC_ID, id);
42  
43          ReportQueryByCriteria rqbc = new ReportQueryByCriteria(RequisitionDocument.class, criteria);
44          rqbc.addOrderByAscending(OLEPropertyConstants.DOCUMENT_NUMBER);
45  
46          List<RequisitionDocument> reqs = (List<RequisitionDocument>) getPersistenceBrokerTemplate().getCollectionByQuery(rqbc);
47  
48          if (reqs.isEmpty()) {
49              return null;
50          }
51          if (reqs.size() > 1) {
52              String errorMsg = "Expected single document number for given criteria but multiple (at least 2) were returned";
53              LOG.error(errorMsg);
54              throw new RuntimeException();
55          } else {
56              RequisitionDocument req = reqs.get(0);
57              return req.getDocumentNumber();
58          }
59      }
60  }