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.krad.service.impl;
17  
18  import java.util.List;
19  
20  import org.apache.commons.lang.StringUtils;
21  import org.kuali.rice.core.api.criteria.OrderByField;
22  import org.kuali.rice.core.api.criteria.OrderDirection;
23  import org.kuali.rice.core.api.criteria.QueryByCriteria;
24  import org.kuali.rice.core.api.criteria.QueryResults;
25  import org.kuali.rice.core.api.mo.common.GloballyUnique;
26  import org.kuali.rice.core.api.util.cache.CopiedObject;
27  import org.kuali.rice.krad.bo.Attachment;
28  import org.kuali.rice.krad.bo.Note;
29  import org.kuali.rice.krad.data.DataObjectService;
30  import org.kuali.rice.krad.service.AttachmentService;
31  import org.kuali.rice.krad.service.NoteService;
32  import org.springframework.beans.factory.annotation.Required;
33  import org.springframework.transaction.annotation.Transactional;
34  
35  /**
36   * Service implementation for the Note structure
37   *
38   * @author Kuali Rice Team (rice.collab@kuali.org)
39   */
40  @Transactional
41  public class NoteServiceImpl implements NoteService {
42  
43      protected AttachmentService attachmentService;
44      protected DataObjectService dataObjectService;
45  
46      @Override
47  	public void saveNoteList(List<Note> notes) {
48          if (notes != null) {
49              for (Note note : notes) {
50              	if (StringUtils.isBlank(note.getRemoteObjectIdentifier())) {
51              		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);
52              	}
53                  save(note);
54              }
55          }
56      }
57  
58      /**
59       * @see org.kuali.rice.krad.service.NoteService#save(org.kuali.rice.krad.bo.Note)
60       */
61      @Override
62  	public Note save(Note note) {
63      	validateNoteNotNull(note);
64      	if (StringUtils.isBlank(note.getRemoteObjectIdentifier())) {
65      		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.");
66      	}
67          if (note.getAttachment() != null && note.getAttachment().getAttachmentFileName() == null) {
68              note.setAttachment(null);
69          }
70          if (note != null && note.getNoteIdentifier() == null && note.getAttachment() != null) {
71              Attachment attachment = note.getAttachment();
72              note.setAttachment(null);
73              // store without attachment
74              note = dataObjectService.save(note);
75              attachment.setNoteIdentifier(note.getNoteIdentifier());
76              // put attachment back
77              note.setAttachment(attachment);
78          }
79          note = dataObjectService.save(note);
80          // move attachment from pending directory
81          if (note.getAttachment() != null) {
82          	attachmentService.moveAttachmentWherePending(note);
83          }
84          return note;
85      }
86  
87      /**
88       * @see org.kuali.rice.krad.service.NoteService#getByRemoteObjectId(java.lang.String)
89       */
90  	@Override
91  	public List<Note> getByRemoteObjectId(String remoteObjectId) {
92      	if (StringUtils.isBlank(remoteObjectId)) {
93      		throw new IllegalArgumentException("The remoteObjectId must not be null or blank.");
94      	}
95          QueryResults<Note> result = dataObjectService.findMatching(Note.class,
96                  QueryByCriteria.Builder.forAttribute("remoteObjectIdentifier", remoteObjectId)
97                  .setOrderByFields(OrderByField.Builder.create("notePostedTimestamp", OrderDirection.ASCENDING).build()
98                  		).build());
99          return result.getResults();
100     }
101 
102     /**
103      * @see org.kuali.rice.krad.service.NoteService#getNoteByNoteId(java.lang.Long)
104      */
105     @Override
106 	public Note getNoteByNoteId(Long noteId) {
107     	if (noteId == null) {
108     		throw new IllegalArgumentException("The noteId must not be null.");
109     	}
110     	return dataObjectService.find(Note.class, noteId);
111 	}
112 
113     /**
114      * @see org.kuali.rice.krad.service.NoteService#deleteNote(org.kuali.rice.krad.bo.Note)
115      */
116     @Override
117 	public void deleteNote(Note note) {
118     	validateNoteNotNull(note);
119     	if ( note.getAttachment() != null ) { // Not sure about this - might blow up when no attachment
120     		dataObjectService.delete(note.getAttachment());
121     	}
122         dataObjectService.delete(note);
123     }
124 
125     /**
126      * TODO this method seems awfully out of place in this service
127      *
128      */
129     @Override
130 	public Note createNote(Note noteToCopy, GloballyUnique bo, String authorPrincipalId) {
131     	validateNoteNotNull(noteToCopy);
132     	if (bo == null) {
133     		throw new IllegalArgumentException("The bo must not be null.");
134     	}
135     	if (StringUtils.isBlank(authorPrincipalId)) {
136     		throw new IllegalArgumentException("The authorPrincipalId must not be null.");
137     	}
138 
139         Note tmpNote = new CopiedObject<Note>(noteToCopy).getContent();
140         tmpNote.setRemoteObjectIdentifier(bo.getObjectId());
141         tmpNote.setAuthorUniversalIdentifier(authorPrincipalId);
142 
143         return tmpNote;
144     }
145 
146     @Required
147     public void setAttachmentService(AttachmentService attachmentService) {
148     	this.attachmentService = attachmentService;
149     }
150 
151     protected AttachmentService getAttachmentService() {
152     	return this.attachmentService;
153     }
154 
155     private void validateNoteNotNull(Note note) {
156     	if (note == null) {
157     		throw new IllegalArgumentException("Note must not be null.");
158     	}
159     }
160 
161     @Required
162 	public void setDataObjectService(DataObjectService dataObjectService) {
163 		this.dataObjectService = dataObjectService;
164 	}
165 
166 }