-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPatchContext.java
542 lines (451 loc) · 17.3 KB
/
PatchContext.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
package com.github.mlytvyn.patches.groovy.context.patch;
import com.github.mlytvyn.patches.groovy.ContentCatalogEnum;
import com.github.mlytvyn.patches.groovy.EmailComponentTemplateEnum;
import com.github.mlytvyn.patches.groovy.EmailTemplateEnum;
import com.github.mlytvyn.patches.groovy.EnvironmentEnum;
import com.github.mlytvyn.patches.groovy.SiteEnum;
import com.github.mlytvyn.patches.groovy.SolrEnum;
import com.github.mlytvyn.patches.groovy.SolrIndexedTypeEnum;
import com.github.mlytvyn.patches.groovy.context.ChangeFieldTypeContext;
import com.github.mlytvyn.patches.groovy.context.DropColumnContext;
import com.github.mlytvyn.patches.groovy.context.global.GlobalContext;
import com.github.mlytvyn.patches.groovy.context.impex.ImpexContext;
import com.github.mlytvyn.patches.groovy.context.impex.ImpexImportConfig;
import com.github.mlytvyn.patches.groovy.context.impex.ImpexTemplateContext;
import com.github.mlytvyn.patches.groovy.context.release.ReleaseContext;
import com.github.mlytvyn.patches.groovy.setup.GroovyPatchesSystemSetup;
import de.hybris.platform.core.initialization.SystemSetupContext;
import org.apache.commons.lang3.ArrayUtils;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumSet;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class PatchContext<G extends GlobalContext, R extends ReleaseContext> implements PatchContextDescriber, PatchContextDescriptor {
protected final List<ImpexTemplateContext> impexTemplateContexts = new ArrayList<>();
protected final List<ChangeFieldTypeContext> changeFieldTypeContexts = new ArrayList<>();
protected final List<DropColumnContext> dropColumnContexts = new ArrayList<>();
protected final Map<ContentCatalogEnum, Boolean> contentCatalogsToBeSyncedNow = new LinkedHashMap<>();
protected final G globalContext;
protected final R releaseContext;
protected final String extensionName;
protected final String number;
protected final String id;
protected List<? super PatchContext<G, R>> nestedPatches = new LinkedList<>();
protected List<? super PatchContext<G, R>> environmentPatches = new LinkedList<>();
protected String hash;
protected String description;
protected Path customPatchDataFolder;
protected PatchDataFolderRelation patchDataFolderRelation = PatchDataFolderRelation.RELEASE;
protected ImpexImportConfig impexImportConfig;
protected List<ImpexContext> impexes;
protected Set<String> resetUserRightsForPrincipals;
protected EnumSet<EnvironmentEnum> environments = EnumSet.allOf(EnvironmentEnum.class);
protected Consumer<SystemSetupContext> beforeConsumer;
protected Consumer<SystemSetupContext> afterConsumer;
public PatchContext(final G globalContext, final R releaseContext, final String extensionName, final String number, final String id) {
this.globalContext = globalContext;
this.releaseContext = releaseContext;
this.extensionName = extensionName;
this.number = number;
this.id = id;
}
@Override
@SuppressWarnings("unchecked")
public boolean equals(final Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final PatchContext<G, R> patchContext = (PatchContext<G, R>) o;
return hash().equals(patchContext.hash());
}
@Override
public int hashCode() {
return Objects.hash(hash());
}
@Override
public String toString() {
return getNumber() + " | " + getId() + " | " + getDescription();
}
// May be used by legacy patches (not groovy based)
@Override
public PatchContextDescriber hash(final String hash) {
this.hash = hash;
return this;
}
@Override
public PatchContextDescriber environment(final EnvironmentEnum... environments) {
if (ArrayUtils.isNotEmpty(environments)) {
this.environments = EnumSet.copyOf(Arrays.asList(environments));
}
return this;
}
@Override
public PatchContextDescriber description(final String description) {
this.description = description;
return this;
}
@Override
public PatchContextDescriber before(final Consumer<SystemSetupContext> consumer) {
if (isNotApplicable()) {
return this;
}
this.beforeConsumer = consumer;
return this;
}
@Override
public PatchContextDescriber after(final Consumer<SystemSetupContext> consumer) {
if (isNotApplicable()) {
return this;
}
this.afterConsumer = consumer;
return this;
}
@Override
@SuppressWarnings("unchecked")
public PatchContextDescriber withNestedPatch(final PatchContextDescriber nested) {
if (isNotApplicable() || !PatchContext.class.isAssignableFrom(nested.getClass())) {
return this;
}
this.nestedPatches.add((PatchContext<G, R>) nested);
return this;
}
@Override
public PatchContextDescriber customPatchDataFolder(final Path customPatchDataFolder) {
return customPatchDataFolder(customPatchDataFolder, PatchDataFolderRelation.RELEASE);
}
@Override
public PatchContextDescriber customPatchDataFolder(final Path customPatchDataFolder, final PatchDataFolderRelation relation) {
if (isNotApplicable()) {
return this;
}
this.customPatchDataFolder = customPatchDataFolder;
this.patchDataFolderRelation = relation;
return this;
}
@Override
public PatchContextDescriber impexImportConfig(final ImpexImportConfig config) {
if (isNotApplicable()) {
return this;
}
this.impexImportConfig = config;
return this;
}
@Override
public PatchContextDescriber withImpexes(final String... impexes) {
if (isNotApplicable()) {
return this;
}
this.impexes = ArrayUtils.isEmpty(impexes)
? Collections.emptyList()
: Stream.of(impexes)
.map(ImpexContext::of)
.collect(Collectors.toList());
return this;
}
@Override
public PatchContextDescriber withFqnImpexes(final String... impexes) {
if (isNotApplicable()) {
return this;
}
this.impexes = ArrayUtils.isEmpty(impexes)
? Collections.emptyList()
: Stream.of(impexes)
.map(ImpexContext::fqn)
.collect(Collectors.toList());
return this;
}
@Override
public PatchContextDescriber withImpexes(final ImpexContext... impexContexts) {
if (isNotApplicable()) {
return this;
}
this.impexes = ArrayUtils.isEmpty(impexContexts)
? Collections.emptyList()
: Arrays.asList(impexContexts);
return this;
}
@Override
public PatchContextDescriber importEmailTemplates(final EmailTemplateEnum... emailTemplates) {
if (isNotApplicable() || ArrayUtils.isEmpty(emailTemplates)) {
return this;
}
globalContext.importEmailTemplates().addAll(Arrays.asList(emailTemplates));
return this;
}
@Override
public PatchContextDescriber importEmailComponentTemplates(final EnumSet<SiteEnum> sites, final EmailComponentTemplateEnum... emailComponentTemplates) {
if (isNotApplicable() || sites.isEmpty() || ArrayUtils.isEmpty(emailComponentTemplates)) {
return this;
}
final Map<EmailComponentTemplateEnum, EnumSet<SiteEnum>> importEmailComponentTemplates = globalContext.importEmailComponentTemplates();
Stream.of(emailComponentTemplates).forEach(emailComponentTemplate -> {
if (importEmailComponentTemplates.containsKey(emailComponentTemplate)) {
importEmailComponentTemplates.get(emailComponentTemplate).addAll(sites);
} else {
importEmailComponentTemplates.put(emailComponentTemplate, sites);
}
});
return this;
}
@Override
public PatchContextDescriber importEmailComponentTemplates(final EmailComponentTemplateEnum... emailComponentTemplates) {
if (isNotApplicable() || ArrayUtils.isEmpty(emailComponentTemplates)) {
return this;
}
return importEmailComponentTemplates(EnumSet.allOf(SiteEnum.class), emailComponentTemplates);
}
@Override
public PatchContextDescriber syncContentCatalogs(final ContentCatalogEnum... contentCatalogs) {
if (isNotApplicable() || ArrayUtils.isEmpty(contentCatalogs)) {
return this;
}
releaseContext.syncContentCatalogs(Arrays.asList(contentCatalogs));
return this;
}
@Override
public PatchContextDescriber forcedSyncContentCatalogs(final ContentCatalogEnum... contentCatalogs) {
if (isNotApplicable() || ArrayUtils.isEmpty(contentCatalogs)) {
return this;
}
releaseContext.forcedSyncContentCatalogs(Arrays.asList(contentCatalogs));
return this;
}
@Override
public PatchContextDescriber removeContentCatalogs(final ContentCatalogEnum... contentCatalogs) {
if (isNotApplicable() || ArrayUtils.isEmpty(contentCatalogs)) {
return this;
}
releaseContext.contentCatalogsToBeRemoved().addAll(Arrays.asList(contentCatalogs));
return this;
}
@Override
public PatchContextDescriber syncContentCatalogsNow(final ContentCatalogEnum... contentCatalogs) {
if (isNotApplicable()) {
return this;
}
Arrays.stream(contentCatalogs)
.forEach(contentCatalog -> contentCatalogsToBeSyncedNow.putIfAbsent(contentCatalog, false));
return this;
}
@Override
public PatchContextDescriber forcedSyncContentCatalogsNow(final ContentCatalogEnum... contentCatalogs) {
if (isNotApplicable()) {
return this;
}
if (ArrayUtils.isNotEmpty(contentCatalogs)) {
Arrays.stream(contentCatalogs)
.forEach(contentCatalog -> contentCatalogsToBeSyncedNow.put(contentCatalog, true));
}
return this;
}
@Override
public PatchContextDescriber withImpexTemplateContexts(final ImpexTemplateContext... impexTemplateContexts) {
if (isNotApplicable()) {
return this;
}
if (ArrayUtils.isNotEmpty(impexTemplateContexts)) {
this.impexTemplateContexts.addAll(Arrays.asList(impexTemplateContexts));
}
return this;
}
@Override
public PatchContextDescriber changeFieldType(final ChangeFieldTypeContext... changeFieldTypeContexts) {
if (isNotApplicable()) {
return this;
}
if (ArrayUtils.isNotEmpty(changeFieldTypeContexts)) {
this.changeFieldTypeContexts.addAll(Arrays.asList(changeFieldTypeContexts));
}
return this;
}
@Override
public PatchContextDescriber dropColumn(final DropColumnContext... dropColumnContexts) {
if (isNotApplicable()) {
return this;
}
if (ArrayUtils.isNotEmpty(dropColumnContexts)) {
this.dropColumnContexts.addAll(Arrays.asList(dropColumnContexts));
}
return this;
}
@Override
public PatchContextDescriber removeOrphanedTypes() {
if (isNotApplicable()) {
return this;
}
globalContext.removeOrphanedTypes(true);
return this;
}
@Override
public PatchContextDescriber partialReIndex(final SolrIndexedTypeEnum indexedType, final String... indexedProperties) {
if (isNotApplicable()) {
return this;
}
if (ArrayUtils.isNotEmpty(indexedProperties)) {
globalContext.scheduleSolrIndexedTypePartialReIndex(indexedType, Arrays.asList(indexedProperties));
}
return this;
}
@Override
public PatchContextDescriber fullReIndex(final SolrEnum... solrIndexes) {
if (isNotApplicable()) {
return this;
}
if (ArrayUtils.isNotEmpty(solrIndexes)) {
globalContext.scheduleSolrCoreFullReIndex(Arrays.asList(solrIndexes));
}
return this;
}
@Override
public PatchContextDescriber removeSolrCore(final SolrEnum... solrIndexes) {
if (isNotApplicable()) {
return this;
}
if (ArrayUtils.isNotEmpty(solrIndexes)) {
globalContext.scheduleSolrCoresForRemoval(Arrays.asList(solrIndexes));
}
return this;
}
@Override
@SuppressWarnings("unchecked")
public PatchContextDescriber withEnvironmentPatch(final EnumSet<EnvironmentEnum> environments, final Supplier<PatchContextDescriber> supplier) {
if (isNotApplicable()) {
return this;
}
final PatchContextDescriber environmentPatch = supplier.get();
if (environments.contains(getCurrentEnvironment()) && PatchContext.class.isAssignableFrom(environmentPatch.getClass())) {
var envPatch = (PatchContext<G, R>) environmentPatch;
envPatch.environments = EnumSet.of(getCurrentEnvironment());
this.environmentPatches.add(envPatch);
}
return this;
}
@Override
public String hash() {
return Optional.ofNullable(hash)
.orElseGet(() -> {
final String releaseId = releaseContext.id();
// almost copy-paste from SystemSetupCollectorResult
// already applied patches will use original hash value retrieved from upper env
final String key = extensionName + "-" + releaseId + "-" + number + "-" + id;
return GroovyPatchesSystemSetup.MD5.hashBytes(key.getBytes()).toString();
});
}
@Override
public PatchContextDescriber createRelatedPatch() {
return new PatchContext<>(globalContext, releaseContext, extensionName, number, id);
}
@Override
public PatchContextDescriber resetUserRightsForPrincipals(final String... principalUIDs) {
if (isNotApplicable()) {
return this;
}
if (ArrayUtils.isNotEmpty(principalUIDs)) {
globalContext.resetUserRightsForPrincipals(Arrays.asList(principalUIDs));
}
return this;
}
@Override
public String getName() {
return getReleaseContext().id() + " | " + getNumber() + " | " + getId();
}
@Override
public EnvironmentEnum getCurrentEnvironment() {
return globalContext.currentEnvironment();
}
@Override
public List<ChangeFieldTypeContext> getChangeFieldTypeContexts() {
return Collections.unmodifiableList(changeFieldTypeContexts);
}
@Override
public List<DropColumnContext> getDropColumnContexts() {
return Collections.unmodifiableList(dropColumnContexts);
}
@Override
public String getNumber() {
return number;
}
@Override
public Optional<ImpexImportConfig> getImpexImportConfig() {
return Optional.ofNullable(impexImportConfig);
}
@Override
public List<ImpexTemplateContext> getImpexContexts() {
return Collections.unmodifiableList(impexTemplateContexts);
}
@Override
public List<ImpexContext> getImpexes() {
return Optional.ofNullable(impexes)
.map(Collections::unmodifiableList)
.orElse(null);
}
@Override
public String getId() {
return id;
}
@Override
public Map<ContentCatalogEnum, Boolean> getContentCatalogsToBeSyncedNow() {
return contentCatalogsToBeSyncedNow;
}
@Override
public Set<EnvironmentEnum> getEnvironments() {
return Collections.unmodifiableSet(environments);
}
@Override
public Optional<Consumer<SystemSetupContext>> getBeforeConsumer() {
return Optional.ofNullable(beforeConsumer);
}
@Override
public Optional<Consumer<SystemSetupContext>> getAfterConsumer() {
return Optional.ofNullable(afterConsumer);
}
@Override
public String getDescription() {
return description;
}
@Override
public List<PatchContextDescriptor> getNestedPatches() {
return (List<PatchContextDescriptor>) nestedPatches;
}
@Override
public ReleaseContext getReleaseContext() {
return releaseContext;
}
@Override
public GlobalContext getGlobalContext() {
return globalContext;
}
@Override
public boolean isApplicable() {
return environments.contains(getCurrentEnvironment());
}
@Override
public boolean isNotApplicable() {
return !isApplicable();
}
@Override
public List<PatchContextDescriptor> getEnvironmentPatches() {
return (List<PatchContextDescriptor>) environmentPatches;
}
@Override
public Path getPatchDataFolder() {
return Optional.ofNullable(customPatchDataFolder)
.orElseGet(() -> Paths.get(getId()));
}
@Override
public PatchDataFolderRelation getPatchDataFolderRelation() {
return patchDataFolderRelation;
}
}