DefaultPageNameProvider.java
3.5 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.wcm.api.Page
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
*/
package com.day.cq.analytics.sitecatalyst.impl;
import com.day.cq.analytics.sitecatalyst.AnalyticsPageNameContext;
import com.day.cq.analytics.sitecatalyst.AnalyticsPageNameProvider;
import com.day.cq.analytics.sitecatalyst.Framework;
import com.day.cq.wcm.api.Page;
import java.util.Iterator;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
@Service
@Component(metatype=0)
@Properties(value={@Property(name="service.description", value={"Default Page Name Resolver implementation"}), @Property(name="service.ranking", intValue={100}, propertyPrivate=0)})
public class DefaultPageNameProvider
implements AnalyticsPageNameProvider {
@Override
public String getPageName(AnalyticsPageNameContext context) {
String pageName = null;
Framework framework = context.getFramework();
Resource resource = context.getResource();
if (resource != null && framework != null && framework.mapsSCVariable("pageName")) {
String cqVar = framework.getMapping("pageName");
Page page = (Page)resource.adaptTo(Page.class);
if (cqVar.equals("pagedata.path")) {
pageName = page.getPath();
} else if (cqVar.equals("pagedata.title")) {
pageName = page.getTitle();
} else if (cqVar.equals("pagedata.navTitle")) {
pageName = page.getNavigationTitle();
}
}
return pageName;
}
@Override
public Resource getResource(AnalyticsPageNameContext context) {
Resource res = null;
Framework framework = context.getFramework();
ResourceResolver resolver = context.getResourceResolver();
String pageName = context.getPageName();
String basePath = context.getBasePath();
if (pageName != null && basePath != null && resolver != null && framework != null && framework.mapsSCVariable("pageName")) {
Iterator hits;
String cqVar = framework.getMapping("pageName");
if (cqVar.equals("pagedata.path")) {
res = resolver.getResource(pageName);
} else if (cqVar.equals("pagedata.title")) {
Iterator hits2 = resolver.findResources(this.createQuery(pageName, basePath, "jcr:title"), "JCR-SQL2");
if (hits2.hasNext()) {
res = (Resource)hits2.next();
res = res.getParent();
}
} else if (cqVar.equals("pagedata.navTitle") && (hits = resolver.findResources(this.createQuery(pageName, basePath, "navTitle"), "JCR-SQL2")).hasNext()) {
res = (Resource)hits.next();
res = res.getParent();
}
}
return res;
}
private String createQuery(String pageName, String basePath, String propName) {
return "SELECT * FROM [cq:PageContent] WHERE ISDESCENDANTNODE([" + basePath + "]) and [" + propName + "] = \"" + pageName + "\"";
}
}