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.kew.notes.web;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.struts.action.ActionForm;
20  import org.apache.struts.action.ActionForward;
21  import org.apache.struts.action.ActionMapping;
22  import org.apache.struts.action.ActionMessages;
23  import org.apache.struts.upload.FormFile;
24  import org.kuali.rice.core.api.util.RiceConstants;
25  import org.kuali.rice.coreservice.framework.CoreFrameworkServiceLocator;
26  import org.kuali.rice.kew.notes.Attachment;
27  import org.kuali.rice.kew.notes.CustomNoteAttribute;
28  import org.kuali.rice.kew.notes.Note;
29  import org.kuali.rice.kew.notes.service.NoteService;
30  import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
31  import org.kuali.rice.kew.routeheader.service.RouteHeaderService;
32  import org.kuali.rice.kew.service.KEWServiceLocator;
33  import org.kuali.rice.kew.api.KewApiConstants;
34  import org.kuali.rice.kew.web.KewKualiAction;
35  import org.kuali.rice.kim.api.identity.Person;
36  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
37  import org.kuali.rice.krad.UserSession;
38  import org.kuali.rice.krad.util.GlobalVariables;
39  import org.kuali.rice.krad.util.KRADConstants;
40  
41  import javax.servlet.http.HttpServletRequest;
42  import javax.servlet.http.HttpServletResponse;
43  import java.sql.Timestamp;
44  import java.text.DateFormat;
45  import java.util.Collections;
46  import java.util.Comparator;
47  import java.util.Date;
48  import java.util.Iterator;
49  import java.util.List;
50  
51  
52  /**
53   * Struts action for interfacing with the Notes system.
54   *
55   * @see NoteService
56   *
57   * @author Kuali Rice Team (rice.collab@kuali.org)
58   */
59  public class NoteAction extends KewKualiAction {
60  
61      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(NoteAction.class);
62  
63      @Override
64      public ActionForward execute(ActionMapping mapping, ActionForm form,
65              HttpServletRequest request, HttpServletResponse response)
66              throws Exception {
67          initForm(request, form);
68          return super.execute(mapping, form, request, response);
69      }
70      
71      //public ActionForward start(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
72      //    return mapping.findForward("allNotesReport");
73      //}
74      
75     
76      @Override
77      public ActionForward start(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
78      	NoteForm noteForm = (NoteForm) form;
79      	if(StringUtils.isBlank(noteForm.getShowEdit())) {
80      		noteForm.setShowEdit("no");
81      	}
82      	return super.start(mapping, noteForm, request, response);
83      }
84  
85      
86      public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
87          NoteForm noteForm = (NoteForm) form;
88          noteForm.setShowEdit("no");
89          noteForm.setNoteIdNumber(null);
90          retrieveNoteList(request, noteForm);
91          noteForm.setShowAdd(Boolean.TRUE);
92          return start(mapping, form, request, response);
93      }
94  
95      public ActionForward deleteAttachment(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
96      	NoteForm noteForm = (NoteForm) form;
97      	NoteService noteService = KEWServiceLocator.getNoteService();
98      	Note note = noteService.getNoteByNoteId(noteForm.getNote().getNoteId());
99      	noteService.deleteAttachment(note.getAttachments().remove(0));
100     	noteForm.setDocId(note.getDocumentId());
101     	noteForm.setNoteIdNumber(note.getNoteId());
102     	edit(mapping, form, request, response);
103     	return start(mapping, form, request, response);
104     	//return mapping.findForward("allNotesReport");
105     }
106 
107     public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
108         NoteForm noteForm = (NoteForm) form;
109         if ("yes".equalsIgnoreCase(noteForm.getShowEdit())) {
110             noteForm.setNoteIdNumber(noteForm.getNote().getNoteId());
111         } else {
112             noteForm.setShowEdit("yes");
113             Note noteToEdit = getNoteService().getNoteByNoteId(noteForm.getNoteIdNumber());
114             noteForm.setNote(noteToEdit);
115             noteForm.getNote().setNoteCreateLongDate(new Long(noteForm.getNote().getNoteCreateDate().getTime()));
116         }
117         retrieveNoteList(request, noteForm);
118         return start(mapping, form, request, response);
119     }
120 
121 //    public ActionForward attachFile(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
122 //
123 //
124 //    	return start(mapping, form, request, response);
125 //    }
126 
127     public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
128         NoteForm noteForm = (NoteForm) form;
129         Note noteToSave = null;
130         if (noteForm.getShowEdit().equals("yes")) {
131             noteToSave = noteForm.getNote();
132             noteToSave.setNoteCreateDate(new Timestamp(noteToSave.getNoteCreateLongDate().longValue()));
133         } else {
134             noteToSave = new Note();
135             noteToSave.setNoteId(null);
136             noteToSave.setDocumentId(noteForm.getDocId());
137             noteToSave.setNoteCreateDate(new Timestamp((new Date()).getTime()));
138             noteToSave.setNoteAuthorWorkflowId(getUserSession().getPrincipalId());
139             noteToSave.setNoteText(noteForm.getAddText());
140         }
141         CustomNoteAttribute customNoteAttribute = null;
142         DocumentRouteHeaderValue routeHeader = getRouteHeaderService().getRouteHeader(noteToSave.getDocumentId());
143         boolean canEditNote = false;
144         boolean canAddNotes = false;
145         if (routeHeader != null) {
146             customNoteAttribute = routeHeader.getCustomNoteAttribute();
147             if (customNoteAttribute != null) {
148                 customNoteAttribute.setUserSession(GlobalVariables.getUserSession());
149                 canAddNotes = customNoteAttribute.isAuthorizedToAddNotes();
150                 canEditNote = customNoteAttribute.isAuthorizedToEditNote(noteToSave);
151             }
152         }
153 
154         if ((noteForm.getShowEdit().equals("yes") && canEditNote) ||
155                 (!noteForm.getShowEdit().equals("yes") && canAddNotes)) {
156         	FormFile uploadedFile = (FormFile)noteForm.getFile();
157         	if (uploadedFile != null && StringUtils.isNotBlank(uploadedFile.getFileName())) {
158         		Attachment attachment = new Attachment();
159         		attachment.setAttachedObject(uploadedFile.getInputStream());
160         		attachment.setFileName(uploadedFile.getFileName());
161         		attachment.setMimeType(uploadedFile.getContentType());
162         		attachment.setNote(noteToSave);
163         		noteToSave.getAttachments().add(attachment);
164             } else {
165                 if(noteToSave.getNoteId() != null) { //note is not new and we need to ensure the attachments are preserved
166                     noteToSave.setAttachments(getNoteService().getNoteByNoteId(noteToSave.getNoteId()).getAttachments());
167                 }
168             }
169             getNoteService().saveNote(noteToSave);
170         }
171         if (noteForm.getShowEdit().equals("yes")) {
172             noteForm.setNote(new Note());
173         } else {
174             noteForm.setAddText(null);
175         }
176         noteForm.setShowEdit("no");
177         noteForm.setNoteIdNumber(null);
178         retrieveNoteList(request, noteForm);
179         return start(mapping, form, request, response);
180     }
181 
182     public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
183         NoteForm noteForm = (NoteForm) form;
184         Note existingNote = getNoteService().getNoteByNoteId(noteForm.getNoteIdNumber());
185         getNoteService().deleteNote(existingNote);
186         noteForm.setShowEdit("no");
187         noteForm.setNoteIdNumber(null);
188         retrieveNoteList(request, noteForm);
189         return start(mapping, form, request, response);
190     }
191 
192     public ActionForward cancel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
193         NoteForm noteForm = (NoteForm) form;
194         noteForm.setShowEdit("no");
195         noteForm.setNote(new Note());
196         noteForm.setNoteIdNumber(null);
197         retrieveNoteList(request, noteForm);
198         return start(mapping, form, request, response);
199     }
200 
201     public ActionForward sort(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
202         return start(mapping, form, request, response);
203     }
204 
205 
206 
207     public ActionMessages initForm(HttpServletRequest request, ActionForm form) throws Exception {
208         NoteForm noteForm = (NoteForm) form;
209         noteForm.setCurrentUserName(getUserSession().getPerson().getName());
210         noteForm.setCurrentDate(getCurrentDate());
211         if (! "workflowReport".equalsIgnoreCase(noteForm.getMethodToCall()) && ! "add".equalsIgnoreCase(noteForm.getMethodToCall()) && ! "cancel".equalsIgnoreCase(noteForm.getMethodToCall()) && ! "edit".equalsIgnoreCase(noteForm.getMethodToCall()) && ! "delete".equalsIgnoreCase(noteForm.getMethodToCall()) && ! "save".equalsIgnoreCase(noteForm.getMethodToCall())) {
212             retrieveNoteList(request, noteForm);
213         }
214         boolean showAttachments = CoreFrameworkServiceLocator.getParameterService().getParameterValueAsBoolean(KewApiConstants.KEW_NAMESPACE, KRADConstants.DetailTypes.ALL_DETAIL_TYPE, KewApiConstants.SHOW_ATTACHMENTS_IND);
215         noteForm.setShowAttachments(new Boolean(showAttachments));
216         return null;
217     }
218 
219     private void retrieveNoteList(HttpServletRequest request, NoteForm noteForm) throws Exception {
220         if (noteForm.getDocId() != null) {
221 //            List allNotes = getNoteService().getNotesByDocumentId(noteForm.getDocId());
222 
223             CustomNoteAttribute customNoteAttribute = null;
224             DocumentRouteHeaderValue routeHeader = getRouteHeaderService().getRouteHeader(noteForm.getDocId());
225 
226             List<Note> allNotes = routeHeader.getNotes();
227             boolean canAddNotes = false;
228             if (routeHeader != null) {
229                 customNoteAttribute = routeHeader.getCustomNoteAttribute();
230                 if (customNoteAttribute != null) {
231                     customNoteAttribute.setUserSession(GlobalVariables.getUserSession());
232                     canAddNotes = customNoteAttribute.isAuthorizedToAddNotes();
233                 }
234             }
235             Iterator<Note> notesIter = allNotes.iterator();
236             while (notesIter.hasNext()) {
237                 Note singleNote = notesIter.next();
238                 singleNote.setNoteCreateLongDate(new Long(singleNote.getNoteCreateDate().getTime()));
239                 getAuthorData(singleNote);
240                 boolean canEditNote = false;
241                 if (customNoteAttribute != null) {
242                 	canEditNote = customNoteAttribute.isAuthorizedToEditNote(singleNote);
243                 }
244                 singleNote.setAuthorizedToEdit(new Boolean(canEditNote));
245                 // KULRICE-5201 - added StringUtils.equals to check equally since this is no longer a numeric (int) comparison
246                 if (StringUtils.equals(noteForm.getNoteIdNumber(), singleNote.getNoteId())) {
247                     singleNote.setEditingNote(Boolean.TRUE);
248                 }
249             }
250             if (noteForm.getSortNotes() != null && noteForm.getSortNotes().booleanValue()) {
251                 if (KewApiConstants.Sorting.SORT_SEQUENCE_DSC.equalsIgnoreCase(noteForm.getSortOrder())) {
252                     noteForm.setSortOrder(KewApiConstants.Sorting.SORT_SEQUENCE_ASC);
253                     noteForm.setSortNotes(Boolean.FALSE);
254                 } else {
255                     noteForm.setSortOrder(KewApiConstants.Sorting.SORT_SEQUENCE_DSC);
256                     noteForm.setSortNotes(Boolean.FALSE);
257                 }
258             } else {
259                 noteForm.setSortOrder(noteForm.getSortOrder());
260             }
261             noteForm.setNoteList(sortNotes(allNotes, noteForm.getSortOrder()));
262             noteForm.setNumberOfNotes(new Integer(allNotes.size()));
263             noteForm.setAuthorizedToAdd(new Boolean(canAddNotes));
264             noteForm.setShowAdd(Boolean.TRUE);
265             if (! canAddNotes) {
266                 noteForm.setShowAdd(Boolean.FALSE);
267             } else if (noteForm.getNoteList().size() == 0) {
268                 noteForm.setShowAdd(Boolean.FALSE);
269             }
270         }
271     }
272 
273     private void getAuthorData(Note note) throws Exception {
274         Person user = null;
275         String id = "";
276         if (note != null && note.getNoteAuthorWorkflowId() != null && ! "".equalsIgnoreCase(note.getNoteAuthorWorkflowId())) {
277         	user = KimApiServiceLocator.getPersonService().getPerson(note.getNoteAuthorWorkflowId());
278             id = note.getNoteAuthorWorkflowId();
279         }
280         if (user != null) {
281             note.setNoteAuthorFullName(user.getName());
282             note.setNoteAuthorEmailAddress(user.getEmailAddressUnmasked());
283             note.setNoteAuthorNetworkId(user.getPrincipalId());
284         } else {
285             note.setNoteAuthorFullName(id + " (Name not Available)");
286             note.setNoteAuthorEmailAddress("Not Available");
287             note.setNoteAuthorNetworkId("Not Available");
288         }
289     }
290 
291     public String getCurrentDate() {
292         Date currentDate = new Date();
293         DateFormat dateFormat = RiceConstants.getDefaultDateFormat();
294         return dateFormat.format(currentDate);
295     }
296 
297     private List<Note> sortNotes(List<Note> allNotes, String sortOrder) {
298         final int returnCode = KewApiConstants.Sorting.SORT_SEQUENCE_DSC.equalsIgnoreCase(sortOrder) ? -1 : 1;
299 
300         try {
301           Collections.sort(allNotes,
302           new Comparator<Note>() {
303             @Override
304 			public int compare(Note o1, Note o2) {
305   			Timestamp date1 = o1.getNoteCreateDate();
306   			Timestamp date2 = o2.getNoteCreateDate();
307 
308                 if (date1.before(date2)) {
309                   return returnCode * -1;
310                 } else if (date1.after(date2)) {
311                   return returnCode;
312                 } else {
313                   return 0;
314                 }
315             }
316           });
317       } catch (Throwable e) {
318         LOG.error(e.getMessage(), e);
319       }
320       return allNotes;
321     }
322 
323     private NoteService getNoteService() {
324         return (NoteService) KEWServiceLocator.getService(KEWServiceLocator.NOTE_SERVICE);
325     }
326 
327     private RouteHeaderService getRouteHeaderService() {
328         return (RouteHeaderService) KEWServiceLocator.getService(KEWServiceLocator.DOC_ROUTE_HEADER_SRV);
329     }
330     private static UserSession getUserSession() {
331         return GlobalVariables.getUserSession();
332     }
333 }