| 1 24 package org.riotfamily.pages.component; 25 26 import java.util.Map ; 27 28 import javax.servlet.http.HttpServletRequest ; 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 48 public class PageComponentListLocator implements ComponentListLocator { 49 50 public static final String TYPE_PAGE = "page"; 51 52 public static final String 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 type) { 72 return type.equals(TYPE_PAGE) || type.startsWith(TYPE_WILDCARD_PAGE_PREFIX); 73 } 74 75 public Location getLocation(HttpServletRequest request) { 76 Location location = new Location(); 77 Page page = PageHandlerMapping.getPage(request); 78 if (page.isWildcardInPath()) { 79 Map attributes = PageHandlerMapping.getWildcardAttributes(request); 80 String 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 getUrl(Location location) { 101 Page page = loadPage(location); 102 String url = resolver.getUrl(page); 103 if (page.isWildcardInPath()) { 104 Map 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 id; 112 if (location.getType().equals(TYPE_PAGE)) { 113 id = location.getPath(); 114 } 115 else { 116 String type = location.getType(); 117 int i = type.indexOf('-'); 118 id = type.substring(i + 1); 119 } 120 return pageDao.loadPage(new Long (id)); 121 } 122 123 } 124 | Popular Tags |