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