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