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        if (converterName == null && destinationMimeType == null) {
051            throw new IllegalArgumentException("'convertName' or 'destinationMimeType' must not be null");
052        }
053        this.converterName = converterName;
054        this.destinationMimeType = destinationMimeType;
055        this.parameters = parameters;
056        if (this.parameters == null) {
057            this.parameters = new HashMap<>();
058        }
059
060        storeInputBlobHolder(blobHolder);
061    }
062
063    protected void storeInputBlobHolder(BlobHolder blobHolder) {
064        inputEntryKey = entryKey + "_input";
065        putBlobHolder(inputEntryKey, blobHolder);
066    }
067
068    @Override
069    public void work() {
070        setStatus("Converting");
071
072        BlobHolder inputBlobHolder = retrieveInputBlobHolder();
073        if (inputBlobHolder == null) {
074            return;
075        }
076
077        ConversionService conversionService = Framework.getService(ConversionService.class);
078        BlobHolder result = converterName != null ? conversionService.convert(converterName, inputBlobHolder,
079                parameters) : conversionService.convertToMimeType(destinationMimeType, inputBlobHolder, parameters);
080        if (result == null) {
081            return;
082        }
083
084        putBlobHolder(result);
085
086        setStatus(null);
087    }
088
089    protected BlobHolder retrieveInputBlobHolder() {
090        return getBlobHolder(inputEntryKey);
091    }
092
093    @Override
094    public void cleanUp(boolean ok, Exception e) {
095        super.cleanUp(ok, e);
096        removeBlobHolder(inputEntryKey);
097    }
098
099    @Override
100    public String getTitle() {
101        if (converterName != null) {
102            return String.format("Conversion using '%s' converter", converterName);
103        } else {
104            return String.format("Conversion using '%s' target mime type", destinationMimeType);
105        }
106    }
107}