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.Blob; 027import org.nuxeo.ecm.core.api.blobholder.BlobHolder; 028import org.nuxeo.ecm.core.api.blobholder.SimpleBlobHolder; 029import org.nuxeo.ecm.core.blob.BlobManager; 030import org.nuxeo.ecm.core.blob.BlobProvider; 031import org.nuxeo.ecm.core.blob.ManagedBlob; 032import org.nuxeo.ecm.core.convert.api.ConversionService; 033import org.nuxeo.ecm.core.transientstore.work.TransientStoreWork; 034import org.nuxeo.runtime.api.Framework; 035 036/** 037 * Work executing a given conversion. 038 * 039 * @since 7.4 040 */ 041public class ConversionWork extends TransientStoreWork { 042 043 private static final long serialVersionUID = 14593653977944460L; 044 045 protected String converterName; 046 047 protected String destinationMimeType; 048 049 protected Map<String, Serializable> parameters; 050 051 protected String inputEntryKey; 052 053 /** 054 * @since 10.1 055 */ 056 protected ManagedBlob managedBlob; 057 058 public ConversionWork(String converterName, String destinationMimeType, BlobHolder blobHolder, 059 Map<String, Serializable> parameters) { 060 if (converterName == null && destinationMimeType == null) { 061 throw new IllegalArgumentException("'convertName' or 'destinationMimeType' must not be null"); 062 } 063 this.converterName = converterName; 064 this.destinationMimeType = destinationMimeType; 065 this.parameters = parameters; 066 if (this.parameters == null) { 067 this.parameters = new HashMap<>(); 068 } 069 070 storeInputBlobHolder(blobHolder); 071 } 072 073 protected void storeInputBlobHolder(BlobHolder blobHolder) { 074 if (!storeManagedBlob(blobHolder)) { 075 // standard conversion 076 inputEntryKey = entryKey + "_input"; 077 putBlobHolder(inputEntryKey, blobHolder); 078 } 079 } 080 081 /** 082 * @since 10.1 083 */ 084 protected boolean storeManagedBlob(BlobHolder blobHolder) { 085 Blob blob = blobHolder.getBlob(); 086 if (!(blob instanceof ManagedBlob) || !(blob instanceof Serializable) || blobHolder.getBlobs().size() > 1) { 087 return false; 088 } 089 090 ManagedBlob mBlob = (ManagedBlob) blob; 091 BlobProvider blobProvider = Framework.getService(BlobManager.class).getBlobProvider(mBlob); 092 if (blobProvider.canConvert(mBlob, destinationMimeType)) { 093 this.managedBlob = mBlob; 094 return true; 095 } 096 return false; 097 } 098 099 @Override 100 public void work() { 101 setStatus("Converting"); 102 103 BlobHolder inputBlobHolder = retrieveInputBlobHolder(); 104 if (inputBlobHolder == null) { 105 return; 106 } 107 108 ConversionService conversionService = Framework.getService(ConversionService.class); 109 BlobHolder result = converterName != null 110 ? conversionService.convert(converterName, inputBlobHolder, parameters) 111 : conversionService.convertToMimeType(destinationMimeType, inputBlobHolder, parameters); 112 if (result == null) { 113 return; 114 } 115 116 putBlobHolder(result); 117 118 setStatus(null); 119 } 120 121 protected BlobHolder retrieveInputBlobHolder() { 122 return managedBlob != null ? new SimpleBlobHolder(managedBlob) : getBlobHolder(inputEntryKey); 123 } 124 125 @Override 126 public void cleanUp(boolean ok, Exception e) { 127 super.cleanUp(ok, e); 128 if (inputEntryKey != null) { 129 removeBlobHolder(inputEntryKey); 130 } 131 } 132 133 @Override 134 public String getTitle() { 135 if (converterName != null) { 136 return String.format("Conversion using '%s' converter", converterName); 137 } else { 138 return String.format("Conversion using '%s' target mime type", destinationMimeType); 139 } 140 } 141}