Cq561SocialContentUpgrade.java
10.3 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.Node
* javax.jcr.NodeIterator
* javax.jcr.PathNotFoundException
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.compat.codeupgrade.impl.cq561;
import com.day.cq.compat.codeupgrade.internal.api.ProgressInfoProvider;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.PathNotFoundException;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Cq561SocialContentUpgrade
implements ProgressInfoProvider {
private static final String SOCIAL_TALLY_COMPONENTS_RESPONSE = "social/tally/components/response";
private static final String SOCIAL_TALLY_COMPONENT_PREFIX = "social/tally/components/";
private static final String CQ_COMMENT_NODE_TYPE = "cq:Comment";
private static final String TALLYCOUNT = "tallycount";
private static final String TALLYRESPONSES = "tallyresponses";
private static final String SLING_FOLDER = "sling:Folder";
private final Logger log;
private String progressInfo;
private static String SLING_RESOURCE_TYPE = "sling:resourceType";
private static String NT_UNSTRUCTURED_NODE_TYPE = "nt:unstructured";
private static String COMMENT_CONTENT_NODE_TYPE = "cq:CommentContent";
private static String PATH_UGC = "/content/usergenerated";
String PROP_COMPONENT;
public Cq561SocialContentUpgrade() {
this.log = LoggerFactory.getLogger(this.getClass());
this.progressInfo = "No info yet";
this.PROP_COMPONENT = "commentsNode";
}
@Override
public String getProgressInfo() {
return this.progressInfo;
}
void setProgressInfo(String info) {
this.progressInfo = info;
this.log.info(this.progressInfo);
}
void doUpgrade(Session sess) throws Exception {
this.setProgressInfo("Start upgrade.");
Map<UPGRADE_TYPE, List<Node>> nodesToBeUpdatedByType = this.findNodesToBeUpdatedByType(sess);
List<Node> commentNodes = nodesToBeUpdatedByType.get((Object)UPGRADE_TYPE.COMMENT);
this.setProgressInfo("Upgrading comments.");
if (!commentNodes.isEmpty()) {
this.upgradeCommentsRootResourceType(sess, commentNodes);
}
List<Node> attachmentNodes = nodesToBeUpdatedByType.get((Object)UPGRADE_TYPE.ATTACHMENT);
this.setProgressInfo("Upgrading attachments.");
if (!attachmentNodes.isEmpty()) {
this.upgradeAttachmentNodeType(sess, attachmentNodes);
}
List<Node> tallyNodes = nodesToBeUpdatedByType.get((Object)UPGRADE_TYPE.TALLY);
this.setProgressInfo("Upgrading tally nodes.");
if (!tallyNodes.isEmpty()) {
this.upgradeTallyNodeType(sess, tallyNodes);
}
this.setProgressInfo("All done.");
}
private void upgradeTallyNodeType(Session sess, List<Node> tallyNodes) {
for (Node tallyNode : tallyNodes) {
try {
this.setProgressInfo("upgrading tally node: " + (Object)tallyNode);
tallyNode.setPrimaryType("sling:Folder");
Cq561SocialContentUpgrade.save(sess);
}
catch (RepositoryException e) {
try {
this.log.error("Failed to upgrade " + tallyNode.getPath(), (Throwable)e);
continue;
}
catch (RepositoryException e1) {
this.log.error("Failed to upgrade attachment", (Throwable)e);
}
}
}
}
private void upgradeAttachmentNodeType(Session sess, List<Node> attachmentNodes) {
for (Node attachmentFolder : attachmentNodes) {
try {
this.setProgressInfo("upgrading attachment node: " + (Object)attachmentFolder);
attachmentFolder.setPrimaryType("sling:Folder");
Cq561SocialContentUpgrade.save(sess);
}
catch (RepositoryException e) {
try {
this.log.error("Failed to upgrade " + attachmentFolder.getPath(), (Throwable)e);
continue;
}
catch (RepositoryException e1) {
this.log.error("Failed to upgrade attachment", (Throwable)e);
}
}
}
}
private Map<UPGRADE_TYPE, List<Node>> findNodesToBeUpdatedByType(Session sess) throws PathNotFoundException, RepositoryException {
Node root = sess.getNode(PATH_UGC);
ArrayList<Node> tallyNodes = new ArrayList<Node>(50);
ArrayList<Node> attachmentNodes = new ArrayList<Node>(10);
ArrayList<Node> commentNodes = new ArrayList<Node>(50);
this.findNodesToBeUpdated(sess, root, commentNodes, attachmentNodes, tallyNodes);
HashMap<UPGRADE_TYPE, List<Node>> nodesToBeUpdated = new HashMap<UPGRADE_TYPE, List<Node>>(3);
nodesToBeUpdated.put(UPGRADE_TYPE.COMMENT, commentNodes);
nodesToBeUpdated.put(UPGRADE_TYPE.ATTACHMENT, attachmentNodes);
nodesToBeUpdated.put(UPGRADE_TYPE.TALLY, tallyNodes);
return nodesToBeUpdated;
}
private void findNodesToBeUpdated(Session session, Node root, List<Node> commentNodes, List<Node> attachmentNodes, List<Node> tallyNodes) throws RepositoryException {
NodeIterator iterator = root.getNodes();
while (iterator.hasNext()) {
Node child = (Node)iterator.next();
boolean searchChildren = true;
try {
if (this.isCommentsRootNode(session, child)) {
commentNodes.add(child);
}
if (this.isTallyNode(session, child)) {
tallyNodes.add(child);
}
if (this.isAttachmentNode(session, child, root)) {
attachmentNodes.add(child);
searchChildren = false;
}
if (!searchChildren) continue;
this.findNodesToBeUpdated(session, child, commentNodes, attachmentNodes, tallyNodes);
}
catch (RepositoryException e) {
this.log.error("Failed to retrieve comment roots", (Throwable)e);
}
}
}
private boolean isAttachmentNode(Session session, Node node, Node parent) throws PathNotFoundException, RepositoryException {
if (node != null && node.getName().equals("attachments") && parent.isNodeType("cq:Comment") && node.isNodeType(NT_UNSTRUCTURED_NODE_TYPE)) {
return true;
}
return false;
}
private boolean isTallyNode(Session session, Node node) throws PathNotFoundException, RepositoryException {
if (node == null || !node.isNodeType(NT_UNSTRUCTURED_NODE_TYPE)) {
return false;
}
if (node.getName().equals("tallycount") || node.getName().equals("tallyresponses")) {
return true;
}
if (node.hasProperty(SLING_RESOURCE_TYPE) && node.getProperty(SLING_RESOURCE_TYPE).getString().indexOf("social/tally/components/") == 0 && !"social/tally/components/response".equals(node.getProperty(SLING_RESOURCE_TYPE).getString())) {
return true;
}
return false;
}
private boolean isCommentsRootNode(Session sess, Node node) throws RepositoryException {
boolean retVal = false;
if (node.hasProperty(this.PROP_COMPONENT) && node.isNodeType(NT_UNSTRUCTURED_NODE_TYPE)) {
String component = node.getProperty(this.PROP_COMPONENT).getString();
if (component.endsWith(".html")) {
retVal = component.endsWith("comments.social.createcomment.html");
} else {
Node componentNode = sess.getNode(component);
if (componentNode.hasProperty(SLING_RESOURCE_TYPE)) {
String resourceType = componentNode.getProperty(SLING_RESOURCE_TYPE).getString();
retVal = !resourceType.contains("collab");
}
}
}
return retVal;
}
private void upgradeCommentsRootResourceType(Session sess, List<Node> commentsRoots) {
for (Node commentRoot : commentsRoots) {
try {
this.setProgressInfo("upgrade " + (Object)commentRoot);
Node pageNode = commentRoot.getParent();
String nodeName = commentRoot.getName();
Cq561SocialContentUpgrade.renameNode(commentRoot, "old" + nodeName);
Node tempNode = pageNode.addNode(nodeName, COMMENT_CONTENT_NODE_TYPE);
this.moveChildren(tempNode, commentRoot);
commentRoot.remove();
Cq561SocialContentUpgrade.save(sess);
}
catch (RepositoryException e) {
try {
this.log.error("Failed to upgrade " + commentRoot.getPath(), (Throwable)e);
continue;
}
catch (RepositoryException e1) {
this.log.error("Failed to upgrade comment", (Throwable)e);
}
}
}
}
public static void save(Session session) throws RepositoryException, IllegalArgumentException {
if (session == null) {
throw new IllegalArgumentException("resolver must be adaptable to session");
}
try {
session.save();
}
catch (RepositoryException e) {
session.refresh(false);
throw e;
}
}
private static void renameNode(Node node, String newName) throws RepositoryException {
node.getSession().move(node.getPath(), node.getParent().getPath() + "/" + newName);
}
private void moveChildren(Node dest, Node src) throws RepositoryException {
Session session = src.getSession();
NodeIterator iterator = src.getNodes();
while (iterator.hasNext()) {
Node child = (Node)iterator.next();
session.move(child.getPath(), dest.getPath() + "/" + child.getName());
}
}
private static enum UPGRADE_TYPE {
COMMENT,
ATTACHMENT,
TALLY;
private UPGRADE_TYPE() {
}
}
}