/
MovingPage.java
156 lines (140 loc) · 6.1 KB
/
MovingPage.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
/*
* ACS AEM Commons
*
* Copyright (C) 2013 - 2023 Adobe
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.adobe.acs.commons.mcp.impl.processes.renovator;
import com.adobe.acs.commons.fam.actions.Actions;
import com.day.cq.replication.ReplicationActionType;
import com.day.cq.replication.ReplicationOptions;
import com.day.cq.replication.Replicator;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.PageManagerFactory;
import com.day.cq.wcm.api.WCMException;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.reflect.FieldUtils;
import org.apache.jackrabbit.JcrConstants;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import javax.jcr.Session;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import static com.adobe.acs.commons.mcp.impl.processes.renovator.Util.*;
/**
* Represents a page being moved.
*/
public class MovingPage extends MovingNode {
PageManagerFactory pageManagerFactory;
public MovingPage(PageManagerFactory pageManagerFactory) {
this.pageManagerFactory = pageManagerFactory;
}
@Override
public boolean isCopiedBeforeMove() {
return false;
}
@Override
public boolean isSupposedToBeReferenced() {
return true;
}
@Override
public boolean isAbleToHaveChildren() {
return true;
}
@Override
protected boolean isAuditableMove() {
return true;
}
@Override
public void move(ReplicatorQueue replicatorQueue, ResourceResolver rr) throws IllegalAccessException, MovingException {
// For starters, create a page manager with a modified replicator queue
PageManager manager = pageManagerFactory.getPageManager(rr);
Replicator replicator = (Replicator) Proxy.newProxyInstance(
Replicator.class.getClassLoader(),
new Class[]{Replicator.class},
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
switch (method.getName()){
case "replicate":
if(args.length == 3){
replicatorQueue.replicate((Session)args[0], (ReplicationActionType)args[1], (String)args[2]);
} else if (args.length == 4){
replicatorQueue.replicate((Session)args[0], (ReplicationActionType)args[1], (String)args[2], (ReplicationOptions)args[3]);
}
break;
default:
break;
}
return null;
}
});
Field replicatorField = FieldUtils.getDeclaredField(manager.getClass(), "replicator", true);
FieldUtils.writeField(replicatorField, manager, replicator, true);
// Some simple transformations
String contentPath = getSourcePath() + "/" + JcrConstants.JCR_CONTENT;
String destinationParent = StringUtils.substringBeforeLast(getDestinationPath(), "/");
// Attempt move operation
try {
Actions.retry(10, 500, res -> {
waitUntilResourceFound(res, destinationParent);
moveOrClonePage(rr, manager, contentPath, destinationParent, res);
movePageChildren(rr, res);
}).accept(rr);
} catch (Exception e) {
throw new MovingException(getSourcePath(), e);
}
}
private void moveOrClonePage(ResourceResolver rr, PageManager manager, String contentPath, String destinationParent, ResourceResolver res) throws WCMException, PersistenceException {
Resource source = rr.getResource(getSourcePath());
if (resourceExists(res, contentPath)) {
manager.move(source,
getDestinationPath(),
getPreviousSibling(),
false,
true,
listToStringArray(getAllReferences()),
listToStringArray(getPublishedReferences()));
} else if (!resourceExists(res, getDestinationPath())) {
Resource parent = res.getResource(destinationParent);
res.create(parent, source.getName(), getClonedProperties(source));
}
res.commit();
res.refresh();
}
private void movePageChildren(ResourceResolver rr, ResourceResolver res) throws MovingException {
Resource source;
source = rr.getResource(getSourcePath());
try {
if (source != null && source.hasChildren()) {
for (Resource child : source.getChildren()) {
if (!hasChild(child.getPath())) {
String childDestination = child.getPath().replaceAll(getSourcePath(), getDestinationPath());
String childDestinationParent = StringUtils.substringBeforeLast(childDestination, "/");
if (!resourceExists(res, childDestination)) {
waitUntilResourceFound(res, childDestinationParent);
res.move(child.getPath(), childDestination);
}
}
}
res.commit();
}
} catch (PersistenceException e) {
throw new MovingException(getSourcePath(), e);
}
}
}