Coverage Report - org.kuali.rice.edl.impl.components.NoteConfigComponent
 
Classes in this File Line Coverage Branch Coverage Complexity
NoteConfigComponent
0%
0/362
0%
0/178
3.033
NoteConfigComponent$1
0%
0/8
0%
0/4
3.033
NoteConfigComponent$NoteForm
0%
0/72
0%
0/14
3.033
 
 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.edl.impl.components;
 18  
 
 19  
 import java.sql.Timestamp;
 20  
 import java.text.DateFormat;
 21  
 import java.util.ArrayList;
 22  
 import java.util.Collections;
 23  
 import java.util.Comparator;
 24  
 import java.util.Date;
 25  
 import java.util.Iterator;
 26  
 import java.util.List;
 27  
 import java.util.StringTokenizer;
 28  
 import java.util.regex.Matcher;
 29  
 import java.util.regex.Pattern;
 30  
 
 31  
 import javax.xml.parsers.DocumentBuilder;
 32  
 import javax.xml.parsers.DocumentBuilderFactory;
 33  
 import javax.xml.transform.Templates;
 34  
 import javax.xml.transform.TransformerConfigurationException;
 35  
 import javax.xml.xpath.XPath;
 36  
 import javax.xml.xpath.XPathConstants;
 37  
 import javax.xml.xpath.XPathExpressionException;
 38  
 import javax.xml.xpath.XPathFactory;
 39  
 
 40  
 import org.apache.commons.fileupload.FileItem;
 41  
 import org.apache.commons.lang.StringUtils;
 42  
 import org.apache.log4j.Logger;
 43  
 import org.kuali.rice.core.api.config.property.ConfigContext;
 44  
 import org.kuali.rice.core.api.services.CoreApiServiceLocator;
 45  
 import org.kuali.rice.core.framework.services.CoreFrameworkServiceLocator;
 46  
 import org.kuali.rice.core.mail.EmailBcList;
 47  
 import org.kuali.rice.core.mail.EmailBody;
 48  
 import org.kuali.rice.core.mail.EmailCcList;
 49  
 import org.kuali.rice.core.mail.EmailContent;
 50  
 import org.kuali.rice.core.mail.EmailFrom;
 51  
 import org.kuali.rice.core.mail.EmailSubject;
 52  
 import org.kuali.rice.core.mail.EmailToList;
 53  
 import org.kuali.rice.core.util.RiceConstants;
 54  
 import org.kuali.rice.core.util.xml.XmlHelper;
 55  
 import org.kuali.rice.core.util.xml.XmlJotter;
 56  
 import org.kuali.rice.edl.impl.EDLContext;
 57  
 import org.kuali.rice.edl.impl.EDLModelComponent;
 58  
 import org.kuali.rice.edl.impl.EDLXmlUtils;
 59  
 import org.kuali.rice.edl.impl.RequestParser;
 60  
 import org.kuali.rice.edl.impl.service.EdlServiceLocator;
 61  
 import org.kuali.rice.kew.dto.RouteHeaderDTO;
 62  
 import org.kuali.rice.kew.exception.WorkflowRuntimeException;
 63  
 import org.kuali.rice.kew.mail.EmailStyleHelper;
 64  
 import org.kuali.rice.kew.notes.Attachment;
 65  
 import org.kuali.rice.kew.notes.CustomNoteAttribute;
 66  
 import org.kuali.rice.kew.notes.Note;
 67  
 import org.kuali.rice.kew.notes.service.NoteService;
 68  
 import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
 69  
 import org.kuali.rice.kew.routeheader.service.RouteHeaderService;
 70  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 71  
 import org.kuali.rice.kew.service.WorkflowDocument;
 72  
 import org.kuali.rice.kew.util.KEWConstants;
 73  
 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
 74  
 import org.kuali.rice.kim.bo.Person;
 75  
 import org.w3c.dom.Document;
 76  
 import org.w3c.dom.Element;
 77  
 
 78  
 import com.thoughtworks.xstream.XStream;
 79  
 
 80  
 
 81  
 /**
 82  
  * Adds notes support to EDL
 83  
  *
 84  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 85  
  *
 86  
  */
 87  0
 public class NoteConfigComponent implements EDLModelComponent {
 88  
 
 89  0
         private static final Logger LOG = Logger.getLogger(NoteConfigComponent.class);
 90  
 
 91  0
         private EmailStyleHelper emailStyleHelper = new EmailStyleHelper();
 92  
         private String styleName;
 93  
         private String from;
 94  
         private List<String> to;
 95  0
         private List<String> cc = new ArrayList<String>();
 96  0
         private List<String> bc = new ArrayList<String>();
 97  0
         private static final String DEFAULT_EMAIL_FROM_ADDRESS = CoreFrameworkServiceLocator.getParameterService().getParameterValueAsString(KEWConstants.KEW_NAMESPACE, "Mailer", "FROM_ADDRESS");//"workflow@indiana.edu";
 98  
 
 99  
         public void updateDOM(Document dom, Element configElement, EDLContext edlContext) {
 100  0
                 NoteForm noteForm = new NoteForm(edlContext.getRequestParser());
 101  0
                 WorkflowDocument document = (WorkflowDocument)edlContext.getRequestParser().getAttribute(RequestParser.WORKFLOW_DOCUMENT_SESSION_KEY);
 102  
                 try {
 103  
                         //establish notes depends on a document id being set on noteform or nothing happens
 104  0
                         if (document != null) {
 105  0
                                 noteForm.setDocId(document.getDocumentId());
 106  
                         }
 107  0
                         establishNotes(noteForm, edlContext, dom);
 108  0
                         addNotes(dom, noteForm);
 109  0
                 } catch (Exception e) {
 110  0
                         throw new WorkflowRuntimeException("Caught exception processing notes", e);
 111  0
                 }
 112  
 
 113  0
         }
 114  
 
 115  
         public void establishNotes(NoteForm form, EDLContext edlContext, Document dom) throws Exception {
 116  
 
 117  0
                 form.setCurrentUserName(edlContext.getUserSession().getPerson().getName());
 118  0
                 form.setCurrentDate(getCurrentDate());
 119  0
                 String methodToCall = form.getMethodToCall();
 120  0
                 if (!org.apache.commons.lang.StringUtils.isEmpty(methodToCall)) {
 121  0
                         if ("save".equalsIgnoreCase(methodToCall)) {
 122  0
                                 this.saveNote(form, edlContext, dom);
 123  0
                         } else if ("edit".equalsIgnoreCase(methodToCall)) {
 124  0
                                 this.editNote(form);
 125  0
                         } else if ("add".equalsIgnoreCase(methodToCall)) {
 126  0
                                 this.addNote(form);
 127  0
                         } else if ("cancel".equalsIgnoreCase(methodToCall)) {
 128  0
                                 this.cancelEdit(form);
 129  0
                         } else if ("delete".equalsIgnoreCase(methodToCall)) {
 130  0
                                 this.deleteNote(form);
 131  0
                         } else if ("sort".equalsIgnoreCase(methodToCall)) {
 132  0
                                 this.sortNotes(form);
 133  0
                         } else if ("deleteAttachment".equalsIgnoreCase(methodToCall)) {
 134  0
                                 this.deleteAttachment(form);
 135  
                         }
 136  
                 }
 137  0
                 retrieveNoteList(form, edlContext);
 138  
 
 139  0
         }
 140  
 
 141  
     /**
 142  
      * Method added for notes editing function. Retrieve Note Listing from Route Header and put that in EdocLiteForm.
 143  
      * @param request
 144  
      * @param noteForm
 145  
      * @throws Exception
 146  
      */
 147  
 
 148  
     private void retrieveNoteList(NoteForm form, EDLContext edlContext) throws Exception {
 149  0
         if (form.getDocId() != null) {
 150  0
             List allNotes = getNoteService().getNotesByDocumentId(form.getDocId());
 151  0
             CustomNoteAttribute customNoteAttribute = null;
 152  0
             DocumentRouteHeaderValue routeHeader = getRouteHeaderService().getRouteHeader(form.getDocId());
 153  0
             boolean canAddNotes = false;
 154  0
             if (routeHeader != null) {
 155  0
                 customNoteAttribute = routeHeader.getCustomNoteAttribute();
 156  0
                 if (customNoteAttribute != null) {
 157  0
                         customNoteAttribute.setUserSession(edlContext.getUserSession());
 158  0
                     canAddNotes = customNoteAttribute.isAuthorizedToAddNotes();
 159  
                 }
 160  
             }
 161  0
             Iterator notesIter = allNotes.iterator();
 162  0
             while (notesIter.hasNext()) {
 163  0
                 Note singleNote = (Note) notesIter.next();
 164  0
                 singleNote.setNoteCreateLongDate(new Long(singleNote.getNoteCreateDate().getTime()));
 165  0
                 getAuthorData(singleNote);
 166  0
                 boolean canEditNote = false;
 167  0
                 if (customNoteAttribute != null) {
 168  0
                         canEditNote = customNoteAttribute.isAuthorizedToEditNote(singleNote);
 169  
                 }
 170  0
                     singleNote.setAuthorizedToEdit(Boolean.valueOf(canEditNote));
 171  0
                 if (form.getNoteIdNumber() != null && (form.getNoteIdNumber().intValue() == singleNote.getNoteId().intValue())) {
 172  0
                     singleNote.setEditingNote(Boolean.TRUE);
 173  
                 }
 174  0
             }
 175  0
             if (form.getSortNotes() != null && form.getSortNotes().booleanValue()) {
 176  0
                 if (KEWConstants.Sorting.SORT_SEQUENCE_DSC.equalsIgnoreCase(form.getSortOrder())) {
 177  0
                     form.setSortOrder(KEWConstants.Sorting.SORT_SEQUENCE_ASC);
 178  0
                     form.setSortNotes(Boolean.FALSE);
 179  
                 } else {
 180  0
                     form.setSortOrder(KEWConstants.Sorting.SORT_SEQUENCE_DSC);
 181  0
                     form.setSortNotes(Boolean.FALSE);
 182  
                 }
 183  
             } else {
 184  0
                 form.setSortOrder(form.getSortOrder());
 185  
             }
 186  0
             form.setNoteList(sortNotes(allNotes, form.getSortOrder()));
 187  0
             form.setNumberOfNotes(new Integer(allNotes.size()));
 188  0
             form.setAuthorizedToAdd(new Boolean(canAddNotes));
 189  0
             form.setShowAdd(Boolean.TRUE);
 190  0
             if (! canAddNotes) {
 191  0
                 form.setShowAdd(Boolean.FALSE);
 192  0
             } else if (form.getNoteList().size() == 0) {
 193  
                 //form.setShowAdd(Boolean.TRUE);
 194  
             }
 195  
         }
 196  0
     }
 197  
 
 198  
     public void editNote(NoteForm form) throws Exception {
 199  0
         form.setShowEdit("yes");
 200  
         // Note noteToEdit =
 201  
                 // getNoteService().getNoteByNoteId(form.getNoteIdNumber());
 202  
         //form.setNote(noteToEdit);
 203  0
         form.getNote().setNoteCreateLongDate(new Long(form.getNote().getNoteCreateDate().getTime()));
 204  0
         form.getNote().setNoteText(form.getNoteText());
 205  
         //retrieveNoteList(request, form);
 206  0
     }
 207  
 
 208  
     public void addNote(NoteForm form) throws Exception {
 209  0
             form.setShowEdit("no");
 210  0
             form.setNoteIdNumber(null);
 211  0
             form.setShowAdd(Boolean.TRUE);
 212  
             //retrieveNoteList(request,form);
 213  
 
 214  0
     }
 215  
 
 216  
     public void cancelEdit(NoteForm form) throws Exception{
 217  0
             form.setShowEdit("no");
 218  0
         form.setNote(new Note());
 219  0
         form.setNoteIdNumber(null);
 220  
         //retrieveNoteList(request, form);
 221  0
     }
 222  
 
 223  
     public void deleteNote(NoteForm form) throws Exception{
 224  0
             Note noteToDelete = getNoteService().getNoteByNoteId(form.getNoteIdNumber());
 225  0
         getNoteService().deleteNote(noteToDelete);
 226  0
             form.setShowEdit("no");
 227  
         //retrieveNoteList(request, form);
 228  0
         form.setNote(new Note());
 229  0
         form.setNoteIdNumber(null);
 230  0
     }
 231  
 
 232  
     public void sortNotes(NoteForm form) throws Exception{
 233  0
             form.setShowEdit("no");
 234  0
     }
 235  
 
 236  
     public void deleteAttachment(NoteForm form) throws Exception{
 237  0
             Note note = getNoteService().getNoteByNoteId(form.getNoteIdNumber());
 238  0
             getNoteService().deleteAttachment((Attachment)note.getAttachments().remove(0));
 239  0
         }
 240  
 
 241  
         public void saveNote(NoteForm form, EDLContext edlContext, Document dom) throws Exception {
 242  0
         Note noteToSave = null;
 243  0
         if (form.getShowEdit()!= null && form.getShowEdit().equals("yes")) {
 244  
                 //LOG.debug(form.getNoteIdNumber());
 245  0
             noteToSave = getNoteService().getNoteByNoteId(form.getNoteIdNumber());
 246  0
             String noteText = form.getNoteText();
 247  0
             if (noteText != null){
 248  0
                     noteToSave.setNoteText(noteText);
 249  
             }
 250  
             //LOG.debug(noteToSave);
 251  
             //LOG.debug(noteToSave.getNoteCreateDate());
 252  
             //noteToSave.setNoteCreateDate(new Timestamp(noteToSave.getNoteCreateLongDate().longValue()));
 253  0
         } else {
 254  0
             noteToSave = new Note();
 255  0
             noteToSave.setNoteId(null);
 256  0
             noteToSave.setDocumentId(form.getDocId());
 257  0
             noteToSave.setNoteCreateDate(new Timestamp((new Date()).getTime()));
 258  0
             noteToSave.setNoteAuthorWorkflowId(edlContext.getUserSession().getPrincipalId());
 259  0
             noteToSave.setNoteText(form.getAddText());
 260  
         }
 261  0
         CustomNoteAttribute customNoteAttribute = null;
 262  0
         DocumentRouteHeaderValue routeHeader = getRouteHeaderService().getRouteHeader(noteToSave.getDocumentId());
 263  0
         boolean canEditNote = false;
 264  0
         boolean canAddNotes = false;
 265  0
         if (routeHeader != null) {
 266  0
             customNoteAttribute = routeHeader.getCustomNoteAttribute();
 267  0
             if (customNoteAttribute != null) {
 268  0
                 customNoteAttribute.setUserSession(edlContext.getUserSession());
 269  0
                 canAddNotes = customNoteAttribute.isAuthorizedToAddNotes();
 270  0
                 canEditNote = customNoteAttribute.isAuthorizedToEditNote(noteToSave);
 271  
             }
 272  
         }
 273  0
         if ((form.getShowEdit()!=null && form.getShowEdit().equals("yes") && canEditNote) ||
 274  
                 ((form.getShowEdit() == null || ! form.getShowEdit().equals("yes")) && canAddNotes)) {
 275  0
                                 FileItem uploadedFile = (FileItem)form.getFile();
 276  0
                                 if (uploadedFile != null && org.apache.commons.lang.StringUtils.isNotBlank(uploadedFile.getName())) {
 277  0
                                         Attachment attachment = new Attachment();
 278  0
                                         attachment.setAttachedObject(uploadedFile.getInputStream());
 279  0
                                         String internalFileIndicator=uploadedFile.getName();
 280  0
                                         int indexOfSlash = internalFileIndicator.lastIndexOf("/");
 281  0
                                         int indexOfBackSlash=internalFileIndicator.lastIndexOf("\\");
 282  0
                                         if(indexOfSlash>=0){
 283  0
                                                 internalFileIndicator=internalFileIndicator.substring(indexOfSlash+1);
 284  
                                         }else{
 285  0
                                                 if (indexOfBackSlash>=0){
 286  0
                                                         internalFileIndicator=internalFileIndicator.substring(indexOfBackSlash+1);
 287  
                                                 }
 288  
                                         }
 289  0
                                         attachment.setFileName(internalFileIndicator);
 290  0
                                         LOG.debug(internalFileIndicator);
 291  0
                                         attachment.setMimeType(uploadedFile.getContentType());
 292  0
                                         attachment.setNote(noteToSave);
 293  0
                                         noteToSave.getAttachments().add(attachment);
 294  
                                 }
 295  0
                 if(org.apache.commons.lang.StringUtils.isEmpty(noteToSave.getNoteText())&&noteToSave.getAttachments().size()==0){
 296  0
                         if (form.getShowEdit()!=null && form.getShowEdit().equals("yes")) {
 297  0
                     form.setNote(new Note());
 298  
                 } else {
 299  0
                     form.setAddText(null);
 300  
                 }
 301  0
                 form.setShowEdit("no");
 302  0
                 form.setNoteIdNumber(null);
 303  
 //                        throw new Exception("Note has empty content");
 304  0
                 EDLXmlUtils.addGlobalErrorMessage(dom, "Note has empty content");
 305  0
                 return;
 306  
                 }
 307  0
             getNoteService().saveNote(noteToSave);
 308  
             
 309  
 
 310  
             // add ability to send emails when a note is saved. 
 311  0
             boolean sendEmailOnNoteSave = false;
 312  
                         // Check if edoclite specifies <param name="sendEmailOnNoteSave">
 313  0
                         Document edlDom = EdlServiceLocator.getEDocLiteService()
 314  
                                         .getDefinitionXml(edlContext.getEdocLiteAssociation());
 315  0
                         XPath xpath = XPathFactory.newInstance().newXPath();
 316  0
                         String xpathExpression = "//config/param[@name='sendEmailOnNoteSave']";
 317  
                         try {
 318  0
                                 String match = (String) xpath.evaluate(xpathExpression, edlDom,        XPathConstants.STRING);
 319  0
                                 if (!StringUtils.isBlank(match) && match.equals("true")) {
 320  0
                                         sendEmailOnNoteSave = true;
 321  
                                 }
 322  0
                         } catch (XPathExpressionException e) {
 323  0
                                 throw new WorkflowRuntimeException(
 324  
                                                 "Unable to evaluate sendEmailOnNoteSave xpath expression in NoteConfigComponent saveNote method"
 325  
                                                                 + xpathExpression, e);
 326  0
                         }
 327  
 
 328  0
                         if (sendEmailOnNoteSave) {
 329  0
                                 xpathExpression = "//data/version[@current='true']/field[@name='emailTo']/value";
 330  0
                                 String emailTo = xpath.evaluate(xpathExpression, dom);
 331  0
                                 if (StringUtils.isBlank(emailTo)) {
 332  0
                                         EDLXmlUtils.addGlobalErrorMessage(dom,"No email notifications were sent because EmailTo field was empty.");
 333  0
                                         return;
 334  
                                 }
 335  
                                 // Actually send the emails.
 336  0
                                 if (isProduction()) {
 337  0
                                         this.to = stringToList(emailTo);
 338  
                                 } else {
 339  0
                                         String testAddress = getTestAddress(edlDom);
 340  0
                                         if (StringUtils.isBlank(testAddress)) {
 341  0
                                                 EDLXmlUtils.addGlobalErrorMessage(
 342  
                                                                                 dom,
 343  
                                                                                 "No email notifications were sent because testAddress edl param was empty or not specified in a non production environment");
 344  0
                                                 return;
 345  
                                         }
 346  0
                                         this.to = stringToList(getTestAddress(edlDom));
 347  
         }
 348  0
                                 if (!isEmailListValid(this.to)) {
 349  0
                                         EDLXmlUtils.addGlobalErrorMessage(
 350  
                                                                         dom,
 351  
                                                                         "No email notifications were sent because emailTo field contains invalid email address.");
 352  0
                                         return;
 353  
                                 }
 354  0
                                 String noteEmailStylesheet = "";
 355  0
                                 xpathExpression = "//config/param[@name='noteEmailStylesheet']";
 356  
                                 try {
 357  0
                                         noteEmailStylesheet = (String) xpath.evaluate(
 358  
                                                         xpathExpression, edlDom, XPathConstants.STRING);
 359  0
                                         if (StringUtils.isBlank(noteEmailStylesheet)) {
 360  0
                                                 EDLXmlUtils.addGlobalErrorMessage(
 361  
                                                                                 dom,
 362  
                                                                                 "No email notifications were sent because noteEmailStylesheet edl param was empty or not specified.");
 363  0
                                                 return;
 364  
                                         }
 365  0
                                 } catch (XPathExpressionException e) {
 366  0
                                         throw new WorkflowRuntimeException(
 367  
                                                         "Unable to evaluate noteEmailStylesheet xpath expression in NoteConfigComponent method"
 368  
                                                                         + xpathExpression, e);
 369  0
                                 }
 370  0
                                 this.styleName = noteEmailStylesheet;
 371  0
                                 this.from = DEFAULT_EMAIL_FROM_ADDRESS;
 372  0
                                 Document document = generateXmlInput(form, edlContext, edlDom);
 373  0
                                 if (LOG.isDebugEnabled()) {
 374  0
                                         LOG.debug("XML input for email tranformation:\n" + XmlJotter.jotNode(document));
 375  
                                 }
 376  0
                                 Templates style = loadStyleSheet(styleName);
 377  0
                                 EmailContent emailContent = emailStyleHelper
 378  
                                                 .generateEmailContent(style, document);
 379  0
                                 if (!this.to.isEmpty()) {
 380  0
                                         KEWServiceLocator.getMailer().sendEmail(
 381  
                                                         new EmailFrom(from), new EmailToList(this.to),
 382  
                                                         new EmailSubject(emailContent.getSubject()),
 383  
                                                         new EmailBody(emailContent.getBody()),
 384  
                                                         new EmailCcList(this.cc), new EmailBcList(this.bc),
 385  
                                                         emailContent.isHtml());
 386  
                                 }
 387  
                         }
 388  
             
 389  
 
 390  
         }
 391  0
         if (form.getShowEdit()!=null && form.getShowEdit().equals("yes")) {
 392  0
             form.setNote(new Note());
 393  
         } else {
 394  0
             form.setAddText(null);
 395  
         }
 396  0
         form.setShowEdit("no");
 397  0
         form.setNoteIdNumber(null);
 398  0
     }
 399  
 
 400  
 
 401  
          protected String getTestAddress(Document edlDom) {
 402  0
                         String testAddress = "";
 403  0
                         XPath xpath = XPathFactory.newInstance().newXPath();
 404  0
                         String xpathExpression = "//config/param[@name='testAddress']";
 405  
                         try {
 406  0
                             testAddress = (String) xpath.evaluate(xpathExpression, edlDom, XPathConstants.STRING);
 407  0
                         } catch (XPathExpressionException e) {
 408  0
                             throw new WorkflowRuntimeException("Unable to evaluate testAddressAttributeFound xpath expression in NoteConfigComponent getTestAddress method" + xpathExpression, e);
 409  0
                         }
 410  0
                         return testAddress;
 411  
                     }
 412  
                     
 413  
                     protected Document generateXmlInput(NoteForm form, EDLContext edlContext, Document dom) throws Exception {
 414  0
                         DocumentBuilder db = getDocumentBuilder(true);
 415  0
                         Document doc = db.newDocument();
 416  0
                         Element emailNodeElem = doc.createElement("emailNode");
 417  0
                         doc.appendChild(emailNodeElem);
 418  0
                         WorkflowDocument document = (WorkflowDocument)edlContext.getRequestParser().getAttribute(RequestParser.WORKFLOW_DOCUMENT_SESSION_KEY);
 419  
 
 420  
                     /* Upgrade Changes 0914 to 1011 */
 421  
                         //RouteHeaderVO routeHeaderVO = document.getRouteHeader();
 422  0
                         RouteHeaderDTO routeHeaderVO = document.getRouteHeader();
 423  0
                         XStream xstream = new XStream();
 424  0
                         Element docElem = XmlHelper.readXml(xstream.toXML(routeHeaderVO)).getDocumentElement();
 425  0
                         emailNodeElem.appendChild(doc.importNode(docElem, true));
 426  0
                         emailNodeElem.appendChild(doc.importNode(dom.getDocumentElement(), true));
 427  0
                         Element dConElem = XmlHelper.readXml(document.getDocumentContent().getApplicationContent()).getDocumentElement(); //Add document Content element for
 428  0
                         emailNodeElem.appendChild(doc.importNode(dConElem, true)); //access by the stylesheet when creating the email
 429  0
                         return doc;
 430  
                     }
 431  
                     
 432  
                     protected DocumentBuilder getDocumentBuilder(boolean coalesce) throws Exception {
 433  0
                         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 434  0
                         dbf.setCoalescing(coalesce);
 435  0
                         return dbf.newDocumentBuilder();
 436  
                     }
 437  
                     
 438  
                     /* Upgrade Changes 0914 to 1011 */
 439  
                     /*
 440  
                     protected boolean isProduction() {
 441  
                         return EdenConstants.PROD_DEPLOYMENT_CODE.equalsIgnoreCase(Core.getCurrentContextConfig().getEnvironment());
 442  
                     }
 443  
                     */
 444  
                     protected boolean isProduction() {
 445  0
                             return ConfigContext.getCurrentContextConfig().getProperty(KEWConstants.PROD_DEPLOYMENT_CODE).equalsIgnoreCase(
 446  
                                             ConfigContext.getCurrentContextConfig().getEnvironment());
 447  
                     }
 448  
                     
 449  
                     protected boolean isEmailListValid(List<String> emailList) {
 450  0
                         Pattern p = Pattern.compile("^\\.|^\\@");
 451  0
                         Matcher m = null;
 452  0
                         for (String emailAddress : emailList) {
 453  0
                             m = p.matcher(emailAddress);
 454  0
                             if (m.find()) {
 455  
                                  //System.err.println("Email addresses don't start with dots or @ signs.");
 456  0
                                 return false;
 457  
                             }
 458  
                         }
 459  0
                         p = Pattern.compile("^www\\.");
 460  0
                         for (String emailAddress : emailList) {
 461  0
                             m = p.matcher(emailAddress);
 462  0
                             if (m.find()) {
 463  
                                  //System.err.println("Email addresses don't start with \"www.\", only web pages do.");
 464  0
                                 return false;
 465  
                             }
 466  
                         }
 467  
                         // find illegal characters.
 468  0
                         p = Pattern.compile("[^A-Za-z0-9\\.\\@_\\-~#]+");
 469  0
                         for (String emailAddress : emailList) {
 470  
                             // strip comma at end if there is one.
 471  0
                             String e2 = stripComma(emailAddress);
 472  0
                             m = p.matcher(e2);
 473  0
                             if (m.find()) {
 474  
                                  //System.err.println("Email address contains illegal character(s).");
 475  0
                                 return false;
 476  
                             }
 477  0
                         }
 478  
                         // email address should match this pattern.
 479  0
                         p = Pattern.compile("^([a-zA-Z0-9_\\-\\.]+)\\@([a-zA-Z0-9_\\-\\.]+)\\.([a-zA-Z]{2,5})$");
 480  0
                         for (String emailAddress : emailList) {
 481  0
                             String e2 = stripComma(emailAddress);
 482  0
                             m = p.matcher(e2);
 483  0
                             if (!m.find()) {
 484  
                                  //System.err.println("Illegal Email address format.");
 485  0
                                 return false;
 486  
                             }
 487  0
                         }
 488  0
                         return true;
 489  
                     }
 490  
                     
 491  
                     protected String stripComma(String s) {
 492  0
                         String sNew = "";
 493  0
                         if (s.endsWith(",")) {
 494  0
                             int x = s.length() -1;
 495  0
                             sNew = s.substring(0, x);
 496  0
                         } else {
 497  0
                             sNew = s;
 498  
                         }
 499  0
                         return sNew;
 500  
                     }
 501  
                     
 502  
                     protected List<String> stringToList(String to) {
 503  0
                         List<String> recipientAddresses = new ArrayList<String>();
 504  0
                         StringTokenizer st = new StringTokenizer(to, " ", false);
 505  0
                              while (st.hasMoreTokens()) {
 506  0
                                  recipientAddresses.add(st.nextToken());
 507  
                              }
 508  0
                         return recipientAddresses;
 509  
                     }
 510  
                     
 511  
                     protected Templates loadStyleSheet(String styleName) {
 512  
                         try {
 513  0
                             Templates style = CoreApiServiceLocator.getStyleService().getStyleAsTranslet(styleName);
 514  0
                             if (style == null) {
 515  0
                                 throw new WorkflowRuntimeException("Failed to locate stylesheet with name '" + styleName + "'");
 516  
                             }
 517  0
                             return style;
 518  0
                         } catch (TransformerConfigurationException tce) {
 519  0
                             throw new WorkflowRuntimeException("Failed to load stylesheet with name '" + styleName + "'");
 520  
                         }
 521  
                     }
 522  
 
 523  
         public static void addNotes(Document doc, NoteForm form) {
 524  0
                 Element noteForm = EDLXmlUtils.getOrCreateChildElement(doc.getDocumentElement(), "NoteForm", true);
 525  0
                 if (form.getShowEdit() != null) {
 526  0
                         Element showEdit = EDLXmlUtils.getOrCreateChildElement(noteForm, "showEdit", true);
 527  0
                         showEdit.appendChild(doc.createTextNode(form.getShowEdit().toLowerCase()));
 528  0
                 } else {
 529  0
                         Element showEdit = EDLXmlUtils.getOrCreateChildElement(noteForm, "showEdit", true);
 530  0
                         showEdit.appendChild(doc.createTextNode("no"));
 531  
                 }
 532  0
                 if (form.getShowAdd() != null) {
 533  0
                         Element showAdd = EDLXmlUtils.getOrCreateChildElement(noteForm, "showAdd", true);
 534  0
                         showAdd.appendChild(doc.createTextNode(form.getShowAdd().toString().toLowerCase()));
 535  
                 }
 536  0
                 if (form.getCurrentUserName() != null) {
 537  0
                         Element currentUserName = EDLXmlUtils.getOrCreateChildElement(noteForm, "currentUserName", true);
 538  0
                         currentUserName.appendChild(doc.createTextNode(form.getCurrentUserName()));
 539  
                 }
 540  0
                 if (form.getCurrentDate() != null) {
 541  0
                         Element currentDate = EDLXmlUtils.getOrCreateChildElement(noteForm, "currentDate", true);
 542  0
                         currentDate.appendChild(doc.createTextNode(form.getCurrentDate()));
 543  
                 }
 544  0
                 if (form.getNoteIdNumber() != null) {
 545  0
                         Element noteIdNumber = EDLXmlUtils.getOrCreateChildElement(noteForm, "noteIdNumber", true);
 546  0
                         noteIdNumber.appendChild(doc.createTextNode(form.getNoteIdNumber().toString()));
 547  
                 }
 548  0
                 if (form.getDocId() != null) {
 549  0
                         Element docId = EDLXmlUtils.getOrCreateChildElement(noteForm, "docId", true);
 550  0
                         docId.appendChild(doc.createTextNode((form.getDocId().toString())));
 551  
                 }
 552  0
                 if (form.getSortNotes() != null) {
 553  0
                         Element sortNotes = EDLXmlUtils.getOrCreateChildElement(noteForm, "sortNotes", true);
 554  0
                         sortNotes.appendChild(doc.createTextNode(form.getSortNotes().toString().toLowerCase()));
 555  
                 }
 556  0
                 if (form.getSortOrder() != null) {
 557  0
                         Element sortOrder = EDLXmlUtils.getOrCreateChildElement(noteForm, "sortOrder", true);
 558  0
                         sortOrder.appendChild(doc.createTextNode(form.getSortOrder().toUpperCase()));
 559  
                 }
 560  0
                 if (form.getNumberOfNotes() != null) {
 561  0
                         Element numberOfNotes = EDLXmlUtils.getOrCreateChildElement(noteForm, "numberOfNotes", true);
 562  0
                         numberOfNotes.appendChild(doc.createTextNode(form.getNumberOfNotes().toString()));
 563  
                 }
 564  0
                 if (form.getAuthorizedToAdd() != null) {
 565  0
                         Element authorizedToAdd = EDLXmlUtils.getOrCreateChildElement(noteForm, "authorizedToAdd", true);
 566  0
                         authorizedToAdd.appendChild(doc.createTextNode(form.getAuthorizedToAdd().toString().toLowerCase()));
 567  
                 }
 568  0
                 if (form.getNumberOfNotes().intValue() > 0) {
 569  0
                         Element notes = EDLXmlUtils.getOrCreateChildElement(noteForm, "Notes", true);
 570  0
                         for (Iterator i = form.getNoteList().iterator(); i.hasNext();) {
 571  0
                                 Note noteObj = (Note) i.next();
 572  0
                                 Element note = notes.getOwnerDocument().createElement("Note");
 573  0
                                 notes.appendChild(note);
 574  
                                 // Element note = Util.getOrCreateChildElement(notes, "Note",
 575  
                                 // true);
 576  0
                                 if (noteObj.getNoteId() != null) {
 577  0
                                         Element noteId = EDLXmlUtils.getOrCreateChildElement(note, "noteId", true);
 578  0
                                         noteId.appendChild(doc.createTextNode(noteObj.getNoteId().toString()));
 579  
                                 }
 580  0
                                 if (noteObj.getFormattedCreateDate() != null) {
 581  0
                                         Element formattedCreateDate = EDLXmlUtils.getOrCreateChildElement(note, "formattedCreateDate", true);
 582  0
                                         formattedCreateDate.appendChild(doc.createTextNode(noteObj.getFormattedCreateDate()));
 583  
                                 }
 584  0
                                 if (noteObj.getFormattedCreateTime() != null) {
 585  0
                                         Element formattedCreateTime = EDLXmlUtils.getOrCreateChildElement(note, "formattedCreateTime", true);
 586  0
                                         formattedCreateTime.appendChild(doc.createTextNode(noteObj.getFormattedCreateTime()));
 587  
                                 }
 588  0
                                 if (noteObj.getNoteAuthorFullName() != null) {
 589  0
                                         Element noteAuthorFullName = EDLXmlUtils.getOrCreateChildElement(note, "noteAuthorFullName", true);
 590  0
                                         noteAuthorFullName.appendChild(doc.createTextNode(noteObj.getNoteAuthorFullName()));
 591  
                                 }
 592  0
                                 if (noteObj.getNoteText() != null) {
 593  0
                                         Element noteText = EDLXmlUtils.getOrCreateChildElement(note, "noteText", true);
 594  0
                                         noteText.appendChild(doc.createTextNode(noteObj.getNoteText()));
 595  
                                 }
 596  0
                                 if (noteObj.getEditingNote() != null) {
 597  0
                                         Element editingNote = EDLXmlUtils.getOrCreateChildElement(note, "editingNote", true);
 598  0
                                         editingNote.appendChild(doc.createTextNode(noteObj.getEditingNote().toString()));
 599  
                                 }
 600  0
                                 if (noteObj.getAuthorizedToEdit() != null) {
 601  0
                                         Element authorizedToEdit = EDLXmlUtils.getOrCreateChildElement(note, "authorizedToEdit", true);
 602  0
                                         authorizedToEdit.appendChild(doc.createTextNode(noteObj.getAuthorizedToEdit().toString()));
 603  
                                 }
 604  0
                                 if (!noteObj.getAttachments().isEmpty()) {
 605  0
                                         Element attachments = EDLXmlUtils.getOrCreateChildElement(note, "attachments", true);
 606  0
                                         for (Iterator j = noteObj.getAttachments().iterator(); j.hasNext();) {
 607  0
                                                 Attachment attachmentObj = (Attachment) j.next();
 608  0
                                                 Element attachment = EDLXmlUtils.getOrCreateChildElement(attachments, "attachment", true);
 609  0
                                                 Element attachmentId = EDLXmlUtils.getOrCreateChildElement(attachment, "attachmentId", true);
 610  0
                                                 attachmentId.appendChild(doc.createTextNode(attachmentObj.getAttachmentId().toString()));
 611  0
                                                 Element fileName = EDLXmlUtils.getOrCreateChildElement(attachment, "fileName", true);
 612  0
                                                 fileName.appendChild(doc.createTextNode(attachmentObj.getFileName()));
 613  0
                                         }
 614  
                                 }
 615  0
                         }
 616  
                 }
 617  
 
 618  0
         }
 619  
 
 620  0
         private static class NoteForm {
 621  
                 private String showEdit;
 622  
                 private Boolean showAdd;
 623  
                 private Long noteIdNumber;
 624  0
                 private Integer numberOfNotes = new Integer(0);
 625  0
                 private String sortOrder = "DESCENDING";
 626  
                 private Boolean sortNotes;
 627  
                 private String currentUserName;
 628  
                 private String currentDate;
 629  
                 private Boolean authorizedToAdd;
 630  
                 private List noteList;
 631  
                 private String addText;
 632  
                 private Long idInEdit;
 633  
                 private Note note;
 634  
                 private String noteText;
 635  
                 private String docId;
 636  
                 private String methodToCall;
 637  
                 private FileItem file;
 638  
 
 639  0
                 public NoteForm(RequestParser requestParser) {
 640  
 
 641  0
                         showEdit = requestParser.getParameterValue("showEdit");
 642  0
                         if (!org.apache.commons.lang.StringUtils.isEmpty(requestParser.getParameterValue("showAdd"))) {
 643  0
                                 showAdd = Boolean.valueOf(requestParser.getParameterValue("showAdd"));
 644  
                         }
 645  0
                         if (!org.apache.commons.lang.StringUtils.isEmpty(requestParser.getParameterValue("noteIdNumber"))) {
 646  0
                                 noteIdNumber = Long.valueOf(requestParser.getParameterValue("noteIdNumber"));
 647  
                         }
 648  0
                         methodToCall = requestParser.getParameterValue("methodToCall");
 649  0
                         sortOrder = "DESCENDING";
 650  0
                         if (!org.apache.commons.lang.StringUtils.isEmpty(requestParser.getParameterValue("sortNotes"))) {
 651  0
                                 sortNotes = Boolean.valueOf(requestParser.getParameterValue("sortNotes"));
 652  
                         }
 653  0
                         addText = requestParser.getParameterValue("addText");
 654  0
                         noteText = requestParser.getParameterValue("noteText");
 655  0
                         if (!org.apache.commons.lang.StringUtils.isEmpty(requestParser.getParameterValue("idInEdit"))) {
 656  0
                                 idInEdit = Long.valueOf(requestParser.getParameterValue("idInEdit"));
 657  
                         }
 658  0
                         if (noteIdNumber != null) {
 659  0
                                 note = KEWServiceLocator.getNoteService().getNoteByNoteId(noteIdNumber);
 660  
                         }
 661  0
                         if (requestParser.getUploadList() != null && ! requestParser.getUploadList().isEmpty()) {
 662  0
                                 file = (FileItem)requestParser.getUploadList().get(0);
 663  
                         }
 664  0
                 }
 665  
 
 666  
                 public String getAddText() {
 667  0
                         return addText;
 668  
                 }
 669  
 
 670  
                 public void setAddText(String addText) {
 671  0
                         this.addText = addText;
 672  0
                 }
 673  
 
 674  
                 public Boolean getAuthorizedToAdd() {
 675  0
                         return authorizedToAdd;
 676  
                 }
 677  
 
 678  
                 public void setAuthorizedToAdd(Boolean authorizedToAdd) {
 679  0
                         this.authorizedToAdd = authorizedToAdd;
 680  0
                 }
 681  
 
 682  
                 public String getCurrentDate() {
 683  0
                         return currentDate;
 684  
                 }
 685  
 
 686  
                 public void setCurrentDate(String currentDate) {
 687  0
                         this.currentDate = currentDate;
 688  0
                 }
 689  
 
 690  
                 public String getCurrentUserName() {
 691  0
                         return currentUserName;
 692  
                 }
 693  
 
 694  
                 public void setCurrentUserName(String currentUserName) {
 695  0
                         this.currentUserName = currentUserName;
 696  0
                 }
 697  
 
 698  
                 public Long getIdInEdit() {
 699  0
                         return idInEdit;
 700  
                 }
 701  
 
 702  
                 public void setIdInEdit(Long idInEdit) {
 703  0
                         this.idInEdit = idInEdit;
 704  0
                 }
 705  
 
 706  
                 public Note getNote() {
 707  0
                         return note;
 708  
                 }
 709  
 
 710  
                 public void setNote(Note note) {
 711  0
                         this.note = note;
 712  0
                 }
 713  
 
 714  
                 public Long getNoteIdNumber() {
 715  0
                         return noteIdNumber;
 716  
                 }
 717  
 
 718  
                 public void setNoteIdNumber(Long noteIdNumber) {
 719  0
                         this.noteIdNumber = noteIdNumber;
 720  0
                 }
 721  
 
 722  
                 public List getNoteList() {
 723  0
                         return noteList;
 724  
                 }
 725  
 
 726  
                 public void setNoteList(List noteList) {
 727  0
                         this.noteList = noteList;
 728  0
                 }
 729  
 
 730  
                 public String getNoteText() {
 731  0
                         return noteText;
 732  
                 }
 733  
 
 734  
                 public void setNoteText(String noteText) {
 735  0
                         this.noteText = noteText;
 736  0
                 }
 737  
 
 738  
                 public Integer getNumberOfNotes() {
 739  0
                         return numberOfNotes;
 740  
                 }
 741  
 
 742  
                 public void setNumberOfNotes(Integer numberOfNotes) {
 743  0
                         this.numberOfNotes = numberOfNotes;
 744  0
                 }
 745  
 
 746  
                 public Boolean getShowAdd() {
 747  0
                         return showAdd;
 748  
                 }
 749  
 
 750  
                 public void setShowAdd(Boolean showAdd) {
 751  0
                         this.showAdd = showAdd;
 752  0
                 }
 753  
 
 754  
                 public String getShowEdit() {
 755  0
                         return showEdit;
 756  
                 }
 757  
 
 758  
                 public void setShowEdit(String showEdit) {
 759  0
                         this.showEdit = showEdit;
 760  0
                 }
 761  
 
 762  
                 public Boolean getSortNotes() {
 763  0
                         return sortNotes;
 764  
                 }
 765  
 
 766  
                 public void setSortNotes(Boolean sortNotes) {
 767  0
                         this.sortNotes = sortNotes;
 768  0
                 }
 769  
 
 770  
                 public String getSortOrder() {
 771  0
                         return sortOrder;
 772  
                 }
 773  
 
 774  
                 public void setSortOrder(String sortOrder) {
 775  0
                         this.sortOrder = sortOrder;
 776  0
                 }
 777  
 
 778  
                 public String getDocId() {
 779  0
                         return docId;
 780  
                 }
 781  
 
 782  
                 public void setDocId(String docId) {
 783  0
                         this.docId = docId;
 784  0
                 }
 785  
 
 786  
                 public String getMethodToCall() {
 787  0
                         return methodToCall;
 788  
                 }
 789  
 
 790  
                 public void setMethodToCall(String methodToCall) {
 791  0
                         this.methodToCall = methodToCall;
 792  0
                 }
 793  
 
 794  
                 public FileItem getFile() {
 795  0
                         return file;
 796  
                 }
 797  
 
 798  
                 public void setFile(FileItem file) {
 799  0
                         this.file = file;
 800  0
                 }
 801  
         }
 802  
 
 803  
     /**
 804  
      * Method added for notes editing function. Called by retrieveNoteList method
 805  
      * @param allNotes
 806  
      * @param sortOrder
 807  
      * @return
 808  
      */
 809  
 
 810  
     private List sortNotes(List allNotes, String sortOrder) {
 811  0
         final int returnCode = KEWConstants.Sorting.SORT_SEQUENCE_DSC.equalsIgnoreCase(sortOrder) ? -1 : 1;
 812  
 
 813  
         try {
 814  0
           Collections.sort(allNotes,
 815  0
           new Comparator() {
 816  
             public int compare(Object o1, Object o2) {
 817  0
                           Timestamp date1 = ((Note) o1).getNoteCreateDate();
 818  0
                           Timestamp date2 = ((Note) o2).getNoteCreateDate();
 819  
 
 820  0
                 if (date1.before(date2)) {
 821  0
                   return returnCode * -1;
 822  0
                 } else if (date1.after(date2)) {
 823  0
                   return returnCode;
 824  
                 } else {
 825  0
                   return 0;
 826  
                 }
 827  
             }
 828  
           });
 829  0
       } catch (Throwable e) {
 830  0
         LOG.error(e.getMessage(), e);
 831  0
       }
 832  0
       return allNotes;
 833  
     }
 834  
 
 835  
     /**
 836  
      * Method added for notes editing function. Called by retrieveNoteList method
 837  
      * @param note
 838  
      * @throws Exception
 839  
      */
 840  
 
 841  
     private void getAuthorData(Note note) throws Exception {
 842  0
             Person workflowUser = null;
 843  0
             String id = "";
 844  0
             if (note != null && note.getNoteAuthorWorkflowId() != null && ! "".equalsIgnoreCase(note.getNoteAuthorWorkflowId())) {
 845  0
                 workflowUser = KimApiServiceLocator.getPersonService().getPerson(note.getNoteAuthorWorkflowId());
 846  0
                 id = note.getNoteAuthorWorkflowId();
 847  
             }
 848  0
             if (workflowUser != null) {
 849  0
                 note.setNoteAuthorFullName(workflowUser.getName());
 850  0
                 note.setNoteAuthorEmailAddress(workflowUser.getEmailAddress());
 851  0
                 note.setNoteAuthorNetworkId(workflowUser.getPrincipalName());
 852  
             } else {
 853  0
                 note.setNoteAuthorFullName(id + " (Name not Available)");
 854  0
                 note.setNoteAuthorEmailAddress("Not Available");
 855  0
                 note.setNoteAuthorNetworkId("Not Available");
 856  
             }
 857  0
         }
 858  
 
 859  
         public String getCurrentDate() {
 860  0
         Date currentDate = new Date();
 861  0
         DateFormat dateFormat = RiceConstants.getDefaultDateFormat();
 862  0
         return dateFormat.format(currentDate);
 863  
     }
 864  
 
 865  
         /**
 866  
          * Method added for notes editing function.
 867  
          * @return
 868  
          */
 869  
     private NoteService getNoteService() {
 870  0
                 return (NoteService) KEWServiceLocator.getService(KEWServiceLocator.NOTE_SERVICE);
 871  
         }
 872  
 
 873  
     /**
 874  
      * Method added for notes editing function.
 875  
      * @return
 876  
      */
 877  
     private RouteHeaderService getRouteHeaderService() {
 878  0
                 return (RouteHeaderService) KEWServiceLocator.getService(KEWServiceLocator.DOC_ROUTE_HEADER_SRV);
 879  
         }
 880  
 }