Cq60SocialContentUpgrade.java
5.35 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.Workspace
* javax.jcr.nodetype.NodeType
* javax.jcr.nodetype.NodeTypeDefinition
* javax.jcr.nodetype.NodeTypeIterator
* javax.jcr.nodetype.NodeTypeManager
* javax.jcr.nodetype.NodeTypeTemplate
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.compat.codeupgrade.impl.cq60;
import com.day.cq.compat.codeupgrade.internal.api.ProgressInfoProvider;
import java.util.Collection;
import java.util.HashMap;
import java.util.Set;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Workspace;
import javax.jcr.nodetype.NodeType;
import javax.jcr.nodetype.NodeTypeDefinition;
import javax.jcr.nodetype.NodeTypeIterator;
import javax.jcr.nodetype.NodeTypeManager;
import javax.jcr.nodetype.NodeTypeTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Cq60SocialContentUpgrade
implements ProgressInfoProvider {
private static final String CQ_COMMENT_NODE_TYPE = "cq:Comment";
private static final String CQ_TAGGABLE_MIXIN = "cq:Taggable";
private final Logger log = LoggerFactory.getLogger(Cq60SocialContentUpgrade.class);
private String progressInfo = "No info yet";
@Override
public String getProgressInfo() {
return this.progressInfo;
}
void setProgressInfo(String info) {
this.progressInfo = info;
this.log.info(this.progressInfo);
}
void doUpgrade(Session session) throws Exception {
this.setProgressInfo("Comment nodetype upgrade started.");
boolean isTaggable = this.hasMixin("cq:Comment", "cq:Taggable", session);
if (isTaggable) {
this.setProgressInfo("Nothing to do, node type cq:Comment is already upgraded.");
} else {
this.setProgressInfo("Upgrading node type cq:Comment");
this.addMixinTo("cq:Comment", "cq:Taggable", session);
this.setProgressInfo("Sucessfully added mixin cq:Taggable to cq:Comment");
}
this.setProgressInfo("Done upgrading comment nodetype.");
}
private boolean hasMixin(String nodeName, String mixin, Session session) throws RepositoryException {
boolean output = false;
String[] superNodeTypeNames = null;
NodeTypeManager ntManager = session.getWorkspace().getNodeTypeManager();
if (ntManager.hasNodeType(nodeName)) {
NodeType nt = ntManager.getNodeType(nodeName);
superNodeTypeNames = nt.getDeclaredSupertypeNames();
}
if (mixin != null && superNodeTypeNames != null && superNodeTypeNames.length > 0) {
for (String name : superNodeTypeNames) {
if (!mixin.equals(name)) continue;
output = true;
}
}
return output;
}
protected void addMixinTo(String nodeName, String mixin, Session session) throws RepositoryException {
NodeTypeManager ntManager = session.getWorkspace().getNodeTypeManager();
if (ntManager.hasNodeType(nodeName)) {
NodeType oldNodeType = ntManager.getNodeType(nodeName);
String[] currentSuperTypes = oldNodeType.getDeclaredSupertypeNames();
String[] newSuperTypes = new String[currentSuperTypes.length + 1];
for (int i = 0; i < currentSuperTypes.length; ++i) {
newSuperTypes[i] = currentSuperTypes[i];
}
newSuperTypes[currentSuperTypes.length] = mixin;
NodeTypeTemplate ntTemplate = ntManager.createNodeTypeTemplate((NodeTypeDefinition)oldNodeType);
ntTemplate.setDeclaredSuperTypeNames(newSuperTypes);
HashMap<String, NodeTypeTemplate> subtypes = this.getSubtypesTemplate(oldNodeType, ntManager);
this.setProgressInfo("Unregistering " + nodeName + "'s subtypes.");
String[] subtypesNames = new String[subtypes.size()];
subtypes.keySet().toArray(subtypesNames);
for (String subtypeName : subtypesNames) {
if (!ntManager.hasNodeType(subtypeName)) continue;
ntManager.unregisterNodeType(subtypeName);
}
this.setProgressInfo("Unregistering node type " + nodeName);
if (ntManager.hasNodeType(nodeName)) {
ntManager.unregisterNodeType(nodeName);
}
this.setProgressInfo("Registering upgraded node type " + nodeName + " with mixin " + mixin);
ntManager.registerNodeType((NodeTypeDefinition)ntTemplate, false);
this.setProgressInfo("Registering " + nodeName + "'s subtypes.");
NodeTypeTemplate[] subtypesTemplates = new NodeTypeTemplate[subtypes.size()];
subtypes.values().toArray((T[])subtypesTemplates);
ntManager.registerNodeTypes((NodeTypeDefinition[])subtypesTemplates, false);
}
}
private HashMap<String, NodeTypeTemplate> getSubtypesTemplate(NodeType node, NodeTypeManager ntManager) throws RepositoryException {
HashMap<String, NodeTypeTemplate> out = new HashMap<String, NodeTypeTemplate>();
NodeTypeIterator subtypesIter = node.getDeclaredSubtypes();
while (subtypesIter.hasNext()) {
NodeType child = subtypesIter.nextNodeType();
out.put(child.getName(), ntManager.createNodeTypeTemplate((NodeTypeDefinition)child));
}
return out;
}
}