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.krad.dao.impl;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import javax.persistence.EntityManager;
22  import javax.persistence.PersistenceContext;
23  
24  import org.kuali.rice.core.framework.persistence.jpa.criteria.Criteria;
25  import org.kuali.rice.core.framework.persistence.jpa.criteria.QueryByCriteria;
26  import org.kuali.rice.krad.bo.Attachment;
27  import org.kuali.rice.krad.bo.Note;
28  import org.kuali.rice.krad.dao.NoteDao;
29  import org.springframework.dao.DataAccessException;
30  
31  /**
32   * This class is the JPA implementation of the NoteDao interface.
33   * 
34   * @author Kuali Rice Team (rice.collab@kuali.org)
35   */
36  public class NoteDaoJpa implements NoteDao {
37  
38  	@PersistenceContext
39  	private EntityManager entityManager;
40  
41  	/**
42  	 * Saves a note to the DB using JPA.
43  	 */
44  	public void save(Note note) throws DataAccessException {
45  		if (note != null && note.getNoteIdentifier() == null && note.getAttachment() != null) {
46  			Attachment attachment = note.getAttachment();
47  			note.setAttachment(null);
48  			// store without attachment
49  			entityManager.merge(note);
50  			attachment.setNoteIdentifier(note.getNoteIdentifier());
51  			// put attachment back
52  			note.setAttachment(attachment);
53  		}
54  		entityManager.merge(note);
55  		
56  	}
57  
58  	/**
59  	 * Deletes a note from the DB using JPA.
60  	 */
61  	public void deleteNote(Note note) throws DataAccessException {
62  		entityManager.remove(note.getAttachment());
63  		note.setAttachment(null);
64  		entityManager.remove(note);
65  	}
66  
67  	/**
68  	 * Retrieves document associated with a given object using JPA.
69  	 */
70  	public List<Note> findByremoteObjectId(String remoteObjectId) {
71  		Criteria criteria = new Criteria(Note.class.getName());
72  		// TODO: Notes - Chris move remoteObjectId string to constants
73  		criteria.eq("remoteObjectIdentifier", remoteObjectId);
74  		criteria.orderBy("notePostedTimestamp", true);
75  		return new ArrayList<Note>(new QueryByCriteria(entityManager, criteria).toQuery().getResultList());
76  	}
77  	
78  	 public Note getNoteByNoteId(Long noteId) {
79  		 Criteria criteria = new Criteria(Note.class.getName());
80  		 criteria.eq("noteIdentifier", noteId);
81  	     return (Note) new QueryByCriteria(entityManager, criteria).toQuery().getSingleResult();
82  	    }
83  
84      /**
85       * @return the entityManager
86       */
87      public EntityManager getEntityManager() {
88          return this.entityManager;
89      }
90  
91      /**
92       * @param entityManager the entityManager to set
93       */
94      public void setEntityManager(EntityManager entityManager) {
95          this.entityManager = entityManager;
96      }
97  
98  }