View Javadoc

1   /**
2    * Copyright 2005-2013 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.impl.note;
17  
18  import java.sql.Timestamp;
19  import java.util.ArrayList;
20  import java.util.Collections;
21  import java.util.List;
22  
23  import org.apache.commons.lang.StringUtils;
24  import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
25  import org.kuali.rice.kew.api.note.Note;
26  import org.kuali.rice.kew.api.note.NoteService;
27  import org.kuali.rice.kew.notes.dao.NoteDAO;
28  
29  /**
30   * TODO 
31   * 
32   * @author Kuali Rice Team (rice.collab@kuali.org)
33   *
34   */
35  public class NoteServiceImpl implements NoteService {
36  
37  	private NoteDAO noteDao;
38  	
39  	@Override
40  	public List<Note> getNotes(String documentId) {
41  		if (StringUtils.isBlank(documentId)) {
42  			throw new RiceIllegalArgumentException("documentId was null or blank");
43  		}
44  		List<org.kuali.rice.kew.notes.Note> noteBos = getNoteDao().getNotesByDocumentId(documentId);
45  		if (noteBos == null) {
46  			return Collections.emptyList();
47  		}
48  		List<Note> notes = new ArrayList<Note>();
49  		for (org.kuali.rice.kew.notes.Note noteBo : noteBos) {
50  			notes.add(org.kuali.rice.kew.notes.Note.to(noteBo));
51  		}
52  		return Collections.unmodifiableList(notes);
53  	}
54  
55  	@Override
56  	public Note getNote(String noteId) {
57  		if (StringUtils.isBlank(noteId)) {
58  			throw new RiceIllegalArgumentException("noteId was null or blank");
59  		}
60  		org.kuali.rice.kew.notes.Note noteBo = getNoteDao().getNoteByNoteId(noteId);
61  		return org.kuali.rice.kew.notes.Note.to(noteBo);
62  	}
63  
64  	@Override
65  	public Note createNote(Note note) {
66  		if (note == null) {
67  			throw new RiceIllegalArgumentException("note was null");
68  		}
69  		if (note.getId() != null) {
70  			throw new RiceIllegalArgumentException("Attempted to create a note that already has an id assigned, id must be null upon creation");
71  		}
72  		if (note.getVersionNumber() != null) {
73  			throw new RiceIllegalArgumentException("Attempted to create a note that already has a version number assigned, version number must be null upon creation");
74  		}
75  		org.kuali.rice.kew.notes.Note noteBo = org.kuali.rice.kew.notes.Note.from(note);
76  		if (noteBo.getNoteCreateDate() == null) {
77  			noteBo.setNoteCreateDate(new Timestamp(System.currentTimeMillis()));
78  		}
79  		getNoteDao().saveNote(noteBo);
80  		return org.kuali.rice.kew.notes.Note.to(noteBo);
81  	}
82  
83  	@Override
84  	public Note updateNote(Note note) {
85  		if (note == null) {
86  			throw new RiceIllegalArgumentException("note was null");
87  		}
88  		if (note.getId() == null) {
89  			throw new RiceIllegalArgumentException("Attempted to update a note without an id, id must be present when updating");
90  		}
91  		if (note.getVersionNumber() == null) {
92  			throw new RiceIllegalArgumentException("Attempted to update a note without a version number, version number must be present when updating");
93  		}
94  		if (note.getCreateDate() == null) {
95  			throw new RiceIllegalArgumentException("Attempted to update a note without a create date, note must have a create date");
96  		}
97  		Note existingNoteBo = getNote(note.getId());
98  		if (existingNoteBo == null) {
99  			throw new RiceIllegalArgumentException("Attempted to udpate a note with an id for a not that does not exist: " + note.getId());
100 		}
101 		org.kuali.rice.kew.notes.Note noteBo = org.kuali.rice.kew.notes.Note.from(note);
102 		getNoteDao().saveNote(noteBo);
103 		return org.kuali.rice.kew.notes.Note.to(noteBo);
104 	}
105 
106 	@Override
107 	public Note deleteNote(String noteId) {
108 		if (StringUtils.isBlank(noteId)) {
109 			throw new RiceIllegalArgumentException("noteId was null or blank");
110 		}
111 		org.kuali.rice.kew.notes.Note noteBo = getNoteDao().getNoteByNoteId(noteId);
112 		if (noteBo == null) {
113 			throw new RiceIllegalArgumentException("A note does not exist for the given note id: " + noteId);
114 		}
115 		Note deletedNote = org.kuali.rice.kew.notes.Note.to(noteBo);
116 		getNoteDao().deleteNote(noteBo);
117 		return deletedNote;
118 	}
119 	
120 	public void setNoteDao(NoteDAO noteDao) {
121 		this.noteDao = noteDao;
122 	}
123 	
124 	public NoteDAO getNoteDao() {
125 		return noteDao;
126 	}
127 
128 }