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