CollectionHelperImpl.java
5.94 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.cq.dam.cfm.CollectionHelper
* com.day.cq.dam.commons.util.DateParser
* com.day.cq.search.PredicateGroup
* com.day.cq.search.Query
* com.day.cq.search.QueryBuilder
* com.day.cq.search.result.SearchResult
* javax.jcr.Session
* org.apache.commons.lang.StringUtils
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.jackrabbit.api.security.user.Authorizable
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.resource.collection.ResourceCollection
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.dam.cfm.impl;
import com.adobe.cq.dam.cfm.CollectionHelper;
import com.day.cq.dam.commons.util.DateParser;
import com.day.cq.search.PredicateGroup;
import com.day.cq.search.Query;
import com.day.cq.search.QueryBuilder;
import com.day.cq.search.result.SearchResult;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import javax.jcr.Session;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.resource.collection.ResourceCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Service
@Component(metatype=0)
public class CollectionHelperImpl
implements CollectionHelper {
private final Logger log;
@Reference
private QueryBuilder queryBuilder;
public CollectionHelperImpl() {
this.log = LoggerFactory.getLogger(this.getClass());
}
private boolean isValidCollection(Resource res) {
return res.adaptTo(ResourceCollection.class) != null;
}
public Iterator<Resource> getCollections(ResourceResolver resourceResolver, boolean includeSmartCollections) {
Session session = (Session)resourceResolver.adaptTo(Session.class);
HashMap<String, String> map = new HashMap<String, String>();
map.put("path", "/content/dam");
map.put("group.0_property", "sling:resourceType");
map.put("group.0_property.0_value", "dam/collection");
if (includeSmartCollections) {
map.put("group.0_property.1_value", "dam/smartcollection");
}
map.put("group.1_property", "sling:resourceSuperType");
map.put("group.1_property.value", "dam/collection");
map.put("group.p.or", "true");
map.put("type", "nt:unstructured");
map.put("p.limit", "0");
map.put("p.offset", "0");
Query query = this.queryBuilder.createQuery(PredicateGroup.create(map), session);
ArrayList<Resource> collections = new ArrayList<Resource>();
SearchResult result = query.getResult();
Iterator it = result.getResources();
while (it.hasNext()) {
Resource p = (Resource)it.next();
if (!this.isValidCollection(p)) continue;
collections.add(p);
}
Collections.sort(collections, new Comparator<Resource>(){
@Override
public int compare(Resource r1, Resource r2) {
long date2;
ValueMap v1 = r1.getValueMap();
ValueMap v2 = r2.getValueMap();
String d1 = (String)v1.get("jcr:lastModified", v1.get("jcr:created", (Object)""));
String d2 = (String)v2.get("jcr:lastModified", v2.get("jcr:created", (Object)""));
if (StringUtils.isEmpty((String)d1)) {
return -1;
}
if (StringUtils.isEmpty((String)d2)) {
return 1;
}
long date1 = DateParser.parseDate((String)d1).getTime();
return date1 < (date2 = DateParser.parseDate((String)d2).getTime()) ? 1 : -1;
}
});
Authorizable user = (Authorizable)resourceResolver.adaptTo(Authorizable.class);
if (null != user) {
try {
String lightboxCollectionName = "lightbox";
ArrayList<Resource> collectionsWithoutLightbox = new ArrayList<Resource>();
for (Resource currentCollection : collections) {
String currentCollectionName = currentCollection.getName();
if (lightboxCollectionName.equals(currentCollectionName)) {
String currentCollectionPath = currentCollection.getPath();
String tenantUserHome = "/home/users";
String userHome = user.getPath();
userHome = userHome.indexOf(tenantUserHome) == 0 ? StringUtils.substringAfter((String)userHome, (String)tenantUserHome) : userHome;
String userCollsHome = "/content/dam/collections" + userHome;
String lightboxCollectionPath = userCollsHome + "/" + lightboxCollectionName;
if (!currentCollectionPath.endsWith(lightboxCollectionPath)) continue;
}
collectionsWithoutLightbox.add(currentCollection);
}
collections = collectionsWithoutLightbox;
}
catch (Exception e) {
// empty catch block
}
}
return collections.iterator();
}
protected void bindQueryBuilder(QueryBuilder queryBuilder) {
this.queryBuilder = queryBuilder;
}
protected void unbindQueryBuilder(QueryBuilder queryBuilder) {
if (this.queryBuilder == queryBuilder) {
this.queryBuilder = null;
}
}
}