001/* 002 * Copyright 2011 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.plugins.blog.repository; 017 018import java.util.Collections; 019import java.util.Set; 020 021import org.ametys.cms.repository.Content; 022import org.ametys.plugins.explorer.resources.ResourceCollection; 023import org.ametys.plugins.repository.AmetysObject; 024import org.ametys.plugins.repository.AmetysObjectIterable; 025import org.ametys.plugins.repository.AmetysObjectResolver; 026import org.ametys.plugins.repository.AmetysRepositoryException; 027import org.ametys.plugins.repository.EmptyIterable; 028import org.ametys.plugins.repository.UnknownAmetysObjectException; 029import org.ametys.plugins.repository.metadata.CompositeMetadata; 030import org.ametys.web.repository.page.Page; 031import org.ametys.web.repository.page.PagesContainer; 032import org.ametys.web.repository.page.Zone; 033import org.ametys.web.repository.site.Site; 034import org.ametys.web.repository.sitemap.Sitemap; 035import org.ametys.web.skin.Skin; 036import org.ametys.web.skin.SkinsManager; 037 038/** 039 * Page representing a post. 040 */ 041public class VirtualPostPage extends AbstractBlogPage 042{ 043 private PagesContainer _root; 044 private Content _post; 045 private String _path; 046 private SkinsManager _skinsManager; 047 048 /** 049 * Constructor. 050 * @param skinsManager the skins manager 051 * @param resolver the {@link AmetysObjectResolver}. 052 * @param root the blog root page. 053 * @param post the post. 054 * @param path path from the virtual root 055 */ 056 public VirtualPostPage(AmetysObjectResolver resolver, SkinsManager skinsManager, PagesContainer root, Content post, String path) 057 { 058 _root = root; 059 _skinsManager = skinsManager; 060 _post = post; 061 _path = path; 062 } 063 064 /** 065 * Returns the associated {@link Content}. 066 * @return the associated {@link Content}. 067 */ 068 public Content getPost() 069 { 070 return _post; 071 } 072 073 @Override 074 public int getDepth() throws AmetysRepositoryException 075 { 076 int depth = 0; 077 if (_root instanceof Page) 078 { 079 depth = ((Page) _root).getDepth(); 080 } 081 082 return depth + _path.split("/").length; 083 } 084 085 @Override 086 public Set<String> getReferers() throws AmetysRepositoryException 087 { 088 throw new UnsupportedOperationException("getReferers not supported on virtual post pages"); 089 } 090 091 @Override 092 public ResourceCollection getRootAttachments() throws AmetysRepositoryException 093 { 094 return null; 095 } 096 097 @Override 098 public String getTitle() throws AmetysRepositoryException 099 { 100 return _post.getTitle(); 101 } 102 103 @Override 104 public String getLongTitle() throws AmetysRepositoryException 105 { 106 return _post.getTitle(); 107 } 108 109 @Override 110 public String getURL() throws AmetysRepositoryException 111 { 112 throw new UnsupportedOperationException("getURL not supported on virtual post pages"); 113 } 114 115 @Override 116 public LinkType getURLType() throws AmetysRepositoryException 117 { 118 throw new UnsupportedOperationException("getURLType not supported on virtual post pages"); 119 } 120 121 @Override 122 protected Zone getDefaultZone() 123 { 124 return new PostZone(this); 125 } 126 127 @Override 128 public AmetysObjectIterable<? extends Page> getChildrenPages() throws AmetysRepositoryException 129 { 130 return new EmptyIterable<>(); 131 } 132 133 @Override 134 public Page getChildPageAt(int index) throws UnknownAmetysObjectException, AmetysRepositoryException 135 { 136 throw new UnknownAmetysObjectException("There's no child page at index " + index + " for page " + this.getId()); 137 } 138 139 @Override 140 public String getPathInSitemap() throws AmetysRepositoryException 141 { 142 String rootPath = _root.getPathInSitemap(); 143 144 StringBuilder buff = new StringBuilder(rootPath); 145 if (!rootPath.isEmpty()) 146 { 147 buff.append('/'); 148 } 149 buff.append(_path).append('/').append(_post.getName()); 150 151 return buff.toString(); 152 } 153 154 @Override 155 public Site getSite() throws AmetysRepositoryException 156 { 157 return _root.getSite(); 158 } 159 160 @Override 161 public String getSiteName() throws AmetysRepositoryException 162 { 163 return _root.getSiteName(); 164 } 165 166 @Override 167 public Sitemap getSitemap() throws AmetysRepositoryException 168 { 169 return _root.getSitemap(); 170 } 171 172 @Override 173 public String getSitemapName() throws AmetysRepositoryException 174 { 175 return _root.getSitemapName(); 176 } 177 178 @Override 179 public <A extends AmetysObject> A getChild(String path) throws AmetysRepositoryException, UnknownAmetysObjectException 180 { 181 throw new UnknownAmetysObjectException("Unknown child page '" + path + "' for page " + getId()); 182 } 183 184 @SuppressWarnings("unchecked") 185 @Override 186 public AmetysObjectIterable<? extends Page> getChildren() throws AmetysRepositoryException 187 { 188 return new EmptyIterable<>(); 189 } 190 191 @Override 192 public AmetysObjectIterable< ? extends Page> getChildrenPages(boolean includeInvisiblePages) throws AmetysRepositoryException 193 { 194 return getChildrenPages(); 195 } 196 197 @Override 198 public boolean hasChild(String name) throws AmetysRepositoryException 199 { 200 return false; 201 } 202 203 @Override 204 public String getId() throws AmetysRepositoryException 205 { 206 return "post://" + _path + "?rootId=" + _root.getId() + "&postId=" + _post.getId(); 207 } 208 209 @Override 210 public String getName() throws AmetysRepositoryException 211 { 212 return _post.getName(); 213 } 214 215 @SuppressWarnings("unchecked") 216 @Override 217 public Page getParent() throws AmetysRepositoryException 218 { 219 return _root.getChild(_path); 220 } 221 222 @Override 223 public String getParentPath() throws AmetysRepositoryException 224 { 225 StringBuilder path = new StringBuilder(_root.getPath()); 226 if (path.length() > 0) 227 { 228 path.append('/'); 229 } 230 231 path.append(_path); 232 233 return path.toString(); 234 } 235 236 @Override 237 public String getPath() throws AmetysRepositoryException 238 { 239 return getParentPath() + "/" + _post.getName(); 240 } 241 242 @Override 243 public CompositeMetadata getMetadataHolder() 244 { 245 return new StaticCompositeMetadata(); 246 } 247 248 @Override 249 public Set<String> getTags() throws AmetysRepositoryException 250 { 251 return Collections.emptySet(); 252 } 253 254 @Override 255 public String getTemplate() throws AmetysRepositoryException 256 { 257 Skin skin = _skinsManager.getSkin(getSite().getSkinId()); 258 259 if (skin.getTemplate(BLOG_POST_TEMPLATE) != null) 260 { 261 return BLOG_POST_TEMPLATE; 262 } 263 264 return super.getTemplate(); 265 } 266 267 @Override 268 public boolean isVisible() throws AmetysRepositoryException 269 { 270 return true; 271 } 272}