View Javadoc

1   /**
2    * Copyright 2005-2014 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.kew.notes.dao.impl;
17  
18  import java.util.List;
19  
20  import javax.persistence.EntityManager;
21  import javax.persistence.PersistenceContext;
22  import javax.persistence.Query;
23  
24  import org.kuali.rice.core.framework.persistence.jpa.OrmUtils;
25  import org.kuali.rice.kew.notes.Attachment;
26  import org.kuali.rice.kew.notes.Note;
27  import org.kuali.rice.kew.notes.dao.NoteDAO;
28  
29  
30  public class NoteDAOJpaImpl implements NoteDAO {
31  
32  	@PersistenceContext(unitName="kew-unit")
33  	EntityManager entityManager;
34  	
35      public Note getNoteByNoteId(String noteId) {
36      	Query query = entityManager.createNamedQuery("KewNote.FindNoteByNoteId");
37      	query.setParameter("noteId", noteId);
38          return (Note) query.getSingleResult();          
39      }
40  
41      public List getNotesByDocumentId(String documentId) {
42      	Query query = entityManager.createNamedQuery("KewNote.FindNoteByDocumentId");
43      	query.setParameter("documentId", documentId);
44          return (List) query.getResultList();        
45      }
46      
47      public void saveNote(Note note) {
48      	if (note.getNoteId() == null){
49      		entityManager.persist(note);
50      	} else {
51      		entityManager.merge(note);
52      	}
53      }
54  
55      public void deleteNote(Note note) {
56      	Note n = getNoteByNoteId(note.getNoteId());
57      	OrmUtils.merge(entityManager, n);
58      	entityManager.remove(n);
59      }
60     
61      public void deleteAttachment(Attachment attachment) {
62      	Attachment a = findAttachment(attachment.getAttachmentId());
63      	entityManager.remove(a);
64      }
65  
66  	
67      public Attachment findAttachment(String attachmentId) {
68      	Query query = entityManager.createNamedQuery("Attachment.FindAttachmentById");
69      	query.setParameter("attachmentId", attachmentId);
70      	return (Attachment)query.getSingleResult();
71      }
72  
73      public EntityManager getEntityManager() {
74          return this.entityManager;
75      }
76  
77      public void setEntityManager(EntityManager entityManager) {
78          this.entityManager = entityManager;
79      }
80  
81  }