Code source wiki de MeetingAddParticipantService
Modifié par Vincent Massol le 2011/08/12 21:26
Afficher les derniers auteurs
| author | version | line-number | content |
|---|---|---|---|
| 1 | {{groovy}} | ||
| 2 | // Add participants to a meeting | ||
| 3 | // Optionally send a mail from template including an additional note | ||
| 4 | |||
| 5 | import com.xpn.xwiki.plugin.mailsender.Mail; | ||
| 6 | import java.util.ArrayList; | ||
| 7 | |||
| 8 | def success = true; | ||
| 9 | def error = ""; | ||
| 10 | def participants; | ||
| 11 | def meeting = request.meeting; | ||
| 12 | def nbUsers = 0; | ||
| 13 | def nbMails = 0; | ||
| 14 | def mailSent = false; | ||
| 15 | def meetingDoc; | ||
| 16 | def toAddr = ""; | ||
| 17 | |||
| 18 | meetingDoc = xwiki.getDocument(request.meeting) | ||
| 19 | meeting = meetingDoc.getObject("MMCode.MeetingClass") | ||
| 20 | |||
| 21 | if (request.emails && request.emails!="") { | ||
| 22 | toAddr = request.emails | ||
| 23 | def extra = request.mailnote; | ||
| 24 | def fromAddr = xwiki.getXWikiPreference("admin_email") | ||
| 25 | def ccAddr = null | ||
| 26 | def bccAddr = null | ||
| 27 | |||
| 28 | templateDoc = xwiki.getDocument(request.template) | ||
| 29 | templateDoc.use(templateDoc.getObject("XWiki.Mail", "language", $context.language)) | ||
| 30 | |||
| 31 | // Prepare mail template context | ||
| 32 | vcontext = xcontext.get("vcontext") | ||
| 33 | vcontext.put("meetingTitle", meetingDoc.title) | ||
| 34 | vcontext.put("meetingURL", meetingDoc.getExternalURL()) | ||
| 35 | |||
| 36 | textContent = xwiki.parseContent(templateDoc.getValue("text")) | ||
| 37 | htmlContent = xwiki.parseContent(templateDoc.getValue("html")) | ||
| 38 | subject = xwiki.parseContent(templateDoc.getValue("subject")) | ||
| 39 | |||
| 40 | // decorate text/HTML content with extra note. | ||
| 41 | if (!extra.equals("")) { | ||
| 42 | def args = new ArrayList(); | ||
| 43 | args.add(xwiki.getUserName(meetingDoc.creator, false)) | ||
| 44 | intro = msg.get("meetings.mail.personalnote", args) | ||
| 45 | textContent += ("\n\n" + intro + "\n\"" + extra + "\"") | ||
| 46 | htmlContent += ("<br /><br />" + intro + "<br />\"" +extra + "\"<br />") | ||
| 47 | } | ||
| 48 | |||
| 49 | // TODO: ultimately the code below should be merged in the mailsender plugin | ||
| 50 | vcontext.put("from.name", fromAddr); | ||
| 51 | vcontext.put("from.address", fromAddr); | ||
| 52 | vcontext.put("to.address", toAddr); | ||
| 53 | vcontext.put("to.cc", ccAddr); | ||
| 54 | vcontext.put("to.bcc", bccAddr); | ||
| 55 | vcontext.put("bounce", fromAddr); | ||
| 56 | |||
| 57 | Mail mail = new Mail(); | ||
| 58 | mail.setFrom((String) vcontext.get("from.address")); | ||
| 59 | mail.setTo((String) vcontext.get("to.address")); | ||
| 60 | mail.setCc((String) vcontext.get("to.cc")); | ||
| 61 | mail.setBcc((String) vcontext.get("to.bcc")); | ||
| 62 | mail.setSubject(subject); | ||
| 63 | mail.setTextPart(textContent); | ||
| 64 | mail.setHtmlPart(htmlContent); | ||
| 65 | |||
| 66 | try { | ||
| 67 | xwiki.getPlugin("mailsender").getProtectedPlugin().sendMail(mail, xcontext.context); | ||
| 68 | mailSent = true; | ||
| 69 | } | ||
| 70 | catch (Exception e) { | ||
| 71 | success = false; | ||
| 72 | error = e.message | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | // the mail was sent with success or was not needed | ||
| 77 | // we should now add the participants to the meeting | ||
| 78 | // and send back the HTML to represent the current participants | ||
| 79 | if (success) { | ||
| 80 | participants = request.participants.split(",") | ||
| 81 | if (participants) { | ||
| 82 | for (participant in participants) { | ||
| 83 | def obj = meetingDoc.newObject("MMCode.MeetingParticipantClass") | ||
| 84 | obj.set("username", participant) | ||
| 85 | obj.set("participation", "undecided") | ||
| 86 | nbUsers++; | ||
| 87 | } | ||
| 88 | } | ||
| 89 | meetingDoc.save(); | ||
| 90 | println "SUCCESS ${meeting} ${nbUsers} ${mailSent} ${nbMails} ${toAddr}"; | ||
| 91 | } else { | ||
| 92 | println "ERROR ${meeting} ${nbUsers} ${mailSent} ${nbMails} ${toAddr}:" + error; | ||
| 93 | } | ||
| 94 | {{/groovy}} |