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.posts;
017
018import org.apache.avalon.framework.context.Context;
019import org.apache.avalon.framework.context.ContextException;
020import org.apache.avalon.framework.context.Contextualizable;
021import org.apache.avalon.framework.logger.AbstractLogEnabled;
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.avalon.framework.service.Serviceable;
025
026import org.ametys.cms.repository.Content;
027import org.ametys.cms.transformation.ConsistencyChecker.CHECK;
028import org.ametys.cms.transformation.URIResolver;
029import org.ametys.core.util.URIUtils;
030import org.ametys.plugins.blog.BlogPageHandler;
031import org.ametys.plugins.repository.AmetysObjectResolver;
032import org.ametys.plugins.repository.UnknownAmetysObjectException;
033import org.ametys.runtime.i18n.I18nizableText;
034import org.ametys.web.URIPrefixHandler;
035import org.ametys.web.renderingcontext.RenderingContext;
036import org.ametys.web.renderingcontext.RenderingContextHandler;
037import org.ametys.web.repository.content.WebContent;
038import org.ametys.web.repository.page.Page;
039import org.ametys.web.repository.site.SiteManager;
040
041/**
042 * Resolves post page uri.
043 */
044public class PostUriResolver extends AbstractLogEnabled implements URIResolver, Serviceable, Contextualizable
045{
046    /** The ametys object resolver. */
047    protected AmetysObjectResolver _resolver;
048    
049    /** The blog cache manager. */
050    protected BlogPageHandler _blogRootPageHandler;
051    
052    /** The site manager. */
053    protected SiteManager _siteManager;
054    
055    /** The avalon context. */
056    protected Context _context;
057    
058    private RenderingContextHandler _renderingContextHandler;
059
060    private URIPrefixHandler _uriPrefixHandler;
061    
062    @Override
063    public void contextualize(Context context) throws ContextException
064    {
065        _context = context;
066    }
067    
068    @Override
069    public void service(ServiceManager manager) throws ServiceException
070    {
071        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
072        _blogRootPageHandler = (BlogPageHandler) manager.lookup(BlogPageHandler.ROLE);
073        _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE);
074        _renderingContextHandler = (RenderingContextHandler) manager.lookup(RenderingContextHandler.ROLE);
075        _uriPrefixHandler = (URIPrefixHandler) manager.lookup(URIPrefixHandler.ROLE);
076    }
077    
078    @Override
079    public String getType()
080    {
081        return "blog-post";
082    }
083
084    @Override
085    public String resolve(String uri, boolean download, boolean absolute, boolean internal)
086    {
087        RenderingContext currentContext = _renderingContextHandler.getRenderingContext();
088        
089        String pagePath;
090        String pageId;
091        String siteName =  null;
092        
093        try
094        {
095            Content content = _resolver.resolveById(uri);
096            
097            if (content instanceof WebContent)
098            {
099                WebContent webContent = (WebContent) content;
100                siteName = webContent.getSiteName();
101                
102                Page page = _blogRootPageHandler.getPostPage(webContent);
103                pageId = page.getId();
104                pagePath = page.getSitemap().getName() + "/" + page.getPathInSitemap() + ".html";
105            }
106            else
107            {
108                getLogger().warn("Link to unexisting page " + uri);
109                return "";
110            }
111        }
112        catch (UnknownAmetysObjectException e)
113        {
114            getLogger().warn("Link to unexisting page " + uri);
115            return "";
116        }
117        
118        if (currentContext == RenderingContext.FRONT)
119        {
120            String url = _siteManager.getSite(siteName).getUrl();
121            return url + "/" + pagePath;
122        }
123        
124        if (currentContext != RenderingContext.BACK)
125        {
126            StringBuilder result = new StringBuilder();
127            
128            result.append(_uriPrefixHandler.computeUriPrefix(siteName, absolute, internal));
129            result.append("/").append(pagePath);
130            
131            return URIUtils.encodePath(result.toString());
132        }
133        else // back
134        {
135            return "javascript:parent.Ametys.tool.ToolsManager.openTool('uitool-page', {id:'" + pageId + "'});";
136        }
137    }
138
139    @Override
140    public String resolveImage(String uri, int height, int width, boolean download, boolean absolute, boolean internal)
141    {
142        return resolve(uri, download, absolute, internal);
143    }
144    
145    @Override
146    public String resolveImageAsBase64(String uri, int height, int width)
147    {
148        return resolve(uri, false, false, false);
149    }
150    
151    @Override
152    public String resolveBoundedImage(String uri, int maxHeight, int maxWidth, boolean download, boolean absolute, boolean internal)
153    {
154        return resolve(uri, download, absolute, internal);
155    }
156    
157    @Override
158    public String resolveBoundedImageAsBase64(String uri, int maxHeight, int maxWidth)
159    {
160        return resolve(uri, false, false, false);
161    }
162    
163    @Override
164    public String resolveCroppedImage(String uri, int cropHeight, int cropWidth, boolean download, boolean absolute, boolean internal)
165    {
166        return resolve(uri, download, absolute, internal);
167    }
168    
169    @Override
170    public String resolveCroppedImageAsBase64(String uri, int cropHeight, int cropWidth)
171    {
172        return resolve(uri, false, false, false);
173    }
174    
175    @Override
176    public CHECK checkLink(String uri, boolean shortTest)
177    {
178        try
179        {
180            _resolver.resolveById(uri);
181            
182            return CHECK.SUCCESS;
183        }
184        catch (UnknownAmetysObjectException e)
185        {
186            return CHECK.NOT_FOUND;
187        }
188    }
189
190    @Override
191    public I18nizableText getLabel(String uri)
192    {
193        try
194        {
195            Content content = _resolver.resolveById(uri);
196            
197            return new I18nizableText(content.getTitle());
198        }
199        catch (UnknownAmetysObjectException e)
200        {
201            return new I18nizableText("");
202        }
203    }
204}