Browse code

BM-14889 Feat: refactor

Thomas Fricker authored on 05/06/2019 10:22:54
Showing 1 changed files
... ...
@@ -99,12 +99,68 @@ public class ReApplyDeletionsRepair extends InternalMaintenanceOperation {
99 99
 
100 100
 	@Override
101 101
 	public void check(String domainUid, DirEntry entry, DiagnosticReport report, IServerTaskMonitor monitor) {
102
-		run(false, domainUid, entry, report, monitor);
102
+		ReApplyDeletion op = new ReApplyDeletion() {
103
+
104
+			@Override
105
+			public void info(DirEntry entry) {
106
+				monitor.log("Checking " + entry);
107
+			}
108
+
109
+			@Override
110
+			public void applyDeletion(Collection<Integer> uids, String flag, StoreClient sc, ListInfo listInfo) {
111
+				monitor.log("Should re-flag " + uids.size() + " message(s)");
112
+			}
113
+		};
114
+		run(op, domainUid, entry, monitor);
103 115
 	}
104 116
 
105 117
 	@Override
106 118
 	public void repair(String domainUid, DirEntry entry, DiagnosticReport report, IServerTaskMonitor monitor) {
107
-		run(true, domainUid, entry, report, monitor);
119
+		ReApplyDeletion op = new ReApplyDeletion() {
120
+
121
+			@Override
122
+			public void info(DirEntry entry) {
123
+				monitor.log("Repairing " + entry);
124
+			}
125
+
126
+			@Override
127
+			public void applyDeletion(Collection<Integer> uids, String flag, StoreClient sc, ListInfo listInfo) {
128
+				String set = uids.stream().map(Object::toString).collect(Collectors.joining(","));
129
+				String cmd = "UID STORE " + set + " +FLAGS.SILENT (" + flag + ")";
130
+				TaggedResult ok = sc.tagged(cmd);
131
+				monitor.log("Updated " + uids.size() + " message(s) => " + ok.isOk());
132
+				report.ok(ID, "Folder " + listInfo.getName() + " fixed");
133
+			}
134
+
135
+			@Override
136
+			public void postProcessing(UserMailApi mailApi, String flag, StoreClient sc, List<ListInfo> folders)
137
+					throws IMAPException {
138
+				for (ListInfo li : folders) {
139
+					monitor.progress(1, "Cleanup " + li.getName());
140
+					if (sc.select(li.getName())) {
141
+						Collection<Integer> deleted = sc.uidSearchDeleted();
142
+						if (deleted.isEmpty()) {
143
+							continue;
144
+						}
145
+						String set = deleted.stream().map(Object::toString).collect(Collectors.joining(","));
146
+						String cmd = "UID STORE " + set + " -FLAGS.SILENT (" + flag + ")";
147
+						TaggedResult ok = sc.tagged(cmd);
148
+						monitor.log("Updated " + deleted.size() + " message(s) => " + ok.isOk());
149
+						sc.expunge();
150
+						monitor.log("Resync mailbox record(s)");
151
+					}
152
+				}
153
+				IMailboxFoldersByContainer subApi = mailApi.ctx.provider().instance(IMailboxFoldersByContainer.class,
154
+						mailApi.subtree);
155
+				for (ItemValue<MailboxFolder> f : subApi.all()) {
156
+					IMailboxItems recsApi = mailApi.ctx.provider().instance(IMailboxItems.class, f.uid);
157
+					recsApi.resync();
158
+				}
159
+			}
160
+
161
+		};
162
+
163
+		run(op, domainUid, entry, monitor);
108 164
 	}
109 165
 
110 166
 	private static class UserMailApi {
... ...
@@ -118,17 +174,12 @@ public class ReApplyDeletionsRepair extends InternalMaintenanceOperation {
118 174
 
119 175
 	}
120 176
 
121
-	private void run(boolean repair, String domainUid, DirEntry entry, DiagnosticReport report,
122
-			IServerTaskMonitor monitor) {
177
+	private void run(ReApplyDeletion op, String domainUid, DirEntry entry, IServerTaskMonitor monitor) {
123 178
 		ItemValue<Mailbox> mbox = context.provider().instance(IMailboxes.class, domainUid).getComplete(entry.entryUid);
124 179
 		if (mbox == null) {
125 180
 			return;
126 181
 		}
127
-		if (repair) {
128
-			monitor.log("Repairing " + entry);
129
-		} else {
130
-			monitor.log("Checking " + entry);
131
-		}
182
+		op.info(entry);
132 183
 		String latd = mbox.value.name + "@" + domainUid;
133 184
 		LoginResponse resp = context.provider().instance(IAuthentication.class).su(latd);
134 185
 		if (resp.authKey != null) {
... ...
@@ -154,7 +205,7 @@ public class ReApplyDeletionsRepair extends InternalMaintenanceOperation {
154 205
 				String subtree = SubtreeContainer.mailSubtreeUid(domainUid, Namespace.users, mbox.uid).subtreeUid;
155 206
 				BmContext userCtx = context.su("repair-" + UUID.randomUUID().toString(), mbox.uid, domainUid);
156 207
 				UserMailApi mailApi = new UserMailApi(subtree, userCtx);
157
-				processFolders(repair, mailApi, report, monitor, flag, sc, folders);
208
+				processFolders(op, mailApi, monitor, flag, sc, folders);
158 209
 			} catch (Exception e) {
159 210
 				logger.error(e.getMessage(), e);
160 211
 			}
... ...
@@ -164,8 +215,8 @@ public class ReApplyDeletionsRepair extends InternalMaintenanceOperation {
164 215
 
165 216
 	}
166 217
 
167
-	private void processFolders(boolean repair, UserMailApi mailApi, DiagnosticReport report,
168
-			IServerTaskMonitor monitor, String flag, StoreClient sc, List<ListInfo> folders) throws IMAPException {
218
+	private void processFolders(ReApplyDeletion op, UserMailApi mailApi, IServerTaskMonitor monitor, String flag,
219
+			StoreClient sc, List<ListInfo> folders) throws IMAPException {
169 220
 		monitor.begin(2d * folders.size(), "Processing " + folders.size() + " folder(s)");
170 221
 		for (ListInfo li : folders) {
171 222
 			monitor.progress(1, "On " + li.getName());
... ...
@@ -175,45 +226,22 @@ public class ReApplyDeletionsRepair extends InternalMaintenanceOperation {
175 226
 					monitor.log("No deletions in " + li.getName());
176 227
 					continue;
177 228
 				}
178
-				if (repair) {
179
-					String set = deleted.stream().map(Object::toString).collect(Collectors.joining(","));
180
-					String cmd = "UID STORE " + set + " +FLAGS.SILENT (" + flag + ")";
181
-					TaggedResult ok = sc.tagged(cmd);
182
-					monitor.log("Updated " + deleted.size() + " message(s) => " + ok.isOk());
183
-					report.ok(ID, "Folder " + li.getName() + " fixed");
184
-				} else {
185
-					monitor.log("Should re-flag " + deleted.size() + " message(s)");
186
-				}
229
+				op.applyDeletion(deleted, flag, sc, li);
187 230
 			}
188 231
 		}
189
-		if (repair) {
190
-			postRepairCleanup(monitor, mailApi, flag, sc, folders);
191
-		}
232
+		op.postProcessing(mailApi, flag, sc, folders);
192 233
 	}
193 234
 
194
-	private void postRepairCleanup(IServerTaskMonitor monitor, UserMailApi mailApi, String flag, StoreClient sc,
195
-			List<ListInfo> folders) throws IMAPException {
196
-		for (ListInfo li : folders) {
197
-			monitor.progress(1, "Cleanup " + li.getName());
198
-			if (sc.select(li.getName())) {
199
-				Collection<Integer> deleted = sc.uidSearchDeleted();
200
-				if (deleted.isEmpty()) {
201
-					continue;
202
-				}
203
-				String set = deleted.stream().map(Object::toString).collect(Collectors.joining(","));
204
-				String cmd = "UID STORE " + set + " -FLAGS.SILENT (" + flag + ")";
205
-				TaggedResult ok = sc.tagged(cmd);
206
-				monitor.log("Updated " + deleted.size() + " message(s) => " + ok.isOk());
207
-				sc.expunge();
208
-				monitor.log("Resync mailbox record(s)");
209
-			}
210
-		}
211
-		IMailboxFoldersByContainer subApi = mailApi.ctx.provider().instance(IMailboxFoldersByContainer.class,
212
-				mailApi.subtree);
213
-		for (ItemValue<MailboxFolder> f : subApi.all()) {
214
-			IMailboxItems recsApi = mailApi.ctx.provider().instance(IMailboxItems.class, f.uid);
215
-			recsApi.resync();
235
+	private static interface ReApplyDeletion {
236
+
237
+		public void info(DirEntry entry);
238
+
239
+		public void applyDeletion(Collection<Integer> uids, String flag, StoreClient sc, ListInfo listInfo);
240
+
241
+		public default void postProcessing(UserMailApi mailApi, String flag, StoreClient sc, List<ListInfo> folders)
242
+				throws IMAPException {
216 243
 		}
244
+
217 245
 	}
218 246
 
219 247
 }