KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > pages > component > PageComponentListLocator


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  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.pages.component;
25
26 import java.util.Map JavaDoc;
27
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29
30 import net.sf.json.JSONObject;
31
32 import org.riotfamily.common.beans.MapWrapper;
33 import org.riotfamily.common.web.mapping.AttributePattern;
34 import org.riotfamily.components.ComponentListLocator;
35 import org.riotfamily.components.Location;
36 import org.riotfamily.components.locator.SlotResolver;
37 import org.riotfamily.components.locator.TemplateSlotResolver;
38 import org.riotfamily.pages.Page;
39 import org.riotfamily.pages.dao.PageDao;
40 import org.riotfamily.pages.mapping.PageHandlerMapping;
41 import org.riotfamily.pages.mapping.PageLocationResolver;
42
43 /**
44  * ComponentListLocator that uses the page-id as component-path.
45  * @author Felix Gnass [fgnass at neteye dot de]
46  * @since 6.5
47  */

48 public class PageComponentListLocator implements ComponentListLocator {
49
50     public static final String JavaDoc TYPE_PAGE = "page";
51
52     public static final String JavaDoc TYPE_WILDCARD_PAGE_PREFIX = "page-";
53
54     private PageDao pageDao;
55
56     private PageLocationResolver resolver;
57
58     private SlotResolver slotResolver = new TemplateSlotResolver();
59
60     public PageComponentListLocator(PageDao pageDao,
61             PageLocationResolver resolver) {
62
63         this.pageDao = pageDao;
64         this.resolver = resolver;
65     }
66
67     public void setSlotResolver(SlotResolver slotResolver) {
68         this.slotResolver = slotResolver;
69     }
70
71     public boolean supports(String JavaDoc type) {
72         return type.equals(TYPE_PAGE) || type.startsWith(TYPE_WILDCARD_PAGE_PREFIX);
73     }
74
75     public Location getLocation(HttpServletRequest JavaDoc request) {
76         Location location = new Location();
77         Page page = PageHandlerMapping.getPage(request);
78         if (page.isWildcardInPath()) {
79             Map JavaDoc attributes = PageHandlerMapping.getWildcardAttributes(request);
80             String JavaDoc jsonString = JSONObject.fromObject(attributes).toString();
81             location.setType(TYPE_WILDCARD_PAGE_PREFIX + page.getId());
82             location.setPath(jsonString);
83         }
84         else {
85             location.setType(TYPE_PAGE);
86             location.setPath(page.getId().toString());
87         }
88         location.setSlot(slotResolver.getSlot(request));
89         return location;
90     }
91
92     public Location getParentLocation(Location location) {
93         Page page = loadPage(location);
94         Location parentLocation = new Location(location);
95         location.setType(TYPE_PAGE);
96         location.setPath(page.getParentPage().getId().toString());
97         return parentLocation;
98     }
99
100     public String JavaDoc getUrl(Location location) {
101         Page page = loadPage(location);
102         String JavaDoc url = resolver.getUrl(page);
103         if (page.isWildcardInPath()) {
104             Map JavaDoc attributes = JSONObject.fromObject(location.getPath());
105             url = new AttributePattern(url).fillInAttributes(new MapWrapper(attributes));
106         }
107         return url;
108     }
109
110     private Page loadPage(Location location) {
111         String JavaDoc id;
112         if (location.getType().equals(TYPE_PAGE)) {
113              id = location.getPath();
114         }
115         else {
116             String JavaDoc type = location.getType();
117             int i = type.indexOf('-');
118             id = type.substring(i + 1);
119         }
120         return pageDao.loadPage(new Long JavaDoc(id));
121     }
122
123 }
124
Popular Tags