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