001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Thomas Roger
016 */
017
018package org.nuxeo.ecm.core.convert.service;
019
020import java.io.Serializable;
021import java.util.HashMap;
022import java.util.Map;
023
024import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
025import org.nuxeo.ecm.core.convert.api.ConversionService;
026import org.nuxeo.ecm.core.transientstore.work.TransientStoreWork;
027import org.nuxeo.runtime.api.Framework;
028
029/**
030 * Work executing a given conversion.
031 *
032 * @since 7.4
033 */
034public class ConversionWork extends TransientStoreWork {
035
036    private static final long serialVersionUID = 14593653977944460L;
037
038    protected String converterName;
039
040    protected String destinationMimeType;
041
042    protected Map<String, Serializable> parameters;
043
044    protected String inputEntryKey;
045
046    public ConversionWork(String converterName, String destinationMimeType, BlobHolder blobHolder,
047            Map<String, Serializable> parameters) {
048        super();
049        if (converterName == null && destinationMimeType == null) {
050            throw new IllegalArgumentException("'convertName' or 'destinationMimeType' must not be null");
051        }
052        this.converterName = converterName;
053        this.destinationMimeType = destinationMimeType;
054        this.parameters = parameters;
055        if (this.parameters == null) {
056            this.parameters = new HashMap<>();
057        }
058
059        storeInputBlobHolder(blobHolder);
060    }
061
062    protected void storeInputBlobHolder(BlobHolder blobHolder) {
063        inputEntryKey = entryKey + "_input";
064        putBlobHolder(inputEntryKey, blobHolder);
065    }
066
067    @Override
068    public void work() {
069        setStatus("Converting");
070
071        BlobHolder inputBlobHolder = retrieveInputBlobHolder();
072        if (inputBlobHolder == null) {
073            return;
074        }
075
076        ConversionService conversionService = Framework.getService(ConversionService.class);
077        BlobHolder result = converterName != null ? conversionService.convert(converterName, inputBlobHolder,
078                parameters) : conversionService.convertToMimeType(destinationMimeType, inputBlobHolder, parameters);
079        if (result == null) {
080            return;
081        }
082
083        putBlobHolder(result);
084
085        setStatus(null);
086    }
087
088    protected BlobHolder retrieveInputBlobHolder() {
089        return getBlobHolder(inputEntryKey);
090    }
091
092    @Override
093    public void cleanUp(boolean ok, Exception e) {
094        super.cleanUp(ok, e);
095        removeBlobHolder(inputEntryKey);
096    }
097
098    @Override
099    public String getTitle() {
100        if (converterName != null) {
101            return String.format("Conversion using '%s' converter", converterName);
102        } else {
103            return String.format("Conversion using '%s' target mime type", destinationMimeType);
104        }
105    }
106}