View Javadoc

1   /**
2    * Copyright 2005-2012 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         	}
165             getNoteService().saveNote(noteToSave);
166         }
167         if (noteForm.getShowEdit().equals("yes")) {
168             noteForm.setNote(new Note());
169         } else {
170             noteForm.setAddText(null);
171         }
172         noteForm.setShowEdit("no");
173         noteForm.setNoteIdNumber(null);
174         retrieveNoteList(request, noteForm);
175         return start(mapping, form, request, response);
176     }
177 
178     public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
179         NoteForm noteForm = (NoteForm) form;
180         Note existingNote = getNoteService().getNoteByNoteId(noteForm.getNoteIdNumber());
181         getNoteService().deleteNote(existingNote);
182         noteForm.setShowEdit("no");
183         noteForm.setNoteIdNumber(null);
184         retrieveNoteList(request, noteForm);
185         return start(mapping, form, request, response);
186     }
187 
188     public ActionForward cancel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
189         NoteForm noteForm = (NoteForm) form;
190         noteForm.setShowEdit("no");
191         noteForm.setNote(new Note());
192         noteForm.setNoteIdNumber(null);
193         retrieveNoteList(request, noteForm);
194         return start(mapping, form, request, response);
195     }
196 
197     public ActionForward sort(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
198         return start(mapping, form, request, response);
199     }
200 
201 
202 
203     public ActionMessages initForm(HttpServletRequest request, ActionForm form) throws Exception {
204         NoteForm noteForm = (NoteForm) form;
205         noteForm.setCurrentUserName(getUserSession().getPerson().getName());
206         noteForm.setCurrentDate(getCurrentDate());
207         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())) {
208             retrieveNoteList(request, noteForm);
209         }
210         boolean showAttachments = CoreFrameworkServiceLocator.getParameterService().getParameterValueAsBoolean(KewApiConstants.KEW_NAMESPACE, KRADConstants.DetailTypes.ALL_DETAIL_TYPE, KewApiConstants.SHOW_ATTACHMENTS_IND);
211         noteForm.setShowAttachments(new Boolean(showAttachments));
212         return null;
213     }
214 
215     private void retrieveNoteList(HttpServletRequest request, NoteForm noteForm) throws Exception {
216         if (noteForm.getDocId() != null) {
217 //            List allNotes = getNoteService().getNotesByDocumentId(noteForm.getDocId());
218 
219             CustomNoteAttribute customNoteAttribute = null;
220             DocumentRouteHeaderValue routeHeader = getRouteHeaderService().getRouteHeader(noteForm.getDocId());
221 
222             List<Note> allNotes = routeHeader.getNotes();
223             boolean canAddNotes = false;
224             if (routeHeader != null) {
225                 customNoteAttribute = routeHeader.getCustomNoteAttribute();
226                 if (customNoteAttribute != null) {
227                     customNoteAttribute.setUserSession(GlobalVariables.getUserSession());
228                     canAddNotes = customNoteAttribute.isAuthorizedToAddNotes();
229                 }
230             }
231             Iterator<Note> notesIter = allNotes.iterator();
232             while (notesIter.hasNext()) {
233                 Note singleNote = notesIter.next();
234                 singleNote.setNoteCreateLongDate(new Long(singleNote.getNoteCreateDate().getTime()));
235                 getAuthorData(singleNote);
236                 boolean canEditNote = false;
237                 if (customNoteAttribute != null) {
238                 	canEditNote = customNoteAttribute.isAuthorizedToEditNote(singleNote);
239                 }
240                 singleNote.setAuthorizedToEdit(new Boolean(canEditNote));
241                 // KULRICE-5201 - added StringUtils.equals to check equally since this is no longer a numeric (int) comparison
242                 if (StringUtils.equals(noteForm.getNoteIdNumber(), singleNote.getNoteId())) {
243                     singleNote.setEditingNote(Boolean.TRUE);
244                 }
245             }
246             if (noteForm.getSortNotes() != null && noteForm.getSortNotes().booleanValue()) {
247                 if (KewApiConstants.Sorting.SORT_SEQUENCE_DSC.equalsIgnoreCase(noteForm.getSortOrder())) {
248                     noteForm.setSortOrder(KewApiConstants.Sorting.SORT_SEQUENCE_ASC);
249                     noteForm.setSortNotes(Boolean.FALSE);
250                 } else {
251                     noteForm.setSortOrder(KewApiConstants.Sorting.SORT_SEQUENCE_DSC);
252                     noteForm.setSortNotes(Boolean.FALSE);
253                 }
254             } else {
255                 noteForm.setSortOrder(noteForm.getSortOrder());
256             }
257             noteForm.setNoteList(sortNotes(allNotes, noteForm.getSortOrder()));
258             noteForm.setNumberOfNotes(new Integer(allNotes.size()));
259             noteForm.setAuthorizedToAdd(new Boolean(canAddNotes));
260             noteForm.setShowAdd(Boolean.TRUE);
261             if (! canAddNotes) {
262                 noteForm.setShowAdd(Boolean.FALSE);
263             } else if (noteForm.getNoteList().size() == 0) {
264                 noteForm.setShowAdd(Boolean.FALSE);
265             }
266         }
267     }
268 
269     private void getAuthorData(Note note) throws Exception {
270         Person user = null;
271         String id = "";
272         if (note != null && note.getNoteAuthorWorkflowId() != null && ! "".equalsIgnoreCase(note.getNoteAuthorWorkflowId())) {
273         	user = KimApiServiceLocator.getPersonService().getPerson(note.getNoteAuthorWorkflowId());
274             id = note.getNoteAuthorWorkflowId();
275         }
276         if (user != null) {
277             note.setNoteAuthorFullName(user.getName());
278             note.setNoteAuthorEmailAddress(user.getEmailAddressUnmasked());
279             note.setNoteAuthorNetworkId(user.getPrincipalId());
280         } else {
281             note.setNoteAuthorFullName(id + " (Name not Available)");
282             note.setNoteAuthorEmailAddress("Not Available");
283             note.setNoteAuthorNetworkId("Not Available");
284         }
285     }
286 
287     public String getCurrentDate() {
288         Date currentDate = new Date();
289         DateFormat dateFormat = RiceConstants.getDefaultDateFormat();
290         return dateFormat.format(currentDate);
291     }
292 
293     private List<Note> sortNotes(List<Note> allNotes, String sortOrder) {
294         final int returnCode = KewApiConstants.Sorting.SORT_SEQUENCE_DSC.equalsIgnoreCase(sortOrder) ? -1 : 1;
295 
296         try {
297           Collections.sort(allNotes,
298           new Comparator<Note>() {
299             @Override
300 			public int compare(Note o1, Note o2) {
301   			Timestamp date1 = o1.getNoteCreateDate();
302   			Timestamp date2 = o2.getNoteCreateDate();
303 
304                 if (date1.before(date2)) {
305                   return returnCode * -1;
306                 } else if (date1.after(date2)) {
307                   return returnCode;
308                 } else {
309                   return 0;
310                 }
311             }
312           });
313       } catch (Throwable e) {
314         LOG.error(e.getMessage(), e);
315       }
316       return allNotes;
317     }
318 
319     private NoteService getNoteService() {
320         return (NoteService) KEWServiceLocator.getService(KEWServiceLocator.NOTE_SERVICE);
321     }
322 
323     private RouteHeaderService getRouteHeaderService() {
324         return (RouteHeaderService) KEWServiceLocator.getService(KEWServiceLocator.DOC_ROUTE_HEADER_SRV);
325     }
326     private static UserSession getUserSession() {
327         return GlobalVariables.getUserSession();
328     }
329 }