001/*
002 *  Copyright 2014 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.flipbook;
017
018import java.io.File;
019import java.io.IOException;
020import java.util.List;
021import java.util.Objects;
022
023import org.apache.avalon.framework.activity.Initializable;
024import org.apache.avalon.framework.component.Component;
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027import org.apache.avalon.framework.service.Serviceable;
028
029import org.ametys.runtime.config.Config;
030import org.ametys.runtime.plugin.ExtensionPoint;
031
032/**
033 * Converts a document file to PNG images, one image per page.
034 * The document images file name must begin with "page".
035 * <br>This component just delegates its behavior to the chosen {@link Document2ImagesConvertorPolicy} in configuration <code>flipbook.convertor</code>
036 */
037public class Document2ImagesConvertor implements Component, Serviceable, Initializable
038{
039    /** The avalon role. */
040    public static final String ROLE = Document2ImagesConvertor.class.getName();
041    
042    private static final String __CONFIGURATION_NAME = "flipbook.convertor";
043    
044    /** The {@link ExtensionPoint} for {@link Document2ImagesConvertorPolicy Document2ImagesConvertorPolicies} */
045    protected Document2ImagesConvertorPolicyExtensionPoint _document2ImagesConvertorPolicies;
046    /** The {@link Document2ImagesConvertorPolicy} chosen in global configuration to use */
047    protected Document2ImagesConvertorPolicy _document2ImagesConvertorPolicyInConf;
048    
049    @Override
050    public void service(ServiceManager manager) throws ServiceException
051    {
052        _document2ImagesConvertorPolicies = (Document2ImagesConvertorPolicyExtensionPoint) manager.lookup(Document2ImagesConvertorPolicyExtensionPoint.ROLE);
053    }
054    
055    @Override
056    public void initialize() throws Exception
057    {
058        String document2ImagesConvertorInConfId = Objects.requireNonNull(
059                Config.getInstance().getValue(__CONFIGURATION_NAME), 
060                String.format("Config param %s is mandatory", __CONFIGURATION_NAME));
061        
062        _document2ImagesConvertorPolicyInConf = Objects.requireNonNull(
063                _document2ImagesConvertorPolicies.getExtension(document2ImagesConvertorInConfId),
064                String.format("Extension for id '%s' does not exist for point '%s'", document2ImagesConvertorInConfId, Document2ImagesConvertorPolicyExtensionPoint.ROLE));
065    }
066    
067    /**
068     * Get the list of mime types the convertor can convert
069     * @return The list of supported mime-types
070     */
071    public List<String> getSupportedMimeTypes()
072    {
073        return _document2ImagesConvertorPolicyInConf.getSupportedMimeTypes();
074    }
075    
076    /**
077     * Convert the given document file to PNG images, one image per page.
078     * @param documentFile the document file to convert.
079     * @param folder the destination folder.
080     * @throws IOException if an I/O error occurs.
081     * @throws FlipbookException if a document error occurs.
082     */
083    public void convert(File documentFile, File folder) throws IOException, FlipbookException
084    {
085        _document2ImagesConvertorPolicyInConf.convert(documentFile, folder);
086    }
087    
088}