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.kew.notes.dao.impl;
017
018import java.util.List;
019
020import javax.persistence.EntityManager;
021import javax.persistence.PersistenceContext;
022import javax.persistence.Query;
023
024import org.kuali.rice.core.framework.persistence.jpa.OrmUtils;
025import org.kuali.rice.kew.notes.Attachment;
026import org.kuali.rice.kew.notes.Note;
027import org.kuali.rice.kew.notes.dao.NoteDAO;
028
029
030public class NoteDAOJpaImpl implements NoteDAO {
031
032        @PersistenceContext(unitName="kew-unit")
033        EntityManager entityManager;
034        
035    public Note getNoteByNoteId(String noteId) {
036        Query query = entityManager.createNamedQuery("KewNote.FindNoteByNoteId");
037        query.setParameter("noteId", noteId);
038        return (Note) query.getSingleResult();          
039    }
040
041    public List getNotesByDocumentId(String documentId) {
042        Query query = entityManager.createNamedQuery("KewNote.FindNoteByDocumentId");
043        query.setParameter("documentId", documentId);
044        return (List) query.getResultList();        
045    }
046    
047    public void saveNote(Note note) {
048        if (note.getNoteId() == null){
049                entityManager.persist(note);
050        } else {
051                entityManager.merge(note);
052        }
053    }
054
055    public void deleteNote(Note note) {
056        Note n = getNoteByNoteId(note.getNoteId());
057        OrmUtils.merge(entityManager, n);
058        entityManager.remove(n);
059    }
060   
061    public void deleteAttachment(Attachment attachment) {
062        Attachment a = findAttachment(attachment.getAttachmentId());
063        entityManager.remove(a);
064    }
065
066        
067    public Attachment findAttachment(String attachmentId) {
068        Query query = entityManager.createNamedQuery("Attachment.FindAttachmentById");
069        query.setParameter("attachmentId", attachmentId);
070        return (Attachment)query.getSingleResult();
071    }
072
073    public EntityManager getEntityManager() {
074        return this.entityManager;
075    }
076
077    public void setEntityManager(EntityManager entityManager) {
078        this.entityManager = entityManager;
079    }
080
081}