View Javadoc

1   /**
2    * Copyright 2005-2012 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.kuali.rice.core.framework.persistence.jpa.OrmUtils;
19  import org.kuali.rice.core.framework.persistence.jpa.criteria.Criteria;
20  import org.kuali.rice.core.framework.persistence.jpa.criteria.QueryByCriteria;
21  import org.kuali.rice.krad.dao.BusinessObjectDao;
22  import org.kuali.rice.krad.dao.DocumentDao;
23  import org.kuali.rice.krad.document.Document;
24  import org.kuali.rice.krad.service.DocumentAdHocService;
25  import org.kuali.rice.krad.util.KRADPropertyConstants;
26  import org.springframework.dao.DataAccessException;
27  
28  import javax.persistence.EntityManager;
29  import javax.persistence.PersistenceContext;
30  import javax.persistence.PersistenceException;
31  import java.util.ArrayList;
32  import java.util.List;
33  
34  /**
35   * This class is the OJB implementation of the DocumentDao interface.
36   */
37  public class DocumentDaoJpa implements DocumentDao {
38      
39  	private BusinessObjectDao businessObjectDao;
40      private DocumentAdHocService documentAdHocService;
41  
42  	@PersistenceContext
43  	private EntityManager entityManager;
44  
45      public DocumentDaoJpa(EntityManager entityManager, BusinessObjectDao businessObjectDao, DocumentAdHocService documentAdHocService) {
46          super();
47          this.entityManager = entityManager;
48          this.businessObjectDao = businessObjectDao;
49          this.documentAdHocService = documentAdHocService;
50      }
51  
52      /**
53       * @see org.kuali.dao.DocumentDao#save(null)
54       */
55      @Override
56      public <T extends Document> T save(T document) throws DataAccessException {
57  		T attachedDoc = (T) findByDocumentHeaderId(document.getClass(),document.getDocumentNumber());
58  		if (attachedDoc == null) {
59  			entityManager.persist(document.getDocumentHeader());
60  			entityManager.persist(document);
61  			return document;
62  		}
63  		OrmUtils.reattach(document, attachedDoc);
64  		return entityManager.merge(attachedDoc);
65      }
66  
67  	/**
68       * Retrieve a Document of a specific type with a given document header ID.
69       *
70       * @param clazz
71       * @param id
72       * @return Document with given id
73       */
74      @Override
75      public <T extends Document> T findByDocumentHeaderId(Class<T> clazz, String id) {
76          return entityManager.find(clazz, id);
77      }
78  
79      /**
80       * Retrieve a List of Document instances with the given ids
81       *
82       * @param clazz
83       * @param idList
84       * @return List
85       */
86      @Override
87      public <T extends Document> List<T> findByDocumentHeaderIds(Class<T> clazz, List<String> idList) {
88  		Criteria criteria = new Criteria(clazz.getName());
89  		criteria.in(KRADPropertyConstants.DOCUMENT_NUMBER, idList);
90  		List<T> list = new ArrayList<T>();
91  		try {
92  			list = new QueryByCriteria(entityManager, criteria).toQuery().getResultList();
93  		} catch (PersistenceException e) {
94  			e.printStackTrace();
95  		}
96          for (T doc : list) {
97          	documentAdHocService.addAdHocs(doc);
98  			entityManager.refresh(doc);
99          }
100 		return list;
101     }
102 
103     @Override
104     public BusinessObjectDao getBusinessObjectDao() {
105         return businessObjectDao;
106     }
107 
108     public void setBusinessObjectDao(BusinessObjectDao businessObjectDao) {
109         this.businessObjectDao = businessObjectDao;
110     }
111 
112     /**
113      * @return the entityManager
114      */
115     public EntityManager getEntityManager() {
116         return this.entityManager;
117     }
118 
119     /**
120      * @param entityManager the entityManager to set
121      */
122     public void setEntityManager(EntityManager entityManager) {
123         this.entityManager = entityManager;
124     }
125 
126     /**
127 	 * @return the documentAdHocService
128 	 */
129     @Override
130 	public DocumentAdHocService getDocumentAdHocService() {
131 		return this.documentAdHocService;
132 	}
133 
134     /**
135      * Setter for injecting the DocumentAdHocService
136      * @param dahs
137      */
138     public void setDocumentAdHocService(DocumentAdHocService dahs) {
139     	this.documentAdHocService = dahs;
140     }
141 
142 }