RolloutContextImpl.java
14.6 KB
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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.commons.Filter
* com.day.cq.wcm.api.Page
* com.day.cq.wcm.api.PageManager
* com.day.cq.wcm.api.WCMException
* com.day.cq.wcm.msm.api.LiveCopy
* com.day.cq.wcm.msm.api.LiveRelationship
* com.day.cq.wcm.msm.api.LiveRelationshipManager
* com.day.cq.wcm.msm.api.LiveStatus
* com.day.cq.wcm.msm.api.RolloutConfig
* com.day.cq.wcm.msm.api.RolloutExceptionHandler
* com.day.cq.wcm.msm.api.RolloutExceptionHandler$RolloutInfo
* com.day.cq.wcm.msm.api.RolloutManager
* com.day.cq.wcm.msm.api.RolloutManager$RolloutProgress
* com.day.cq.wcm.msm.api.RolloutManager$Trigger
* com.day.text.Text
* javax.jcr.RangeIterator
* org.apache.commons.collections.CollectionUtils
* org.apache.sling.api.resource.NonExistingResource
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ValueMap
*/
package com.day.cq.wcm.msm.impl;
import com.day.cq.commons.Filter;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.WCMException;
import com.day.cq.wcm.msm.api.LiveCopy;
import com.day.cq.wcm.msm.api.LiveRelationship;
import com.day.cq.wcm.msm.api.LiveRelationshipManager;
import com.day.cq.wcm.msm.api.LiveStatus;
import com.day.cq.wcm.msm.api.RolloutConfig;
import com.day.cq.wcm.msm.api.RolloutExceptionHandler;
import com.day.cq.wcm.msm.api.RolloutManager;
import com.day.cq.wcm.msm.impl.LiveCopyManagerImpl;
import com.day.text.Text;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import javax.jcr.RangeIterator;
import org.apache.commons.collections.CollectionUtils;
import org.apache.sling.api.resource.NonExistingResource;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
public class RolloutContextImpl {
private static final RolloutManager.Trigger[] TRIGGER_SEARCH_ORDER = new RolloutManager.Trigger[]{RolloutManager.Trigger.ROLLOUT, RolloutManager.Trigger.MODIFICATION, RolloutManager.Trigger.PUBLICATION, RolloutManager.Trigger.DEACTIVATION};
private final boolean isReset;
private final RolloutManager.RolloutProgress progress;
private final LiveRelationship relationship;
private final ResourceResolver resourceResolver;
private final RolloutManager.Trigger trigger;
private final HashMap<String, RolloutContextImpl> configChanges = new HashMap();
private Resource source;
private Resource target;
private List<RolloutConfig> allConfigs;
private int currentConfigIdx = -1;
private HashSet<RolloutContextImpl> nameConflicts = new HashSet();
private ArrayList<RolloutContextImpl> children = null;
private RolloutContextImpl parent;
private boolean isDeep;
private boolean forceUpdate;
RolloutContextImpl(ResourceResolver resourceResolver, RolloutManager.Trigger trigger, boolean isDeep, boolean isReset, LiveRelationship liveRelationship, RolloutManager.RolloutProgress rolloutProgress) {
this.resourceResolver = resourceResolver;
this.trigger = trigger;
this.isDeep = isDeep;
this.isReset = isReset;
this.relationship = liveRelationship;
this.progress = rolloutProgress;
this.source = resourceResolver.getResource(this.relationship.getSourcePath());
}
private RolloutContextImpl(RolloutContextImpl parent, LiveRelationship liveRelationship, Resource targetResource) {
this(parent.resourceResolver, parent.trigger, parent.isDeep, parent.isReset, liveRelationship, parent.progress);
this.parent = parent;
this.target = targetResource;
}
public boolean isDeep() {
return this.isDeep;
}
public boolean isReset() {
return this.isReset;
}
public RolloutManager.Trigger getTrigger() {
return this.trigger;
}
public void reportProgress(String oppertation) {
if (this.relationship == null) {
throw new IllegalStateException("Only report Progress on Relationship");
}
this.reportProgress(this.relationship.getTargetPath(), oppertation);
}
public void reportProgress(String path, String opperation) {
this.progress.reportProgress(path, opperation);
}
public void setSource(Resource source) {
this.source = source;
}
public void loadTarget() {
if (this.relationship != null) {
this.target = this.resourceResolver.getResource(this.relationship.getTargetPath());
}
}
public RolloutContextImpl forRelationship(LiveRelationship relation) {
return this.forRelationship(relation, false);
}
public RolloutContextImpl forRelationship(LiveRelationship relation, boolean reset) {
RolloutContextImpl relContext = new RolloutContextImpl(this.resourceResolver, this.trigger, this.isDeep, this.isReset, relation, this.progress);
relContext.currentConfigIdx = reset ? -1 : this.currentConfigIdx;
relContext.nameConflicts = this.nameConflicts;
relContext.parent = this.parent;
relContext.loadTarget();
return relContext;
}
public LiveRelationship getRelationship() {
return this.relationship;
}
public Resource getSource() {
return this.getSource(false);
}
public Resource getSource(boolean forced) {
if (this.source == null && this.relationship == null) {
throw new IllegalStateException("Source Resource only available if Relationship is set");
}
return this.source == null && forced ? new NonExistingResource(this.resourceResolver, this.relationship.getSourcePath()) : this.source;
}
public Resource getTarget() {
if (this.relationship == null) {
throw new IllegalStateException("Target Resource only available if Relationship is set");
}
return this.target;
}
public ResourceResolver getResourceResolver() {
return this.resourceResolver;
}
public RolloutConfig getCurrentRolloutConfig() {
RolloutConfig result = null;
if (this.parent == null) {
List<RolloutConfig> all = this.getRolloutConfigs();
if (all != null) {
result = this.currentConfigIdx == -1 && this.hasNextRolloutConfig() ? this.getNextRolloutConfig() : all.get(this.currentConfigIdx);
}
} else {
result = this.parent.getCurrentRolloutConfig();
}
return result;
}
public boolean hasNextRolloutConfig() {
return this.relationship != null && this.getRolloutConfigs() != null && this.currentConfigIdx < this.getRolloutConfigs().size() - 1;
}
public RolloutContextImpl nextRolloutConfig() {
if (this.parent == null) {
if (!this.hasNextRolloutConfig()) {
throw new IllegalStateException("Call has next first");
}
++this.currentConfigIdx;
} else {
this.parent.getNextRolloutConfig();
}
return this;
}
private List<RolloutConfig> getRolloutConfigs() {
if (this.allConfigs == null) {
if (this.trigger == null) {
for (int i = 0; this.allConfigs == null && i < TRIGGER_SEARCH_ORDER.length; ++i) {
List check = this.relationship.getRolloutConfigs(TRIGGER_SEARCH_ORDER[i]);
if (check.isEmpty()) continue;
this.allConfigs = check;
}
} else {
this.allConfigs = this.relationship.getRolloutConfigs(this.trigger);
}
}
return this.allConfigs;
}
public void setDeep(boolean deep) {
this.isDeep = deep;
}
public void setForceUpdate(boolean force) {
this.forceUpdate = force;
}
public boolean isForceUpdate() {
if (this.parent == null) {
return this.forceUpdate || this.isReset() || this.getSource() == null || this.isMoved();
}
return this.parent.isForceUpdate();
}
public RolloutExceptionHandler.RolloutInfo getRolloutInfo() {
return new RolloutExceptionHandler.RolloutInfo(){};
}
public void addNameConflict(RolloutContextImpl conflictingContet) {
if (this.parent == null) {
this.nameConflicts.add(conflictingContet);
} else {
this.parent.addNameConflict(conflictingContet);
}
}
public Set<RolloutContextImpl> getAllNameConflicts() {
return this.parent == null ? Collections.unmodifiableSet(this.nameConflicts) : this.parent.getAllNameConflicts();
}
public boolean hasNameConflicts() {
return this.parent == null ? !this.nameConflicts.isEmpty() : this.parent.hasNameConflicts();
}
public boolean hasChangesToRollout() {
boolean hasChanges;
LiveStatus status = this.relationship.getStatus();
if (status.getLastRolledOut() == null) {
hasChanges = true;
} else if (status.isPage()) {
if (this.getTarget() == null && status.isTargetExisting()) {
this.loadTarget();
}
Resource source = this.getSource(false);
Resource target = this.getTarget();
if (source == null || target == null) {
hasChanges = true;
} else {
Calendar targetMod;
Calendar now = Calendar.getInstance();
Calendar sourceMod = (Calendar)((ValueMap)source.adaptTo(ValueMap.class)).get("cq:lastModified", (Object)now);
Calendar youngest = sourceMod.before(targetMod = (Calendar)((ValueMap)target.adaptTo(ValueMap.class)).get("cq:lastModified", (Object)now)) ? targetMod : sourceMod;
hasChanges = status.getLastRolledOut().before(youngest.getTime());
}
} else {
hasChanges = true;
}
return hasChanges;
}
public Iterator<RolloutContextImpl> getChildren(LiveRelationshipManager relManager) throws WCMException {
if (this.children == null) {
ArrayList<RolloutContextImpl> children = new ArrayList<RolloutContextImpl>();
PageManager pageManager = (PageManager)this.getResourceResolver().adaptTo(PageManager.class);
Page page = this.getSource() == null ? null : (Page)this.getSource().getParent().adaptTo(Page.class);
String targetPath = this.getRelationship().getTargetPath();
Page slave = pageManager.getContainingPage(targetPath);
if (slave != null) {
Filter<Page> filter = null;
if (page != null) {
Iterator sourceChildren = page.listChildren();
while (sourceChildren.hasNext()) {
RangeIterator ships = relManager.getLiveRelationships(((Page)sourceChildren.next()).getContentResource(), slave.getPath(), this.getTrigger());
while (ships.hasNext()) {
LiveRelationship ship = (LiveRelationship)ships.next();
if (ship.getStatus().isCancelled()) continue;
if (this.isSame(ship)) {
Resource targetResource = this.resourceResolver.getResource(ship.getTargetPath());
children.add(new RolloutContextImpl(this, ship, targetResource));
continue;
}
if (!this.contains(ship)) continue;
this.configChanges.put(ship.getTargetPath(), this.forRelationship(ship, true));
}
}
final Resource pageResource = (Resource)page.adaptTo(Resource.class);
filter = new Filter<Page>(){
public boolean includes(Page element) {
return pageResource.getChild(element.getName()) == null;
}
};
}
Iterator targetChildren = slave.listChildren((Filter)filter);
while (targetChildren.hasNext()) {
Page child = (Page)targetChildren.next();
LiveRelationship childRelation = relManager.getLiveRelationship(child.getContentResource(), true);
if (childRelation == null || childRelation.getStatus().isCancelled()) continue;
if (this.isSame(childRelation)) {
children.add(new RolloutContextImpl(this, childRelation, child.getContentResource()));
continue;
}
if (!this.contains(childRelation)) continue;
this.configChanges.put(childRelation.getTargetPath(), this.forRelationship(childRelation, true));
}
}
this.children = children;
}
return this.children.listIterator();
}
public Map<String, RolloutContextImpl> getConfigChanges() {
TreeMap<String, RolloutContextImpl> all = new TreeMap<String, RolloutContextImpl>();
all.putAll(this.configChanges);
if (this.children != null) {
for (RolloutContextImpl child : this.children) {
all.putAll(child.getConfigChanges());
}
}
return all;
}
boolean isSame(LiveRelationship other) {
String otherBlueprint = other.getLiveCopy().getBlueprintPath();
return otherBlueprint.equals(this.getRelationship().getLiveCopy().getBlueprintPath()) && CollectionUtils.isEqualCollection((Collection)other.getRolloutConfigs(), this.getRolloutConfigs());
}
boolean contains(LiveRelationship other) {
LiveCopyManagerImpl.LiveCopyImpl liveCopy = (LiveCopyManagerImpl.LiveCopyImpl)this.getRelationship().getLiveCopy();
boolean parent = Text.getRelativeParent((String)other.getLiveCopy().getPath(), (int)1).equals(liveCopy.getPath());
return parent || Text.isDescendantOrEqual((String)liveCopy.getBlueprintPath(), (String)other.getSourcePath()) && liveCopy.contains(other.getTargetPath());
}
boolean isMoved() {
return this.getRelationship().getLiveCopy() instanceof LiveCopyManagerImpl.LiveCopyImpl && ((LiveCopyManagerImpl.LiveCopyImpl)this.getRelationship().getLiveCopy()).isMoved();
}
private RolloutConfig getNextRolloutConfig() {
return this.getRolloutConfigs().get(++this.currentConfigIdx);
}
}