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