PageView.java
4.05 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.wcm.api.Page
* com.day.cq.wcm.api.WCMMode
* com.day.crx.statistics.Entry
* com.day.crx.statistics.PathBuilder
* javax.jcr.Item
* javax.jcr.Node
* javax.jcr.PathNotFoundException
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.ValueFormatException
*/
package com.day.cq.wcm.core.stats;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.WCMMode;
import com.day.cq.wcm.core.stats.PageViewPathBuilder;
import com.day.crx.statistics.Entry;
import com.day.crx.statistics.PathBuilder;
import java.util.Calendar;
import javax.jcr.Item;
import javax.jcr.Node;
import javax.jcr.PathNotFoundException;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.ValueFormatException;
public class PageView
extends Entry {
public static final String VIEWS = "views";
public static final String ROLLING_WEEK_COUNT = "rollingWeekViews";
public static final String ROLLING_MONTH_COUNT = "rollingMonthViews";
private final Page page;
private final WCMMode mode;
public PageView(String pathPrefix, Page page, WCMMode mode) {
super(pathPrefix);
this.page = page;
this.mode = mode;
}
protected PathBuilder getPathBuilder() {
return new PageViewPathBuilder(this.page.getPath());
}
public void write(Node node) throws RepositoryException {
Node month = node.getParent();
Node year = month.getParent();
this.updateViews(node);
this.updateViews(month);
this.updateViews(year);
this.updateCumulativeViews(node, "rollingWeekViews", 6);
this.updateCumulativeViews(node, "rollingMonthViews", 29);
}
private void updateViews(Node node) throws RepositoryException {
long viewCount = 0;
if (node.hasProperty("views")) {
viewCount = node.getProperty("views").getLong();
}
node.setProperty("views", ++viewCount);
long modeViewCount = 0;
String modeViewProp = "views-" + (Object)this.mode;
if (node.hasProperty(modeViewProp)) {
modeViewCount = node.getProperty(modeViewProp).getLong();
}
node.setProperty(modeViewProp, ++modeViewCount);
}
private void updateCumulativeViews(Node node, String propertyName, int numDays) throws RepositoryException {
long viewCount = node.hasProperty(propertyName) ? node.getProperty(propertyName).getLong() : this.getCumulativeCount(node, numDays, "views");
node.setProperty(propertyName, ++viewCount);
propertyName = propertyName + "-" + (Object)this.mode;
long modeViewCount = node.hasProperty(propertyName) ? node.getProperty(propertyName).getLong() : this.getCumulativeCount(node, numDays, "views-" + (Object)this.mode);
node.setProperty(propertyName, ++modeViewCount);
}
private long getCumulativeCount(Node node, int numDays, String propName) throws RepositoryException, ValueFormatException {
long viewCount = 0;
Session session = node.getSession();
PathBuilder builder = this.getPathBuilder();
Calendar date = Calendar.getInstance();
date.setTimeInMillis(this.getTimestamp());
PageView view = new PageView(this.getPathPrefix(), this.page, this.mode);
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < numDays; ++i) {
buffer.setLength(0);
date.add(5, -1);
view.setTimestamp(date.getTimeInMillis());
builder.formatPath((Entry)view, buffer);
String path = buffer.toString();
try {
Node n;
Item item = session.getItem(path);
if (!item.isNode() || !(n = (Node)item).hasProperty(propName)) continue;
viewCount += n.getProperty(propName).getLong();
continue;
}
catch (PathNotFoundException e) {
// empty catch block
}
}
return viewCount;
}
}