001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and others.
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 *
016 * Contributors:
017 *     Thomas Roger
018 */
019
020package org.nuxeo.ecm.core.convert.service;
021
022import java.io.Serializable;
023import java.util.HashMap;
024import java.util.Map;
025
026import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
027import org.nuxeo.ecm.core.convert.api.ConversionService;
028import org.nuxeo.ecm.core.transientstore.work.TransientStoreWork;
029import org.nuxeo.runtime.api.Framework;
030
031/**
032 * Work executing a given conversion.
033 *
034 * @since 7.4
035 */
036public class ConversionWork extends TransientStoreWork {
037
038    private static final long serialVersionUID = 14593653977944460L;
039
040    protected String converterName;
041
042    protected String destinationMimeType;
043
044    protected Map<String, Serializable> parameters;
045
046    protected String inputEntryKey;
047
048    public ConversionWork(String converterName, String destinationMimeType, BlobHolder blobHolder,
049            Map<String, Serializable> parameters) {
050        super();
051        if (converterName == null && destinationMimeType == null) {
052            throw new IllegalArgumentException("'convertName' or 'destinationMimeType' must not be null");
053        }
054        this.converterName = converterName;
055        this.destinationMimeType = destinationMimeType;
056        this.parameters = parameters;
057        if (this.parameters == null) {
058            this.parameters = new HashMap<>();
059        }
060
061        storeInputBlobHolder(blobHolder);
062    }
063
064    protected void storeInputBlobHolder(BlobHolder blobHolder) {
065        inputEntryKey = entryKey + "_input";
066        putBlobHolder(inputEntryKey, blobHolder);
067    }
068
069    @Override
070    public void work() {
071        setStatus("Converting");
072
073        BlobHolder inputBlobHolder = retrieveInputBlobHolder();
074        if (inputBlobHolder == null) {
075            return;
076        }
077
078        ConversionService conversionService = Framework.getService(ConversionService.class);
079        BlobHolder result = converterName != null ? conversionService.convert(converterName, inputBlobHolder,
080                parameters) : conversionService.convertToMimeType(destinationMimeType, inputBlobHolder, parameters);
081        if (result == null) {
082            return;
083        }
084
085        putBlobHolder(result);
086
087        setStatus(null);
088    }
089
090    protected BlobHolder retrieveInputBlobHolder() {
091        return getBlobHolder(inputEntryKey);
092    }
093
094    @Override
095    public void cleanUp(boolean ok, Exception e) {
096        super.cleanUp(ok, e);
097        removeBlobHolder(inputEntryKey);
098    }
099
100    @Override
101    public String getTitle() {
102        if (converterName != null) {
103            return String.format("Conversion using '%s' converter", converterName);
104        } else {
105            return String.format("Conversion using '%s' target mime type", destinationMimeType);
106        }
107    }
108}