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