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