ReferencesUpdateActionFactory.java
9.87 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.wcm.api.WCMException
* com.day.cq.wcm.commons.ReferenceSearch
* com.day.cq.wcm.msm.api.LiveAction
* com.day.cq.wcm.msm.api.LiveCopy
* com.day.cq.wcm.msm.api.LiveRelationship
* com.day.cq.wcm.msm.api.LiveStatus
* com.day.cq.wcm.msm.api.RolloutManager
* com.day.cq.wcm.msm.commons.BaseActionFactory
* com.day.cq.wcm.msm.commons.FilteredAction
* com.day.cq.wcm.msm.commons.FilteredActionFactoryBase
* com.day.cq.wcm.msm.commons.ItemFilterImpl
* com.day.cq.wcm.msm.commons.ItemFilterUtil
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.PropertyIterator
* javax.jcr.RepositoryException
* org.apache.felix.scr.annotations.Activate
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Modified
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.jackrabbit.util.Text
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.commons.osgi.PropertiesUtil
* org.osgi.service.component.ComponentContext
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.wcm.msm.impl.actions;
import com.day.cq.wcm.api.WCMException;
import com.day.cq.wcm.commons.ReferenceSearch;
import com.day.cq.wcm.msm.api.LiveAction;
import com.day.cq.wcm.msm.api.LiveCopy;
import com.day.cq.wcm.msm.api.LiveRelationship;
import com.day.cq.wcm.msm.api.LiveStatus;
import com.day.cq.wcm.msm.api.RolloutManager;
import com.day.cq.wcm.msm.commons.BaseActionFactory;
import com.day.cq.wcm.msm.commons.FilteredAction;
import com.day.cq.wcm.msm.commons.FilteredActionFactoryBase;
import com.day.cq.wcm.msm.commons.ItemFilterImpl;
import com.day.cq.wcm.msm.commons.ItemFilterUtil;
import com.day.cq.wcm.msm.impl.LiveCopyManagerImpl;
import com.day.cq.wcm.msm.impl.LiveCopyServiceImpl;
import com.day.cq.wcm.msm.impl.Utils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Dictionary;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.RepositoryException;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Modified;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.jackrabbit.util.Text;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=1, label="%cq.wcm.msm.impl.actions.referencesupdate.label", description="%cq.wcm.msm.impl.actions.referencesupdate.description")
@Service
public class ReferencesUpdateActionFactory
extends FilteredActionFactoryBase<ReferencesUpdateAction> {
@org.apache.felix.scr.annotations.Property(name="liveActionName", propertyPrivate=1)
private static final String[] LIVE_ACTION_NAME = new String[]{ReferencesUpdateAction.class.getSimpleName(), "referencesUpdate"};
@org.apache.felix.scr.annotations.Property(name="cq.wcm.msm.action.excludednodetypes")
private static final String[] DEFAULT_FILTER_NODE_TYPES = ItemFilterUtil.EMPTY_PROPERTIES;
@org.apache.felix.scr.annotations.Property(name="cq.wcm.msm.action.excludedparagraphitems")
private static final String[] DEFAULT_EXCLUDED_PARAGRAPH_ITEMS = ItemFilterUtil.EMPTY_PROPERTIES;
@org.apache.felix.scr.annotations.Property(name="cq.wcm.msm.action.excludedprops")
private static final String[] DEFAULT_EXCLUDED_PROPERTIES = ItemFilterUtil.EMPTY_PROPERTIES;
private static final boolean NESTED_DEFAULT = false;
@org.apache.felix.scr.annotations.Property(boolValue={0})
static final String PN_ADAPT_NESTED = "cq.wcm.msm.impl.action.referencesupdate.prop_updateNested";
private static final Logger log = LoggerFactory.getLogger(ReferencesUpdateActionFactory.class);
@Reference
private LiveCopyServiceImpl liveCopyService = null;
@Reference
private RolloutManager rolloutManager = null;
private boolean updateNestedLiveCopies;
protected ReferencesUpdateAction newActionInstance(ValueMap config) {
return new ReferencesUpdateAction(config, this, this.liveCopyService);
}
public String createsAction() {
return LIVE_ACTION_NAME[0];
}
@Activate
@Modified
protected void configure(ComponentContext context) {
this.setupFilter(context, this.rolloutManager);
this.updateNestedLiveCopies = PropertiesUtil.toBoolean(context.getProperties().get("cq.wcm.msm.impl.action.referencesupdate.prop_updateNested"), (boolean)false);
}
protected void bindLiveCopyService(LiveCopyServiceImpl liveCopyServiceImpl) {
this.liveCopyService = liveCopyServiceImpl;
}
protected void unbindLiveCopyService(LiveCopyServiceImpl liveCopyServiceImpl) {
if (this.liveCopyService == liveCopyServiceImpl) {
this.liveCopyService = null;
}
}
protected void bindRolloutManager(RolloutManager rolloutManager) {
this.rolloutManager = rolloutManager;
}
protected void unbindRolloutManager(RolloutManager rolloutManager) {
if (this.rolloutManager == rolloutManager) {
this.rolloutManager = null;
}
}
static final class ReferencesUpdateAction
extends FilteredAction {
private static final Collection<? extends String> DEFAULT_EXCLUSION = Collections.singleton("cq:master");
private final LiveCopyServiceImpl liveCopyService;
private final Boolean updateNested;
private ReferencesUpdateAction(ValueMap config, ReferencesUpdateActionFactory factory, LiveCopyServiceImpl lcService) {
super(config, factory.getPagePropertyFilter(), factory.getComponentFilter(), (BaseActionFactory)factory);
this.liveCopyService = lcService;
this.updateNested = config == null ? factory.updateNestedLiveCopies : (Boolean)config.get("cq.wcm.msm.impl.action.referencesupdate.prop_updateNested", (Object)factory.updateNestedLiveCopies);
}
protected boolean doHandle(Resource source, Resource target, LiveRelationship relation, boolean resetRollout) throws RepositoryException, WCMException {
return Utils.resourceHasNode(source) && Utils.resourceHasNode(target);
}
protected void doExecute(Resource source, Resource target, LiveRelationship relation, boolean resetRollout) throws RepositoryException, WCMException {
Node masterNode = Utils.getWorkingNode((Node)source.adaptTo(Node.class));
Node slaveNode = Utils.getWorkingNode((Node)target.adaptTo(Node.class));
Iterator mpi = this.getFilteredProperties(masterNode);
if (mpi.hasNext()) {
Set<String> excluded = this.buildExclusionSet(slaveNode, relation.getStatus().getCanceledProperties());
ArrayList<LiveCopy> all = new ArrayList<LiveCopy>();
if (this.updateNested()) {
LiveCopyManagerImpl lcManager = this.liveCopyService.createLiveCopyManager(target.getResourceResolver());
ReferencesUpdateAction.collectRoots(lcManager, relation.getLiveCopy(), all);
}
all.add(relation.getLiveCopy());
for (LiveCopy liveCopy : all) {
new ReferenceSearch().adjustReferences(slaveNode, liveCopy.getBlueprintPath(), liveCopy.getPath(), true, excluded);
}
log.debug("Executed references adjustment for {}", (Object)slaveNode.getPath());
}
}
boolean updateNested() {
return this.updateNested;
}
private Set<String> buildExclusionSet(Node slaveNode, List<String> canceledProperties) throws RepositoryException {
HashSet<String> exclusion = new HashSet<String>();
ItemFilterImpl itemFilter = this.getFilter(slaveNode);
PropertyIterator itr = slaveNode.getProperties();
while (itr.hasNext()) {
Property prop = itr.nextProperty();
if (!itemFilter.excludes(prop)) continue;
exclusion.add(prop.getName());
}
exclusion.addAll(canceledProperties);
exclusion.addAll(DEFAULT_EXCLUSION);
return exclusion;
}
private static void collectRoots(LiveCopyManagerImpl lcManager, LiveCopy liveCopy, ArrayList<LiveCopy> all) throws WCMException, RepositoryException {
LiveCopyManagerImpl.LiveCopyImpl rootLc;
String rootPath = liveCopy.getPath();
String parent = Text.getRelativeParent((String)rootPath, (int)1);
while ((rootLc = lcManager.getContainingLiveCopy(parent)) != null && !(parent = Text.getRelativeParent((String)(rootPath = rootLc.getPath()), (int)1)).equals("/")) {
}
LiveCopyManagerImpl.LiveCopyImpl previous = null;
for (LiveCopyManagerImpl.LiveCopyImpl lc : lcManager.getLiveCopiesInBranch(rootPath)) {
if (previous == null) {
all.add(lc);
} else if (Text.isDescendant((String)previous.getPath(), (String)lc.getPath())) {
String relPath = Utils.relativePath(previous.getPath(), lc.getPath());
if (!Utils.appendPath(previous.getBlueprintPath(), relPath).equals(lc.getBlueprintPath())) {
all.add(lc);
}
} else {
all.add(lc);
}
previous = lc;
}
}
}
}