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