Cq62TargetContentUpgrade.java
3.95 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
/*
* 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.Value
* javax.jcr.Workspace
* javax.jcr.query.Query
* javax.jcr.query.QueryManager
* javax.jcr.query.QueryResult
* org.apache.commons.lang.StringUtils
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.compat.codeupgrade.impl.cq62;
import com.day.cq.compat.codeupgrade.internal.api.ProgressInfoProvider;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
import javax.jcr.Workspace;
import javax.jcr.query.Query;
import javax.jcr.query.QueryManager;
import javax.jcr.query.QueryResult;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Cq62TargetContentUpgrade
implements ProgressInfoProvider {
private static final String PN_LOCATION = "location";
static final String QUERY_TARGET_RESOURCES = "SELECT * FROM [nt:base] AS s WHERE ISDESCENDANTNODE([/content]) AND (s.[sling:resourceType] = 'cq/personalization/components/target' OR s.[sling:resourceType] = 'mobileapps/components/target') AND (s.[location] IS NULL OR s.[location] = '')";
private static final String RT_TARGET = "cq/personalization/components/target";
private final Logger log;
private String progressInfo;
public Cq62TargetContentUpgrade() {
this.log = LoggerFactory.getLogger(this.getClass());
}
@Override
public String getProgressInfo() {
return this.progressInfo;
}
void setProgressInfo(String info) {
this.progressInfo = info;
this.log.info(this.progressInfo);
}
void doUpgrade(Session session) {
this.setProgressInfo("Migrating target resources");
int num = this.upgradeTargetResources(session);
this.setProgressInfo(String.format("%s target resources migrated.", num));
}
private int upgradeTargetResources(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] = 'cq/personalization/components/target' OR s.[sling:resourceType] = 'mobileapps/components/target') AND (s.[location] IS NULL OR s.[location] = '')", "JCR-SQL2");
NodeIterator targetNodes = q.execute().getNodes();
while (targetNodes.hasNext()) {
Node targetNode = targetNodes.nextNode();
try {
this.upgradeNode(targetNode);
this.save(session);
++i;
}
catch (Exception e) {
this.log.error(String.format("Cannot update node of type %s at %s.", "cq/personalization/components/target", targetNode.getPath()), (Throwable)e);
}
}
}
catch (Exception e) {
this.log.error(String.format("Cannot query repository for resources of type %s", "cq/personalization/components/target"), (Throwable)e);
}
return i;
}
private void upgradeNode(Node node) throws RepositoryException {
String locationValue = null;
if (node.hasProperty("location")) {
Property location = node.getProperty("location");
locationValue = !location.isMultiple() ? location.getString() : location.getValues()[0].getString();
}
if (StringUtils.isEmpty((String)locationValue)) {
this.setProgressInfo("Setting property 'location' of " + node.getPath());
node.setProperty("location", node.getPath());
}
}
private void save(Session session) throws RepositoryException {
if (session.hasPendingChanges()) {
session.save();
}
}
}