ChannelOfflineInfo.java
3.62 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
package libs.screens.dcc.components.dashboard.channelinfo;
import com.adobe.cq.screens.binding.ScreensConstants;
import com.adobe.cq.sightly.WCMUsePojo;
import com.day.cq.commons.Filter;
import com.day.cq.contentsync.ContentSyncManager;
import com.day.cq.contentsync.config.Config;
import com.day.cq.wcm.api.Page;
import org.apache.jackrabbit.util.ISO9075;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import javax.jcr.query.Query;
import javax.jcr.Session;
import java.util.Calendar;
import java.util.Iterator;
public class ChannelOfflineInfo extends WCMUsePojo {
private Resource channel;
private boolean offline;
private long lastSyncTimestamp;
private long channelLastModified;
private boolean updates;
private String configPath;
private String channelPath;
private long getPageLastModified(Page page) {
if ( page.getLastModified() != null ) {
return page.getLastModified().getTimeInMillis();
} else {
ValueMap properties = channel.adaptTo(ValueMap.class);
Calendar creationDate = properties.get("jcr:created", Calendar.class);
return creationDate.getTimeInMillis();
}
}
@Override
public void activate() throws Exception {
channel = getRequest().getRequestPathInfo().getSuffixResource();
ContentSyncManager contentSyncManager = getSlingScriptHelper().getService(ContentSyncManager.class);
if ( channel != null && contentSyncManager != null) {
channelPath = channel.getPath();
String query = "/jcr:root" + ISO9075.encodePath(channelPath) + "//element(*, cq:ContentSyncConfig)";
Iterator<Resource> contentSyncConfigIter = channel.getResourceResolver().findResources(query, Query.XPATH);
if ( contentSyncConfigIter.hasNext() ) {
Resource contentSyncResource = contentSyncConfigIter.next();
Config config = contentSyncResource.adaptTo(Config.class);
Page page = channel.adaptTo(Page.class);
if ( config != null && page != null) {
offline = !page.getContentResource().adaptTo(ValueMap.class).get(ScreensConstants.PN_FORCE_REMOTE_CONTENT, false);
configPath = config.getPath();
Session session = getResourceResolver().adaptTo(Session.class);
lastSyncTimestamp = contentSyncManager.getLatestTimestamp(config, session);
channelLastModified = getPageLastModified(page);
updates = channelLastModified > lastSyncTimestamp;
// Check sub-pages (zones)
if (!updates) {
Iterator<Page> modifiedZones = page.listChildren(new Filter<Page>() {
@Override
public boolean includes(Page zone) {
return getPageLastModified(zone) > lastSyncTimestamp;
}
});
if (modifiedZones.hasNext()) {
updates = true;
}
}
}
}
}
}
public boolean isOffline() {
return offline;
}
public boolean hasUpdates() {
return updates;
}
public long getLastSyncTimestamp() {
return lastSyncTimestamp;
}
public long getChannelLastModified() {
return channelLastModified;
}
public String getConfigPath() {
return configPath;
}
public String getChannelPath() {
return channelPath;
}
}