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.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.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      /**
56       * @see org.kuali.dao.DocumentDao#save(null)
57       */
58      @Override
59      public <T extends Document> T save(T document) throws DataAccessException {
60      	if ( LOG.isDebugEnabled() ) {
61      		LOG.debug( "About to store document: " + document, new Throwable() );
62      	}
63          Document retrievedDocument = findByDocumentHeaderId(document.getClass(),document.getDocumentNumber());
64          getOjbCollectionHelper().processCollections(this, document, retrievedDocument);
65          this.getPersistenceBrokerTemplate().store(document);
66          return document;
67      }
68  
69      /**
70       * Retrieve a Document of a specific type with a given document header ID.
71       *
72       * @param clazz
73       * @param id
74       * @return Document with given id
75       */
76      @Override
77      public <T extends Document> T findByDocumentHeaderId(Class<T> clazz, String id) {
78          List<String> idList = new ArrayList<String>();
79          idList.add(id);
80  
81          List<T> documentList = findByDocumentHeaderIds(clazz, idList);
82  
83          T document = null;
84          if ((null != documentList) && (documentList.size() > 0)) {
85              document = documentList.get(0);
86          }
87  
88          return document;
89      }
90  
91      /**
92       * Retrieve a List of Document instances with the given ids
93       *
94       * @param clazz
95       * @param idList
96       * @return List
97       */
98      @Override
99      public <T extends Document> List<T> findByDocumentHeaderIds(Class<T> clazz, List<String> idList) {
100         Criteria criteria = new Criteria();
101         criteria.addIn(KRADPropertyConstants.DOCUMENT_NUMBER, idList);
102 
103         QueryByCriteria query = QueryFactory.newQuery(clazz, criteria);
104         
105         // this cast is correct because OJB produces a collection which contains elements of the class defined on the query
106         @SuppressWarnings("unchecked")
107         List<T> tempList = new ArrayList<T>(this.getPersistenceBrokerTemplate().getCollectionByQuery(query));
108         
109         for (T doc : tempList) {
110         	documentAdHocService.addAdHocs(doc);
111         }
112         return tempList;
113     }
114 
115     /**
116      * Returns the {@link BusinessObjectDao}
117      * @see org.kuali.rice.krad.dao.DocumentDao#getBusinessObjectDao()
118      * @return the {@link BusinessObjectDao}
119      */
120     @Override
121     public BusinessObjectDao getBusinessObjectDao() {
122         return businessObjectDao;
123     }
124 
125     /**
126      * Sets the {@link BusinessObjectDao}
127      * @param businessObjectDao ths {@link BusinessObjectDao}
128      */
129     public void setBusinessObjectDao(BusinessObjectDao businessObjectDao) {
130         this.businessObjectDao = businessObjectDao;
131     }
132 
133     /**
134 	 * @return the documentAdHocService
135 	 */
136     @Override
137 	public DocumentAdHocService getDocumentAdHocService() {
138 		return this.documentAdHocService;
139 	}
140 
141     /**
142      * Setter for injecting the DocumentAdHocService
143      * @param dahs
144      */
145     public void setDocumentAdHocService(DocumentAdHocService dahs) {
146     	this.documentAdHocService = dahs;
147     }
148 
149     protected OjbCollectionHelper getOjbCollectionHelper() {
150         if (ojbCollectionHelper == null) {
151             ojbCollectionHelper = KRADServiceLocatorInternal.getOjbCollectionHelper();
152         }
153 
154         return ojbCollectionHelper;
155     }
156 
157     public void setOjbCollectionHelper(OjbCollectionHelper ojbCollectionHelper) {
158         this.ojbCollectionHelper = ojbCollectionHelper;
159     }
160 }