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