Cq62MobileContentUpgrade.java
6.15 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.Node
* javax.jcr.NodeIterator
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.Workspace
* javax.jcr.nodetype.NodeType
* javax.jcr.query.Query
* javax.jcr.query.QueryManager
* javax.jcr.query.QueryResult
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.compat.codeupgrade.impl.cq62;
import com.day.cq.compat.codeupgrade.internal.api.ProgressInfoProvider;
import java.util.HashSet;
import java.util.Set;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Workspace;
import javax.jcr.nodetype.NodeType;
import javax.jcr.query.Query;
import javax.jcr.query.QueryManager;
import javax.jcr.query.QueryResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Cq62MobileContentUpgrade
implements ProgressInfoProvider {
static final String QUERY_MOBILE_APPS = "SELECT * FROM [nt:base] AS s WHERE ISDESCENDANTNODE([/content]) AND (s.[sling:resourceType] = 'mobileapps/core/components/group' OR s.[pge-type] = 'app-group') ";
public static final String MIXIN_CQ_MOBILE_APP = "cq:MobileApp";
private static final String PN_SLING_RESOURCE_TYPE = "sling:resourceType";
private static final String RT_INSTANCE_PG_OLD = "mobileapps/components/phonegap/instance";
private static final String RT_INSTANCE_PG = "mobileapps/phonegap/components/instance";
private static Set<String> pathList = new HashSet<String>(2);
static final String QUERY_INSTANCE_RESOURCES = "SELECT * FROM [nt:base] AS s WHERE ISDESCENDANTNODE('%s') AND s.[sling:resourceType] = 'mobileapps/components/phonegap/instance'";
private final Logger log;
private String progressInfo;
public Cq62MobileContentUpgrade() {
this.log = LoggerFactory.getLogger(this.getClass());
}
@Override
public String getProgressInfo() {
return this.progressInfo;
}
void doUpgrade(Session session) {
this.setProgressInfo("Migrating mobile app resources");
int num = this.upgradeMobileResources(session);
this.setProgressInfo(String.format("%s mobile app resources migrated.", num));
this.setProgressInfo("Migrating PhoneGap resources");
num = this.upgradePhoneGapResources(session);
this.setProgressInfo(String.format("%s PhoneGap resources migrated.", num));
}
private int upgradeMobileResources(Session session) {
int i = 0;
try {
QueryManager qm = session.getWorkspace().getQueryManager();
Query q = qm.createQuery("SELECT * FROM [nt:base] AS s WHERE ISDESCENDANTNODE([/content]) AND (s.[sling:resourceType] = 'mobileapps/core/components/group' OR s.[pge-type] = 'app-group') ", "JCR-SQL2");
NodeIterator groupNodes = q.execute().getNodes();
while (groupNodes.hasNext()) {
Node appNode = groupNodes.nextNode();
try {
boolean isDirty = this.upgradeApp(appNode);
if (!isDirty) continue;
this.save(session);
++i;
}
catch (Exception e) {
this.log.error(String.format("Cannot update app at %s.", appNode.getPath()), (Throwable)e);
}
}
}
catch (Exception e) {
this.log.error("Unable to query repository for mobile app resources", (Throwable)e);
}
return i;
}
private int upgradePhoneGapResources(Session session) {
int i = 0;
try {
QueryManager qm = session.getWorkspace().getQueryManager();
for (String path : pathList) {
String query = String.format("SELECT * FROM [nt:base] AS s WHERE ISDESCENDANTNODE('%s') AND s.[sling:resourceType] = 'mobileapps/components/phonegap/instance'", path);
Query q = qm.createQuery(query, "JCR-SQL2");
NodeIterator targetNodes = q.execute().getNodes();
while (targetNodes.hasNext()) {
Node targetNode = targetNodes.nextNode();
try {
boolean isDirty = this.upgradeType(targetNode);
if (!isDirty) continue;
this.save(session);
++i;
}
catch (Exception e) {
this.log.error(String.format("Cannot update node of type %s at %s.", "mobileapps/components/phonegap/instance", targetNode.getPath()), (Throwable)e);
}
}
}
}
catch (Exception e) {
this.log.error(String.format("Cannot query repository for resources of type %s", "mobileapps/components/phonegap/instance"), (Throwable)e);
}
return i;
}
private boolean upgradeApp(Node appNode) throws RepositoryException {
boolean hasChanged = this.addMixin(appNode);
return hasChanged;
}
private boolean addMixin(Node groupNode) throws RepositoryException {
NodeType[] mixinNodeTypes;
boolean hasMixin = false;
Node parent = groupNode.getParent();
for (NodeType mixin : mixinNodeTypes = parent.getMixinNodeTypes()) {
if (!mixin.isNodeType("cq:MobileApp")) continue;
hasMixin = true;
break;
}
if (!hasMixin) {
parent.addMixin("cq:MobileApp");
}
return !hasMixin;
}
private boolean upgradeType(Node node) throws RepositoryException {
if (node.hasProperty("sling:resourceType")) {
node.setProperty("sling:resourceType", "mobileapps/phonegap/components/instance");
return true;
}
return false;
}
private void setProgressInfo(String info) {
this.progressInfo = info;
this.log.info(info);
}
private void save(Session session) throws RepositoryException {
if (session.hasPendingChanges()) {
session.save();
}
}
static {
pathList.add("/content");
pathList.add("/apps");
}
}