Browse code

BM-11675 Fix: retrieve all pending events in one call, retrieve pending events calendar by calendar on notification

Laurent Belmonte authored on 20/06/2017 19:26:32
Showing 4 changed files
... ...
@@ -25,6 +25,7 @@ import net.bluemind.icalendar.api.ICalendarElement.ParticipationStatus;
25 25
 public class VEventAttendeeQuery {
26 26
 
27 27
 	public String dir;
28
+	public boolean calendarOwnerAsDir;
28 29
 	public ParticipationStatus partStatus;
29 30
 
30 31
 	public boolean singleValueSearch() {
... ...
@@ -61,6 +61,8 @@ import net.bluemind.core.container.service.internal.RBACManager;
61 61
 import net.bluemind.core.rest.BmContext;
62 62
 import net.bluemind.core.sanitizer.Sanitizer;
63 63
 import net.bluemind.core.validator.Validator;
64
+import net.bluemind.directory.api.DirEntry;
65
+import net.bluemind.directory.api.IDirectory;
64 66
 import net.bluemind.icalendar.api.ICalendarElement.Classification;
65 67
 import net.bluemind.icalendar.api.ICalendarElement.VAlarm;
66 68
 import net.bluemind.lib.vertx.VertxPlatform;
... ...
@@ -422,6 +424,12 @@ public class CalendarService implements ICalendar {
422 424
 		// FIXME double acl load
423 425
 		// use RBacManager insteadofff accessMAanger
424 426
 		ListResult<String> res = null;
427
+		if (query.attendee != null && query.attendee.calendarOwnerAsDir) {
428
+			DirEntry dirEntry = context.su().provider().instance(IDirectory.class, container.domainUid)
429
+					.findByEntryUid(container.owner);
430
+			query.attendee.dir = "bm://" + dirEntry.path;
431
+		}
432
+
425 433
 		res = indexStore.search(query, (context.getSecurityContext().isDomainGlobal()
426 434
 				|| context.getSecurityContext().getSubject().equals(container.owner)));
427 435
 
... ...
@@ -29,6 +29,7 @@ goog.require("net.bluemind.container.service.ContainersService");
29 29
 goog.require("net.bluemind.container.service.ContainersService.EventType");
30 30
 goog.require("net.bluemind.core.container.api.ContainerManagementClient");
31 31
 goog.require("net.bluemind.calendar.api.CalendarClient");
32
+goog.require("net.bluemind.calendar.api.CalendarsClient");
32 33
 goog.require("net.bluemind.container.service.ContainersObserver");
33 34
 /**
34 35
  * Service provider object for PendingEventsMgmt
... ...
@@ -45,12 +46,18 @@ net.bluemind.calendar.PendingEventsMgmt = function(ctx) {
45 46
   this.handler = new goog.events.EventHandler(this);
46 47
   this.totalCache_ = null;
47 48
   this.handler.listen(this.containersObserver, net.bluemind.container.service.ContainersObserver.EventType.CHANGE,
48
-      function() {
49
-        this.totalCache_ = null;
50
-        this.dispatchEvent('change');
49
+      function(e) {
50
+        if (e.containerType == 'folder.hierarchy') {
51
+          this.observeCalendars();
52
+        } else if (e.containerType == 'calendar') {
53
+          this.retrievePendingActionsForOne(e.container).then(function() {
54
+            this.dispatchEvent('change');
55
+          }, null, this);
56
+        }
51 57
       });
52 58
 
53 59
   this.calendars = [];
60
+  this.calendarsCounts = [];
54 61
   this.containersObserver.observerContainers('folder.hierarchy', [ this.getFolderContainerUid_() ]);
55 62
   this.observeCalendars();
56 63
 };
... ...
@@ -77,8 +84,14 @@ net.bluemind.calendar.PendingEventsMgmt.prototype.observeCalendars = function()
77 84
       return container['uid'];
78 85
     });
79 86
     this.calendars = containers;
87
+    this.calendarsCounts = [];
88
+    goog.array.forEach(this.calendars, function(container) {
89
+      this.calendarsCounts[container['uid']] = 0;
90
+    }, this);
80 91
     this.containersObserver.observerContainers('calendar', cals);
81
-    this.dispatchEvent('change');
92
+    this.retrievePendingActions_().then(function() {
93
+      this.dispatchEvent('change');
94
+    }, null, this);
82 95
   }, null, this);
83 96
 }
84 97
 
... ...
@@ -87,52 +100,111 @@ net.bluemind.calendar.PendingEventsMgmt.prototype.getCalendars = function() {
87 100
 }
88 101
 
89 102
 net.bluemind.calendar.PendingEventsMgmt.prototype.retrievePendingActions = function() {
90
-  if (this.totalCache_ != null) {
91
-    return goog.Promise.resolve(this.totalCache_);
92
-  }
103
+  return goog.Promise.resolve(this.totalCache_);
104
+}
93 105
 
94
-  var today = new net.bluemind.date.DateTime();
106
+net.bluemind.calendar.PendingEventsMgmt.prototype.retrievePendingActions_ = function() {
107
+  this.calendarsCounts = [];
108
+  goog.array.forEach(this.calendars, function(container) {
109
+    this.calendarsCounts[container['uid']] = 0;
110
+  }, this);
111
+
112
+  return this.retrievePendingActionsCals(this.calendars).then(function(totals) {
113
+    var total = 0;
114
+    goog.object.forEach(totals, function(ctotal, key) {
115
+      this.calendarsCounts[key] = ctotal;
116
+    }, this);
95 117
 
96
-  var defaultCalendars = goog.array.filter(this.calendars, function(cal) {
97
-    return cal['defaultContainer'];
118
+    goog.object.forEach(this.calendarsCounts, function(ctotal) {
119
+      total += ctotal;
120
+    });
121
+    console.log(" full ", this.calendarsCounts);
122
+    console.log("total ", total);
123
+    return total;
124
+  }, null, this).then(function(total) {
125
+    this.totalCache_ = total;
126
+    console.log("cache updated ", total);
127
+    this.totalCache_ = total;
128
+  }, null, this);
129
+}
130
+
131
+net.bluemind.calendar.PendingEventsMgmt.prototype.retrievePendingActionsForOne = function(calUid) {
132
+  this.calendarsCounts[calUid] = 0;
133
+  var defaultCalendars = goog.array.filter(this.calendars, function(c) {
134
+    return c['uid'] == calUid
98 135
   });
99 136
 
100
-  var futures = goog.array.map(defaultCalendars, function(cal) {
101
-    var calendarClient = new net.bluemind.calendar.api.CalendarClient(this.ctx.rpc, '', cal['uid']);
102
-    var query = {
137
+  return this.retrievePendingActionsCals(defaultCalendars).then(function(totals) {
138
+    var total = 0;
139
+    goog.object.forEach(totals, function(ctotal, key) {
140
+      this.calendarsCounts[key] = ctotal;
141
+    }, this);
142
+
143
+    goog.object.forEach(this.calendarsCounts, function(ctotal) {
144
+      total += ctotal;
145
+    });
146
+
147
+    console.log(" full ", this.calendarsCounts);
148
+    console.log("total ", total);
149
+
150
+    return total;
151
+  }, null, this).then(function(total) {
152
+    this.totalCache_ = total;
153
+    console.log("cache updated ", total);
154
+    this.totalCache_ = total;
155
+  }, null, this);
156
+}
157
+
158
+net.bluemind.calendar.PendingEventsMgmt.prototype.retrievePendingActionsCals = function(cals) {
159
+  var today = new net.bluemind.date.DateTime();
160
+
161
+  var defaultCalendars = cals;
162
+  var calendarsClient = new net.bluemind.calendar.api.CalendarsClient(this.ctx.rpc, '');
163
+  var query = {
164
+    'containers' : goog.array.map(defaultCalendars, function(cal) {
165
+      return cal['uid'];
166
+    }),
167
+    'eventQuery' : {
103 168
       'dateMin' : new net.bluemind.date.DateHelper().toBMDateTime(today),
104 169
       'attendee' : {
105
-        'dir' : 'bm://' + cal['ownerDirEntryPath'],
106
-        'partStatus' : 'NeedsAction'
170
+        'dir' : null,
171
+        'partStatus' : 'NeedsAction',
172
+        'calendarOwnerAsDir' : true
107 173
       }
108 174
     }
175
+  };
176
+
177
+  var future = calendarsClient.search(query).then(
178
+      function(res) {
179
+        var totals = [];
180
+        goog.array.forEach(res, function(series) {
181
+          var cal = goog.array.find(defaultCalendars, function(c) {
182
+            return c['uid'] == series["containerUid"]
183
+          });
109 184
 
110
-    return calendarClient.search(query).then(
111
-        function(res) {
112
-          var total = 0;
113
-          goog.array.forEach(res["values"], function(series) {
114
-            if (series["value"]["main"]
115
-                && net.bluemind.calendar.PendingEventsMgmt.attends(series["value"]["main"], 'bm://'
116
-                    + cal['ownerDirEntryPath'])) {
117
-              total += 1;
185
+          if (series["value"]["main"]
186
+              && net.bluemind.calendar.PendingEventsMgmt.attends(series["value"]["main"], 'bm://'
187
+                  + cal['ownerDirEntryPath'])) {
188
+            if (!totals[cal['uid']]) {
189
+              totals[cal['uid']] = 0;
190
+            }
191
+            totals[cal['uid']] += 1;
192
+          }
193
+          goog.array.forEach(series["value"]["occurrences"], function(vevent) {
194
+            if (net.bluemind.calendar.PendingEventsMgmt.attends(vevent, 'bm://' + cal['ownerDirEntryPath'])) {
195
+              if (!totals[cal['uid']]) {
196
+                totals[cal['uid']] = 0;
197
+              }
198
+              totals[cal['uid']] += 1;
118 199
             }
119
-            total += goog.array.filter(series["value"]["occurrences"], function(vevent) {
120
-              return net.bluemind.calendar.PendingEventsMgmt.attends(vevent, 'bm://' + cal['ownerDirEntryPath']);
121
-            }).length;
122 200
           });
123
-          return total;
124 201
         });
125
-  }, this);
126 202
 
127
-  return goog.Promise.all(futures).then(function(counts) {
203
+        console.log("totals ", totals, " for res", res);
204
+        return totals;
205
+      }, null, this);
128 206
 
129
-    var total = goog.array.reduce(counts, function(prev, curr) {
130
-      return prev + curr;
131
-    }, 0);
132
-    this.totalCache_ = total;
133
-    console.log("cache updated ", total);
134
-    return total;
135
-  }, null, this);
207
+  return future;
136 208
 }
137 209
 
138 210
 net.bluemind.calendar.PendingEventsMgmt.attends = function(vevent, dir) {
... ...
@@ -27,6 +27,7 @@ goog.require("goog.log");
27 27
 goog.require("goog.date.DateTime");
28 28
 goog.require("net.bluemind.date.DateTime");
29 29
 goog.require("net.bluemind.calendar.api.CalendarClient");
30
+goog.require("net.bluemind.calendar.api.CalendarsClient");
30 31
 goog.require("net.bluemind.date.DateHelper");
31 32
 goog.require("net.bluemind.container.service.ContainerObserver");
32 33
 goog.require("net.bluemind.container.service.ContainersObserver");
... ...
@@ -48,6 +49,7 @@ net.bluemind.calendar.banner.CalendarBanner = function() {
48 49
   this.handler.listen(this.containersObserver, net.bluemind.container.service.ContainersObserver.EventType.CHANGE,
49 50
       this.changed);
50 51
   this.calendars = [];
52
+  this.calendarsCounts = [];
51 53
   this.containersObserver.observerContainers('folder.hierarchy', [ this.getFolderContainerUid_() ]);
52 54
   this.observeCalendars();
53 55
 }
... ...
@@ -56,7 +58,7 @@ net.bluemind.calendar.banner.CalendarBanner.prototype.changed = function(e) {
56 58
   if (e.containerType == 'folder.hierarchy') {
57 59
     this.observeCalendars();
58 60
   } else if (e.containerType == 'calendar') {
59
-    this.retrievePendingActions();
61
+    this.retrievePendingActionsForOne(e.container);
60 62
   }
61 63
 }
62 64
 
... ...
@@ -78,49 +80,113 @@ net.bluemind.calendar.banner.CalendarBanner.prototype.observeCalendars = functio
78 80
     var cals = goog.array.map(defaultCalendars, function(container) {
79 81
       return container['uid'];
80 82
     });
83
+    this.calendarsCounts = [];
84
+    goog.array.forEach(defaultCalendars, function(container) {
85
+      this.calendarsCounts[container['uid']] = 0;
86
+    }, this);
81 87
     this.calendars = defaultCalendars;
82 88
     this.containersObserver.observerContainers('calendar', cals);
83
-    console.log("watched calendars ",cals);
89
+    console.log("watched calendars ", cals);
84 90
     this.retrievePendingActions();
85 91
   }, null, this);
86 92
 }
87 93
 
94
+net.bluemind.calendar.banner.CalendarBanner.prototype.retrievePendingActionsForOne = function(containerUid) {
95
+
96
+  var defaultCalendars = goog.array.filter(this.calendars, function(c) {
97
+    return c['uid'] == containerUid
98
+  });
99
+  
100
+  this.calendarsCounts[containerUid] = 0;
101
+  this.retrievePendingActionsCals(defaultCalendars).then(function(totals) {
102
+    var total = 0;
103
+    goog.object.forEach(totals, function(ctotal, key) {
104
+      this.calendarsCounts[key] = ctotal;
105
+    }, this);
106
+
107
+    goog.object.forEach(this.calendarsCounts, function(ctotal) {
108
+      total += ctotal;
109
+    });
110
+
111
+    return total;
112
+  }, null, this).then(function(total) {
113
+    net.bluemind.calendar.banner.CalendarBanner.notifyListener(total);
114
+  }, null, this);
115
+}
116
+
88 117
 net.bluemind.calendar.banner.CalendarBanner.prototype.retrievePendingActions = function() {
118
+  goog.array.forEach(this.calendars, function(container) {
119
+    this.calendarsCounts[container['uid']] = 0;
120
+  }, this);
121
+
122
+  this.retrievePendingActionsCals(this.calendars).then(function(totals) {
123
+    var total = 0;
124
+    goog.object.forEach(totals, function(ctotal, key) {
125
+      this.calendarsCounts[key] = ctotal;
126
+    }, this);
127
+    goog.object.forEach(this.calendarsCounts, function(ctotal) {
128
+      total += ctotal;
129
+    });
130
+    return total;
131
+  }, null, this).then(function(total) {
132
+    net.bluemind.calendar.banner.CalendarBanner.notifyListener(total);
133
+  }, null, this);
134
+}
135
+
136
+net.bluemind.calendar.banner.CalendarBanner.prototype.retrievePendingActionsCals = function(cals) {
89 137
 
90 138
   var today = new net.bluemind.date.DateTime();
91 139
 
92
-  var futures = goog.array.map(this.calendars, function(cal) {
93
-    var calendarClient = new net.bluemind.calendar.api.CalendarClient(this.rpc, '', cal['uid']);
94
-    var query = {
140
+  var defaultCalendars = cals;
141
+
142
+  var calendarsClient = new net.bluemind.calendar.api.CalendarsClient(this.rpc, '');
143
+  var query = {
144
+    'containers' : goog.array.map(defaultCalendars, function(cal) {
145
+      return cal['uid'];
146
+    }),
147
+    'eventQuery' : {
95 148
       'dateMin' : new net.bluemind.date.DateHelper().toBMDateTime(today),
96 149
       'attendee' : {
97
-        'dir' : 'bm://' + cal['ownerDirEntryPath'],
150
+        'dir' : null,
151
+        'calendarOwnerAsDir' : true,
98 152
         'partStatus' : 'NeedsAction'
99 153
       }
100 154
     }
101
-
102
-    return calendarClient.search(query).then(function(res) {
103
-      var total = 0;
104
-      goog.array.forEach(res["values"], function(series) {
105
-        if (series["value"]["main"] && net.bluemind.calendar.banner.CalendarBanner.attends(series["value"]["main"], 'bm://' + cal['ownerDirEntryPath'])) {
106
-          total += 1;
107
-        }
108
-        total += goog.array.filter(series["value"]["occurrences"], function(vevent) {
109
-          return net.bluemind.calendar.banner.CalendarBanner.attends(vevent, 'bm://' + cal['ownerDirEntryPath']);
110
-        }).length;
111
-      });
112
-      return total;
113
-    });
114
-  }, this);
115
-
116
-  goog.Promise.all(futures).then(function(counts) {
117
-
118
-    var total = goog.array.reduce(counts, function(prev, curr) {
119
-      return prev + curr;
120
-    }, 0);
121
-
122
-    net.bluemind.calendar.banner.CalendarBanner.notifyListener(total);
123
-  });
155
+  };
156
+
157
+  console.log("query ", query, calendarsClient);
158
+  var future = calendarsClient.search(query).then(
159
+      function(res) {
160
+        console.log("res ", res);
161
+        var total = 0;
162
+        var totals = [];
163
+        goog.array.forEach(res, function(series) {
164
+          var cal = goog.array.find(defaultCalendars, function(c) {
165
+            return c['uid'] == series["containerUid"]
166
+          });
167
+          console.log("cal ", cal, " for ", series, " from ", defaultCalendars);
168
+          if (series["value"]["main"]
169
+              && net.bluemind.calendar.banner.CalendarBanner.attends(series["value"]["main"], 'bm://'
170
+                  + cal['ownerDirEntryPath'])) {
171
+            if (!totals[cal['uid']]) {
172
+              totals[cal['uid']] = 0;
173
+            }
174
+            totals[cal['uid']] += 1;
175
+          }
176
+          goog.array.forEach(series["value"]["occurrences"], function(vevent) {
177
+            if (net.bluemind.calendar.banner.CalendarBanner.attends(vevent, 'bm://' + cal['ownerDirEntryPath'])) {
178
+              if (!totals[cal['uid']]) {
179
+                totals[cal['uid']] = 0;
180
+              }
181
+              totals[cal['uid']] += 1;
182
+            }
183
+          });
184
+        });
185
+
186
+        return totals;
187
+      }, null, this);
188
+
189
+  return future;
124 190
 }
125 191
 
126 192
 net.bluemind.calendar.banner.CalendarBanner.notifyListener = function(pendingActions) {