001/**
002 * Copyright 2005-2017 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.service.impl;
017
018import java.io.File;
019import java.util.Iterator;
020import java.util.List;
021
022import org.kuali.rice.kew.notes.Attachment;
023import org.kuali.rice.kew.notes.Note;
024import org.kuali.rice.kew.notes.dao.NoteDAO;
025import org.kuali.rice.kew.notes.service.AttachmentService;
026import org.kuali.rice.kew.notes.service.NoteService;
027import org.kuali.rice.krad.data.DataObjectService;
028import org.springframework.beans.factory.annotation.Required;
029import org.springframework.core.io.FileSystemResource;
030import org.springframework.core.io.Resource;
031
032public class NoteServiceImpl implements NoteService {
033
034        private NoteDAO noteDAO;
035
036        private AttachmentService attachmentService;
037
038    private DataObjectService dataObjectService;
039
040        public Note getNoteByNoteId(String noteId) {
041                return getDataObjectService().find(Note.class,noteId);
042        }
043
044        public List<Note> getNotesByDocumentId(String documentId) {
045                return getNoteDAO().getNotesByDocumentId(documentId);
046        }
047
048        public Note saveNote(Note note) {
049                try {
050                        if (! note.getAttachments().isEmpty()){
051                                for (Iterator iter = note.getAttachments().iterator(); iter.hasNext();) {
052                                        Attachment attachment = (Attachment) iter.next();
053                                        if (attachment.getAttachedObject()!= null){
054                                                attachmentService.persistAttachedFileAndSetAttachmentBusinessObjectValue(attachment);
055                                        }
056                                }
057                        }
058                        return getDataObjectService().save(note);
059                } catch (Exception e) {
060                        throw new RuntimeException(e);
061                }
062        }
063
064        public void deleteNote(Note note) {
065                try {
066           if (note != null && !note.getAttachments().isEmpty()){
067               for (Iterator iter = note.getAttachments().iterator(); iter.hasNext();) {
068                   Attachment attachment = (Attachment) iter.next();
069                   attachmentService.deleteAttachedFile(attachment);
070               }
071           }
072           if (note != null) {
073               getDataObjectService().delete(note);
074           }
075                } catch (Exception e) {
076                        throw new RuntimeException("caught exception deleting attachment", e);
077                }
078        }
079
080        public void deleteAttachment(Attachment attachment) {
081        getDataObjectService().delete(attachment);
082                try {
083                        attachmentService.deleteAttachedFile(attachment);
084                } catch (Exception e) {
085                        throw new RuntimeException("caught exception deleting attachment", e);
086                }
087        }
088
089        public File findAttachmentFile(Attachment attachment) {
090                try {
091                        return attachmentService.findAttachedFile(attachment);
092                } catch (Exception e) {
093                        throw new RuntimeException(e);
094                }
095
096        }
097        
098        public Resource findAttachmentResource(Attachment attachment) {
099                return attachmentService.findAttachedResource(attachment);
100        }
101
102        public Attachment findAttachment(String attachmentId) {
103                return getDataObjectService().find(Attachment.class,attachmentId);
104        }
105
106        public AttachmentService getAttachmentService() {
107                return attachmentService;
108        }
109
110        public void setAttachmentService(AttachmentService attachmentService) {
111                this.attachmentService = attachmentService;
112        }
113
114    public DataObjectService getDataObjectService() {
115        return dataObjectService;
116    }
117
118    @Required
119    public void setDataObjectService(DataObjectService dataObjectService) {
120        this.dataObjectService = dataObjectService;
121    }
122
123    public NoteDAO getNoteDAO() {
124        return noteDAO;
125    }
126
127    public void setNoteDAO(NoteDAO noteDAO) {
128        this.noteDAO = noteDAO;
129    }
130}