Coverage Report - org.kuali.rice.kew.notes.web.NoteAction
 
Classes in this File Line Coverage Branch Coverage Complexity
NoteAction
0%
0/154
0%
0/70
3.222
NoteAction$1
0%
0/8
0%
0/4
3.222
 
 1  
 /*
 2  
  * Copyright 2005-2008 The Kuali Foundation
 3  
  *
 4  
  *
 5  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  *
 9  
  * http://www.opensource.org/licenses/ecl2.php
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 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.util.RiceConstants;
 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.util.KEWConstants;
 34  
 import org.kuali.rice.kew.util.Utilities;
 35  
 import org.kuali.rice.kew.web.KewKualiAction;
 36  
 import org.kuali.rice.kew.web.session.UserSession;
 37  
 import org.kuali.rice.kim.bo.Person;
 38  
 import org.kuali.rice.kim.service.KIMServiceLocator;
 39  
 import org.kuali.rice.kns.util.KNSConstants;
 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.*;
 46  
 
 47  
 
 48  
 /**
 49  
  * Struts action for interfacing with the Notes system.
 50  
  *
 51  
  * @see NoteService
 52  
  *
 53  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 54  
  */
 55  0
 public class NoteAction extends KewKualiAction {
 56  
 
 57  0
     private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(NoteAction.class);
 58  
 
 59  
     @Override
 60  
     public ActionForward execute(ActionMapping mapping, ActionForm form,
 61  
             HttpServletRequest request, HttpServletResponse response)
 62  
             throws Exception {
 63  0
         initForm(request, form);
 64  0
         return super.execute(mapping, form, request, response);
 65  
     }
 66  
     
 67  
     //public ActionForward start(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 68  
     //    return mapping.findForward("allNotesReport");
 69  
     //}
 70  
     
 71  
    
 72  
     @Override
 73  
     public ActionForward start(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 74  0
             NoteForm noteForm = (NoteForm) form;
 75  0
             if(StringUtils.isBlank(noteForm.getShowEdit())) {
 76  0
                     noteForm.setShowEdit("no");
 77  
             }
 78  0
             return super.start(mapping, noteForm, request, response);
 79  
     }
 80  
 
 81  
     
 82  
     public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 83  0
         NoteForm noteForm = (NoteForm) form;
 84  0
         noteForm.setShowEdit("no");
 85  0
         noteForm.setNoteIdNumber(null);
 86  0
         retrieveNoteList(request, noteForm);
 87  0
         noteForm.setShowAdd(Boolean.TRUE);
 88  0
         return start(mapping, form, request, response);
 89  
     }
 90  
 
 91  
     public ActionForward deleteAttachment(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 92  0
             NoteForm noteForm = (NoteForm) form;
 93  0
             NoteService noteService = KEWServiceLocator.getNoteService();
 94  0
             Note note = noteService.getNoteByNoteId(noteForm.getNote().getNoteId());
 95  0
             noteService.deleteAttachment((Attachment)note.getAttachments().remove(0));
 96  0
             noteForm.setDocId(note.getRouteHeaderId());
 97  0
             noteForm.setNoteIdNumber(note.getNoteId());
 98  0
             edit(mapping, form, request, response);
 99  0
             return start(mapping, form, request, response);
 100  
             //return mapping.findForward("allNotesReport");
 101  
     }
 102  
 
 103  
     public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 104  0
         NoteForm noteForm = (NoteForm) form;
 105  0
         if ("yes".equalsIgnoreCase(noteForm.getShowEdit())) {
 106  0
             noteForm.setNoteIdNumber(noteForm.getNote().getNoteId());
 107  
         } else {
 108  0
             noteForm.setShowEdit("yes");
 109  0
             Note noteToEdit = getNoteService().getNoteByNoteId(noteForm.getNoteIdNumber());
 110  0
             noteForm.setNote(noteToEdit);
 111  0
             noteForm.getNote().setNoteCreateLongDate(new Long(noteForm.getNote().getNoteCreateDate().getTime()));
 112  
         }
 113  0
         retrieveNoteList(request, noteForm);
 114  0
         return start(mapping, form, request, response);
 115  
     }
 116  
 
 117  
 //    public ActionForward attachFile(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 118  
 //
 119  
 //
 120  
 //            return start(mapping, form, request, response);
 121  
 //    }
 122  
 
 123  
     public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 124  0
         NoteForm noteForm = (NoteForm) form;
 125  0
         Note noteToSave = null;
 126  0
         if (noteForm.getShowEdit().equals("yes")) {
 127  0
             noteToSave = noteForm.getNote();
 128  0
             noteToSave.setNoteCreateDate(new Timestamp(noteToSave.getNoteCreateLongDate().longValue()));
 129  
         } else {
 130  0
             noteToSave = new Note();
 131  0
             noteToSave.setNoteId(null);
 132  0
             noteToSave.setRouteHeaderId(noteForm.getDocId());
 133  0
             noteToSave.setNoteCreateDate(new Timestamp((new Date()).getTime()));
 134  0
             noteToSave.setNoteAuthorWorkflowId(getUserSession(request).getPrincipalId());
 135  0
             noteToSave.setNoteText(noteForm.getAddText());
 136  
         }
 137  0
         CustomNoteAttribute customNoteAttribute = null;
 138  0
         DocumentRouteHeaderValue routeHeader = getRouteHeaderService().getRouteHeader(noteToSave.getRouteHeaderId());
 139  0
         boolean canEditNote = false;
 140  0
         boolean canAddNotes = false;
 141  0
         if (routeHeader != null) {
 142  0
             customNoteAttribute = routeHeader.getCustomNoteAttribute();
 143  0
             if (customNoteAttribute != null) {
 144  0
                 customNoteAttribute.setUserSession(getUserSession(request));
 145  0
                 canAddNotes = customNoteAttribute.isAuthorizedToAddNotes();
 146  0
                 canEditNote = customNoteAttribute.isAuthorizedToEditNote(noteToSave);
 147  
             }
 148  
         }
 149  
 
 150  0
         if ((noteForm.getShowEdit().equals("yes") && canEditNote) ||
 151  
                 (!noteForm.getShowEdit().equals("yes") && canAddNotes)) {
 152  0
                 FormFile uploadedFile = (FormFile)noteForm.getFile();
 153  0
                 if (uploadedFile != null && StringUtils.isNotBlank(uploadedFile.getFileName())) {
 154  0
                         Attachment attachment = new Attachment();
 155  0
                         attachment.setAttachedObject(uploadedFile.getInputStream());
 156  0
                         attachment.setFileName(uploadedFile.getFileName());
 157  0
                         attachment.setMimeType(uploadedFile.getContentType());
 158  0
                         attachment.setNote(noteToSave);
 159  0
                         noteToSave.getAttachments().add(attachment);
 160  
                 }
 161  0
             getNoteService().saveNote(noteToSave);
 162  
         }
 163  0
         if (noteForm.getShowEdit().equals("yes")) {
 164  0
             noteForm.setNote(new Note());
 165  
         } else {
 166  0
             noteForm.setAddText(null);
 167  
         }
 168  0
         noteForm.setShowEdit("no");
 169  0
         noteForm.setNoteIdNumber(null);
 170  0
         retrieveNoteList(request, noteForm);
 171  0
         return start(mapping, form, request, response);
 172  
     }
 173  
 
 174  
     public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 175  0
         NoteForm noteForm = (NoteForm) form;
 176  0
         Note existingNote = getNoteService().getNoteByNoteId(noteForm.getNoteIdNumber());
 177  0
         getNoteService().deleteNote(existingNote);
 178  0
         noteForm.setShowEdit("no");
 179  0
         noteForm.setNoteIdNumber(null);
 180  0
         retrieveNoteList(request, noteForm);
 181  0
         return start(mapping, form, request, response);
 182  
     }
 183  
 
 184  
     public ActionForward cancel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 185  0
         NoteForm noteForm = (NoteForm) form;
 186  0
         noteForm.setShowEdit("no");
 187  0
         noteForm.setNote(new Note());
 188  0
         noteForm.setNoteIdNumber(null);
 189  0
         retrieveNoteList(request, noteForm);
 190  0
         return start(mapping, form, request, response);
 191  
     }
 192  
 
 193  
     public ActionForward sort(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
 194  0
         return start(mapping, form, request, response);
 195  
     }
 196  
 
 197  
 
 198  
 
 199  
     public ActionMessages initForm(HttpServletRequest request, ActionForm form) throws Exception {
 200  0
         NoteForm noteForm = (NoteForm) form;
 201  0
         noteForm.setCurrentUserName(getUserSession(request).getPerson().getName());
 202  0
         noteForm.setCurrentDate(getCurrentDate());
 203  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())) {
 204  0
             retrieveNoteList(request, noteForm);
 205  
         }
 206  0
         boolean showAttachments = Utilities.getKNSParameterBooleanValue(KEWConstants.KEW_NAMESPACE, KNSConstants.DetailTypes.ALL_DETAIL_TYPE, KEWConstants.SHOW_ATTACHMENTS_IND);
 207  0
         noteForm.setShowAttachments(new Boolean(showAttachments));
 208  0
         return null;
 209  
     }
 210  
 
 211  
     private void retrieveNoteList(HttpServletRequest request, NoteForm noteForm) throws Exception {
 212  0
         if (noteForm.getDocId() != null) {
 213  
 //            List allNotes = getNoteService().getNotesByRouteHeaderId(noteForm.getDocId());
 214  
 
 215  0
             CustomNoteAttribute customNoteAttribute = null;
 216  0
             DocumentRouteHeaderValue routeHeader = getRouteHeaderService().getRouteHeader(noteForm.getDocId());
 217  
 
 218  0
             List allNotes = routeHeader.getNotes();
 219  0
             boolean canAddNotes = false;
 220  0
             if (routeHeader != null) {
 221  0
                 customNoteAttribute = routeHeader.getCustomNoteAttribute();
 222  0
                 if (customNoteAttribute != null) {
 223  0
                     customNoteAttribute.setUserSession(getUserSession(request));
 224  0
                     canAddNotes = customNoteAttribute.isAuthorizedToAddNotes();
 225  
                 }
 226  
             }
 227  0
             Iterator notesIter = allNotes.iterator();
 228  0
             while (notesIter.hasNext()) {
 229  0
                 Note singleNote = (Note) notesIter.next();
 230  0
                 singleNote.setNoteCreateLongDate(new Long(singleNote.getNoteCreateDate().getTime()));
 231  0
                 getAuthorData(singleNote);
 232  0
                 boolean canEditNote = false;
 233  0
                 if (customNoteAttribute != null) {
 234  0
                         canEditNote = customNoteAttribute.isAuthorizedToEditNote(singleNote);
 235  
                 }
 236  0
                 singleNote.setAuthorizedToEdit(new Boolean(canEditNote));
 237  0
                 if (noteForm.getNoteIdNumber() != null && (noteForm.getNoteIdNumber().intValue() == singleNote.getNoteId().intValue())) {
 238  0
                     singleNote.setEditingNote(Boolean.TRUE);
 239  
                 }
 240  0
             }
 241  0
             if (noteForm.getSortNotes() != null && noteForm.getSortNotes().booleanValue()) {
 242  0
                 if (KEWConstants.Sorting.SORT_SEQUENCE_DSC.equalsIgnoreCase(noteForm.getSortOrder())) {
 243  0
                     noteForm.setSortOrder(KEWConstants.Sorting.SORT_SEQUENCE_ASC);
 244  0
                     noteForm.setSortNotes(Boolean.FALSE);
 245  
                 } else {
 246  0
                     noteForm.setSortOrder(KEWConstants.Sorting.SORT_SEQUENCE_DSC);
 247  0
                     noteForm.setSortNotes(Boolean.FALSE);
 248  
                 }
 249  
             } else {
 250  0
                 noteForm.setSortOrder(noteForm.getSortOrder());
 251  
             }
 252  0
             noteForm.setNoteList(sortNotes(allNotes, noteForm.getSortOrder()));
 253  0
             noteForm.setNumberOfNotes(new Integer(allNotes.size()));
 254  0
             noteForm.setAuthorizedToAdd(new Boolean(canAddNotes));
 255  0
             noteForm.setShowAdd(Boolean.TRUE);
 256  0
             if (! canAddNotes) {
 257  0
                 noteForm.setShowAdd(Boolean.FALSE);
 258  0
             } else if (noteForm.getNoteList().size() == 0) {
 259  0
                 noteForm.setShowAdd(Boolean.FALSE);
 260  
             }
 261  
         }
 262  0
     }
 263  
 
 264  
     private void getAuthorData(Note note) throws Exception {
 265  0
         Person user = null;
 266  0
         String id = "";
 267  0
         if (note != null && note.getNoteAuthorWorkflowId() != null && ! "".equalsIgnoreCase(note.getNoteAuthorWorkflowId())) {
 268  0
                 user = KIMServiceLocator.getPersonService().getPerson(note.getNoteAuthorWorkflowId());
 269  0
             id = note.getNoteAuthorWorkflowId();
 270  
         }
 271  0
         if (user != null) {
 272  0
             note.setNoteAuthorFullName(user.getName());
 273  0
             note.setNoteAuthorEmailAddress(user.getEmailAddressUnmasked());
 274  0
             note.setNoteAuthorNetworkId(user.getPrincipalId());
 275  
         } else {
 276  0
             note.setNoteAuthorFullName(id + " (Name not Available)");
 277  0
             note.setNoteAuthorEmailAddress("Not Available");
 278  0
             note.setNoteAuthorNetworkId("Not Available");
 279  
         }
 280  0
     }
 281  
 
 282  
     public String getCurrentDate() {
 283  0
         Date currentDate = new Date();
 284  0
         DateFormat dateFormat = RiceConstants.getDefaultDateFormat();
 285  0
         return dateFormat.format(currentDate);
 286  
     }
 287  
 
 288  
     private List sortNotes(List allNotes, String sortOrder) {
 289  0
         final int returnCode = KEWConstants.Sorting.SORT_SEQUENCE_DSC.equalsIgnoreCase(sortOrder) ? -1 : 1;
 290  
 
 291  
         try {
 292  0
           Collections.sort(allNotes,
 293  0
           new Comparator() {
 294  
             public int compare(Object o1, Object o2) {
 295  0
                           Timestamp date1 = ((Note) o1).getNoteCreateDate();
 296  0
                           Timestamp date2 = ((Note) o2).getNoteCreateDate();
 297  
 
 298  0
                 if (date1.before(date2)) {
 299  0
                   return returnCode * -1;
 300  0
                 } else if (date1.after(date2)) {
 301  0
                   return returnCode;
 302  
                 } else {
 303  0
                   return 0;
 304  
                 }
 305  
             }
 306  
           });
 307  0
       } catch (Throwable e) {
 308  0
         LOG.error(e.getMessage(), e);
 309  0
       }
 310  0
       return allNotes;
 311  
     }
 312  
 
 313  
     private NoteService getNoteService() {
 314  0
         return (NoteService) KEWServiceLocator.getService(KEWServiceLocator.NOTE_SERVICE);
 315  
     }
 316  
 
 317  
     private RouteHeaderService getRouteHeaderService() {
 318  0
         return (RouteHeaderService) KEWServiceLocator.getService(KEWServiceLocator.DOC_ROUTE_HEADER_SRV);
 319  
     }
 320  
     private static UserSession getUserSession(HttpServletRequest request) {
 321  0
         return UserSession.getAuthenticatedUser();
 322  
     }
 323  
 }