Ejemplo n.º 1
0
        private async Task <AssetReady> Convert(ITransformAsset message)
        {
            var currentStatus = AufbereitungsStatusEnum.PaketTransferiert;
            var id            = message.AssetType == AssetType.Gebrauchskopie ? message.ArchiveRecordId : message.OrderItemId.ToString();
            var assetReady    = new AssetReady
            {
                ArchiveRecordId       = message.ArchiveRecordId,
                OrderItemId           = message.OrderItemId,
                CallerId              = message.CallerId,
                AssetType             = message.AssetType,
                Recipient             = message.Recipient,
                RetentionCategory     = message.RetentionCategory,
                PrimaerdatenAuftragId = message.PrimaerdatenAuftragId
            };

            try
            {
                // Let's convert the package
                var result = await assetManager.ConvertPackage(id, message.AssetType,
                                                               message.ProtectWithPassword, message.FileName, message.PackageId);

                currentStatus = AufbereitungsStatusEnum.AssetUmwandlungAbgeschlossen;
                await UpdatePrimaerdatenAuftragStatus(message, AufbereitungsServices.AssetService, currentStatus, result.ErrorMessage);

                if (!result.Valid)
                {
                    // We have failed in the conversion inform the world about the failed package
                    assetReady.Valid        = false;
                    assetReady.ErrorMessage = result.ErrorMessage;
                    return(assetReady);
                }

                // Upload to cache and then delete the package
                var uploadSuccess = await cacheHelper.SaveToCache(bus, message.RetentionCategory, result.FileName);

                var errorMessage = uploadSuccess ? null : $"Unable to upload file {result.FileName} to cache";
                currentStatus = AufbereitungsStatusEnum.ImCacheAbgelegt;
                await UpdatePrimaerdatenAuftragStatus(message, AufbereitungsServices.CacheService, currentStatus, errorMessage);

                File.Delete(result.FileName);

                if (!uploadSuccess)
                {
                    assetReady.Valid        = false;
                    assetReady.ErrorMessage = errorMessage;
                    return(assetReady);
                }

                assetReady.Valid = true;
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Unexpected error in the conversion of the package for ID {id} and AssetType {AssetType}.", id, message.AssetType);
                assetReady.Valid        = false;
                assetReady.ErrorMessage = $"Unexpected exception occured.\nException:\n{ex}";
                await UpdatePrimaerdatenAuftragStatus(message, AufbereitungsServices.CacheService, currentStatus, ex.Message);
            }

            return(assetReady);
        }