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.service.impl;
17  
18  import java.io.File;
19  import java.util.Iterator;
20  import java.util.List;
21  
22  import org.kuali.rice.kew.notes.Attachment;
23  import org.kuali.rice.kew.notes.Note;
24  import org.kuali.rice.kew.notes.dao.NoteDAO;
25  import org.kuali.rice.kew.notes.service.AttachmentService;
26  import org.kuali.rice.kew.notes.service.NoteService;
27  
28  
29  public class NoteServiceImpl implements NoteService {
30  
31  	private NoteDAO noteDAO;
32  
33  	private AttachmentService attachmentService;
34  
35  	public Note getNoteByNoteId(String noteId) {
36  		return getNoteDAO().getNoteByNoteId(noteId);
37  	}
38  
39  	public List<Note> getNotesByDocumentId(String documentId) {
40  		return getNoteDAO().getNotesByDocumentId(documentId);
41  	}
42  
43  	public void saveNote(Note note) {
44  		try {
45  			if (! note.getAttachments().isEmpty()){
46  				for (Iterator iter = note.getAttachments().iterator(); iter.hasNext();) {
47  					Attachment attachment = (Attachment) iter.next();
48  					if (attachment.getAttachedObject()!= null){
49  						attachmentService.persistAttachedFileAndSetAttachmentBusinessObjectValue(attachment);
50  					}
51  				}
52  			}
53  			getNoteDAO().saveNote(note);
54  		} catch (Exception e) {
55  			throw new RuntimeException(e);
56  		}
57  	}
58  
59  	public void deleteNote(Note note) {
60  		try {
61             if (note != null && !note.getAttachments().isEmpty()){
62                 for (Iterator iter = note.getAttachments().iterator(); iter.hasNext();) {
63                     Attachment attachment = (Attachment) iter.next();
64                     attachmentService.deleteAttachedFile(attachment);
65                 }
66             }
67             if (note != null) {
68                 getNoteDAO().deleteNote(note);
69             }
70  		} catch (Exception e) {
71  			throw new RuntimeException("caught exception deleting attachment", e);
72  		}
73  	}
74  
75  	public NoteDAO getNoteDAO() {
76  		return noteDAO;
77  	}
78  
79  	public void setNoteDAO(NoteDAO noteDAO) {
80  		this.noteDAO = noteDAO;
81  	}
82  
83  	public void deleteAttachment(Attachment attachment) {
84  		this.noteDAO.deleteAttachment(attachment);
85  		try {
86  			attachmentService.deleteAttachedFile(attachment);
87  		} catch (Exception e) {
88  			throw new RuntimeException("caught exception deleting attachment", e);
89  		}
90  	}
91  
92  	public File findAttachmentFile(Attachment attachment) {
93  		try {
94  			return attachmentService.findAttachedFile(attachment);
95  		} catch (Exception e) {
96  			throw new RuntimeException(e);
97  		}
98  
99  	}
100 
101 	public Attachment findAttachment(String attachmentId) {
102 		return noteDAO.findAttachment(attachmentId);
103 	}
104 
105 	public AttachmentService getAttachmentService() {
106 		return attachmentService;
107 	}
108 
109 	public void setAttachmentService(AttachmentService attachmentService) {
110 		this.attachmentService = attachmentService;
111 	}
112 }