1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
35
36
37
38 @Transactional
39 public class NoteServiceImpl implements NoteService {
40
41 protected LegacyDataAdapter lda;
42 protected AttachmentService attachmentService;
43
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
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
78 note = lda.save(note);
79 attachment.setNoteIdentifier(note.getNoteIdentifier());
80
81 note.setAttachment(attachment);
82 }
83 note = lda.save(note);
84
85 if (note.getAttachment() != null) {
86 attachmentService.moveAttachmentWherePending(note);
87 }
88 return note;
89 }
90
91
92
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
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
116
117 @Override
118 public void deleteNote(Note note) {
119 validateNoteNotNull(note);
120 if ( note.getAttachment() != null ) {
121 lda.delete(note.getAttachment());
122 }
123 lda.delete(note);
124 }
125
126
127
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 }