CQ62SearchFacetsConfigUpdate.java
6.29 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.commons.jcr.JcrUtil
* javax.jcr.Node
* javax.jcr.NodeIterator
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.nodetype.NodeDefinition
* org.apache.felix.scr.annotations.Activate
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Property
* org.osgi.service.component.ComponentContext
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.compat.codeupgrade.impl.cq62;
import com.day.cq.commons.jcr.JcrUtil;
import com.day.cq.compat.codeupgrade.internal.api.ProgressInfoProvider;
import java.util.Dictionary;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.nodetype.NodeDefinition;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=1, immediate=1, label="Adobe CQ 6.2 Search Facets Config upgrader", description="moves customized search facets from apps to conf")
public class CQ62SearchFacetsConfigUpdate
implements ProgressInfoProvider {
private static final Logger log = LoggerFactory.getLogger((String)CQ62SearchFacetsConfigUpdate.class.getName());
private static final String TENANTS_ROOT = "/etc/tenants";
private static final String RESOURCE_TYPE_CONTSYS = "granite/ui/components/foundation/contsys";
private String progressInfo;
@Property(value={"cq/gui/content/facets:cq/search/facets", "dam/gui/content/facets:dam/search/facets", "commerce/gui/content/facets:commerce/search/facets", "screens/dcc/content/facets:screens/dcc/search/facets", "cq/core/content/projects/facets:projects/search/facets"})
public static final String PROP_FACET_PATH_LIST = "facet.path.list";
private static String[][] facetPathList;
@Activate
public void activate(ComponentContext ctx) throws Exception {
Dictionary properties = ctx.getProperties();
String[] facetMap = (String[])properties.get("facet.path.list");
facetPathList = new String[facetMap.length][2];
for (int i = 0; i < facetMap.length; ++i) {
CQ62SearchFacetsConfigUpdate.facetPathList[i] = facetMap[i].split(":");
}
}
@Override
public String getProgressInfo() {
return this.progressInfo;
}
void setProgressInfo(String info) {
this.progressInfo = info;
log.info(this.progressInfo);
}
void doUpgrade(Session session) {
try {
this.setProgressInfo("Migrating search facets from /apps to /conf");
for (String[] relativePath : facetPathList) {
Node tenantsNode;
if (session.nodeExists("/conf/global/settings")) {
this.move("/apps", "/conf/global/settings", relativePath, session);
}
if ((tenantsNode = session.getNode("/etc/tenants")) == null) continue;
NodeIterator it = tenantsNode.getNodes();
while (it.hasNext()) {
Node node = it.nextNode();
if (node.getDefinition().isProtected()) continue;
String tenantId = node.getName();
if (!session.nodeExists("/conf/tenants/" + tenantId + "/settings")) continue;
this.move("/apps/" + tenantId, "/conf/tenants/" + tenantId + "/settings", relativePath, session);
}
}
if (session.hasPendingChanges()) {
session.save();
}
this.setProgressInfo("migration of custom search facets configurations complete");
}
catch (RepositoryException e) {
this.setProgressInfo("migration of custom search facets configurations failed");
log.error("migration failed", (Throwable)e);
}
}
private void move(String fromRP, String toRP, String[] mapping, Session session) {
String from = fromRP + "/" + mapping[0];
String to = toRP + "/" + mapping[1];
try {
Node fromNode = session.getNode(from);
if (!session.nodeExists(from)) {
return;
}
Node toNode = !session.nodeExists(to) ? JcrUtil.createPath((String)to, (String)"cq:Page", (String)"cq:Page", (Session)session, (boolean)false) : session.getNode(to);
toNode.addNode("jcr:content", "nt:unstructured").setProperty("mergeList", true);
NodeIterator fromItr = fromNode.getNodes();
while (fromItr.hasNext()) {
Node fromFacet = fromItr.nextNode();
String fromFacetPath = fromFacet.getPath();
String facetName = fromFacet.getName();
if (toNode.hasNode(facetName) || !fromFacet.hasProperty("sling:resourceType") || !fromFacet.getProperty("sling:resourceType").getString().equals("granite/ui/components/foundation/contsys")) continue;
Node toFacetNode = toNode.addNode(facetName, "cq:Page");
session.move(fromFacet.getPath(), toFacetNode.getPath() + "/" + "jcr:content");
Node toJcrContent = toFacetNode.getNode("jcr:content");
String predicatesConfig = toJcrContent.getProperty("predicatesConfig").getString();
int index = predicatesConfig.indexOf(mapping[0]);
if (index > -1) {
toJcrContent.setProperty("predicatesConfig", "/mnt/overlay/settings/" + predicatesConfig.substring(index));
}
String libsPath = "/libs" + toJcrContent.getPath().substring(toJcrContent.getPath().indexOf("/settings"));
Node libsNode = null;
libsNode = session.nodeExists(libsPath) ? session.getNode(libsPath) : session.getNode("/libs" + fromFacetPath.substring(fromRP.length()));
if (libsNode == null || !libsNode.hasProperty("jcr:title")) continue;
toJcrContent.setProperty("jcr:title", libsNode.getProperty("jcr:title").getString());
}
}
catch (RepositoryException e) {
log.error("error in moving facets from " + from + " to " + to, (Throwable)e);
}
}
}