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