/*
 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl-2.1.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * Contributors:
 *     Thomas Roger
 */

package org.nuxeo.ecm.platform.thumbnail.converter;

import static org.nuxeo.ecm.platform.thumbnail.ThumbnailConstants.ANY_TO_PDF_TO_THUMBNAIL_CONVERTER_NAME;
import static org.nuxeo.ecm.platform.thumbnail.ThumbnailConstants.PDF_AND_IMAGE_TO_THUMBNAIL_CONVERTER_NAME;

import java.io.Serializable;
import java.util.Map;
import java.util.regex.Pattern;

import org.nuxeo.ecm.core.api.Blob;
import org.nuxeo.ecm.core.api.ClientException;
import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
import org.nuxeo.ecm.core.convert.api.ConversionException;
import org.nuxeo.ecm.core.convert.api.ConversionService;
import org.nuxeo.ecm.core.convert.extension.Converter;
import org.nuxeo.ecm.core.convert.extension.ConverterDescriptor;
import org.nuxeo.runtime.api.Framework;

/**
 * Base converter choosing the correct convert to generate a thumbnail
 * according to the Blob's mime type.
 *
 * @since 5.8
 */
public class AnyToThumbnailConverter implements Converter {

    public static final String PDF_MIME_TYPE = "application/pdf";

    public static final Pattern PDF_MIME_TYPE_PATTERN = Pattern.compile("application/.*pdf");

    public static final String ANY_TO_PDF_CONVERTER_NAME = "any2pdf";

    @Override
    public void init(ConverterDescriptor descriptor) {
    }

    @Override
    public BlobHolder convert(BlobHolder blobHolder,
            Map<String, Serializable> parameters) throws ConversionException {
        Blob sourceBlob;

        try {
            sourceBlob = blobHolder.getBlob();
        } catch (ClientException e) {
            throw new ConversionException("Unable to fetch Blob", e);
        }

        if (sourceBlob == null) {
            return null;
        }

        String mimeType = sourceBlob.getMimeType();
        if (mimeType == null) {
            return null;
        }

        ConversionService conversionService = Framework.getLocalService(ConversionService.class);

        String converterName = null;
        if (mimeType.startsWith("image/")
                || PDF_MIME_TYPE_PATTERN.matcher(mimeType).matches()) {
            converterName = PDF_AND_IMAGE_TO_THUMBNAIL_CONVERTER_NAME;
        } else {
            if (conversionService.isSourceMimeTypeSupported(
                    ANY_TO_PDF_CONVERTER_NAME, mimeType)
                    && conversionService.isConverterAvailable(
                            ANY_TO_PDF_TO_THUMBNAIL_CONVERTER_NAME, true).isAvailable()) {
                converterName = ANY_TO_PDF_TO_THUMBNAIL_CONVERTER_NAME;
            }
        }
        return converterName == null ? null : conversionService.convert(
                converterName, blobHolder, parameters);
    }
}

@@ -1473,19 +1473,16 @@
 humbnail
-%0A *
  accordi
@@ -1483,16 +1483,19 @@
 ccording
+%0A *
  to the 
@@ -2584,32 +2584,33 @@
 ull;%0A        if 
+(
 (mimeType.starts
@@ -2623,32 +2623,16 @@
 image/%22)
-%0A               
  %7C%7C PDF_
@@ -2657,16 +2657,33 @@
 matcher(
+%0A                
 mimeType
@@ -2694,19 +2694,21 @@
 tches())
- %7B%0A
+%0A    
         
@@ -2702,39 +2702,184 @@
 %0A               
+ &&
  conver
+sionService.isConverterAvailable(%0A                        PDF_AND_IMAGE_TO_THUMBNAIL_CONVERTER_NAME, true).isAvailable()) %7B%0A            conver
 terName = PDF_AN
@@ -2918,25 +2918,8 @@
 AME;
-%0A        %7D else %7B
 %0A   
@@ -2919,27 +2919,30 @@
 ME;%0A        
-   
+%7D else
  if (convers
@@ -2987,36 +2987,32 @@
                 
-    
 ANY_TO_PDF_CONVE
@@ -3032,20 +3032,16 @@
 meType)%0A
-    
         
@@ -3107,36 +3107,32 @@
                 
-    
 ANY_TO_PDF_TO_TH
@@ -3122,37 +3122,24 @@
  ANY_TO_PDF_
-TO_THUMBNAIL_
 CONVERTER_NA
@@ -3157,36 +3157,32 @@
 sAvailable()) %7B%0A
-    
             conv
@@ -3237,22 +3237,8 @@
 ME;%0A
-            %7D%0A
     

check that the converters are available before computing the thumbnail