View Javadoc

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