AudienceManagerFolderImpl.java
6.35 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.commons.jcr.JcrUtil
* com.day.cq.wcm.api.Page
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.PropertyIterator
* javax.jcr.RepositoryException
* org.apache.sling.api.resource.Resource
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.JSONObject
* org.apache.sling.jcr.resource.JcrPropertyMap
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.aam.client;
import com.adobe.cq.aam.client.AudienceManagerFolderList;
import com.adobe.cq.aam.client.RootFolder;
import com.adobe.cq.aam.client.spi.AudienceManagerConfiguration;
import com.adobe.cq.aam.client.spi.AudienceManagerFolder;
import com.adobe.cq.aam.client.spi.InvalidItemPage;
import com.day.cq.commons.jcr.JcrUtil;
import com.day.cq.wcm.api.Page;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.RepositoryException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.apache.sling.jcr.resource.JcrPropertyMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class AudienceManagerFolderImpl
implements AudienceManagerFolder {
private static final String[] NODE_PROPS = new String[]{"path", "pid", "folderId", "name"};
private static final Logger LOGGER = LoggerFactory.getLogger(AudienceManagerFolderImpl.class);
private String title;
private String path;
private String name;
private String template;
private long folderId;
private Map<String, Object> toNode = new HashMap<String, Object>();
private AudienceManagerFolderList folders;
private long parentFolderId;
private int depth;
private Page segmentPage;
public AudienceManagerFolderImpl(AudienceManagerFolderList referenceFolders, JSONObject json, String folderTemplate) throws JSONException {
this.folders = referenceFolders;
this.path = referenceFolders.getFolderPath(json.getString("path"));
this.depth = this.path.split("/").length;
this.folderId = json.getLong("folderId");
this.parentFolderId = json.getLong("parentFolderId");
this.title = json.getString("name");
this.name = JcrUtil.createValidName((String)this.title);
for (String k : NODE_PROPS) {
this.toNode.put("aam_" + k, json.get(k));
}
this.template = folderTemplate;
}
public AudienceManagerFolderImpl(Page page) throws InvalidItemPage {
Resource resource = page.getContentResource();
if ("cq/personalization/audiencemanager/components/segmentation/aamsegmentpage".equals(resource.getResourceType())) {
throw new InvalidItemPage("Page is not a Audience Manager Segment, incorrect resource type. " + page.getPath());
}
try {
Node node = (Node)resource.adaptTo(Node.class);
PropertyIterator pi = node.getProperties();
while (pi.hasNext()) {
Property p = pi.nextProperty();
String pname = p.getName();
if (!pname.startsWith("aam_")) continue;
this.toNode.put(pname, this.getValue(p));
}
}
catch (RepositoryException e) {
LOGGER.debug(e.getMessage(), (Throwable)e);
}
if (!this.toNode.containsKey("aam_folderId") || this.toNode.containsKey("aam_sid")) {
throw new InvalidItemPage("Page is not a Audience Manager Segment Folder, no folderId " + page.getPath());
}
this.path = page.getPath();
this.folderId = this.toLong(this.toNode.get("folderId"));
this.title = (String)this.toNode.get("aam_name");
this.name = JcrUtil.createValidName((String)this.title);
this.segmentPage = page.getParent();
}
private long toLong(Object object) {
if (object instanceof Long) {
return (Long)object;
}
if (object instanceof Double) {
double d = (Double)object;
return (long)d;
}
LOGGER.debug("Failed, object is {} {} ", object, object.getClass());
return Long.parseLong(String.valueOf(object));
}
private Object getValue(Property p) throws RepositoryException {
if (p.getType() == 3 || p.getType() == 4) {
return p.getLong();
}
return p.getString();
}
@Override
public String getPath() {
return this.path;
}
@Override
public String getName() {
return this.name;
}
@Override
public String getTitle() {
return this.title;
}
@Override
public String getTemplate() {
return this.template;
}
@Override
public long getFolderId() {
return this.folderId;
}
@Override
public Set<Map.Entry<String, Object>> getEntrySet() {
return this.toNode.entrySet();
}
@Override
public String getAbsolutePath(AudienceManagerConfiguration config) {
return config.getTarget() + "/" + this.path;
}
@Override
public AudienceManagerFolder getParent() {
if (this.segmentPage != null) {
try {
return new AudienceManagerFolderImpl(this.segmentPage.getParent());
}
catch (InvalidItemPage e) {
return new RootFolder(this.segmentPage.getParent());
}
}
if (this.parentFolderId == this.folderId) {
return new RootFolder(this.folders);
}
return this.folders.getFolder(this.parentFolderId);
}
@Override
public long getParentFolderId() {
if (this.segmentPage != null) {
return (Long)new JcrPropertyMap((Node)this.segmentPage.getParent().getContentResource().adaptTo(Node.class)).get((Object)"aam_folderId");
}
return this.parentFolderId;
}
@Override
public int getDepth() {
return this.depth;
}
public String toString() {
return this.path;
}
public int hashCode() {
return this.path.hashCode();
}
public boolean equals(Object obj) {
if (obj instanceof AudienceManagerFolderImpl) {
return this.path.equals(((AudienceManagerFolderImpl)obj).path);
}
return super.equals(obj);
}
}