001/*
002 *  Copyright 2013 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.cms.transformation;
017
018import java.util.Collections;
019
020import org.apache.avalon.framework.logger.AbstractLogEnabled;
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.avalon.framework.service.Serviceable;
024
025import org.ametys.cms.repository.Content;
026import org.ametys.cms.transformation.ConsistencyChecker.CHECK;
027import org.ametys.plugins.repository.AmetysObjectResolver;
028import org.ametys.plugins.repository.UnknownAmetysObjectException;
029import org.ametys.runtime.i18n.I18nizableText;
030
031/**
032 * {@link URIResolver} for a Content.
033 */
034public class ContentURIResolver extends AbstractLogEnabled implements URIResolver, Serviceable
035{
036
037    /** The Ametys object resolver */
038    protected AmetysObjectResolver _resolver;
039
040    @Override
041    public void service(ServiceManager manager) throws ServiceException
042    {
043        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
044    }
045    
046    @Override
047    public String resolve(String uri, boolean download, boolean absolute, boolean internal)
048    {
049        String contentId = uri;
050        int i = uri.indexOf(";");
051        if (i != -1)
052        {
053            contentId = uri.substring(0, i);
054        }
055                
056        return "javascript:(function(){parent.Ametys.tool.ToolsManager.openTool('uitool-content', {id:'" + contentId + "'});})()";
057    }
058    
059    @Override
060    public String resolveImage(String uri, int height, int width, boolean download, boolean absolute, boolean internal)
061    {
062        return resolve(uri, download, absolute, internal);
063    }
064    
065    @Override
066    public String resolveImageAsBase64(String uri, int height, int width)
067    {
068        return resolve(uri, false, false, false);
069    }
070    
071    @Override
072    public String resolveBoundedImage(String uri, int maxHeight, int maxWidth, boolean download, boolean absolute, boolean internal)
073    {
074        return resolve(uri, download, absolute, internal);
075    }
076    
077    @Override
078    public String resolveBoundedImageAsBase64(String uri, int maxHeight, int maxWidth)
079    {
080        return resolve(uri, false, false, false);
081    }
082    
083    @Override
084    public CHECK checkLink(String uri, boolean shortTest)
085    {
086        try
087        {
088            _resolver.resolveById(uri);
089            return CHECK.SUCCESS;
090        }
091        catch (UnknownAmetysObjectException e)
092        {
093            return CHECK.NOT_FOUND;
094        }
095    }
096    
097    @Override
098    public String getType()
099    {
100        return "content";
101    }
102    
103    @Override
104    public I18nizableText getLabel(String uri)
105    {
106        Content content = _resolver.resolveById(uri);
107        return new I18nizableText("plugin.cms", "PLUGINS_CMS_LINK_CONTENT_LABEL", Collections.singletonList(content.getTitle()));
108    }
109
110}