KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > pages > macro > PageMacroHelper


1 /* ***** BEGIN LICENSE BLOCK *****
2     * Version: MPL 1.1
3     * The contents of this file are subject to the Mozilla Public License Version
4     * 1.1 (the "License"); you may not use this file except in compliance with
5     * the License. You may obtain a copy of the License at
6     * http://www.mozilla.org/MPL/
7     *
8     * Software distributed under the License is distributed on an "AS IS" basis,
9     * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10     * for the specific language governing rights and limitations under the
11     * License.
12     *
13     * The Original Code is Riot.
14     *
15     * The Initial Developer of the Original Code is
16     * Neteye GmbH.
17     * Portions created by the Initial Developer are Copyright (C) 2007
18     * the Initial Developer. All Rights Reserved.
19     *
20     * Contributor(s):
21     * Felix Gnass [fgnass at neteye dot de]
22     * Carsten Woelk [cwoelk at neteye dot de]
23     *
24     * ***** END LICENSE BLOCK ***** */

25 package org.riotfamily.pages.macro;
26
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Locale JavaDoc;
32
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34
35 import org.riotfamily.components.context.PageRequestUtils;
36 import org.riotfamily.components.editor.EditModeUtils;
37 import org.riotfamily.pages.Page;
38 import org.riotfamily.pages.Site;
39 import org.riotfamily.pages.dao.PageDao;
40 import org.riotfamily.pages.mapping.PageHandlerMapping;
41 import org.riotfamily.pages.mapping.PageLocationResolver;
42 import org.springframework.util.StringUtils;
43
44 /**
45     * @author Felix Gnass [fgnass at neteye dot de]
46     * @since 6.5
47     */

48 public class PageMacroHelper {
49
50     private static final String JavaDoc PAGE_REFRESHED = PageMacroHelper.class.getName()
51             + ".refreshedPage";
52
53     private PageDao pageDao;
54
55     private PageLocationResolver resolver;
56
57     private HttpServletRequest JavaDoc request;
58
59     public PageMacroHelper(PageDao pageDao, PageLocationResolver resolver,
60             HttpServletRequest JavaDoc request) {
61
62         this.pageDao = pageDao;
63         this.resolver = resolver;
64         this.request = request;
65     }
66
67     public Page getCurrentPage() {
68         Page page = PageHandlerMapping.getPage(request);
69         if (PageRequestUtils.isPartialRequest(request)
70                 && request.getAttribute(PAGE_REFRESHED) == null) {
71
72             pageDao.refreshPage(page);
73             request.setAttribute(PAGE_REFRESHED, Boolean.TRUE);
74         }
75         return page;
76     }
77
78     public Page getPageForHandler(String JavaDoc handlerName, String JavaDoc localeString) {
79         Locale JavaDoc locale = StringUtils.parseLocaleString(localeString);
80         return getPageForHandler(handlerName, locale);
81     }
82
83     public Page getPageForHandler(String JavaDoc handlerName, Locale JavaDoc locale) {
84         return pageDao.findPageForHandler(handlerName, locale);
85     }
86
87     public List JavaDoc getPagesForHandler(String JavaDoc handlerName, Locale JavaDoc locale) {
88         return pageDao.findPagesForHandler(handlerName, locale);
89     }
90
91     public Collection JavaDoc getTopLevelPages(Locale JavaDoc locale) {
92         Site site = getCurrentPage().getNode().getSite();
93         return pageDao.findRootNode(site).getChildPages(locale);
94     }
95
96     public String JavaDoc getUrl(Page page) {
97         if (page != null) {
98             return resolver.getUrl(page);
99         }
100         return null;
101     }
102
103     public boolean isVisible(Page page) {
104         return !page.isHidden() && !page.getNode().isHidden()
105                 && (page.isEnabled() || EditModeUtils.isEditMode(request));
106     }
107
108     public Collection JavaDoc getVisiblePages(Collection JavaDoc pages) {
109         ArrayList JavaDoc result = new ArrayList JavaDoc();
110         Iterator JavaDoc it = pages.iterator();
111         while (it.hasNext()) {
112             Page page = (Page) it.next();
113             if (isVisible(page)) {
114                 result.add(page);
115             }
116         }
117         return result;
118     }
119
120     public List JavaDoc group(Collection JavaDoc pages, int size) {
121         ArrayList JavaDoc groups = new ArrayList JavaDoc();
122         int i = 0;
123         ArrayList JavaDoc group = null;
124         Iterator JavaDoc it = pages.iterator();
125         while (it.hasNext()) {
126             Page page = (Page) it.next();
127             if (isVisible(page)) {
128                 if (i++ % size == 0) {
129                     group = new ArrayList JavaDoc();
130                     groups.add(group);
131                 }
132                 group.add(page);
133             }
134         }
135         return groups;
136     }
137 }
138
Popular Tags