001: /*
002: * Copyright 2005-2006 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.notes.web;
018:
019: import java.sql.Timestamp;
020: import java.text.DateFormat;
021: import java.util.Collections;
022: import java.util.Comparator;
023: import java.util.Date;
024: import java.util.Iterator;
025: import java.util.List;
026:
027: import javax.servlet.http.HttpServletRequest;
028: import javax.servlet.http.HttpServletResponse;
029:
030: import org.apache.commons.lang.StringUtils;
031: import org.apache.struts.action.ActionForm;
032: import org.apache.struts.action.ActionForward;
033: import org.apache.struts.action.ActionMapping;
034: import org.apache.struts.action.ActionMessages;
035: import org.apache.struts.upload.FormFile;
036:
037: import edu.iu.uis.eden.EdenConstants;
038: import edu.iu.uis.eden.KEWServiceLocator;
039: import edu.iu.uis.eden.notes.Attachment;
040: import edu.iu.uis.eden.notes.CustomNoteAttribute;
041: import edu.iu.uis.eden.notes.Note;
042: import edu.iu.uis.eden.notes.NoteService;
043: import edu.iu.uis.eden.routeheader.DocumentRouteHeaderValue;
044: import edu.iu.uis.eden.routeheader.RouteHeaderService;
045: import edu.iu.uis.eden.user.UserService;
046: import edu.iu.uis.eden.user.WorkflowUser;
047: import edu.iu.uis.eden.user.WorkflowUserId;
048: import edu.iu.uis.eden.util.Utilities;
049: import edu.iu.uis.eden.web.WorkflowAction;
050:
051: /**
052: * Struts action for interfacing with the Notes system.
053: *
054: * @see NoteService
055: *
056: * @author temay
057: * @author rkirkend
058: */
059: public class NoteAction extends WorkflowAction {
060:
061: private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
062: .getLogger(NoteAction.class);
063:
064: public ActionForward start(ActionMapping mapping, ActionForm form,
065: HttpServletRequest request, HttpServletResponse response)
066: throws Exception {
067: return mapping.findForward("allNotesReport");
068: }
069:
070: public ActionForward add(ActionMapping mapping, ActionForm form,
071: HttpServletRequest request, HttpServletResponse response)
072: throws Exception {
073: NoteForm noteForm = (NoteForm) form;
074: noteForm.setShowEdit("no");
075: noteForm.setNoteIdNumber(null);
076: retrieveNoteList(request, noteForm);
077: noteForm.setShowAdd(Boolean.TRUE);
078: return start(mapping, form, request, response);
079: }
080:
081: public ActionForward deleteAttachment(ActionMapping mapping,
082: ActionForm form, HttpServletRequest request,
083: HttpServletResponse response) throws Exception {
084: NoteForm noteForm = (NoteForm) form;
085: NoteService noteService = KEWServiceLocator.getNoteService();
086: Note note = noteService.getNoteByNoteId(noteForm.getNote()
087: .getNoteId());
088: noteService.deleteAttachment((Attachment) note.getAttachments()
089: .remove(0));
090: noteForm.setDocId(note.getRouteHeaderId());
091: noteForm.setNoteIdNumber(note.getNoteId());
092: edit(mapping, form, request, response);
093: return mapping.findForward("allNotesReport");
094: }
095:
096: public ActionForward edit(ActionMapping mapping, ActionForm form,
097: HttpServletRequest request, HttpServletResponse response)
098: throws Exception {
099: NoteForm noteForm = (NoteForm) form;
100: if ("yes".equalsIgnoreCase(noteForm.getShowEdit())) {
101: noteForm.setNoteIdNumber(noteForm.getNote().getNoteId());
102: } else {
103: noteForm.setShowEdit("yes");
104: Note noteToEdit = getNoteService().getNoteByNoteId(
105: noteForm.getNoteIdNumber());
106: noteForm.setNote(noteToEdit);
107: noteForm.getNote().setNoteCreateLongDate(
108: new Long(noteForm.getNote().getNoteCreateDate()
109: .getTime()));
110: }
111: retrieveNoteList(request, noteForm);
112: return start(mapping, form, request, response);
113: }
114:
115: // public ActionForward attachFile(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
116: //
117: //
118: // return start(mapping, form, request, response);
119: // }
120:
121: public ActionForward save(ActionMapping mapping, ActionForm form,
122: HttpServletRequest request, HttpServletResponse response)
123: throws Exception {
124: NoteForm noteForm = (NoteForm) form;
125: Note noteToSave = null;
126: if (noteForm.getShowEdit().equals("yes")) {
127: noteToSave = noteForm.getNote();
128: noteToSave.setNoteCreateDate(new Timestamp(noteToSave
129: .getNoteCreateLongDate().longValue()));
130: } else {
131: noteToSave = new Note();
132: noteToSave.setNoteId(null);
133: noteToSave.setRouteHeaderId(noteForm.getDocId());
134: noteToSave.setNoteCreateDate(new Timestamp((new Date())
135: .getTime()));
136: noteToSave.setNoteAuthorWorkflowId(getUserSession(request)
137: .getWorkflowUser().getWorkflowUserId()
138: .getWorkflowId());
139: noteToSave.setNoteText(noteForm.getAddText());
140: }
141: CustomNoteAttribute customNoteAttribute = null;
142: DocumentRouteHeaderValue routeHeader = getRouteHeaderService()
143: .getRouteHeader(noteToSave.getRouteHeaderId());
144: boolean canEditNote = false;
145: boolean canAddNotes = false;
146: if (routeHeader != null) {
147: customNoteAttribute = routeHeader.getCustomNoteAttribute();
148: if (customNoteAttribute != null) {
149: customNoteAttribute
150: .setUserSession(getUserSession(request));
151: canAddNotes = customNoteAttribute
152: .isAuthorizedToAddNotes();
153: canEditNote = customNoteAttribute
154: .isAuthorizedToEditNote(noteToSave);
155: }
156: }
157:
158: if ((noteForm.getShowEdit().equals("yes") && canEditNote)
159: || (!noteForm.getShowEdit().equals("yes") && canAddNotes)) {
160: FormFile uploadedFile = (FormFile) noteForm.getFile();
161: if (uploadedFile != null
162: && StringUtils.isNotBlank(uploadedFile
163: .getFileName())) {
164: Attachment attachment = new Attachment();
165: attachment.setAttachedObject(uploadedFile
166: .getInputStream());
167: attachment.setFileName(uploadedFile.getFileName());
168: attachment.setMimeType(uploadedFile.getContentType());
169: attachment.setNote(noteToSave);
170: noteToSave.getAttachments().add(attachment);
171: }
172: getNoteService().saveNote(noteToSave);
173: }
174: if (noteForm.getShowEdit().equals("yes")) {
175: noteForm.setNote(new Note());
176: } else {
177: noteForm.setAddText(null);
178: }
179: noteForm.setShowEdit("no");
180: noteForm.setNoteIdNumber(null);
181: retrieveNoteList(request, noteForm);
182: return start(mapping, form, request, response);
183: }
184:
185: public ActionForward delete(ActionMapping mapping, ActionForm form,
186: HttpServletRequest request, HttpServletResponse response)
187: throws Exception {
188: NoteForm noteForm = (NoteForm) form;
189: Note existingNote = getNoteService().getNoteByNoteId(
190: noteForm.getNoteIdNumber());
191: getNoteService().deleteNote(existingNote);
192: noteForm.setShowEdit("no");
193: noteForm.setNoteIdNumber(null);
194: retrieveNoteList(request, noteForm);
195: return start(mapping, form, request, response);
196: }
197:
198: public ActionForward cancel(ActionMapping mapping, ActionForm form,
199: HttpServletRequest request, HttpServletResponse response)
200: throws Exception {
201: NoteForm noteForm = (NoteForm) form;
202: noteForm.setShowEdit("no");
203: noteForm.setNote(new Note());
204: noteForm.setNoteIdNumber(null);
205: retrieveNoteList(request, noteForm);
206: return start(mapping, form, request, response);
207: }
208:
209: public ActionForward sort(ActionMapping mapping, ActionForm form,
210: HttpServletRequest request, HttpServletResponse response)
211: throws Exception {
212: return start(mapping, form, request, response);
213: }
214:
215: public ActionMessages establishRequiredState(
216: HttpServletRequest request, ActionForm form)
217: throws Exception {
218: NoteForm noteForm = (NoteForm) form;
219: noteForm.setCurrentUserName(getUserSession(request)
220: .getWorkflowUser().getDisplayName());
221: noteForm.setCurrentDate(getCurrentDate());
222: if (!"workflowReport".equalsIgnoreCase(noteForm
223: .getMethodToCall())
224: && !"add".equalsIgnoreCase(noteForm.getMethodToCall())
225: && !"cancel".equalsIgnoreCase(noteForm
226: .getMethodToCall())
227: && !"edit".equalsIgnoreCase(noteForm.getMethodToCall())
228: && !"delete".equalsIgnoreCase(noteForm
229: .getMethodToCall())
230: && !"save".equalsIgnoreCase(noteForm.getMethodToCall())) {
231: retrieveNoteList(request, noteForm);
232: }
233: noteForm
234: .setShowAttachments(new Boolean(
235: Utilities
236: .getApplicationConstant(EdenConstants.APP_CONST_SHOW_ATTACHMENTS)));
237: return null;
238: }
239:
240: private void retrieveNoteList(HttpServletRequest request,
241: NoteForm noteForm) throws Exception {
242: if (noteForm.getDocId() != null) {
243: // List allNotes = getNoteService().getNotesByRouteHeaderId(noteForm.getDocId());
244:
245: CustomNoteAttribute customNoteAttribute = null;
246: DocumentRouteHeaderValue routeHeader = getRouteHeaderService()
247: .getRouteHeader(noteForm.getDocId());
248:
249: List allNotes = routeHeader.getNotes();
250: boolean canAddNotes = false;
251: if (routeHeader != null) {
252: customNoteAttribute = routeHeader
253: .getCustomNoteAttribute();
254: if (customNoteAttribute != null) {
255: customNoteAttribute
256: .setUserSession(getUserSession(request));
257: canAddNotes = customNoteAttribute
258: .isAuthorizedToAddNotes();
259: }
260: }
261: Iterator notesIter = allNotes.iterator();
262: while (notesIter.hasNext()) {
263: Note singleNote = (Note) notesIter.next();
264: singleNote.setNoteCreateLongDate(new Long(singleNote
265: .getNoteCreateDate().getTime()));
266: getAuthorData(singleNote);
267: boolean canEditNote = false;
268: if (customNoteAttribute != null) {
269: canEditNote = customNoteAttribute
270: .isAuthorizedToEditNote(singleNote);
271: }
272: singleNote
273: .setAuthorizedToEdit(new Boolean(canEditNote));
274: if (noteForm.getNoteIdNumber() != null
275: && (noteForm.getNoteIdNumber().intValue() == singleNote
276: .getNoteId().intValue())) {
277: singleNote.setEditingNote(Boolean.TRUE);
278: }
279: }
280: if (noteForm.getSortNotes() != null
281: && noteForm.getSortNotes().booleanValue()) {
282: if (EdenConstants.Sorting.SORT_SEQUENCE_DSC
283: .equalsIgnoreCase(noteForm.getSortOrder())) {
284: noteForm
285: .setSortOrder(EdenConstants.Sorting.SORT_SEQUENCE_ASC);
286: noteForm.setSortNotes(new Boolean(false));
287: } else {
288: noteForm
289: .setSortOrder(EdenConstants.Sorting.SORT_SEQUENCE_DSC);
290: noteForm.setSortNotes(new Boolean(false));
291: }
292: } else {
293: noteForm.setSortOrder(noteForm.getSortOrder());
294: }
295: noteForm.setNoteList(sortNotes(allNotes, noteForm
296: .getSortOrder()));
297: noteForm.setNumberOfNotes(new Integer(allNotes.size()));
298: noteForm.setAuthorizedToAdd(new Boolean(canAddNotes));
299: noteForm.setShowAdd(Boolean.TRUE);
300: if (!canAddNotes) {
301: noteForm.setShowAdd(Boolean.FALSE);
302: } else if (noteForm.getNoteList().size() == 0) {
303: noteForm.setShowAdd(Boolean.FALSE);
304: }
305: }
306: }
307:
308: private void getAuthorData(Note note) throws Exception {
309: WorkflowUser workflowUser = null;
310: String id = "";
311: if (note != null && note.getNoteAuthorWorkflowId() != null
312: && !"".equalsIgnoreCase(note.getNoteAuthorWorkflowId())) {
313: workflowUser = getUserService().getWorkflowUser(
314: new WorkflowUserId(note.getNoteAuthorWorkflowId()));
315: id = note.getNoteAuthorWorkflowId();
316: }
317: if (workflowUser != null) {
318: note.setNoteAuthorFullName(workflowUser.getDisplayName());
319: note.setNoteAuthorEmailAddress(workflowUser
320: .getEmailAddress());
321: note.setNoteAuthorNetworkId(workflowUser
322: .getAuthenticationUserId().getAuthenticationId());
323: } else {
324: note.setNoteAuthorFullName(id + " (Name not Available)");
325: note.setNoteAuthorEmailAddress("Not Available");
326: note.setNoteAuthorNetworkId("Not Available");
327: }
328: }
329:
330: public String getCurrentDate() {
331: Date currentDate = new Date();
332: DateFormat dateFormat = EdenConstants.getDefaultDateFormat();
333: return dateFormat.format(currentDate);
334: }
335:
336: private List sortNotes(List allNotes, String sortOrder) {
337: final int returnCode = EdenConstants.Sorting.SORT_SEQUENCE_DSC
338: .equalsIgnoreCase(sortOrder) ? -1 : 1;
339:
340: try {
341: Collections.sort(allNotes, new Comparator() {
342: public int compare(Object o1, Object o2) {
343: Timestamp date1 = ((Note) o1).getNoteCreateDate();
344: Timestamp date2 = ((Note) o2).getNoteCreateDate();
345:
346: if (date1.before(date2)) {
347: return returnCode * -1;
348: } else if (date1.after(date2)) {
349: return returnCode;
350: } else {
351: return 0;
352: }
353: }
354: });
355: } catch (Throwable e) {
356: LOG.error(e.getMessage(), e);
357: }
358: return allNotes;
359: }
360:
361: private NoteService getNoteService() {
362: return (NoteService) KEWServiceLocator
363: .getService(KEWServiceLocator.NOTE_SERVICE);
364: }
365:
366: private UserService getUserService() {
367: return (UserService) KEWServiceLocator
368: .getService(KEWServiceLocator.USER_SERVICE);
369: }
370:
371: private RouteHeaderService getRouteHeaderService() {
372: return (RouteHeaderService) KEWServiceLocator
373: .getService(KEWServiceLocator.DOC_ROUTE_HEADER_SRV);
374: }
375:
376: }
|