|  1 |     | 
     | 
  |  2 |     | 
     | 
  |  3 |     | 
     | 
  |  4 |     | 
     | 
  |  5 |     | 
     | 
  |  6 |     | 
     | 
  |  7 |     | 
     | 
  |  8 |     | 
     | 
  |  9 |     | 
     | 
  |  10 |     | 
     | 
  |  11 |     | 
     | 
  |  12 |     | 
     | 
  |  13 |     | 
     | 
  |  14 |     | 
     | 
  |  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.bo.Person;  | 
  |  37 |     | 
   import org.kuali.rice.kim.service.KIMServiceLocator;  | 
  |  38 |     | 
   import org.kuali.rice.kns.UserSession;  | 
  |  39 |     | 
   import org.kuali.rice.kns.util.GlobalVariables;  | 
  |  40 |     | 
   import org.kuali.rice.kns.util.KNSConstants;  | 
  |  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 |     | 
     | 
  |  55 |     | 
     | 
  |  56 |     | 
     | 
  |  57 |     | 
     | 
  |  58 |     | 
     | 
  |  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 |     | 
         | 
  |  73 |     | 
         | 
  |  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.getRouteHeaderId());  | 
  |  102 |    0 |                noteForm.setNoteIdNumber(note.getNoteId());  | 
  |  103 |    0 |                edit(mapping, form, request, response);  | 
  |  104 |    0 |                return start(mapping, form, request, response);  | 
  |  105 |     | 
                 | 
  |  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 |     | 
     | 
  |  123 |     | 
     | 
  |  124 |     | 
     | 
  |  125 |     | 
     | 
  |  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.setRouteHeaderId(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.getRouteHeaderId());  | 
  |  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, KNSConstants.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 |     | 
     | 
  |  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 |    0 |                    if (noteForm.getNoteIdNumber() != null && (noteForm.getNoteIdNumber().intValue() == singleNote.getNoteId().intValue())) { | 
  |  243 |    0 |                        singleNote.setEditingNote(Boolean.TRUE);  | 
  |  244 |     | 
                   }  | 
  |  245 |    0 |                }  | 
  |  246 |    0 |                if (noteForm.getSortNotes() != null && noteForm.getSortNotes().booleanValue()) { | 
  |  247 |    0 |                    if (KEWConstants.Sorting.SORT_SEQUENCE_DSC.equalsIgnoreCase(noteForm.getSortOrder())) { | 
  |  248 |    0 |                        noteForm.setSortOrder(KEWConstants.Sorting.SORT_SEQUENCE_ASC);  | 
  |  249 |    0 |                        noteForm.setSortNotes(Boolean.FALSE);  | 
  |  250 |     | 
                   } else { | 
  |  251 |    0 |                        noteForm.setSortOrder(KEWConstants.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 = KIMServiceLocator.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 sortNotes(List allNotes, String sortOrder) { | 
  |  294 |    0 |            final int returnCode = KEWConstants.Sorting.SORT_SEQUENCE_DSC.equalsIgnoreCase(sortOrder) ? -1 : 1;  | 
  |  295 |     | 
     | 
  |  296 |     | 
           try { | 
  |  297 |    0 |              Collections.sort(allNotes,  | 
  |  298 |    0 |              new Comparator() { | 
  |  299 |     | 
               @Override  | 
  |  300 |     | 
                           public int compare(Object o1, Object o2) { | 
  |  301 |    0 |                              Timestamp date1 = ((Note) o1).getNoteCreateDate();  | 
  |  302 |    0 |                              Timestamp date2 = ((Note) 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 |     | 
   }  |