001/**
002 * Copyright 2005-2014 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.impl.note;
017
018import java.sql.Timestamp;
019import java.util.ArrayList;
020import java.util.Collections;
021import java.util.List;
022
023import org.apache.commons.lang.StringUtils;
024import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
025import org.kuali.rice.kew.api.note.Note;
026import org.kuali.rice.kew.api.note.NoteService;
027import org.kuali.rice.kew.notes.dao.NoteDAO;
028import org.kuali.rice.krad.data.DataObjectService;
029import org.kuali.rice.krad.data.PersistenceOption;
030import org.springframework.beans.factory.annotation.Required;
031
032/**
033 * TODO 
034 * 
035 * @author Kuali Rice Team (rice.collab@kuali.org)
036 *
037 */
038public class NoteServiceImpl implements NoteService {
039
040        private NoteDAO noteDao;
041
042    private DataObjectService dataObjectService;
043        
044        @Override
045        public List<Note> getNotes(String documentId) {
046                if (StringUtils.isBlank(documentId)) {
047                        throw new RiceIllegalArgumentException("documentId was null or blank");
048                }
049                List<org.kuali.rice.kew.notes.Note> noteBos = getNoteDao().getNotesByDocumentId(documentId);
050                if (noteBos == null) {
051                        return Collections.emptyList();
052                }
053                List<Note> notes = new ArrayList<Note>();
054                for (org.kuali.rice.kew.notes.Note noteBo : noteBos) {
055                        notes.add(org.kuali.rice.kew.notes.Note.to(noteBo));
056                }
057                return Collections.unmodifiableList(notes);
058        }
059
060        @Override
061        public Note getNote(String noteId) {
062                if (StringUtils.isBlank(noteId)) {
063                        throw new RiceIllegalArgumentException("noteId was null or blank");
064                }
065                org.kuali.rice.kew.notes.Note noteBo = getDataObjectService().find(org.kuali.rice.kew.notes.Note.class,
066                                                    noteId);
067                return org.kuali.rice.kew.notes.Note.to(noteBo);
068        }
069
070        @Override
071        public Note createNote(Note note) {
072                if (note == null) {
073                        throw new RiceIllegalArgumentException("note was null");
074                }
075                if (note.getId() != null) {
076                        throw new RiceIllegalArgumentException("Attempted to create a note that already has an id assigned, id must be null upon creation");
077                }
078                if (note.getVersionNumber() != null) {
079                        throw new RiceIllegalArgumentException("Attempted to create a note that already has a version number assigned, version number must be null upon creation");
080                }
081                org.kuali.rice.kew.notes.Note noteBo = org.kuali.rice.kew.notes.Note.from(note);
082                if (noteBo.getNoteCreateDate() == null) {
083                        noteBo.setNoteCreateDate(new Timestamp(System.currentTimeMillis()));
084                }
085                noteBo = getDataObjectService().save(noteBo, PersistenceOption.FLUSH);
086                return org.kuali.rice.kew.notes.Note.to(noteBo);
087        }
088
089        @Override
090        public Note updateNote(Note note) {
091                if (note == null) {
092                        throw new RiceIllegalArgumentException("note was null");
093                }
094                if (note.getId() == null) {
095                        throw new RiceIllegalArgumentException("Attempted to update a note without an id, id must be present when updating");
096                }
097                if (note.getVersionNumber() == null) {
098                        throw new RiceIllegalArgumentException("Attempted to update a note without a version number, version number must be present when updating");
099                }
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}