Code source wiki de MeetingNotification

Modifié par Vincent Massol le 2011/08/12 21:26

Afficher les derniers auteurs
1 /* Groovy Class #* */
2
3 import java.util.*;
4 import com.xpn.xwiki.*;
5 import com.xpn.xwiki.api.*;
6 import com.xpn.xwiki.doc.*;
7 import com.xpn.xwiki.notify.*;
8
9 /**
10 * Automatic update of meetings.
11 */
12 public class MeetingUpdater implements XWikiDocChangeNotificationInterface
13 {
14
15 def rule;
16 def xwiki;
17
18 public MeetingUpdater()
19 {
20 this.rule = new DocChangeRule(this);
21 }
22
23 public void init(xwiki, context)
24 {
25 this.xwiki = xwiki;
26 xwiki.getXWiki().getNotificationManager().addGeneralRule(this.rule);
27
28 def sc = context.context.getEngineContext().getServletContext();
29
30 // Initializes in the servlet context the HashSet that will contains all flags
31 // for socracy documents. See the #notifiy method below.
32 sc.setAttribute("meetings_updated_documents", new HashSet())
33 }
34
35 public void cleanup()
36 {
37 xwiki.getXWiki().getNotificationManager().removeGeneralRule(this.rule);
38 }
39
40 private String getFullName(XWikiDocument doc, XWikiContext context) {
41 return context.getDatabase() + ":" + doc.fullName
42 }
43
44 public void notify(XWikiNotificationRule rule, XWikiDocument newdoc, XWikiDocument olddoc,
45 int event, XWikiContext context)
46 {
47 def newmeeting = null;
48 def oldmeeting = null;
49
50 if (newdoc.getObject("MMCode.MeetingClass") != null) {
51 newmeeting = newdoc.getObject("MMCode.MeetingClass")
52 if (olddoc.getObject("MMCode.MeetingClass") != null) {
53 oldmeeting = olddoc.getObject("MMCode.MeetingClass")
54 }
55 }
56
57 if(newmeeting != null && oldmeeting != null) {
58
59 if(newmeeting.getStringValue('privacy') != oldmeeting.getStringValue('privacy')
60 || newdoc.getObjectNumbers("MMCode.MeetingParticipantClass") !=
61 olddoc.getObjectNumbers("MMCode.MeetingParticipantClass")) {
62
63 def sc = context.getEngineContext().getServletContext();
64
65 // Just in case the HashSet has been removed from the servlet context
66 if (sc.getAttribute("meetings_updated_documents") == null)
67 sc.setAttribute("meetings_updated_documents", new HashSet())
68
69 // If the flag is set, last save was an update by this notification, so we remove
70 // the flag, and break.
71 if (sc.getAttribute("meetings_updated_documents").contains(getFullName(newdoc, context))) {
72 sc.getAttribute("meetings_updated_documents").remove(getFullName(newdoc, context))
73 return;
74 }
75
76 sc.getAttribute("meetings_updated_documents").add(getFullName(newdoc, context))
77
78 if (newmeeting.getStringValue('privacy') == "private") {
79 makePrivate(newdoc, context)
80 }
81 else {
82 makePublic(newdoc, context)
83 }
84
85 }
86 }
87 else if (oldmeeting == null) {
88 if(newdoc.getObjectNumbers("MMCode.MeetingParticipantClass") == 0) {
89 // meeting just created, add automatically the creator as a participant
90 def creator = newdoc.getObject("MMCode.MeetingParticipantClass", true, context);
91 creator.set("username", newdoc.getCreator(), context)
92
93 xwiki.saveDocument(newdoc, "[Automatic save] added creator as participant", true)
94 }
95 }
96 }
97
98 public void makePublic(XWikiDocument doc, XWikiContext context) {
99 def rights = doc.getObject("XWiki.XWikiRights", true, context)
100 rights.set("users", "", context)
101 rights.set("groups", "XWiki.XWikiAllGroup", context)
102 rights.set("levels", "view,edit", context)
103 rights.set("allow", "1", context)
104
105 xwiki.getXWiki().saveDocument(doc, "[Automatic save] - Updated rights", true, context)
106 }
107
108 public void makePrivate(XWikiDocument doc, XWikiContext context) {
109 def pList = doc.getObjects("MMCode.MeetingParticipantClass").collect({ return it.getStringValue('username') })
110 def rights = doc.getObject("XWiki.XWikiRights", true, context)
111 rights.set("users", pList.join(","), context)
112 rights.set("groups", "", context)
113 rights.set("levels", "view,edit", context)
114 rights.set("allow", "1", context)
115
116 xwiki.getXWiki().saveDocument(doc, "[Automatic save] - Updated rights", true, context)
117 }
118
119 }
120
121 /* *# */