AudienceManagerFolderList.java
3.95 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.commons.jcr.JcrUtil
* 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.aam.client;
import com.adobe.cq.aam.client.AudienceManagerFolderImpl;
import com.adobe.cq.aam.client.RootFolder;
import com.adobe.cq.aam.client.spi.AudienceManagerFolder;
import com.adobe.cq.aam.client.spi.AudienceManagerFolders;
import com.day.cq.commons.jcr.JcrUtil;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
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 AudienceManagerFolderList
extends ArrayList<AudienceManagerFolder>
implements AudienceManagerFolders {
private static final long serialVersionUID = 3321320093760648696L;
private static final Logger LOGGER = LoggerFactory.getLogger(AudienceManagerFolderList.class);
private String template;
private Map<Long, AudienceManagerFolder> folders = new HashMap<Long, AudienceManagerFolder>();
private AudienceManagerFolder defaultRoot;
public AudienceManagerFolderList(JSONArray jsonArray, String folderTemplate) throws JSONException {
this.template = folderTemplate;
this.add(jsonArray);
Collections.sort(this, new Comparator<AudienceManagerFolder>(){
@Override
public int compare(AudienceManagerFolder before, AudienceManagerFolder after) {
return before.getDepth() - after.getDepth();
}
});
LOGGER.debug("Folder list {} ", (Object)this);
ArrayList<AudienceManagerFolder> roots = new ArrayList<AudienceManagerFolder>();
for (Map.Entry<Long, AudienceManagerFolder> e : this.folders.entrySet()) {
if (this.folders.containsKey(e.getValue().getParentFolderId())) continue;
roots.add(e.getValue());
}
if (roots.size() > 0) {
this.defaultRoot = (AudienceManagerFolder)roots.get(0);
} else if (this.size() > 0) {
this.defaultRoot = (AudienceManagerFolder)this.get(0);
for (AudienceManagerFolder f : this) {
if (!(f.getParent() instanceof RootFolder)) continue;
this.defaultRoot = f;
break;
}
}
}
private void add(JSONArray jsonArray) throws JSONException {
for (int i = 0; i < jsonArray.length(); ++i) {
this.add(jsonArray.getJSONObject(i));
}
}
private void add(JSONObject json) throws JSONException {
AudienceManagerFolderImpl am = new AudienceManagerFolderImpl(this, json, this.template);
this.add(am);
this.folders.put(am.getFolderId(), am);
if (json.has("subFolders")) {
this.add(json.getJSONArray("subFolders"));
}
}
@Override
public AudienceManagerFolder getFolder(long folderId) {
AudienceManagerFolder folder = this.folders.get(folderId);
if (folder == null) {
folder = this.defaultRoot;
}
return folder;
}
public String getFolderPath(String folderSubPath) {
return this.createValidPath(folderSubPath);
}
private String createValidPath(String invaldPath) {
if (invaldPath == null) {
return null;
}
String[] parts = invaldPath.split("/");
StringBuilder sb = new StringBuilder();
for (String p : parts) {
if (p == null || p.trim().length() <= 0) continue;
if (sb.length() > 0) {
sb.append("/");
}
sb.append(JcrUtil.createValidName((String)p));
}
return sb.toString();
}
}