KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > pages > riot > chooser > PageChooserController


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) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.pages.riot.chooser;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.HashMap 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 import javax.servlet.http.HttpServletResponse JavaDoc;
35
36 import org.riotfamily.common.util.ResourceUtils;
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.PageLocationResolver;
41 import org.springframework.util.StringUtils;
42 import org.springframework.web.bind.ServletRequestUtils;
43 import org.springframework.web.servlet.ModelAndView;
44 import org.springframework.web.servlet.mvc.Controller;
45 import org.springframework.web.servlet.support.RequestContextUtils;
46
47 public class PageChooserController implements Controller {
48
49     private PageDao pageDao;
50
51     private PageLocationResolver resolver;
52
53     private String JavaDoc viewName = ResourceUtils.getPath(
54             PageChooserController.class, "PageChooserView.ftl");
55
56
57     public PageChooserController(PageDao pageDao, PageLocationResolver resolver) {
58         this.pageDao = pageDao;
59         this.resolver = resolver;
60     }
61
62     private Locale JavaDoc getLocale(HttpServletRequest JavaDoc request) {
63         String JavaDoc localeString = request.getParameter("locale");
64         if (localeString != null) {
65             return StringUtils.parseLocaleString(localeString);
66         }
67         List JavaDoc locales = pageDao.getLocales();
68         if (locales != null && !locales.isEmpty()) {
69             if (locales.size() == 1) {
70                 return (Locale JavaDoc) locales.get(0);
71             }
72             Locale JavaDoc locale = RequestContextUtils.getLocale(request);
73             if (!locales.contains(locale)) {
74                 locale = (Locale JavaDoc) locales.get(0);
75             }
76             return locale;
77         }
78         return null;
79     }
80
81     public ModelAndView handleRequest(HttpServletRequest JavaDoc request,
82             HttpServletResponse JavaDoc response) throws Exception JavaDoc {
83
84         Long JavaDoc siteId = ServletRequestUtils.getLongParameter(request, "site");
85         Site site = siteId != null
86                 ? pageDao.loadSite(siteId)
87                 : pageDao.getDefaultSite();
88
89         Locale JavaDoc locale = getLocale(request);
90
91         HashMap JavaDoc model = new HashMap JavaDoc();
92         model.put("mode", ServletRequestUtils.getStringParameter(
93                 request, "mode", "forms"));
94
95         model.put("locales", pageDao.getLocales());
96         model.put("selectedLocale", locale);
97         model.put("sites", pageDao.listSites());
98         model.put("selectedSite", site);
99         model.put("pages", createpageLinks(
100                 pageDao.findRootNode(site).getChildPages(locale)));
101
102         return new ModelAndView(viewName, model);
103     }
104
105     private List JavaDoc createpageLinks(Collection JavaDoc pages) {
106         ArrayList JavaDoc links = new ArrayList JavaDoc();
107         Iterator JavaDoc it = pages.iterator();
108         while (it.hasNext()) {
109             Page page = (Page) it.next();
110             if (!page.isWildcardInPath()) {
111                 PageLink link = new PageLink();
112                 link.setPathComponent(page.getPathComponent());
113                 link.setLink(resolver.getUrl(page));
114                 link.setTitle(page.getProperty("title", true));
115                 link.setPublished(page.isPublished());
116                 link.setChildPages(createpageLinks(page.getChildPages()));
117                 links.add(link);
118             }
119         }
120         return links;
121     }
122 }
123
Popular Tags