Browse code

BM-13097 Fix: check default container before deletion/update. reset container before deletion

David Phan authored on 13/04/2018 11:58:05
Showing 4 changed files
... ...
@@ -144,8 +144,8 @@ public class FolderBackend extends CoreConnect {
144 144
 	 * @param sf
145 145
 	 * @return
146 146
 	 */
147
-	public boolean updateFolder(BackendSession bs, BMFolder folder, SyncFolder sf) {
148
-		return storage.updateFolder(bs, folder.uri, sf.getDisplayName());
147
+	public boolean updateFolder(BackendSession bs, ItemDataType type, BMFolder folder, SyncFolder sf) {
148
+		return storage.updateFolder(bs, type, folder.uri, sf.getDisplayName());
149 149
 	}
150 150
 
151 151
 	/**
... ...
@@ -103,7 +103,7 @@ public class HierarchyImporter implements IHierarchyImporter {
103 103
 		if (folder != null) {
104 104
 			switch (ItemDataType.getValue(folder.type)) {
105 105
 			case CALENDAR:
106
-				ret = folderBackend.updateFolder(bs, folder, sf);
106
+				ret = folderBackend.updateFolder(bs, ItemDataType.CALENDAR, folder, sf);
107 107
 				break;
108 108
 			case CONTACTS:
109 109
 				logger.info("Update contacts folder is not implemented");
... ...
@@ -112,7 +112,7 @@ public class HierarchyImporter implements IHierarchyImporter {
112 112
 				ret = folderBackend.updateMailFolder(bs, sf);
113 113
 				break;
114 114
 			case TASKS:
115
-				ret = folderBackend.updateFolder(bs, folder, sf);
115
+				ret = folderBackend.updateFolder(bs, ItemDataType.TASKS, folder, sf);
116 116
 				break;
117 117
 			default:
118 118
 				break;
... ...
@@ -44,6 +44,9 @@ import net.bluemind.core.container.model.acl.AccessControlEntry;
44 44
 import net.bluemind.core.container.model.acl.Verb;
45 45
 import net.bluemind.core.rest.IServiceProvider;
46 46
 import net.bluemind.core.rest.http.ClientSideServiceProvider;
47
+import net.bluemind.core.task.api.ITask;
48
+import net.bluemind.core.task.api.TaskRef;
49
+import net.bluemind.core.task.api.TaskStatus;
47 50
 import net.bluemind.device.api.Device;
48 51
 import net.bluemind.device.api.IDevice;
49 52
 import net.bluemind.device.api.IDevices;
... ...
@@ -66,7 +69,6 @@ import net.bluemind.locator.client.LocatorClient;
66 66
 import net.bluemind.mailbox.api.IMailboxFolderHierarchy;
67 67
 import net.bluemind.system.api.ISystemConfiguration;
68 68
 import net.bluemind.todolist.api.ITodoList;
69
-import net.bluemind.todolist.api.VTodo;
70 69
 import net.bluemind.user.api.IUserSubscription;
71 70
 
72 71
 /**
... ...
@@ -257,26 +259,45 @@ public class SyncStorage implements ISyncStorage {
257 257
 	public boolean deleteFolder(BackendSession bs, ItemDataType type, String folderUri) {
258 258
 		boolean ret = false;
259 259
 
260
+		IContainers containers = provider().instance(IContainers.class);
261
+
260 262
 		try {
261 263
 			// split folderType://folderUid
262 264
 			List<String> s = Splitter.on("://").splitToList(folderUri);
263 265
 			String containerUid = s.get(1);
264 266
 
265 267
 			if (type == ItemDataType.TASKS) {
266
-				ITodoList service = provider().instance(ITodoList.class, containerUid);
267
-				List<ItemValue<VTodo>> tasks = service.all();
268
-				for (ItemValue<VTodo> task : tasks) {
269
-					service.delete(task.uid);
268
+				ContainerDescriptor container = containers.get(containerUid);
269
+				if (container.defaultContainer) {
270
+					logger.warn("Cannot delete default todolist {}", containerUid);
271
+					return true;
270 272
 				}
273
+
274
+				ITodoList service = provider().instance(ITodoList.class, containerUid);
275
+				// ITodoList.reset should return a TaskRef.
276
+				// see BM-13104
277
+				service.reset();
271 278
 			} else if (type == ItemDataType.CALENDAR) {
272
-				ICalendar service = provider().instance(ICalendar.class, containerUid);
273
-				List<String> all = service.all();
274
-				for (String uid : all) {
275
-					service.delete(uid, false);
279
+				ContainerDescriptor container = containers.get(containerUid);
280
+				if (container.defaultContainer) {
281
+					logger.warn("Cannot delete default calendar {}", containerUid);
282
+					return true;
276 283
 				}
284
+
285
+				ICalendar service = provider().instance(ICalendar.class, containerUid);
286
+				TaskRef tr = service.reset();
287
+				ITask taskApi = provider().instance(ITask.class, tr.id + "");
288
+				TaskStatus ts = null;
289
+				do {
290
+					try {
291
+						Thread.sleep(100);
292
+					} catch (InterruptedException e) {
293
+					}
294
+					ts = taskApi.status();
295
+				} while (!ts.state.ended);
296
+
277 297
 			}
278 298
 
279
-			IContainers containers = provider().instance(IContainers.class);
280 299
 			containers.delete(containerUid);
281 300
 
282 301
 			ret = true;
... ...
@@ -287,7 +308,7 @@ public class SyncStorage implements ISyncStorage {
287 287
 	}
288 288
 
289 289
 	@Override
290
-	public boolean updateFolder(BackendSession bs, String folderUri, String folderName) {
290
+	public boolean updateFolder(BackendSession bs, ItemDataType type, String folderUri, String folderName) {
291 291
 		boolean ret = false;
292 292
 
293 293
 		try {
... ...
@@ -296,6 +317,15 @@ public class SyncStorage implements ISyncStorage {
296 296
 			// split folderType://folderUid
297 297
 			List<String> s = Splitter.on("://").splitToList(folderUri);
298 298
 
299
+			if (type == ItemDataType.CALENDAR || type == ItemDataType.TASKS) {
300
+				String containerUid = s.get(1);
301
+				ContainerDescriptor container = containers.get(containerUid);
302
+				if (container.defaultContainer) {
303
+					logger.warn("Cannot update default {} {}", type, containerUid);
304
+					return true;
305
+				}
306
+			}
307
+
299 308
 			ContainerModifiableDescriptor descriptor = new ContainerModifiableDescriptor();
300 309
 			descriptor.name = folderName;
301 310
 			descriptor.defaultContainer = false;
... ...
@@ -76,7 +76,7 @@ public interface ISyncStorage {
76 76
 
77 77
 	boolean deleteFolder(BackendSession bs, ItemDataType type, String folderUri);
78 78
 
79
-	boolean updateFolder(BackendSession bs, String folderUri, String folderName);
79
+	boolean updateFolder(BackendSession bs, ItemDataType type, String folderUri, String folderName);
80 80
 
81 81
 	// Reset
82 82
 	boolean needReset(BackendSession bs, int folderId);