View Javadoc

1   /*
2    * Copyright 2005-2008 The Kuali Foundation
3    * 
4    * 
5    * Licensed under the Educational Community License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    * 
9    * http://www.opensource.org/licenses/ecl2.php
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.kuali.rice.kew.notes.dao.impl;
18  
19  import java.util.List;
20  
21  import javax.persistence.EntityManager;
22  import javax.persistence.PersistenceContext;
23  import javax.persistence.Query;
24  
25  import org.kuali.rice.core.util.OrmUtils;
26  import org.kuali.rice.kew.notes.Attachment;
27  import org.kuali.rice.kew.notes.Note;
28  import org.kuali.rice.kew.notes.dao.NoteDAO;
29  
30  
31  public class NoteDAOJpaImpl implements NoteDAO {
32  
33  	@PersistenceContext(unitName="kew-unit")
34  	EntityManager entityManager;
35  	
36      public Note getNoteByNoteId(Long noteId) {
37      	Query query = entityManager.createNamedQuery("KewNote.FindNoteByNoteId");
38      	query.setParameter("noteId", noteId);
39          return (Note) query.getSingleResult();          
40      }
41  
42      public List getNotesByRouteHeaderId(Long routeHeaderId) {
43      	Query query = entityManager.createNamedQuery("KewNote.FindNoteByRouteHeaderId");
44      	query.setParameter("routeHeaderId", routeHeaderId);
45          return (List) query.getResultList();        
46      }
47      
48      public void saveNote(Note note) {
49      	if (note.getNoteId() == null){
50      		entityManager.persist(note);
51      	} else {
52      		entityManager.merge(note);
53      	}
54      }
55  
56      public void deleteNote(Note note) {
57      	Note n = getNoteByNoteId(note.getNoteId());
58      	OrmUtils.merge(entityManager, n);
59      	entityManager.remove(n);
60      }
61     
62      public void deleteAttachment(Attachment attachment) {
63      	Attachment a = findAttachment(attachment.getAttachmentId());
64      	entityManager.remove(a);
65      }
66  
67  	
68      public Attachment findAttachment(Long attachmentId) {
69      	Query query = entityManager.createNamedQuery("Attachment.FindAttachmentById");
70      	query.setParameter("attachmentId", attachmentId);
71      	return (Attachment)query.getSingleResult();
72      }
73  
74      public EntityManager getEntityManager() {
75          return this.entityManager;
76      }
77  
78      public void setEntityManager(EntityManager entityManager) {
79          this.entityManager = entityManager;
80      }
81  
82  }