-
Notifications
You must be signed in to change notification settings - Fork 732
/
ListImpl.java
114 lines (103 loc) · 5.48 KB
/
ListImpl.java
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
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ Copyright 2023 Adobe
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
package com.adobe.cq.wcm.core.components.internal.models.v4;
import com.adobe.cq.export.json.ComponentExporter;
import com.adobe.cq.export.json.ExporterConstants;
import com.adobe.cq.wcm.core.components.commons.link.Link;
import com.adobe.cq.wcm.core.components.commons.link.LinkManager;
import com.adobe.cq.wcm.core.components.internal.link.LinkManagerImpl;
import com.adobe.cq.wcm.core.components.internal.models.v1.AbstractListItemImpl;
import com.adobe.cq.wcm.core.components.models.List;
import com.adobe.cq.wcm.core.components.models.ListItem;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.components.Component;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.Exporter;
import org.apache.sling.models.annotations.Model;
import org.jetbrains.annotations.NotNull;
import java.text.Collator;
import java.util.Collection;
import java.util.Comparator;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
@Model(adaptables = SlingHttpServletRequest.class, adapters = {List.class, ComponentExporter.class}, resourceType = ListImpl.RESOURCE_TYPE)
@Exporter(name = ExporterConstants.SLING_MODEL_EXPORTER_NAME, extensions = ExporterConstants.SLING_MODEL_EXTENSION)
public class ListImpl extends com.adobe.cq.wcm.core.components.internal.models.v3.ListImpl implements List {
protected static final String RESOURCE_TYPE = "core/wcm/components/list/v4/list";
@Override
protected ListItem newPageListItem(@NotNull LinkManager linkManager, @NotNull Page page, String parentId, Component component) {
Resource listResource = getListResource();
return new PageListItemImpl(linkManager.get(page).build(), page, parentId, component, showDescription, linkItems || displayItemAsTeaser, listResource);
}
@Override
@NotNull
@JsonProperty("items")
public Collection<ListItem> getListItems() {
if (this.listItems == null) {
if (Source.STATIC.equals(getListType())) {
Resource staticNode = this.resource.getChild(NN_STATIC);
if (staticNode != null) {
this.listItems = getStaticListItems();
}
}
}
return super.getListItems();
}
private Collection<ListItem> getStaticListItems() {
Stream<AbstractListItemImpl> itemStream = getStaticItemResourceStream().map(linkResource -> {
Link link = linkManager.get(linkResource).build();
if (LinkManagerImpl.isExternalLink(link.getURL()) && (link.getReference() == null)) {
return new ExternalLinkListItemImpl(link, linkResource, getId(), component);
} else {
Object reference = link.getReference();
if (reference instanceof Page) {
return new PageListItemImpl(link, (Page) reference, linkResource, getId(), component, showDescription, linkItems || displayItemAsTeaser, resource);
} else {
return null;
}
}
}).filter(Objects::nonNull).filter(item -> item.getLink() != null && item.getLink().isValid());
// apply sorting
OrderBy orderBy = OrderBy.fromString(properties.get(PN_ORDER_BY, StringUtils.EMPTY));
SortOrder sortOrder = SortOrder.fromString(properties.get(PN_SORT_ORDER, SortOrder.ASC.value));
int direction = sortOrder.equals(SortOrder.ASC) ? 1 : -1;
if (OrderBy.TITLE.equals(orderBy)) {
Collator collator = Collator.getInstance(currentPage.getLanguage());
collator.setStrength(Collator.PRIMARY);
// getTitle may return null, define null to be greater than nonnull values
Comparator<String> titleComparator = Comparator.nullsLast(collator);
itemStream = itemStream.sorted((item1, item2) -> direction * titleComparator.compare(item1.getTitle(), item2.getTitle()));
} else if (OrderBy.MODIFIED.equals(orderBy)) {
// getLastModified may return null, define null to be after nonnull values
itemStream = itemStream.sorted((item1, item2) -> direction * ObjectUtils.compare(item1.getLastModified(), item2.getLastModified(), true));
}
return itemStream.collect(Collectors.toList());
}
private Stream<Resource> getStaticItemResourceStream() {
Resource staticNode = this.resource.getChild(NN_STATIC);
if (staticNode == null) {
return Stream.empty();
} else {
return StreamSupport.stream(staticNode.getChildren().spliterator(), false);
}
}
}