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