Browse code

POPIT-166 Fix: new sub folder sync

David Phan authored on 11/06/2018 13:39:35
Showing 3 changed files
... ...
@@ -24,13 +24,15 @@ public class BMFolder {
24 24
 
25 25
 	public long uid;
26 26
 	public int id;
27
+	public long parentId;
27 28
 	public String uri;
28 29
 	public Folder.Type type;
29 30
 	public String name;
30 31
 	public String owner;
31 32
 	public String path;
32 33
 
33
-	public BMFolder(int id, long uid, String uri, Folder.Type type, String name, String owner, String path) {
34
+	public BMFolder(int id, long uid, String uri, Folder.Type type, String name, String owner, String path,
35
+			long parentId) {
34 36
 		this.uid = uid;
35 37
 		this.id = id;
36 38
 		this.uri = uri;
... ...
@@ -38,6 +40,7 @@ public class BMFolder {
38 38
 		this.name = name;
39 39
 		this.owner = owner;
40 40
 		this.path = path;
41
+		this.parentId = parentId;
41 42
 	}
42 43
 
43 44
 	@Override
... ...
@@ -359,7 +359,7 @@ public class FolderBackend extends CoreConnect {
359 359
 			}
360 360
 		}
361 361
 
362
-		checkHierarchy(folders);
362
+		checkHierarchy(folders, service);
363 363
 
364 364
 		return folders;
365 365
 	}
... ...
@@ -528,15 +528,16 @@ public class FolderBackend extends CoreConnect {
528 528
 		return cm.getDescriptor();
529 529
 	}
530 530
 
531
-	private void checkHierarchy(LinkedList<FolderChangeReference> folders) {
531
+	private void checkHierarchy(LinkedList<FolderChangeReference> folders, IFolderHierarchy service) {
532 532
 
533 533
 		Map<Long, FolderChangeReference> all = folders.stream()
534 534
 				.collect(Collectors.toMap(item -> item.folderId, item -> item));
535 535
 
536
-		folders.removeIf(f -> !isFolderValid(f, all));
536
+		folders.removeIf(f -> !isFolderValid(f, all, service));
537 537
 	}
538 538
 
539
-	private boolean isFolderValid(FolderChangeReference f, Map<Long, FolderChangeReference> all) {
539
+	private boolean isFolderValid(FolderChangeReference f, Map<Long, FolderChangeReference> currentFolderChanges,
540
+			IFolderHierarchy service) {
540 541
 		if (f.folderId == f.parentId) {
541 542
 			return false;
542 543
 		}
... ...
@@ -545,11 +546,25 @@ public class FolderBackend extends CoreConnect {
545 545
 		Set<Long> parentsId = new HashSet<Long>();
546 546
 
547 547
 		while (parentId > 0) {
548
-			FolderChangeReference parent = all.get(parentId);
549
-			if (parent == null) {
550
-				return false;
548
+			FolderChangeReference parent = currentFolderChanges.get(parentId);
549
+			if (parent != null) {
550
+				parentId = parent.parentId;
551
+			} else {
552
+
553
+				try {
554
+					ItemValue<Folder> parentFolder = service.byUid(parentId);
555
+					parentId = parentFolder.value.parentId;
556
+
557
+					if (parentId > 0) {
558
+						parentId = service.byId(parentFolder.value.parentId).value.uid;
559
+					}
560
+
561
+				} catch (Exception e) {
562
+					logger.info("{} parent id {} does not exist", f.displayName, parentId, e);
563
+					return false;
564
+				}
551 565
 			}
552
-			parentId = parent.parentId;
566
+
553 567
 			if (parentsId.contains(parentId)) {
554 568
 				return false;
555 569
 			}
... ...
@@ -171,7 +171,7 @@ public class SyncStorage implements ISyncStorage {
171 171
 		try {
172 172
 			Folder folder = getFoldersService().byUid(new Long(collectionId));
173 173
 			return new BMFolder(collectionId, folder.uid, folder.contentUri, folder.type, folder.name, folder.owner,
174
-					folder.path);
174
+					folder.path, folder.parentId);
175 175
 		} catch (Exception e) {
176 176
 			throw new CollectionNotFoundException("Collection " + collectionId + " not found");
177 177
 		}
... ...
@@ -183,7 +183,7 @@ public class SyncStorage implements ISyncStorage {
183 183
 		try {
184 184
 			ItemValue<Folder> folder = getFolderHierarchyService(userUid).byContentUri(folderUri);
185 185
 			return new BMFolder(new Long(folder.value.uid).intValue(), folder.value.uid, folder.value.contentUri,
186
-					folder.value.type, folder.value.name, folder.value.owner, folder.value.path);
186
+					folder.value.type, folder.value.name, folder.value.owner, folder.value.path, folder.value.parentId);
187 187
 		} catch (ServerFault t) {
188 188
 			logger.error(t.getMessage(), t);
189 189
 			throw new CollectionNotFoundException("Collection uri " + folderUri + " not found");
... ...
@@ -195,7 +195,7 @@ public class SyncStorage implements ISyncStorage {
195 195
 		try {
196 196
 			ItemValue<Folder> folder = getMailboxFolderHierarchyService(userUid).byPath(path);
197 197
 			return new BMFolder(new Long(folder.value.uid).intValue(), folder.value.uid, folder.value.contentUri,
198
-					folder.value.type, folder.value.name, folder.value.owner, folder.value.path);
198
+					folder.value.type, folder.value.name, folder.value.owner, folder.value.path, folder.value.parentId);
199 199
 
200 200
 		} catch (ServerFault t) {
201 201
 			logger.error(t.getMessage(), t);
... ...
@@ -209,7 +209,8 @@ public class SyncStorage implements ISyncStorage {
209 209
 		ArrayList<BMFolder> ret = new ArrayList<BMFolder>();
210 210
 		for (ItemValue<Folder> folder : folders) {
211 211
 			ret.add(new BMFolder(new Long(folder.value.uid).intValue(), folder.value.uid, folder.value.contentUri,
212
-					folder.value.type, folder.value.name, folder.value.owner, folder.value.path));
212
+					folder.value.type, folder.value.name, folder.value.owner, folder.value.path,
213
+					folder.value.parentId));
213 214
 		}
214 215
 
215 216
 		return ret;