Browse code

Chore: sonar issue, deduplicate code a bit

Thomas Cataldo authored on 14/03/2019 07:37:42
Showing 3 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,81 @@
1
+/* BEGIN LICENSE
2
+  * Copyright © Blue Mind SAS, 2012-2019
3
+  *
4
+  * This file is part of BlueMind. BlueMind is a messaging and collaborative
5
+  * solution.
6
+  *
7
+  * This program is free software; you can redistribute it and/or modify
8
+  * it under the terms of either the GNU Affero General Public License as
9
+  * published by the Free Software Foundation (version 3 of the License).
10
+  *
11
+  * This program is distributed in the hope that it will be useful,
12
+  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
+  *
15
+  * See LICENSE.txt
16
+  * END LICENSE
17
+  */
18
+package net.bluemind.backend.mail.replica.service.internal.repair;
19
+
20
+import java.util.List;
21
+
22
+import org.slf4j.Logger;
23
+import org.slf4j.LoggerFactory;
24
+
25
+import net.bluemind.backend.mail.replica.api.IDbByContainerReplicatedMailboxes;
26
+import net.bluemind.backend.mail.replica.api.MailReplicaContainerTypes;
27
+import net.bluemind.backend.mail.replica.api.MailboxReplica;
28
+import net.bluemind.core.api.report.DiagnosticReport;
29
+import net.bluemind.core.container.model.ItemValue;
30
+import net.bluemind.core.rest.BmContext;
31
+import net.bluemind.core.task.service.IServerTaskMonitor;
32
+import net.bluemind.directory.api.DirEntry;
33
+import net.bluemind.directory.service.IDirEntryRepairSupport.InternalMaintenanceOperation;
34
+import net.bluemind.mailbox.api.IMailboxes;
35
+import net.bluemind.mailbox.api.Mailbox;
36
+
37
+public abstract class MailboxFoldersRepairOp extends InternalMaintenanceOperation {
38
+
39
+	protected final Logger logger = LoggerFactory.getLogger(getClass());
40
+	protected final BmContext context;
41
+
42
+	protected MailboxFoldersRepairOp(BmContext ctx, String identifier, String beforeOp, String afterOp, int cost) {
43
+		super(identifier, beforeOp, afterOp, cost);
44
+		this.context = ctx;
45
+	}
46
+
47
+	@Override
48
+	public final void check(String domainUid, DirEntry entry, DiagnosticReport report, IServerTaskMonitor monitor) {
49
+		run(false, domainUid, entry, report, monitor);
50
+	}
51
+
52
+	@Override
53
+	public final void repair(String domainUid, DirEntry entry, DiagnosticReport report, IServerTaskMonitor monitor) {
54
+		run(true, domainUid, entry, report, monitor);
55
+	}
56
+
57
+	public void run(boolean repair, String domainUid, DirEntry entry, DiagnosticReport report,
58
+			IServerTaskMonitor monitor) {
59
+		logger.info("Repair subtree {} {}", domainUid, entry);
60
+
61
+		IMailboxes mboxApi = context.provider().instance(IMailboxes.class, domainUid);
62
+		ItemValue<Mailbox> mbox = mboxApi.getComplete(entry.entryUid);
63
+		if (mbox == null) {
64
+			logger.warn("{} does not have a mailbox, nothing to repair", entry);
65
+			return;
66
+		}
67
+		if (mbox.value.dataLocation == null) {
68
+			logger.error("{} lacks a dataLocation, can't repair", mbox);
69
+			return;
70
+		}
71
+		String subUid = MailReplicaContainerTypes.subtreeUid(domainUid, mbox);
72
+		IDbByContainerReplicatedMailboxes foldersApi = context.provider()
73
+				.instance(IDbByContainerReplicatedMailboxes.class, subUid);
74
+
75
+		List<ItemValue<MailboxReplica>> fullList = foldersApi.allReplicas();
76
+		runOnFolders(repair, monitor, report, subUid, domainUid, mbox, fullList);
77
+	}
78
+
79
+	protected abstract void runOnFolders(boolean repair, IServerTaskMonitor mon, DiagnosticReport report,
80
+			String subTree, String domainUid, ItemValue<Mailbox> mbox, List<ItemValue<MailboxReplica>> fullList);
81
+}
... ...
@@ -24,9 +24,6 @@ import java.util.List;
24 24
 import java.util.Set;
25 25
 import java.util.stream.Collectors;
26 26
 
27
-import org.slf4j.Logger;
28
-import org.slf4j.LoggerFactory;
29
-
30 27
 import com.google.common.collect.ArrayListMultimap;
31 28
 import com.google.common.collect.ImmutableSet;
32 29
 import com.google.common.collect.Multimap;
... ...
@@ -43,10 +40,8 @@ import net.bluemind.core.container.model.ItemValue;
43 40
 import net.bluemind.core.rest.BmContext;
44 41
 import net.bluemind.core.task.service.IServerTaskMonitor;
45 42
 import net.bluemind.directory.api.BaseDirEntry.Kind;
46
-import net.bluemind.directory.api.DirEntry;
47 43
 import net.bluemind.directory.api.MaintenanceOperation;
48 44
 import net.bluemind.directory.service.IDirEntryRepairSupport;
49
-import net.bluemind.mailbox.api.IMailboxes;
50 45
 import net.bluemind.mailbox.api.Mailbox;
51 46
 
52 47
 public class MultiInboxRepair implements IDirEntryRepairSupport {
... ...
@@ -58,55 +53,28 @@ public class MultiInboxRepair implements IDirEntryRepairSupport {
58 53
 		}
59 54
 	}
60 55
 
61
-	private static final Logger logger = LoggerFactory.getLogger(MultiInboxRepair.class);
62 56
 	public static final MaintenanceOperation minboxOp = MaintenanceOperation
63 57
 			.create(MailReplicaContainerTypes.REPAIR_MINBOX_OP, "Multiple INBOX in subtree");
64 58
 
65
-	private static class FullNameDuplicatesMaintenance extends InternalMaintenanceOperation {
66
-
67
-		private final BmContext context;
59
+	private static class FullNameDuplicatesMaintenance extends MailboxFoldersRepairOp {
68 60
 
69 61
 		public FullNameDuplicatesMaintenance(BmContext ctx) {
70
-			super(minboxOp.identifier, null, MailReplicaContainerTypes.REPAIR_SUBTREE_OP, 1);
71
-			this.context = ctx;
72
-		}
73
-
74
-		@Override
75
-		public void check(String domainUid, DirEntry entry, DiagnosticReport report, IServerTaskMonitor monitor) {
76
-			run(false, domainUid, entry, report, monitor);
62
+			super(ctx, minboxOp.identifier, null, MailReplicaContainerTypes.REPAIR_SUBTREE_OP, 1);
77 63
 		}
78 64
 
79 65
 		@Override
80
-		public void repair(String domainUid, DirEntry entry, DiagnosticReport report, IServerTaskMonitor monitor) {
81
-			run(true, domainUid, entry, report, monitor);
82
-		}
83
-
84
-		public void run(boolean repair, String domainUid, DirEntry entry, DiagnosticReport report,
85
-				IServerTaskMonitor monitor) {
86
-			logger.info("Repair subtree {} {}", domainUid, entry);
87
-
88
-			IMailboxes mboxApi = context.provider().instance(IMailboxes.class, domainUid);
89
-			ItemValue<Mailbox> mbox = mboxApi.getComplete(entry.entryUid);
90
-			if (mbox == null) {
91
-				logger.warn("{} does not have a mailbox, nothing to repair", entry);
92
-				return;
93
-			}
94
-			if (mbox.value.dataLocation == null) {
95
-				logger.error("{} lacks a dataLocation, can't repair", mbox);
96
-				return;
97
-			}
98
-
99
-			IDbByContainerReplicatedMailboxes foldersApi = context.provider().instance(
100
-					IDbByContainerReplicatedMailboxes.class, MailReplicaContainerTypes.subtreeUid(domainUid, mbox));
101
-			List<ItemValue<MailboxReplica>> fullList = foldersApi.allReplicas();
66
+		public void runOnFolders(boolean repair, IServerTaskMonitor monitor, DiagnosticReport report, String subTree,
67
+				String domainUid, ItemValue<Mailbox> mbox, List<ItemValue<MailboxReplica>> fullList) {
68
+			IDbByContainerReplicatedMailboxes foldersApi = context.provider()
69
+					.instance(IDbByContainerReplicatedMailboxes.class, subTree);
102 70
 
103 71
 			monitor.begin(1, "Inspecting subtree for mailbox " + mbox.value.name + "@" + domainUid);
104 72
 
105 73
 			// get rid of orphan
106 74
 			IContainers contApi = context.provider().instance(IContainers.class);
107 75
 			Set<String> uniqueIdsOfRecords = contApi
108
-					.allLight(ContainerQuery.ownerAndType(entry.entryUid, MailReplicaContainerTypes.MAILBOX_RECORDS))
109
-					.stream().map(c -> MailReplicaContainerTypes.uniqueId(c.uid)).collect(Collectors.toSet());
76
+					.allLight(ContainerQuery.ownerAndType(mbox.uid, MailReplicaContainerTypes.MAILBOX_RECORDS)).stream()
77
+					.map(c -> MailReplicaContainerTypes.uniqueId(c.uid)).collect(Collectors.toSet());
110 78
 			Sets.difference(uniqueIdsOfRecords, fullList.stream().map(iv -> iv.uid).collect(Collectors.toSet()))
111 79
 					.forEach(uniqueId -> {
112 80
 						monitor.log("Purging orphan records container with unique id " + uniqueId + "...");
... ...
@@ -22,9 +22,6 @@ import java.util.LinkedList;
22 22
 import java.util.List;
23 23
 import java.util.Set;
24 24
 
25
-import org.slf4j.Logger;
26
-import org.slf4j.LoggerFactory;
27
-
28 25
 import com.google.common.collect.ImmutableSet;
29 26
 
30 27
 import net.bluemind.backend.mail.replica.api.IDbByContainerReplicatedMailboxes;
... ...
@@ -36,10 +33,8 @@ import net.bluemind.core.container.persistance.DataSourceRouter;
36 33
 import net.bluemind.core.rest.BmContext;
37 34
 import net.bluemind.core.task.service.IServerTaskMonitor;
38 35
 import net.bluemind.directory.api.BaseDirEntry.Kind;
39
-import net.bluemind.directory.api.DirEntry;
40 36
 import net.bluemind.directory.api.MaintenanceOperation;
41 37
 import net.bluemind.directory.service.IDirEntryRepairSupport;
42
-import net.bluemind.mailbox.api.IMailboxes;
43 38
 import net.bluemind.mailbox.api.Mailbox;
44 39
 
45 40
 public class RecordsInDirectoryRepair implements IDirEntryRepairSupport {
... ...
@@ -51,49 +46,21 @@ public class RecordsInDirectoryRepair implements IDirEntryRepairSupport {
51 46
 		}
52 47
 	}
53 48
 
54
-	private static final Logger logger = LoggerFactory.getLogger(RecordsInDirectoryRepair.class);
55 49
 	public static final MaintenanceOperation wrongDbOp = MaintenanceOperation
56 50
 			.create(MailReplicaContainerTypes.REPAIR_RECS_IN_DIR, "Record containers in wrong DB");
57 51
 
58
-	private static class FullNameDuplicatesMaintenance extends InternalMaintenanceOperation {
59
-
60
-		private final BmContext context;
61
-
62
-		public FullNameDuplicatesMaintenance(BmContext ctx) {
63
-			super(wrongDbOp.identifier, null, MailReplicaContainerTypes.REPAIR_SUBTREE_OP, 1);
64
-			this.context = ctx;
65
-		}
52
+	private static class WrongLocationMaintenance extends MailboxFoldersRepairOp {
66 53
 
67
-		@Override
68
-		public void check(String domainUid, DirEntry entry, DiagnosticReport report, IServerTaskMonitor monitor) {
69
-			run(false, domainUid, entry, report, monitor);
54
+		public WrongLocationMaintenance(BmContext ctx) {
55
+			super(ctx, wrongDbOp.identifier, null, MailReplicaContainerTypes.REPAIR_SUBTREE_OP, 1);
70 56
 		}
71 57
 
72 58
 		@Override
73
-		public void repair(String domainUid, DirEntry entry, DiagnosticReport report, IServerTaskMonitor monitor) {
74
-			run(true, domainUid, entry, report, monitor);
75
-		}
76
-
77
-		public void run(boolean repair, String domainUid, DirEntry entry, DiagnosticReport report,
78
-				IServerTaskMonitor monitor) {
79
-			logger.info("Repair subtree {} {}", domainUid, entry);
80
-
81
-			IMailboxes mboxApi = context.provider().instance(IMailboxes.class, domainUid);
82
-			ItemValue<Mailbox> mbox = mboxApi.getComplete(entry.entryUid);
83
-			if (mbox == null) {
84
-				logger.warn("{} does not have a mailbox, nothing to repair", entry);
85
-				return;
86
-			}
87
-			if (mbox.value.dataLocation == null) {
88
-				logger.error("{} lacks a dataLocation, can't repair", mbox);
89
-				return;
90
-			}
91
-			String subUid = MailReplicaContainerTypes.subtreeUid(domainUid, mbox);
92
-			String subLoc = DataSourceRouter.location(context, subUid);
59
+		public void runOnFolders(boolean repair, IServerTaskMonitor monitor, DiagnosticReport report, String subTree,
60
+				String domainUid, ItemValue<Mailbox> mbox, List<ItemValue<MailboxReplica>> fullList) {
61
+			String subLoc = DataSourceRouter.location(context, subTree);
93 62
 			IDbByContainerReplicatedMailboxes foldersApi = context.provider()
94
-					.instance(IDbByContainerReplicatedMailboxes.class, subUid);
95
-
96
-			List<ItemValue<MailboxReplica>> fullList = foldersApi.allReplicas();
63
+					.instance(IDbByContainerReplicatedMailboxes.class, subTree);
97 64
 			List<ItemValue<MailboxReplica>> toPurge = new LinkedList<>();
98 65
 			monitor.begin(1, "Inspecting subtree for mailbox " + mbox.value.name + "@" + domainUid);
99 66
 			for (ItemValue<MailboxReplica> folder : fullList) {
... ...
@@ -134,7 +101,7 @@ public class RecordsInDirectoryRepair implements IDirEntryRepairSupport {
134 101
 	@Override
135 102
 	public Set<InternalMaintenanceOperation> ops(Kind kind) {
136 103
 		if (kind == Kind.USER || kind == Kind.MAILSHARE || kind == Kind.GROUP || kind == Kind.RESOURCE) {
137
-			return ImmutableSet.of(new FullNameDuplicatesMaintenance(context));
104
+			return ImmutableSet.of(new WrongLocationMaintenance(context));
138 105
 		} else {
139 106
 			return Collections.emptySet();
140 107
 		}