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 2005-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  
 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.core.framework.services.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  0
 public class NoteAction extends KewKualiAction {
 60  
 
 61  0
     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  0
         initForm(request, form);
 68  0
         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  0
             NoteForm noteForm = (NoteForm) form;
 79  0
             if(StringUtils.isBlank(noteForm.getShowEdit())) {
 80  0
                     noteForm.setShowEdit("no");
 81  
             }
 82  0
             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  0
         NoteForm noteForm = (NoteForm) form;
 88  0
         noteForm.setShowEdit("no");
 89  0
         noteForm.setNoteIdNumber(null);
 90  0
         retrieveNoteList(request, noteForm);
 91  0
         noteForm.setShowAdd(Boolean.TRUE);
 92  0
         return start(mapping, form, request, response);
 93  
     }
 94  
 
 95  
     public ActionForward deleteAttachment(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 96  0
             NoteForm noteForm = (NoteForm) form;
 97  0
             NoteService noteService = KEWServiceLocator.getNoteService();
 98  0
             Note note = noteService.getNoteByNoteId(noteForm.getNote().getNoteId());
 99  0
             noteService.deleteAttachment(note.getAttachments().remove(0));
 100  0
             noteForm.setDocId(note.getDocumentId());
 101  0
             noteForm.setNoteIdNumber(note.getNoteId());
 102  0
             edit(mapping, form, request, response);
 103  0
             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  0
         NoteForm noteForm = (NoteForm) form;
 109  0
         if ("yes".equalsIgnoreCase(noteForm.getShowEdit())) {
 110  0
             noteForm.setNoteIdNumber(noteForm.getNote().getNoteId());
 111  
         } else {
 112  0
             noteForm.setShowEdit("yes");
 113  0
             Note noteToEdit = getNoteService().getNoteByNoteId(noteForm.getNoteIdNumber());
 114  0
             noteForm.setNote(noteToEdit);
 115  0
             noteForm.getNote().setNoteCreateLongDate(new Long(noteForm.getNote().getNoteCreateDate().getTime()));
 116  
         }
 117  0
         retrieveNoteList(request, noteForm);
 118  0
         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  0
         NoteForm noteForm = (NoteForm) form;
 129  0
         Note noteToSave = null;
 130  0
         if (noteForm.getShowEdit().equals("yes")) {
 131  0
             noteToSave = noteForm.getNote();
 132  0
             noteToSave.setNoteCreateDate(new Timestamp(noteToSave.getNoteCreateLongDate().longValue()));
 133  
         } else {
 134  0
             noteToSave = new Note();
 135  0
             noteToSave.setNoteId(null);
 136  0
             noteToSave.setDocumentId(noteForm.getDocId());
 137  0
             noteToSave.setNoteCreateDate(new Timestamp((new Date()).getTime()));
 138  0
             noteToSave.setNoteAuthorWorkflowId(getUserSession().getPrincipalId());
 139  0
             noteToSave.setNoteText(noteForm.getAddText());
 140  
         }
 141  0
         CustomNoteAttribute customNoteAttribute = null;
 142  0
         DocumentRouteHeaderValue routeHeader = getRouteHeaderService().getRouteHeader(noteToSave.getDocumentId());
 143  0
         boolean canEditNote = false;
 144  0
         boolean canAddNotes = false;
 145  0
         if (routeHeader != null) {
 146  0
             customNoteAttribute = routeHeader.getCustomNoteAttribute();
 147  0
             if (customNoteAttribute != null) {
 148  0
                 customNoteAttribute.setUserSession(GlobalVariables.getUserSession());
 149  0
                 canAddNotes = customNoteAttribute.isAuthorizedToAddNotes();
 150  0
                 canEditNote = customNoteAttribute.isAuthorizedToEditNote(noteToSave);
 151  
             }
 152  
         }
 153  
 
 154  0
         if ((noteForm.getShowEdit().equals("yes") && canEditNote) ||
 155  
                 (!noteForm.getShowEdit().equals("yes") && canAddNotes)) {
 156  0
                 FormFile uploadedFile = (FormFile)noteForm.getFile();
 157  0
                 if (uploadedFile != null && StringUtils.isNotBlank(uploadedFile.getFileName())) {
 158  0
                         Attachment attachment = new Attachment();
 159  0
                         attachment.setAttachedObject(uploadedFile.getInputStream());
 160  0
                         attachment.setFileName(uploadedFile.getFileName());
 161  0
                         attachment.setMimeType(uploadedFile.getContentType());
 162  0
                         attachment.setNote(noteToSave);
 163  0
                         noteToSave.getAttachments().add(attachment);
 164  
                 }
 165  0
             getNoteService().saveNote(noteToSave);
 166  
         }
 167  0
         if (noteForm.getShowEdit().equals("yes")) {
 168  0
             noteForm.setNote(new Note());
 169  
         } else {
 170  0
             noteForm.setAddText(null);
 171  
         }
 172  0
         noteForm.setShowEdit("no");
 173  0
         noteForm.setNoteIdNumber(null);
 174  0
         retrieveNoteList(request, noteForm);
 175  0
         return start(mapping, form, request, response);
 176  
     }
 177  
 
 178  
     public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 179  0
         NoteForm noteForm = (NoteForm) form;
 180  0
         Note existingNote = getNoteService().getNoteByNoteId(noteForm.getNoteIdNumber());
 181  0
         getNoteService().deleteNote(existingNote);
 182  0
         noteForm.setShowEdit("no");
 183  0
         noteForm.setNoteIdNumber(null);
 184  0
         retrieveNoteList(request, noteForm);
 185  0
         return start(mapping, form, request, response);
 186  
     }
 187  
 
 188  
     public ActionForward cancel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 189  0
         NoteForm noteForm = (NoteForm) form;
 190  0
         noteForm.setShowEdit("no");
 191  0
         noteForm.setNote(new Note());
 192  0
         noteForm.setNoteIdNumber(null);
 193  0
         retrieveNoteList(request, noteForm);
 194  0
         return start(mapping, form, request, response);
 195  
     }
 196  
 
 197  
     public ActionForward sort(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 198  0
         return start(mapping, form, request, response);
 199  
     }
 200  
 
 201  
 
 202  
 
 203  
     public ActionMessages initForm(HttpServletRequest request, ActionForm form) throws Exception {
 204  0
         NoteForm noteForm = (NoteForm) form;
 205  0
         noteForm.setCurrentUserName(getUserSession().getPerson().getName());
 206  0
         noteForm.setCurrentDate(getCurrentDate());
 207  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())) {
 208  0
             retrieveNoteList(request, noteForm);
 209  
         }
 210  0
         boolean showAttachments = CoreFrameworkServiceLocator.getParameterService().getParameterValueAsBoolean(KewApiConstants.KEW_NAMESPACE, KRADConstants.DetailTypes.ALL_DETAIL_TYPE, KewApiConstants.SHOW_ATTACHMENTS_IND);
 211  0
         noteForm.setShowAttachments(new Boolean(showAttachments));
 212  0
         return null;
 213  
     }
 214  
 
 215  
     private void retrieveNoteList(HttpServletRequest request, NoteForm noteForm) throws Exception {
 216  0
         if (noteForm.getDocId() != null) {
 217  
 //            List allNotes = getNoteService().getNotesByDocumentId(noteForm.getDocId());
 218  
 
 219  0
             CustomNoteAttribute customNoteAttribute = null;
 220  0
             DocumentRouteHeaderValue routeHeader = getRouteHeaderService().getRouteHeader(noteForm.getDocId());
 221  
 
 222  0
             List<Note> allNotes = routeHeader.getNotes();
 223  0
             boolean canAddNotes = false;
 224  0
             if (routeHeader != null) {
 225  0
                 customNoteAttribute = routeHeader.getCustomNoteAttribute();
 226  0
                 if (customNoteAttribute != null) {
 227  0
                     customNoteAttribute.setUserSession(GlobalVariables.getUserSession());
 228  0
                     canAddNotes = customNoteAttribute.isAuthorizedToAddNotes();
 229  
                 }
 230  
             }
 231  0
             Iterator<Note> notesIter = allNotes.iterator();
 232  0
             while (notesIter.hasNext()) {
 233  0
                 Note singleNote = notesIter.next();
 234  0
                 singleNote.setNoteCreateLongDate(new Long(singleNote.getNoteCreateDate().getTime()));
 235  0
                 getAuthorData(singleNote);
 236  0
                 boolean canEditNote = false;
 237  0
                 if (customNoteAttribute != null) {
 238  0
                         canEditNote = customNoteAttribute.isAuthorizedToEditNote(singleNote);
 239  
                 }
 240  0
                 singleNote.setAuthorizedToEdit(new Boolean(canEditNote));
 241  
                 // KULRICE-5201 - added StringUtils.equals to check equally since this is no longer a numeric (int) comparison
 242  0
                 if (StringUtils.equals(noteForm.getNoteIdNumber(), singleNote.getNoteId())) {
 243  0
                     singleNote.setEditingNote(Boolean.TRUE);
 244  
                 }
 245  0
             }
 246  0
             if (noteForm.getSortNotes() != null && noteForm.getSortNotes().booleanValue()) {
 247  0
                 if (KewApiConstants.Sorting.SORT_SEQUENCE_DSC.equalsIgnoreCase(noteForm.getSortOrder())) {
 248  0
                     noteForm.setSortOrder(KewApiConstants.Sorting.SORT_SEQUENCE_ASC);
 249  0
                     noteForm.setSortNotes(Boolean.FALSE);
 250  
                 } else {
 251  0
                     noteForm.setSortOrder(KewApiConstants.Sorting.SORT_SEQUENCE_DSC);
 252  0
                     noteForm.setSortNotes(Boolean.FALSE);
 253  
                 }
 254  
             } else {
 255  0
                 noteForm.setSortOrder(noteForm.getSortOrder());
 256  
             }
 257  0
             noteForm.setNoteList(sortNotes(allNotes, noteForm.getSortOrder()));
 258  0
             noteForm.setNumberOfNotes(new Integer(allNotes.size()));
 259  0
             noteForm.setAuthorizedToAdd(new Boolean(canAddNotes));
 260  0
             noteForm.setShowAdd(Boolean.TRUE);
 261  0
             if (! canAddNotes) {
 262  0
                 noteForm.setShowAdd(Boolean.FALSE);
 263  0
             } else if (noteForm.getNoteList().size() == 0) {
 264  0
                 noteForm.setShowAdd(Boolean.FALSE);
 265  
             }
 266  
         }
 267  0
     }
 268  
 
 269  
     private void getAuthorData(Note note) throws Exception {
 270  0
         Person user = null;
 271  0
         String id = "";
 272  0
         if (note != null && note.getNoteAuthorWorkflowId() != null && ! "".equalsIgnoreCase(note.getNoteAuthorWorkflowId())) {
 273  0
                 user = KimApiServiceLocator.getPersonService().getPerson(note.getNoteAuthorWorkflowId());
 274  0
             id = note.getNoteAuthorWorkflowId();
 275  
         }
 276  0
         if (user != null) {
 277  0
             note.setNoteAuthorFullName(user.getName());
 278  0
             note.setNoteAuthorEmailAddress(user.getEmailAddressUnmasked());
 279  0
             note.setNoteAuthorNetworkId(user.getPrincipalId());
 280  
         } else {
 281  0
             note.setNoteAuthorFullName(id + " (Name not Available)");
 282  0
             note.setNoteAuthorEmailAddress("Not Available");
 283  0
             note.setNoteAuthorNetworkId("Not Available");
 284  
         }
 285  0
     }
 286  
 
 287  
     public String getCurrentDate() {
 288  0
         Date currentDate = new Date();
 289  0
         DateFormat dateFormat = RiceConstants.getDefaultDateFormat();
 290  0
         return dateFormat.format(currentDate);
 291  
     }
 292  
 
 293  
     private List<Note> sortNotes(List<Note> allNotes, String sortOrder) {
 294  0
         final int returnCode = KewApiConstants.Sorting.SORT_SEQUENCE_DSC.equalsIgnoreCase(sortOrder) ? -1 : 1;
 295  
 
 296  
         try {
 297  0
           Collections.sort(allNotes,
 298  0
           new Comparator<Note>() {
 299  
             @Override
 300  
                         public int compare(Note o1, Note o2) {
 301  0
                           Timestamp date1 = o1.getNoteCreateDate();
 302  0
                           Timestamp date2 = o2.getNoteCreateDate();
 303  
 
 304  0
                 if (date1.before(date2)) {
 305  0
                   return returnCode * -1;
 306  0
                 } else if (date1.after(date2)) {
 307  0
                   return returnCode;
 308  
                 } else {
 309  0
                   return 0;
 310  
                 }
 311  
             }
 312  
           });
 313  0
       } catch (Throwable e) {
 314  0
         LOG.error(e.getMessage(), e);
 315  0
       }
 316  0
       return allNotes;
 317  
     }
 318  
 
 319  
     private NoteService getNoteService() {
 320  0
         return (NoteService) KEWServiceLocator.getService(KEWServiceLocator.NOTE_SERVICE);
 321  
     }
 322  
 
 323  
     private RouteHeaderService getRouteHeaderService() {
 324  0
         return (RouteHeaderService) KEWServiceLocator.getService(KEWServiceLocator.DOC_ROUTE_HEADER_SRV);
 325  
     }
 326  
     private static UserSession getUserSession() {
 327  0
         return GlobalVariables.getUserSession();
 328  
     }
 329  
 }