View Javadoc

1   /**
2    * Copyright 2005-2011 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.rice.krad.dao.impl;
17  
18  import org.apache.log4j.Logger;
19  import org.apache.ojb.broker.query.Criteria;
20  import org.apache.ojb.broker.query.QueryByCriteria;
21  import org.apache.ojb.broker.query.QueryFactory;
22  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
23  import org.kuali.rice.krad.dao.BusinessObjectDao;
24  import org.kuali.rice.krad.dao.DocumentDao;
25  import org.kuali.rice.krad.document.Document;
26  import org.kuali.rice.krad.service.DocumentAdHocService;
27  import org.kuali.rice.krad.service.KRADServiceLocatorInternal;
28  import org.kuali.rice.krad.util.KRADPropertyConstants;
29  import org.kuali.rice.krad.util.OjbCollectionAware;
30  import org.springframework.dao.DataAccessException;
31  
32  import java.util.ArrayList;
33  import java.util.List;
34  
35  /**
36   * This class is the OJB implementation of the DocumentDao interface.
37   */
38  public class DocumentDaoOjb extends PlatformAwareDaoBaseOjb implements DocumentDao, OjbCollectionAware{
39      private static final Logger LOG = Logger.getLogger(DocumentDaoOjb.class);
40      protected BusinessObjectDao businessObjectDao;
41      protected DocumentAdHocService documentAdHocService;
42  
43  
44      public DocumentDaoOjb(BusinessObjectDao businessObjectDao, DocumentAdHocService documentAdHocService) {
45          super();
46          this.businessObjectDao = businessObjectDao;
47          this.documentAdHocService = documentAdHocService;
48      }
49  
50      /**
51       * @see org.kuali.dao.DocumentDao#save(null)
52       */
53      @Override
54      public <T extends Document> T save(T document) throws DataAccessException {
55      	if ( LOG.isDebugEnabled() ) {
56      		LOG.debug( "About to store document: " + document, new Throwable() );
57      	}
58          Document retrievedDocument = findByDocumentHeaderId(document.getClass(),document.getDocumentNumber());
59          KRADServiceLocatorInternal.getOjbCollectionHelper().processCollections(this, document, retrievedDocument);
60          this.getPersistenceBrokerTemplate().store(document);
61          return document;
62      }
63  
64      /**
65       * Retrieve a Document of a specific type with a given document header ID.
66       *
67       * @param clazz
68       * @param id
69       * @return Document with given id
70       */
71      @Override
72      public <T extends Document> T findByDocumentHeaderId(Class<T> clazz, String id) {
73          List<String> idList = new ArrayList<String>();
74          idList.add(id);
75  
76          List<T> documentList = findByDocumentHeaderIds(clazz, idList);
77  
78          T document = null;
79          if ((null != documentList) && (documentList.size() > 0)) {
80              document = documentList.get(0);
81          }
82  
83          return document;
84      }
85  
86      /**
87       * Retrieve a List of Document instances with the given ids
88       *
89       * @param clazz
90       * @param idList
91       * @return List
92       */
93      @Override
94      public <T extends Document> List<T> findByDocumentHeaderIds(Class<T> clazz, List<String> idList) {
95          Criteria criteria = new Criteria();
96          criteria.addIn(KRADPropertyConstants.DOCUMENT_NUMBER, idList);
97  
98          QueryByCriteria query = QueryFactory.newQuery(clazz, criteria);
99          
100         // this cast is correct because OJB produces a collection which contains elements of the class defined on the query
101         @SuppressWarnings("unchecked")
102         List<T> tempList = new ArrayList<T>(this.getPersistenceBrokerTemplate().getCollectionByQuery(query));
103         
104         for (T doc : tempList) {
105         	documentAdHocService.addAdHocs(doc);
106         }
107         return tempList;
108     }
109 
110     /**
111      * Returns the {@link BusinessObjectDao}
112      * @see org.kuali.rice.krad.dao.DocumentDao#getBusinessObjectDao()
113      * @return the {@link BusinessObjectDao}
114      */
115     @Override
116     public BusinessObjectDao getBusinessObjectDao() {
117         return businessObjectDao;
118     }
119 
120     /**
121      * Sets the {@link BusinessObjectDao}
122      * @param businessObjectDao ths {@link BusinessObjectDao}
123      */
124     public void setBusinessObjectDao(BusinessObjectDao businessObjectDao) {
125         this.businessObjectDao = businessObjectDao;
126     }
127 
128     /**
129 	 * @return the documentAdHocService
130 	 */
131     @Override
132 	public DocumentAdHocService getDocumentAdHocService() {
133 		return this.documentAdHocService;
134 	}
135 
136     /**
137      * Setter for injecting the DocumentAdHocService
138      * @param dahs
139      */
140     public void setDocumentAdHocService(DocumentAdHocService dahs) {
141     	this.documentAdHocService = dahs;
142     }
143 }