View Javadoc
1   /**
2    * Copyright 2005-2016 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  import org.kuali.rice.krad.data.DataObjectService;
29  import org.kuali.rice.krad.data.PersistenceOption;
30  import org.springframework.beans.factory.annotation.Required;
31  
32  /**
33   * TODO 
34   * 
35   * @author Kuali Rice Team (rice.collab@kuali.org)
36   *
37   */
38  public class NoteServiceImpl implements NoteService {
39  
40  	private NoteDAO noteDao;
41  
42      private DataObjectService dataObjectService;
43  	
44  	@Override
45  	public List<Note> getNotes(String documentId) {
46  		if (StringUtils.isBlank(documentId)) {
47  			throw new RiceIllegalArgumentException("documentId was null or blank");
48  		}
49  		List<org.kuali.rice.kew.notes.Note> noteBos = getNoteDao().getNotesByDocumentId(documentId);
50  		if (noteBos == null) {
51  			return Collections.emptyList();
52  		}
53  		List<Note> notes = new ArrayList<Note>();
54  		for (org.kuali.rice.kew.notes.Note noteBo : noteBos) {
55  			notes.add(org.kuali.rice.kew.notes.Note.to(noteBo));
56  		}
57  		return Collections.unmodifiableList(notes);
58  	}
59  
60  	@Override
61  	public Note getNote(String noteId) {
62  		if (StringUtils.isBlank(noteId)) {
63  			throw new RiceIllegalArgumentException("noteId was null or blank");
64  		}
65  		org.kuali.rice.kew.notes.Note noteBo = getDataObjectService().find(org.kuali.rice.kew.notes.Note.class,
66                                                      noteId);
67  		return org.kuali.rice.kew.notes.Note.to(noteBo);
68  	}
69  
70  	@Override
71  	public Note createNote(Note note) {
72  		if (note == null) {
73  			throw new RiceIllegalArgumentException("note was null");
74  		}
75  		if (note.getId() != null) {
76  			throw new RiceIllegalArgumentException("Attempted to create a note that already has an id assigned, id must be null upon creation");
77  		}
78  		if (note.getVersionNumber() != null) {
79  			throw new RiceIllegalArgumentException("Attempted to create a note that already has a version number assigned, version number must be null upon creation");
80  		}
81  		org.kuali.rice.kew.notes.Note noteBo = org.kuali.rice.kew.notes.Note.from(note);
82  		if (noteBo.getNoteCreateDate() == null) {
83  			noteBo.setNoteCreateDate(new Timestamp(System.currentTimeMillis()));
84  		}
85  		noteBo = getDataObjectService().save(noteBo, PersistenceOption.FLUSH);
86  		return org.kuali.rice.kew.notes.Note.to(noteBo);
87  	}
88  
89  	@Override
90  	public Note updateNote(Note note) {
91  		if (note == null) {
92  			throw new RiceIllegalArgumentException("note was null");
93  		}
94  		if (note.getId() == null) {
95  			throw new RiceIllegalArgumentException("Attempted to update a note without an id, id must be present when updating");
96  		}
97  		if (note.getVersionNumber() == null) {
98  			throw new RiceIllegalArgumentException("Attempted to update a note without a version number, version number must be present when updating");
99  		}
100 		if (note.getCreateDate() == null) {
101 			throw new RiceIllegalArgumentException("Attempted to update a note without a create date, note must have a create date");
102 		}
103 		Note existingNoteBo = getNote(note.getId());
104 		if (existingNoteBo == null) {
105 			throw new RiceIllegalArgumentException("Attempted to udpate a note with an id for a not that does not exist: " + note.getId());
106 		}
107 		org.kuali.rice.kew.notes.Note noteBo = org.kuali.rice.kew.notes.Note.from(note);
108 		getDataObjectService().save(noteBo);
109 		return org.kuali.rice.kew.notes.Note.to(noteBo);
110 	}
111 
112 	@Override
113 	public Note deleteNote(String noteId) {
114 		if (StringUtils.isBlank(noteId)) {
115 			throw new RiceIllegalArgumentException("noteId was null or blank");
116 		}
117 		org.kuali.rice.kew.notes.Note noteBo = getDataObjectService().find(org.kuali.rice.kew.notes.Note.class, noteId);
118 		if (noteBo == null) {
119 			throw new RiceIllegalArgumentException("A note does not exist for the given note id: " + noteId);
120 		}
121 		Note deletedNote = org.kuali.rice.kew.notes.Note.to(noteBo);
122 		getDataObjectService().delete(noteBo);
123 		return deletedNote;
124 	}
125 	
126 	public void setNoteDao(NoteDAO noteDao) {
127 		this.noteDao = noteDao;
128 	}
129 	
130 	public NoteDAO getNoteDao() {
131 		return noteDao;
132 	}
133 
134 
135     public DataObjectService getDataObjectService() {
136         return dataObjectService;
137     }
138 
139     @Required
140     public void setDataObjectService(DataObjectService dataObjectService) {
141         this.dataObjectService = dataObjectService;
142     }
143 
144 }