public void Execute(Entity resourceEntity) { if (!resourceEntity.EntityType.Id.Equals(EntityTypeIds.Resource)) { return; } if (resourceEntity.LoadLevel < LoadLevel.DataOnly) { resourceEntity = _inRiverContext.ExtensionManager.DataService.GetEntity(resourceEntity.Id, LoadLevel.DataOnly); } string bynderDownloadState = (string)resourceEntity.GetField(FieldTypeIds.ResourceBynderDownloadState)?.Data; if (string.IsNullOrWhiteSpace(bynderDownloadState) || bynderDownloadState != BynderStates.Todo) { return; } string bynderId = (string)resourceEntity.GetField(FieldTypeIds.ResourceBynderId)?.Data; if (string.IsNullOrWhiteSpace(bynderId)) { return; } // download asset information Asset asset = _bynderClient.GetAssetByAssetId(bynderId); if (asset == null) { _inRiverContext.Log(LogLevel.Error, "Asset information is empty"); return; } // check for existing file var resourceFileId = resourceEntity.GetField(FieldTypeIds.ResourceFileId)?.Data; int existingFileId = resourceFileId != null ? (int)resourceFileId : 0; // add new asset string resourceFileName = (string)resourceEntity.GetField(FieldTypeIds.ResourceFilename)?.Data; int newFileId = _inRiverContext.ExtensionManager.UtilityService.AddFileFromUrl(resourceFileName, _bynderClient.GetAssetDownloadLocation(asset.Id).S3_File); // delete older asset file if (existingFileId > 0) { _inRiverContext.Log(LogLevel.Verbose, $"existing fileId found {existingFileId}"); _inRiverContext.ExtensionManager.UtilityService.DeleteFile(existingFileId); } // set fieltypes for resource entity resourceEntity.GetField(FieldTypeIds.ResourceFileId).Data = newFileId; resourceEntity.GetField(FieldTypeIds.ResourceMimeType).Data = asset.GetOriginalMimeType(); resourceEntity.GetField(FieldTypeIds.ResourceBynderDownloadState).Data = BynderStates.Done; _inRiverContext.ExtensionManager.DataService.UpdateEntity(resourceEntity); _inRiverContext.Logger.Log(LogLevel.Information, $"Updated resource entity {resourceEntity.Id}"); }
public WorkerResult Execute(string bynderAssetId) { var result = new WorkerResult(); // get original filename, as we need to evaluate this for further processing var asset = _bynderClient.GetAssetByAssetId(bynderAssetId); string originalFileName = asset.GetOriginalFileName(); // evaluate filename var evaluatorResult = _fileNameEvaluator.Evaluate(originalFileName); if (!evaluatorResult.IsMatch()) { result.Messages.Add($"Not processing '{originalFileName}'; does not match regex."); return(result); } // find resourceEntity based on bynderAssetId Entity resourceEntity = _inRiverContext.ExtensionManager.DataService.GetEntityByUniqueValue(FieldTypeIds.ResourceBynderId, bynderAssetId, LoadLevel.DataAndLinks); if (resourceEntity == null) { EntityType resourceType = _inRiverContext.ExtensionManager.ModelService.GetEntityType(EntityTypeIds.Resource); resourceEntity = Entity.CreateEntity(resourceType); // add asset id to new ResourceEntity resourceEntity.GetField(FieldTypeIds.ResourceBynderId).Data = bynderAssetId; // set filename (only for *new* resource) resourceEntity.GetField(FieldTypeIds.ResourceFilename).Data = $"{bynderAssetId}_{asset.GetOriginalFileName()}"; } // status for new and existing ResourceEntity resourceEntity.GetField(FieldTypeIds.ResourceBynderDownloadState).Data = BynderStates.Todo; // resource fields from regular expression created from filename foreach (var keyValuePair in evaluatorResult.GetResourceDataInFilename()) { resourceEntity.GetField(keyValuePair.Key.Id).Data = keyValuePair.Value; } // save IdHash for re-creation of public CDN Urls in inRiver resourceEntity.GetField(FieldTypeIds.ResourceBynderIdHash).Data = asset.IdHash; var resultString = new StringBuilder(); if (resourceEntity.Id == 0) { resourceEntity = _inRiverContext.ExtensionManager.DataService.AddEntity(resourceEntity); resultString.Append($"Resource {resourceEntity.Id} added"); } else { resourceEntity = _inRiverContext.ExtensionManager.DataService.UpdateEntity(resourceEntity); resultString.Append($"Resource {resourceEntity.Id} updated"); } // get related entity data found in filename so we can create or update link to these entities // all found field/values are supposed to be unique fields in the correspondent entitytype // get all *inbound* linktypes towards the Resource entitytype in the model (e.g. ProductResource, ItemResource NOT ResourceOtherEntity) var inboundResourceLinkTypes = _inRiverContext.ExtensionManager.ModelService.GetLinkTypesForEntityType(EntityTypeIds.Resource) .Where(lt => lt.TargetEntityTypeId == EntityTypeIds.Resource).OrderBy(lt => lt.Index).ToList(); foreach (var keyValuePair in evaluatorResult.GetRelatedEntityDataInFilename()) { var fieldTypeId = keyValuePair.Key.Id; var value = keyValuePair.Value; // find sourcentity (e.g. Product) var sourceEntity = _inRiverContext.ExtensionManager.DataService.GetEntityByUniqueValue(fieldTypeId, value, LoadLevel.Shallow); if (sourceEntity == null) { continue; } // find linktype in our previously found list var linkType = inboundResourceLinkTypes.FirstOrDefault(lt => lt.SourceEntityTypeId == sourceEntity.EntityType.Id); if (linkType == null) { continue; } if (!_inRiverContext.ExtensionManager.DataService.LinkAlreadyExists(sourceEntity.Id, resourceEntity.Id, null, linkType.Id)) { _inRiverContext.ExtensionManager.DataService.AddLink(new Link() { Source = sourceEntity, Target = resourceEntity, LinkType = linkType }); } resultString.Append($"; {sourceEntity.EntityType.Id} entity {sourceEntity.Id} found and linked"); } result.Messages.Add(resultString.ToString()); return(result); }
/// <summary> /// Main method of the worker /// </summary> /// <param name="bynderAssetId"></param> /// <param name="notificationType"></param> /// <returns></returns> public WorkerResult Execute(string bynderAssetId, NotificationType notificationType) { var result = new WorkerResult(); // get original filename, as we need to evaluate this for further processing var asset = _bynderClient.GetAssetByAssetId(bynderAssetId); // evaluate filename string originalFileName = asset.GetOriginalFileName(); var evaluatorResult = _fileNameEvaluator.Evaluate(originalFileName); if (!evaluatorResult.IsMatch()) { result.Messages.Add($"Not processing '{originalFileName}'; does not match regex."); return(result); } // evaluate conditions if (!AssetAppliesToConditions(asset)) { _inRiverContext.Log(LogLevel.Debug, $"Asset {bynderAssetId} does not apply to the conditions"); result.Messages.Add($"Not processing '{originalFileName}'; does not apply to import conditions."); return(result); } _inRiverContext.Log(LogLevel.Debug, $"Asset {asset.Id} applies to conditions."); // find resourceEntity based on bynderAssetId Entity resourceEntity = _inRiverContext.ExtensionManager.DataService.GetEntityByUniqueValue(FieldTypeIds.ResourceBynderId, bynderAssetId, LoadLevel.DataAndLinks); // handle notification logic switch (notificationType) { case NotificationType.DataUpsert: return(CreateOrUpdateEntityAndRelations(result, asset, evaluatorResult, resourceEntity)); case NotificationType.MetadataUpdated: if (resourceEntity != null) { return(UpdateMetadata(result, asset, resourceEntity)); } else { return(CreateOrUpdateEntityAndRelations(result, asset, evaluatorResult, resourceEntity)); } case NotificationType.IsArchived: if (resourceEntity != null) { return(SetValuesOnResource(result, bynderAssetId, resourceEntity)); } else { _inRiverContext.Log(LogLevel.Debug, $"Archived asset {bynderAssetId}, does not exist in inRiver as Resource."); result.Messages.Add($"Archived asset {bynderAssetId}, does not exist in inRiver as Resource."); return(result); } default: _inRiverContext.Log(LogLevel.Warning, $"Notification type {notificationType} is not implemented yet! This notification will not be processed for asset {bynderAssetId}."); result.Messages.Add($"Notification type {notificationType} is not implemented yet! This notification will not be processed for asset {bynderAssetId}."); return(result); } }