AnalyticsContentUpdater.java
6.89 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.search.Predicate
* com.day.cq.search.PredicateGroup
* com.day.cq.search.Query
* com.day.cq.search.QueryBuilder
* com.day.cq.search.result.Hit
* com.day.cq.search.result.SearchResult
* com.day.cq.wcm.webservicesupport.Configuration
* com.day.cq.wcm.webservicesupport.ConfigurationManager
* com.day.cq.wcm.webservicesupport.ConfigurationManagerFactory
* javax.jcr.Session
* org.apache.felix.scr.annotations.Activate
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceResolverFactory
* org.apache.sling.api.resource.ValueMap
* org.osgi.service.component.ComponentContext
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.analytics.sitecatalyst.impl;
import com.day.cq.analytics.sitecatalyst.impl.util.ReportConfigUtils;
import com.day.cq.search.Predicate;
import com.day.cq.search.PredicateGroup;
import com.day.cq.search.Query;
import com.day.cq.search.QueryBuilder;
import com.day.cq.search.result.Hit;
import com.day.cq.search.result.SearchResult;
import com.day.cq.wcm.webservicesupport.Configuration;
import com.day.cq.wcm.webservicesupport.ConfigurationManager;
import com.day.cq.wcm.webservicesupport.ConfigurationManagerFactory;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.jcr.Session;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.resource.ValueMap;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(name="Analytics Content Updater Component", label="Analytics Content Updater Component", description="Automatically detects and updates the content structure that relates to the analytics integration", immediate=1)
@Service(value={AnalyticsContentUpdater.class})
public class AnalyticsContentUpdater {
private static final Logger LOG = LoggerFactory.getLogger(AnalyticsContentUpdater.class);
private static final String ANALYTICS_CONTENT_UPDATER_SUBSERVICE = "content-updater";
private boolean performedUpgrade = false;
@Reference
private QueryBuilder queryBuilder;
@Reference
private ResourceResolverFactory resourceResolverFactory;
@Reference
private ConfigurationManagerFactory configManagerFactory;
@Activate
protected void activate(ComponentContext ctx) {
this.doUpgrade();
}
public void doUpgrade() {
if (this.performedUpgrade) {
return;
}
PredicateGroup rootPred = new PredicateGroup("attachedaaconfigs");
Predicate pathPred = new Predicate("path");
pathPred.set("path", "/content");
rootPred.add(pathPred);
Predicate typePred = new Predicate("nodeType", "property");
typePred.set("property", "sling:resourceType");
typePred.set("value", "cq/analytics/components/sitecatalyst");
rootPred.add(typePred);
Predicate namePred = new Predicate("nodeName", "nodename");
namePred.set("nodename", "analytics");
rootPred.add(namePred);
try {
ResourceResolver resolver = this.resourceResolverFactory.getServiceResourceResolver(Collections.singletonMap("sling.service.subservice", "content-updater"));
Session session = (Session)resolver.adaptTo(Session.class);
LOG.info("Searching for all content pages having an attached Analytics cloud configuration...");
Query query = this.queryBuilder.createQuery(rootPred, session);
SearchResult result = query.getResult();
List hits = result.getHits();
for (Hit searchHit : hits) {
String analyticsNodePath = searchHit.getPath();
LOG.info("Checking analytics report configurations for path " + analyticsNodePath);
Resource reportConfigRes = resolver.getResource(analyticsNodePath + "/" + "reportConfigs");
Resource analyticsResource = resolver.getResource(analyticsNodePath);
if (reportConfigRes == null) {
LOG.info("Old analytics cloud configuration detected for path " + analyticsNodePath + " updating configuration... ");
Resource pageContentResource = analyticsResource.getParent();
ValueMap props = (ValueMap)pageContentResource.adaptTo(ValueMap.class);
String[] configurations = (String[])props.get("cq:cloudserviceconfigs", (Object)new String[0]);
ConfigurationManager configManager = this.configManagerFactory.getConfigurationManager(resolver);
Configuration scCloudConfig = configManager.getConfiguration("sitecatalyst", configurations);
ReportConfigUtils.addAnalytics(pageContentResource, scCloudConfig.getResource(), configManager);
resolver.commit();
LOG.info("Analytics cloud configuration at path " + analyticsNodePath + " successfully updated!");
continue;
}
LOG.info("Latest analytics cloud configuration detected for path " + analyticsNodePath + ", not performing any upgrades!");
}
this.performedUpgrade = true;
}
catch (Exception e) {
LOG.error("Can't update Analytics content structure!", (Throwable)e);
}
}
protected void bindQueryBuilder(QueryBuilder queryBuilder) {
this.queryBuilder = queryBuilder;
}
protected void unbindQueryBuilder(QueryBuilder queryBuilder) {
if (this.queryBuilder == queryBuilder) {
this.queryBuilder = null;
}
}
protected void bindResourceResolverFactory(ResourceResolverFactory resourceResolverFactory) {
this.resourceResolverFactory = resourceResolverFactory;
}
protected void unbindResourceResolverFactory(ResourceResolverFactory resourceResolverFactory) {
if (this.resourceResolverFactory == resourceResolverFactory) {
this.resourceResolverFactory = null;
}
}
protected void bindConfigManagerFactory(ConfigurationManagerFactory configurationManagerFactory) {
this.configManagerFactory = configurationManagerFactory;
}
protected void unbindConfigManagerFactory(ConfigurationManagerFactory configurationManagerFactory) {
if (this.configManagerFactory == configurationManagerFactory) {
this.configManagerFactory = null;
}
}
}