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.IOException; 021import java.io.Serializable; 022import java.util.HashMap; 023import java.util.Map; 024 025import org.nuxeo.ecm.core.api.NuxeoException; 026import org.nuxeo.ecm.core.api.blobholder.BlobHolder; 027import org.nuxeo.ecm.core.api.blobholder.SimpleBlobHolderWithProperties; 028import org.nuxeo.ecm.core.convert.api.ConversionService; 029import org.nuxeo.ecm.core.transientstore.StorageEntryImpl; 030import org.nuxeo.ecm.core.transientstore.api.StorageEntry; 031import org.nuxeo.ecm.core.transientstore.work.TransientStoreWork; 032import org.nuxeo.runtime.api.Framework; 033 034/** 035 * Work executing a given conversion. 036 * 037 * @since 7.4 038 */ 039public class ConversionWork extends TransientStoreWork { 040 041 protected String converterName; 042 043 protected Map<String, Serializable> parameters; 044 045 protected String inputEntryKey; 046 047 public ConversionWork(String converterName, BlobHolder blobHolder, Map<String, Serializable> parameters) { 048 super(); 049 this.converterName = converterName; 050 this.parameters = parameters; 051 if (this.parameters == null) { 052 this.parameters = new HashMap<>(); 053 } 054 055 storeInputBlobHolder(blobHolder); 056 } 057 058 protected void storeInputBlobHolder(BlobHolder blobHolder) { 059 inputEntryKey = entryKey + "_input"; 060 StorageEntry entry = new StorageEntryImpl(inputEntryKey); 061 entry.setBlobs(blobHolder.getBlobs()); 062 Map<String, Serializable> properties = blobHolder.getProperties(); 063 if (properties != null) { 064 entry.putAll(properties); 065 } 066 saveStorageEntry(entry); 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 = conversionService.convert(converterName, inputBlobHolder, parameters); 080 if (result == null) { 081 return; 082 } 083 084 StorageEntry entry = getStorageEntry(); 085 entry.setBlobs(result.getBlobs()); 086 Map<String, Serializable> properties = result.getProperties(); 087 if (properties != null) { 088 entry.putAll(properties); 089 } 090 saveStorageEntry(); 091 092 setStatus(null); 093 } 094 095 protected BlobHolder retrieveInputBlobHolder() { 096 StorageEntry inputEntry = getStorageEntry(inputEntryKey); 097 if (inputEntry == null) { 098 return null; 099 } 100 return new SimpleBlobHolderWithProperties(inputEntry.getBlobs(), inputEntry.getParameters()); 101 } 102 103 @Override 104 public void cleanUp(boolean ok, Exception e) { 105 super.cleanUp(ok, e); 106 getStore().remove(inputEntryKey); 107 } 108 109 @Override 110 public String getTitle() { 111 return String.format("Conversion using '%s' converter", converterName); 112 } 113}