Cq61CloudSettingsContentUpgrade.java
3.87 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
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.compat.codeupgrade.impl.cq61;
import com.day.cq.compat.codeupgrade.internal.api.ProgressInfoProvider;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Cq61CloudSettingsContentUpgrade
implements ProgressInfoProvider {
private static final String CLOUDSETTINGS_PATH = "etc/cloudsettings";
private final Logger log;
private String progressInfo;
public Cq61CloudSettingsContentUpgrade() {
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);
}
private Node findBackupMergerRepositoryRoot(Session sess) throws RepositoryException {
ArrayList<Node> matchingNodes = new ArrayList<Node>();
Node mainRoot = sess.getNode("/var/upgrade/PreUpgradeBackup");
NodeIterator it = mainRoot.getNodes();
while (it.hasNext()) {
Node child = it.nextNode();
if (!child.hasNode("to-process")) continue;
matchingNodes.add(child.getNode("to-process"));
}
if (matchingNodes.size() == 0) {
this.log.info("No backup node matching {} found under {}, no content to upgrade", (Object)"to-process", (Object)mainRoot.getPath());
return null;
}
if (matchingNodes.size() > 1) {
this.log.error("Multiple backup nodes found, please delete the extraneous ones are rerun this upgrade component: {}", matchingNodes);
return null;
}
return (Node)matchingNodes.get(0);
}
void doUpgrade(Session sess) throws Exception {
Node root = this.findBackupMergerRepositoryRoot(sess);
if (root == null) {
return;
}
this.log.info("Starting migration");
this.migrateContextHubConfiguration(sess, root);
this.log.info("Completed migration");
}
private void migrateContextHubConfiguration(Session sess, Node root) throws RepositoryException {
if (!root.hasNode("etc/cloudsettings")) {
this.log.error("Node 'etc/cloudsettings' was not found.");
return;
}
this.setProgressInfo("Migrating Adobe ContextHub configurations");
Node cloudsettingsNode = root.getNode("etc/cloudsettings");
this.backupItem(cloudsettingsNode, "default");
sess.save();
}
private void backupItem(Node source, String itemName) throws RepositoryException {
if (source.hasNode(itemName)) {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());
String backedUpItem = itemName + "-pre-upgrade_" + timeStamp;
String nodeName = source.getPath() + "/" + itemName;
Node item = source.getNode(itemName);
String itemTitle = "";
if (item.hasProperty("jcr:title")) {
itemTitle = itemTitle + ((itemTitle = item.getProperty("jcr:title").getString()).isEmpty() ? "" : " ");
}
itemTitle = itemTitle + "(pre-upgrade backup)";
item.setProperty("jcr:title", itemTitle);
this.log.info("Backing up '" + nodeName + "' to '" + backedUpItem + "'.");
source.getSession().move(nodeName, source.getPath() + "/" + backedUpItem);
}
}
}