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 package info.magnolia.module.templatingkit.templates.components;
35
36
37 import info.magnolia.context.MgnlContext;
38 import info.magnolia.jcr.util.NodeUtil;
39 import info.magnolia.jcr.util.PropertyUtil;
40 import info.magnolia.module.templatingkit.functions.STKTemplatingFunctions;
41 import info.magnolia.module.templatingkit.templates.AbstractSTKTemplateModel;
42 import info.magnolia.module.templatingkit.templates.category.TemplateCategory;
43 import info.magnolia.module.templatingkit.util.STKPager;
44 import info.magnolia.rendering.model.RenderingModel;
45 import info.magnolia.rendering.template.TemplateDefinition;
46 import info.magnolia.repository.RepositoryConstants;
47 import info.magnolia.templating.functions.TemplatingFunctions;
48
49 import java.util.Collection;
50 import java.util.List;
51
52 import javax.inject.Inject;
53 import javax.jcr.Node;
54 import javax.jcr.RepositoryException;
55
56 import org.apache.commons.lang3.StringUtils;
57
58
59
60
61
62
63
64 public abstract class AbstractItemListModel<RD extends TemplateDefinition> extends AbstractSTKTemplateModel<TemplateDefinition> {
65
66 @Inject
67 public AbstractItemListModel(Node content, RD definition, RenderingModel<?> parent, STKTemplatingFunctions stkFunctions, TemplatingFunctions templatingFunctions) {
68 super(content, definition, parent, stkFunctions, templatingFunctions);
69 }
70
71
72 protected String getRepository() {
73 return RepositoryConstants.WEBSITE;
74 }
75
76 protected String getCategoryName() {
77 return StringUtils.defaultString((String) definition.getParameters().get("searchForCategory"), TemplateCategory.CONTENT);
78 }
79
80 protected String getSubcategoryName() {
81 return (String) definition.getParameters().get("searchForSubcategory");
82 }
83
84 abstract protected int getMaxResults();
85
86
87 protected Node getSearchRoot() throws RepositoryException {
88
89 Node siteRoot = getSiteRoot();
90
91
92
93 Node constrainedSearchRoot = getConstrainedSearchRoot();
94 if (constrainedSearchRoot != null) {
95 return constrainedSearchRoot;
96 }
97
98 return siteRoot;
99 }
100
101 private Node getConstrainedSearchRoot() {
102
103
104
105
106 String id = PropertyUtil.getString(content, "searchLink");
107 Node subNodeSearchPath = null;
108 if (StringUtils.isNotEmpty(id)) {
109 try {
110 subNodeSearchPath = NodeUtil.getNodeByIdentifier(getRepository(), id);
111 } catch (RepositoryException e) {
112 }
113 }
114 return subNodeSearchPath;
115 }
116
117 public Collection<Node> getItems() throws RepositoryException {
118 List<Node> itemsList = search();
119
120 this.filter(itemsList);
121 this.sort(itemsList);
122 itemsList = this.shrink(itemsList);
123
124 return itemsList;
125 }
126
127 public STKPager getPager() throws RepositoryException {
128
129 final String currentPageLink;
130 if (MgnlContext.isWebContext() && MgnlContext.getAggregationState() != null) {
131 currentPageLink = MgnlContext.getAggregationState().getOriginalURL();
132 } else {
133 currentPageLink = templatingFunctions.link(MgnlContext.getAggregationState().getMainContentNode());
134 }
135 return new STKPager(currentPageLink, getItems(), content);
136 }
137
138 protected List<Node> search() throws RepositoryException {
139 return stkFunctions.getContentListByTemplateCategorySubCategory(getSearchRoot(), getCategoryName(), getSubcategoryName());
140 }
141
142 abstract protected void filter(List<Node> itemList);
143
144 abstract protected void sort(List<Node> itemList);
145
146 private List<Node> shrink(List<Node> itemsList) {
147 return stkFunctions.cutList(itemsList, getMaxResults());
148 }
149
150 }