JcrImageSet.java
10.3 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.asset.api.Asset
* com.adobe.granite.asset.api.Rendition
* com.day.cq.dam.api.Asset
* com.scene7.is.provider.ImageSet
* com.scene7.is.ps.provider.fvctx.AssetType
* com.scene7.is.ps.provider.parsers.ImageSetConverter
* com.scene7.is.util.callbacks.Option
* com.scene7.is.util.collections.CollectionUtil
* javax.jcr.RepositoryException
* org.apache.sling.api.adapter.Adaptable
* org.apache.sling.api.resource.Resource
* org.jetbrains.annotations.NotNull
*/
package com.adobe.cq.dam.s7imaging.impl.catalog;
import com.adobe.cq.dam.s7imaging.impl.catalog.JcrUtil;
import com.adobe.cq.dam.s7imaging.impl.jcr.adapters.GraniteAssetWrapper;
import com.adobe.cq.dam.s7imaging.impl.jcr.adapters.Relation;
import com.adobe.cq.dam.s7imaging.impl.jcr.props.JcrProps;
import com.adobe.cq.dam.s7imaging.impl.jcr.props.PropExtractor;
import com.adobe.cq.dam.s7imaging.impl.jcr.props.PropKey;
import com.adobe.cq.dam.s7imaging.impl.jcr.props.StandardExtractors;
import com.adobe.granite.asset.api.Rendition;
import com.day.cq.dam.api.Asset;
import com.scene7.is.provider.ImageSet;
import com.scene7.is.ps.provider.fvctx.AssetType;
import com.scene7.is.ps.provider.parsers.ImageSetConverter;
import com.scene7.is.util.callbacks.Option;
import com.scene7.is.util.collections.CollectionUtil;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.jcr.RepositoryException;
import org.apache.sling.api.adapter.Adaptable;
import org.apache.sling.api.resource.Resource;
import org.jetbrains.annotations.NotNull;
public class JcrImageSet {
public final Type setType;
public final AssetType assetType;
public final String value;
private static final String S7_SET = "s7Set";
private static final String DAM_S7_SWATCH = "dam:s7Swatch";
private static final String MANUAL_THUMBNAIL = "manualThumbnail";
private static final String ROW_RELATIONS = "rowRelations";
private static final String METADATA = "metadata";
private static final PropKey<String> S7Type = PropKey.optional("jcr:content/dam:s7damType", "", StandardExtractors.toString);
private static final PropKey<String[]> RowRelations = PropKey.optional("jcr:content/rowRelations", new String[0], StandardExtractors.toStringArray);
private static final PropKey<Option<String>> Thumbnail = PropKey.optional("manualThumbnail", StandardExtractors.toString);
private static final PropKey<Option<String>> SWATCH = PropKey.optional("dam:s7Swatch", StandardExtractors.toString);
public static Option<JcrImageSet> jcrImageSet(@NotNull String rootId, @NotNull Resource resource) throws RepositoryException {
Iterator i$ = JcrImageSet.imageSetType(resource).iterator();
if (i$.hasNext()) {
Type type = (Type)((Object)i$.next());
switch (type) {
case VIDEO_SET: {
return Option.some((Object)JcrImageSet.buildVideoSet(resource));
}
case VIDEO_GROUP: {
return Option.some((Object)JcrImageSet.buildVideoGroup(resource));
}
case IMAGE_SET:
case CAROUSEL_SET: {
return Option.some((Object)JcrImageSet.buildImageSet(rootId, resource));
}
case MIXED_MEDIA_SET: {
return Option.some((Object)JcrImageSet.buildMixedMediaSet(rootId, resource));
}
case SPIN_SET: {
return Option.some((Object)JcrImageSet.buildSpinSet(rootId, resource));
}
}
throw new AssertionError((Object)("Unsupported image set type: " + (Object)((Object)type)));
}
return Option.none();
}
private static JcrImageSet buildSpinSet(String rootId, Resource resource) throws RepositoryException {
GraniteAssetWrapper graniteAsset = GraniteAssetWrapper.graniteAssetWrapper(JcrUtil.as(com.adobe.granite.asset.api.Asset.class, (Adaptable)resource));
ArrayList<String> rowAcc = new ArrayList<String>();
for (String row : JcrProps.jcrProps(resource).get(RowRelations)) {
ArrayList<String> acc = new ArrayList<String>();
for (Relation relation : graniteAsset.relatedRefs(row)) {
acc.add(JcrImageSet.imageSetId(rootId, relation.path));
}
rowAcc.add(CollectionUtil.mkString(acc, (String)"{", (String)",", (String)"}"));
}
String value = CollectionUtil.mkString(rowAcc, (String)",");
return new JcrImageSet(Type.SPIN_SET, value);
}
private static JcrImageSet buildVideoSet(Resource resource) {
ArrayList<String> buffer = new ArrayList<String>();
for (Rendition r : JcrImageSet.videoRenditions(JcrUtil.as(com.adobe.granite.asset.api.Asset.class, (Adaptable)resource))) {
buffer.add(JcrImageSet.urlEncode(r.getPath()));
}
return new JcrImageSet(Type.VIDEO_SET, CollectionUtil.mkString(buffer, (String)","));
}
private static JcrImageSet buildVideoGroup(Resource resource) {
ArrayList<String> buffer = new ArrayList<String>();
for (Rendition r : JcrImageSet.videoRenditions(JcrUtil.as(com.adobe.granite.asset.api.Asset.class, (Adaptable)resource))) {
buffer.add(JcrImageSet.urlEncode(r.getPath()));
}
return new JcrImageSet(Type.VIDEO_GROUP, CollectionUtil.mkString(buffer, (String)","));
}
private static JcrImageSet buildImageSet(String rootId, Resource resource) throws RepositoryException {
ArrayList<String> acc = new ArrayList<String>();
GraniteAssetWrapper graniteAsset = GraniteAssetWrapper.graniteAssetWrapper(JcrUtil.as(com.adobe.granite.asset.api.Asset.class, (Adaptable)resource));
for (Relation ref : graniteAsset.relatedRefs("s7Set")) {
String item = JcrImageSet.imageSetId(rootId, ref.path);
String swatch = ref.props.get(SWATCH, item);
acc.add(item + ";" + swatch);
}
return new JcrImageSet(Type.IMAGE_SET, CollectionUtil.mkString(acc, (String)","));
}
private static String imageSetId(String rootId, String path) {
assert (!rootId.startsWith("/"));
assert (path.startsWith("/"));
Pattern sameRoot = Pattern.compile("^/" + rootId + "/(.*)$$");
Matcher matcher = sameRoot.matcher(path);
if (matcher.matches()) {
return matcher.group(1);
}
return path;
}
private static JcrImageSet buildMixedMediaSet(String rootId, Resource resource) throws RepositoryException {
ArrayList<String> acc = new ArrayList<String>();
for (GraniteAssetWrapper asset : GraniteAssetWrapper.graniteAssetWrapper((Adaptable)resource)) {
for (com.adobe.granite.asset.api.Asset member : asset.related("s7Set")) {
String path = JcrImageSet.imageSetId(rootId, member.getPath());
String thumb = (String)JcrImageSet.thumbPath(member).getOrElse((Object)path);
acc.add(path + ';' + thumb);
}
}
String value = CollectionUtil.mkString(acc, (String)",");
return new JcrImageSet(Type.MIXED_MEDIA_SET, value);
}
private static Option<Type> imageSetType(Resource resource) throws RepositoryException {
String s7damType;
Iterator i$;
Asset damAsset = (Asset)resource.adaptTo(Asset.class);
if (damAsset != null && (i$ = Type.forS7DamType(s7damType = JcrProps.jcrProps(resource).get(S7Type)).iterator()).hasNext()) {
Type t = (Type)((Object)i$.next());
return Option.some((Object)((Object)t));
}
return Option.none();
}
private static List<Rendition> videoRenditions(com.adobe.granite.asset.api.Asset asset) {
ArrayList<Rendition> result = new ArrayList<Rendition>();
Iterator it = asset.listRenditions();
while (it.hasNext()) {
Rendition r = (Rendition)it.next();
String name = r.getName();
if (!name.startsWith("cqdam.video.dm.") || name.endsWith(".m3u8") || name.endsWith(".f4m")) continue;
result.add(r);
}
return result;
}
private static Option<String> thumbPath(com.adobe.granite.asset.api.Asset asset) throws RepositoryException {
for (Resource n : JcrUtil.getChild((Resource)asset, "jcr:content/metadata")) {
Iterator i$ = JcrProps.jcrProps(n).get(Thumbnail).iterator();
if (!i$.hasNext()) continue;
String thumbnail = (String)i$.next();
return Option.some((Object)thumbnail);
}
return Option.none();
}
private static ImageSet toImageSet(List<String> items) {
String imageSetString = CollectionUtil.mkString(items, (String)",");
return (ImageSet)ImageSetConverter.imageSetConverter().convert((Object)imageSetString);
}
private static String urlEncode(String str) {
try {
return URLEncoder.encode(str, "UTF-8");
}
catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
}
private JcrImageSet(Type setType, String value) {
this.setType = setType;
this.assetType = setType.assetType;
this.value = value;
}
static enum Type {
VIDEO_SET("VideoAVS", AssetType.VIDEOSET),
VIDEO_GROUP("Video", AssetType.VIDEOGROUP),
IMAGE_SET("ImageSet", AssetType.IMAGESET),
MIXED_MEDIA_SET("MixedMediaSet", AssetType.MEDIASET),
SPIN_SET("SpinSet", AssetType.SPINSET),
CAROUSEL_SET("CarouselSet", AssetType.IMAGESET);
private static final Map<String, Type> s7damTypeTable;
private final String s7damType;
private final AssetType assetType;
private static Option<Type> forS7DamType(String s7damType) {
return Option.some((Object)((Object)s7damTypeTable.get(s7damType)));
}
public String toString() {
return this.s7damType;
}
private Type(String name, AssetType assetType) {
this.s7damType = name;
this.assetType = assetType;
}
static {
s7damTypeTable = CollectionUtil.lookupTable(Type.class);
}
}
}