001/** 002 * Copyright 2005-2016 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.rice.kew.notes.web; 017 018import org.apache.commons.lang.StringUtils; 019import org.apache.struts.action.ActionForm; 020import org.apache.struts.action.ActionForward; 021import org.apache.struts.action.ActionMapping; 022import org.apache.struts.action.ActionMessages; 023import org.apache.struts.upload.FormFile; 024import org.kuali.rice.core.api.util.RiceConstants; 025import org.kuali.rice.coreservice.framework.CoreFrameworkServiceLocator; 026import org.kuali.rice.kew.notes.Attachment; 027import org.kuali.rice.kew.notes.CustomNoteAttribute; 028import org.kuali.rice.kew.notes.Note; 029import org.kuali.rice.kew.notes.service.NoteService; 030import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue; 031import org.kuali.rice.kew.routeheader.service.RouteHeaderService; 032import org.kuali.rice.kew.service.KEWServiceLocator; 033import org.kuali.rice.kew.api.KewApiConstants; 034import org.kuali.rice.kew.web.KewKualiAction; 035import org.kuali.rice.kim.api.identity.Person; 036import org.kuali.rice.kim.api.services.KimApiServiceLocator; 037import org.kuali.rice.krad.UserSession; 038import org.kuali.rice.krad.util.GlobalVariables; 039import org.kuali.rice.krad.util.KRADConstants; 040 041import javax.servlet.http.HttpServletRequest; 042import javax.servlet.http.HttpServletResponse; 043import java.sql.Timestamp; 044import java.text.DateFormat; 045import java.util.Collections; 046import java.util.Comparator; 047import java.util.Date; 048import java.util.Iterator; 049import java.util.List; 050 051 052/** 053 * Struts action for interfacing with the Notes system. 054 * 055 * @see NoteService 056 * 057 * @author Kuali Rice Team (rice.collab@kuali.org) 058 */ 059public class NoteAction extends KewKualiAction { 060 061 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(NoteAction.class); 062 063 @Override 064 public ActionForward execute(ActionMapping mapping, ActionForm form, 065 HttpServletRequest request, HttpServletResponse response) 066 throws Exception { 067 initForm(request, form); 068 return super.execute(mapping, form, request, response); 069 } 070 071 //public ActionForward start(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 072 // return mapping.findForward("allNotesReport"); 073 //} 074 075 076 @Override 077 public ActionForward start(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 078 NoteForm noteForm = (NoteForm) form; 079 if(StringUtils.isBlank(noteForm.getShowEdit())) { 080 noteForm.setShowEdit("no"); 081 } 082 return super.start(mapping, noteForm, request, response); 083 } 084 085 086 public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 087 NoteForm noteForm = (NoteForm) form; 088 noteForm.setShowEdit("no"); 089 noteForm.setNoteIdNumber(null); 090 retrieveNoteList(request, noteForm); 091 noteForm.setShowAdd(Boolean.TRUE); 092 return start(mapping, form, request, response); 093 } 094 095 public ActionForward deleteAttachment(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 096 NoteForm noteForm = (NoteForm) form; 097 NoteService noteService = KEWServiceLocator.getNoteService(); 098 Note note = noteService.getNoteByNoteId(noteForm.getNote().getNoteId()); 099 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}