OrderChildrenActionFactory.java
7.24 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.wcm.api.WCMException
* com.day.cq.wcm.msm.api.LiveAction
* com.day.cq.wcm.msm.api.LiveRelationship
* 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
* javax.jcr.Node
* javax.jcr.NodeIterator
* javax.jcr.RepositoryException
* javax.jcr.nodetype.NodeDefinition
* javax.jcr.nodetype.NodeType
* 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.sling.api.resource.Resource
* org.apache.sling.api.resource.ValueMap
* 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.msm.api.LiveAction;
import com.day.cq.wcm.msm.api.LiveRelationship;
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.impl.Utils;
import java.util.ArrayList;
import java.util.List;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.RepositoryException;
import javax.jcr.nodetype.NodeDefinition;
import javax.jcr.nodetype.NodeType;
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.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=0)
@Service
public class OrderChildrenActionFactory
extends FilteredActionFactoryBase<FilteredAction> {
@Deprecated
@Property(name="cq.wcm.msm.action.rank")
private static final int RANK = 10001;
@Deprecated
@Property(name="cq.wcm.msm.action.title")
private static final String TITLE = "Order children";
@Deprecated
@Property(name="actiontype")
private static final String ACTION_TYPE = "ordering";
static final String DEFAULT_ACTION_NAME = "orderChildren";
@Property(name="liveActionName")
private static final String[] LIVE_ACTION_NAME = new String[]{OrderChildrenAction.class.getSimpleName(), "orderChildren"};
@Reference
private RolloutManager rolloutManager = null;
public String createsAction() {
return LIVE_ACTION_NAME[0];
}
protected OrderChildrenAction newActionInstance(ValueMap config) {
return new OrderChildrenAction(config, this);
}
@Activate
@Modified
protected void configure(ComponentContext componentContext) {
this.setupFilter(componentContext, this.rolloutManager);
}
protected void bindRolloutManager(RolloutManager rolloutManager) {
this.rolloutManager = rolloutManager;
}
protected void unbindRolloutManager(RolloutManager rolloutManager) {
if (this.rolloutManager == rolloutManager) {
this.rolloutManager = null;
}
}
private static final class OrderChildrenAction
extends FilteredAction {
private static final Logger log = LoggerFactory.getLogger(OrderChildrenAction.class);
private OrderChildrenAction(ValueMap config, OrderChildrenActionFactory factory) {
super(config, factory.getPagePropertyFilter(), factory.getComponentFilter(), (BaseActionFactory)factory);
}
protected boolean doHandle(Resource source, Resource target, LiveRelationship relation, boolean resetRollout) throws RepositoryException, WCMException {
return Utils.resourceHasNode(source) && Utils.resourceHasNode(target) && Utils.getHierarchyNode((Node)target.adaptTo(Node.class)).getPrimaryNodeType().hasOrderableChildNodes();
}
protected void doExecute(Resource source, Resource target, LiveRelationship relation, boolean resetRollout) throws RepositoryException, WCMException {
Node masterNode = Utils.getHierarchyNode((Node)source.adaptTo(Node.class));
Node slaveNode = Utils.getHierarchyNode((Node)target.adaptTo(Node.class));
if (!slaveNode.getDefinition().isProtected()) {
OrderChildrenAction.orderChildren(masterNode, slaveNode);
log.debug("Ordered children on {}", (Object)relation.getTargetPath());
}
}
private static void orderChildren(Node masterNode, Node slaveNode) throws WCMException {
try {
if (!slaveNode.getPrimaryNodeType().hasOrderableChildNodes()) {
return;
}
log.debug("ordering for parent {}", (Object)slaveNode.getPath());
List<String> reverseOrderedMasterChildren = OrderChildrenAction.getReverseOrderedChildren(masterNode);
List<String> reverseOrderedSlaveChildren = OrderChildrenAction.getReverseOrderedChildren(slaveNode);
if (reverseOrderedMasterChildren.size() > 1) {
String followingMasterSibling = null;
for (String childName : reverseOrderedMasterChildren) {
if (!slaveNode.hasNode(childName)) continue;
log.debug("order from master: ordering {} before {}", (Object)childName, (Object)followingMasterSibling);
slaveNode.orderBefore(childName, followingMasterSibling);
followingMasterSibling = childName;
}
}
if (reverseOrderedSlaveChildren.size() > 1) {
String followingSlaveSibling = null;
for (String childName : reverseOrderedSlaveChildren) {
if (slaveNode.hasNode(childName) && !masterNode.hasNode(childName)) {
log.debug("only on slave: ordering {} before {}", (Object)childName, (Object)followingSlaveSibling);
slaveNode.orderBefore(childName, followingSlaveSibling);
}
followingSlaveSibling = childName;
}
}
}
catch (RepositoryException e) {
throw new WCMException("Unable to order children", (Throwable)e);
}
}
private static List<String> getReverseOrderedChildren(Node node) throws RepositoryException {
ArrayList<String> res = new ArrayList<String>();
if (node != null) {
NodeIterator ni = node.getNodes();
while (ni.hasNext()) {
Node nodeChild = ni.nextNode();
res.add(0, nodeChild.getName());
}
}
return res;
}
}
}