001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.dao.impl;
017
018import java.util.ArrayList;
019import java.util.List;
020
021import javax.persistence.EntityManager;
022import javax.persistence.PersistenceContext;
023
024import org.kuali.rice.core.framework.persistence.jpa.criteria.Criteria;
025import org.kuali.rice.core.framework.persistence.jpa.criteria.QueryByCriteria;
026import org.kuali.rice.krad.bo.Attachment;
027import org.kuali.rice.krad.bo.Note;
028import org.kuali.rice.krad.dao.NoteDao;
029import org.springframework.dao.DataAccessException;
030
031/**
032 * This class is the JPA implementation of the NoteDao interface.
033 * 
034 * @author Kuali Rice Team (rice.collab@kuali.org)
035 */
036public class NoteDaoJpa implements NoteDao {
037
038        @PersistenceContext
039        private EntityManager entityManager;
040
041        /**
042         * Saves a note to the DB using JPA.
043         */
044        public void save(Note note) throws DataAccessException {
045                if (note != null && note.getNoteIdentifier() == null && note.getAttachment() != null) {
046                        Attachment attachment = note.getAttachment();
047                        note.setAttachment(null);
048                        // store without attachment
049                        entityManager.merge(note);
050                        attachment.setNoteIdentifier(note.getNoteIdentifier());
051                        // put attachment back
052                        note.setAttachment(attachment);
053                }
054                entityManager.merge(note);
055                
056        }
057
058        /**
059         * Deletes a note from the DB using JPA.
060         */
061        public void deleteNote(Note note) throws DataAccessException {
062                entityManager.remove(note.getAttachment());
063                note.setAttachment(null);
064                entityManager.remove(note);
065        }
066
067        /**
068         * Retrieves document associated with a given object using JPA.
069         */
070        public List<Note> findByremoteObjectId(String remoteObjectId) {
071                Criteria criteria = new Criteria(Note.class.getName());
072                // TODO: Notes - Chris move remoteObjectId string to constants
073                criteria.eq("remoteObjectIdentifier", remoteObjectId);
074                criteria.orderBy("notePostedTimestamp", true);
075                return new ArrayList<Note>(new QueryByCriteria(entityManager, criteria).toQuery().getResultList());
076        }
077        
078         public Note getNoteByNoteId(Long noteId) {
079                 Criteria criteria = new Criteria(Note.class.getName());
080                 criteria.eq("noteIdentifier", noteId);
081             return (Note) new QueryByCriteria(entityManager, criteria).toQuery().getSingleResult();
082            }
083
084    /**
085     * @return the entityManager
086     */
087    public EntityManager getEntityManager() {
088        return this.entityManager;
089    }
090
091    /**
092     * @param entityManager the entityManager to set
093     */
094    public void setEntityManager(EntityManager entityManager) {
095        this.entityManager = entityManager;
096    }
097
098}