001/*
002 *  Copyright 2012 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.odf;
017
018import java.util.Collections;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022import org.apache.avalon.framework.service.Serviceable;
023
024import org.ametys.cms.repository.Content;
025import org.ametys.cms.transformation.ConsistencyChecker.CHECK;
026import org.ametys.cms.transformation.URIResolver;
027import org.ametys.plugins.repository.AmetysObjectResolver;
028import org.ametys.plugins.repository.UnknownAmetysObjectException;
029import org.ametys.runtime.i18n.I18nizableText;
030import org.ametys.runtime.plugin.component.AbstractLogEnabled;
031
032/**
033 * {@link URIResolver} for a Content.
034 */
035public class OdfURIResolver extends AbstractLogEnabled implements URIResolver, Serviceable
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: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 String resolveCroppedImage(String uri, int cropHeight, int cropWidth, boolean download, boolean absolute, boolean internal)
085    {
086        return resolve(uri, download, absolute, internal);
087    }
088    
089    @Override
090    public String resolveCroppedImageAsBase64(String uri, int cropHeight, int cropWidth)
091    {
092        return resolve(uri, false, false, false);
093    }
094    
095    @Override
096    public CHECK checkLink(String uri, boolean shortTest)
097    {
098        try
099        {
100            _resolver.resolveById(uri);
101            return CHECK.SUCCESS;
102        }
103        catch (UnknownAmetysObjectException e)
104        {
105            return CHECK.UNKNOWN;
106        }
107    }
108    
109    @Override
110    public String getType()
111    {
112        return "odf";
113    }
114    
115    @Override
116    public I18nizableText getLabel(String uri)
117    {
118        try
119        {
120            Content content = _resolver.resolveById(uri);
121            return new I18nizableText("plugin.odf", "PLUGINS_ODF_LINK_CONTENT_LABEL", Collections.singletonList(content.getTitle()));
122        }
123        catch (UnknownAmetysObjectException e)
124        {
125            return new I18nizableText("plugin.odf", "PLUGINS_ODF_LINK_CONTENT_UNKNOWN");
126        }
127    }
128}