AppCacheDataSourceImpl.java
8.24 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.cq.mobile.ui.AppCacheDataSource
* com.day.cq.contentsync.config.Config
* javax.jcr.Node
* javax.jcr.PathNotFoundException
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.commons.json.JSONArray
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.JSONObject
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.mobile.ui.impl;
import com.adobe.cq.mobile.appcache.impl.AppCacheException;
import com.adobe.cq.mobile.appcache.impl.AppCacheManager;
import com.adobe.cq.mobile.appcache.impl.AppCacheManagerAdapterFactory;
import com.adobe.cq.mobile.appcache.impl.CacheReporter;
import com.adobe.cq.mobile.appcache.impl.CacheUpdate;
import com.adobe.cq.mobile.appcache.impl.ConfigUtil;
import com.adobe.cq.mobile.platform.MobileResource;
import com.adobe.cq.mobile.platform.impl.MobileAppProvider;
import com.adobe.cq.mobile.platform.impl.utils.MobileUtil;
import com.adobe.cq.mobile.ui.AppCacheDataSource;
import com.day.cq.contentsync.config.Config;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.jcr.Node;
import javax.jcr.PathNotFoundException;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class AppCacheDataSourceImpl
implements AppCacheDataSource {
private static final Logger LOGGER = LoggerFactory.getLogger(AppCacheDataSourceImpl.class);
private AppCacheManagerAdapterFactory appCacheManagerAdapterFactory;
private Session session = null;
private Config config = null;
private Resource resource = null;
public AppCacheDataSourceImpl(Resource resource, AppCacheManagerAdapterFactory appCacheManagerAdapterFactory) {
if (resource == null) {
throw new IllegalArgumentException("Cannot instantiate AppCacheDataSource. Resource cannot be null.");
}
this.resource = resource;
this.session = (Session)resource.getResourceResolver().adaptTo(Session.class);
this.config = this.getConfig(resource);
this.appCacheManagerAdapterFactory = appCacheManagerAdapterFactory;
}
public JSONObject getCacheUpdates() throws Exception {
if (this.config == null) {
throw new Exception("Unable to find a content sync config for " + this.resource.getPath());
}
AppCacheManager appCacheManager = this.appCacheManagerAdapterFactory.getAdapter((Object)this.session, AppCacheManager.class);
return this.getJSON(appCacheManager, this.config, this.session);
}
public JSONObject getContentListing(long modifiedSince) throws Exception {
if (this.config == null) {
throw new Exception("Unable to find a content sync config for " + this.resource.getPath());
}
return this.getUpdateFileList(this.config, modifiedSince);
}
private JSONObject getJSON(AppCacheManager appCacheManager, Config config, Session session) throws AppCacheException, JSONException {
JSONObject cacheJSON = new JSONObject();
if (appCacheManager == null || config == null || session == null) {
return cacheJSON;
}
cacheJSON.put("name", (Object)config.getName());
String title = null;
try {
Node configNode = session.getNode(config.getPath());
title = configNode.hasProperty("jcr:title") ? configNode.getProperty("jcr:title").getString() : null;
}
catch (RepositoryException e) {
// empty catch block
}
cacheJSON.put("title", (Object)title);
cacheJSON.put("path", (Object)config.getPath());
cacheJSON.put("authorizable", (Object)config.getAuthorizable());
cacheJSON.put("updateUser", (Object)config.getUpdateUser());
Long lastFilled = appCacheManager.getLastFill(config);
cacheJSON.put("lastFilled", (Object)lastFilled);
boolean cacheHasPendingChangesNotYetUpdatedOfficially = false;
CacheUpdate lastUpdated = appCacheManager.getLastUpdate(config);
if (lastUpdated != null) {
cacheJSON.put("lastUpdated", lastUpdated.getCreated().getTimeInMillis());
cacheHasPendingChangesNotYetUpdatedOfficially = lastFilled == null ? false : lastFilled > lastUpdated.getCreated().getTimeInMillis();
} else {
cacheHasPendingChangesNotYetUpdatedOfficially = lastFilled == null ? false : lastFilled > new Date(0).getTime();
}
cacheJSON.put("modifiedPastUpdate", cacheHasPendingChangesNotYetUpdatedOfficially);
CacheUpdate lastPublishedUpdate = appCacheManager.getLastPublishedUpdate(config);
if (lastPublishedUpdate != null && lastPublishedUpdate.getLastPublished() != null) {
cacheJSON.put("lastPublished", lastPublishedUpdate.getLastPublished().getTimeInMillis());
String lastPublishedBy = lastPublishedUpdate.getLastPublishedBy();
if (lastPublishedBy == null) {
lastPublishedBy = lastPublishedUpdate.getCreatedBy();
}
cacheJSON.put("lastPublishedBy", (Object)lastPublishedBy);
}
JSONArray updatesJSON = new JSONArray();
cacheJSON.put("updates", (Object)updatesJSON);
List<CacheUpdate> cacheUpdateList = appCacheManager.getUpdates(config);
for (CacheUpdate cacheUpdate : cacheUpdateList) {
JSONObject updateJSON = new JSONObject();
updateJSON.put("ts", cacheUpdate.getCreated().getTimeInMillis());
updateJSON.put("date", (Object)MobileUtil.getDateAsString(cacheUpdate.getCreated().getTime()));
updateJSON.put("updateUserId", (Object)cacheUpdate.getCreatedBy());
updateJSON.put("title", (Object)cacheUpdate.getTitle());
Calendar lastPublishedCal = cacheUpdate.getLastPublished();
if (lastPublishedCal != null) {
updateJSON.put("publishedTS", lastPublishedCal.getTimeInMillis());
updateJSON.put("publishedDate", (Object)MobileUtil.getDateAsString(lastPublishedCal.getTime()));
updateJSON.put("publishedBy", (Object)cacheUpdate.getLastPublishedBy());
}
updateJSON.put("description", (Object)cacheUpdate.getDescription());
updatesJSON.put((Object)updateJSON);
}
JSONArray zipsJSON = appCacheManager.getZips(config);
cacheJSON.put("zips", (Object)zipsJSON);
return cacheJSON;
}
private JSONObject getUpdateFileList(Config config, Long ifModifiedSince) throws Exception {
JSONObject jsonResponse = new JSONObject();
try {
String cachePath = ConfigUtil.configToCachePath(config, this.session);
Node cacheFolder = this.session.getNode(cachePath);
CacheReporter listingUtil = new CacheReporter(cacheFolder);
jsonResponse = listingUtil.getFileListingJSON(ifModifiedSince);
}
catch (PathNotFoundException pathNotFoundException) {
jsonResponse.put("count", 0);
}
catch (RepositoryException repEx) {
throw new Exception((Throwable)repEx);
}
catch (JSONException jsonEx) {
throw new Exception((Throwable)jsonEx);
}
return jsonResponse;
}
private Config getConfig(Resource resource) {
Config cacheConfig = (Config)resource.adaptTo(Config.class);
if (cacheConfig != null) {
return cacheConfig;
}
MobileResource mobileResource = (MobileResource)resource.adaptTo(MobileResource.class);
MobileAppProvider appProvider = (MobileAppProvider)mobileResource.adaptTo(MobileAppProvider.class);
Map<String, Config> configMap = appProvider.findResourceConfigs();
if (configMap != null && configMap.containsKey("STAGE")) {
return configMap.get("STAGE");
}
return null;
}
}