MVTEntry.java
3.54 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.wcm.api.Page
* 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.mvt;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.core.mvt.MVTPathBuilder;
import com.day.cq.wcm.core.mvt.PageView;
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;
@Deprecated
abstract class MVTEntry
extends Entry {
protected final String count;
protected final String rollingWeekCount;
protected final String rollingMonthCount;
private final Page page;
public MVTEntry(String pathPrefix, String countPropName, String rollingWeekCountPropName, String rollingMonthCountPropName, Page page) {
super(pathPrefix);
this.count = countPropName;
this.rollingWeekCount = rollingWeekCountPropName;
this.rollingMonthCount = rollingMonthCountPropName;
this.page = page;
}
protected PathBuilder getPathBuilder() {
return new MVTPathBuilder(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, this.rollingWeekCount, 6);
this.updateCumulativeViews(node, this.rollingMonthCount, 29);
}
private void updateViews(Node node) throws RepositoryException {
long num = 0;
if (node.hasProperty(this.count)) {
num = node.getProperty(this.count).getLong();
}
node.setProperty(this.count, ++num);
}
private void updateCumulativeViews(Node node, String propertyName, int numDays) throws RepositoryException {
long num = node.hasProperty(propertyName) ? node.getProperty(propertyName).getLong() : this.getCumulativeCount(node, numDays, this.count);
node.setProperty(propertyName, ++num);
}
private long getCumulativeCount(Node node, int numDays, String propName) throws RepositoryException, ValueFormatException {
long num = 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, "dummy");
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;
num += n.getProperty(propName).getLong();
continue;
}
catch (PathNotFoundException e) {
// empty catch block
}
}
return num;
}
}