001/**
002 * Copyright 2005-2016 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.krad.service.impl;
017
018import java.util.List;
019
020import org.apache.commons.lang.StringUtils;
021import org.kuali.rice.krad.bo.Note;
022import org.kuali.rice.krad.bo.PersistableBusinessObject;
023import org.kuali.rice.krad.dao.NoteDao;
024import org.kuali.rice.krad.service.AttachmentService;
025import org.kuali.rice.krad.service.NoteService;
026import org.kuali.rice.krad.util.ObjectUtils;
027import org.springframework.transaction.annotation.Transactional;
028
029/**
030 * This class is the service implementation for the Note structure.
031 * 
032 * @author Kuali Rice Team (rice.collab@kuali.org)
033 */
034@Transactional
035public class NoteServiceImpl implements NoteService {
036
037    private NoteDao noteDao;
038    
039    private AttachmentService attachmentService;
040
041    public NoteServiceImpl() {
042        super();
043    }
044
045    /**
046     * @see org.kuali.rice.krad.service.NoteService#saveNoteValueList(java.util.List)
047     */
048    public void saveNoteList(List<Note> notes) {
049        if (notes != null) {
050            for (Note note : notes) {
051                if (StringUtils.isBlank(note.getRemoteObjectIdentifier())) {
052                        throw new IllegalStateException("The remote object identifier must be established on a Note before it can be saved.  The following note in the given list had a null or empty remote object identifier: " + note);
053                }
054                save(note);
055            }
056        }
057    }
058
059    /**
060     * @see org.kuali.rice.krad.service.NoteService#save(org.kuali.rice.krad.bo.Note)
061     */
062    public Note save(Note note) {
063        validateNoteNotNull(note);
064        if (StringUtils.isBlank(note.getRemoteObjectIdentifier())) {
065                throw new IllegalStateException("The remote object identifier must be established on a Note before it can be saved.  Given note had a null or empty remote object identifier.");
066        }
067        noteDao.save(note);
068        // move attachment from pending directory
069        if (note.getAttachment() != null) {
070                attachmentService.moveAttachmentWherePending(note);
071        }
072        return note;
073    }
074
075    /**
076     * @see org.kuali.rice.krad.service.NoteService#getByRemoteObjectId(java.lang.String)
077     */
078    public List<Note> getByRemoteObjectId(String remoteObjectId) {
079        if (StringUtils.isBlank(remoteObjectId)) {
080                throw new IllegalArgumentException("The remoteObjectId must not be null or blank.");
081        }
082        return noteDao.findByremoteObjectId(remoteObjectId);
083    }
084    
085    /**
086     * @see org.kuali.rice.krad.service.NoteService#getNoteByNoteId(java.lang.Long)
087     */
088    public Note getNoteByNoteId(Long noteId) {
089        if (noteId == null) {
090                throw new IllegalArgumentException("The noteId must not be null.");
091        }
092                return noteDao.getNoteByNoteId(noteId);
093        }
094
095    /**
096     * @see org.kuali.rice.krad.service.NoteService#deleteNote(org.kuali.rice.krad.bo.Note)
097     */
098    public void deleteNote(Note note) {
099        validateNoteNotNull(note);
100        noteDao.deleteNote(note);
101    }
102    
103    /**
104     * TODO this method seems awfully out of place in this service
105     * 
106     * @see org.kuali.rice.krad.service.NoteService#createNote(org.kuali.rice.krad.bo.Note, org.kuali.rice.krad.bo.PersistableBusinessObject)
107     */
108    public Note createNote(Note noteToCopy, PersistableBusinessObject bo, String authorPrincipalId) {
109        validateNoteNotNull(noteToCopy);
110        if (bo == null) {
111                throw new IllegalArgumentException("The bo must not be null.");
112        }
113        if (StringUtils.isBlank(authorPrincipalId)) {
114                throw new IllegalArgumentException("The authorPrincipalId must not be null.");
115        }
116        // TODO: Why is a deep copy being done?  Nowhere that this is called uses the given note argument
117        // again after calling this method.
118        Note tmpNote = (Note) ObjectUtils.deepCopy(noteToCopy);
119        tmpNote.setRemoteObjectIdentifier(bo.getObjectId());
120        tmpNote.setAuthorUniversalIdentifier(authorPrincipalId);
121        return tmpNote;
122    }
123
124    /**
125     * Sets the data access object
126     * 
127     * @param d
128     */
129    public void setNoteDao(NoteDao d) {
130        this.noteDao = d;
131    }
132
133    /**
134     * Retrieves a data access object
135     */
136    protected NoteDao getNoteDao() {
137        return noteDao;
138    }
139    
140    public void setAttachmentService(AttachmentService attachmentService) {
141        this.attachmentService = attachmentService;
142    }
143    
144    protected AttachmentService getAttachmentService() {
145        return this.attachmentService;
146    }
147    
148    private void validateNoteNotNull(Note note) {
149        if (note == null) {
150                throw new IllegalArgumentException("Note must not be null.");
151        }
152    }
153    
154}