/// <summary>
        /// Find and return an existing Category or create a new one
        /// </summary>
        /// <param name="entityData"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        private async Task <SellableItem> GetOrCreateSellableItem(CatalogEntityDataModel entityData, CommercePipelineExecutionContext context)
        {
            SellableItem sellableItem = await _commerceCommander.Command <FindEntityCommand>().Process(context.CommerceContext, typeof(SellableItem), entityData.CommerceEntityId) as SellableItem;

            if (sellableItem == null)
            {
                sellableItem = await _commerceCommander.Command <CreateSellableItemCommand>().Process(context.CommerceContext,
                                                                                                      entityData.EntityId,
                                                                                                      entityData.EntityName,
                                                                                                      entityData.EntityFields.ContainsKey("DisplayName") ? entityData.EntityFields["DisplayName"] : entityData.EntityName,
                                                                                                      entityData.EntityFields.ContainsKey("Description") ? entityData.EntityFields["Description"] : string.Empty,
                                                                                                      entityData.EntityFields.ContainsKey("Brand") ? entityData.EntityFields["Brand"] : string.Empty,
                                                                                                      entityData.EntityFields.ContainsKey("Manufacturer") ? entityData.EntityFields["Manufacturer"] : string.Empty,
                                                                                                      entityData.EntityFields.ContainsKey("TypeOfGoods") ? entityData.EntityFields["TypeOfGoods"] : string.Empty);
            }
            else
            {
                sellableItem.DisplayName  = entityData.EntityFields.ContainsKey("DisplayName") ? entityData.EntityFields["DisplayName"] : entityData.EntityName;
                sellableItem.Description  = entityData.EntityFields.ContainsKey("Description") ? entityData.EntityFields["Description"] : string.Empty;
                sellableItem.Brand        = entityData.EntityFields.ContainsKey("Brand") ? entityData.EntityFields["Brand"] : string.Empty;
                sellableItem.Manufacturer = entityData.EntityFields.ContainsKey("Manufacturer") ? entityData.EntityFields["Manufacturer"] : string.Empty;
                sellableItem.TypeOfGood   = entityData.EntityFields.ContainsKey("TypeOfGoods") ? entityData.EntityFields["TypeOfGoods"] : string.Empty;

                var persistResult = await _commerceCommander.Pipeline <IPersistEntityPipeline>().Run(new PersistEntityArgument(sellableItem), context);
            }

            return(await _commerceCommander.Command <FindEntityCommand>().Process(context.CommerceContext, typeof(SellableItem), sellableItem.Id) as SellableItem);
        }
Beispiel #2
0
        /// <summary>
        /// Main execution point
        /// </summary>
        /// <param name="arg"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public override async Task <ImportCatalogEntityArgument> Run(ImportCatalogEntityArgument arg, CommercePipelineExecutionContext context)
        {
            var mappingPolicy = arg.MappingPolicy;

            var jsonData = arg.Request as JObject;

            Condition.Requires(jsonData, "Commerce Entity JSON parameter is required").IsNotNull();
            context.AddModel(new JsonDataModel(jsonData));

            var entityDataModel = context.GetModel <CatalogEntityDataModel>();
            var entityData      = new CatalogEntityDataModel
            {
                EntityId           = jsonData.SelectValue <string>(mappingPolicy.EntityId),
                EntityName         = jsonData.SelectValue <string>(mappingPolicy.EntityName),
                ParentCatalogName  = jsonData.SelectValue <string>(mappingPolicy.ParentCatalogName),
                ParentCategoryName = jsonData.SelectValue <string>(mappingPolicy.ParentCategoryName),
                EntityFields       = jsonData.SelectMappedValues(mappingPolicy.EntityFieldsPaths),
                ComposerFields     = jsonData.SelectMappedValues(mappingPolicy.ComposerFieldsPaths),
                CustomFields       = jsonData.SelectMappedValues(mappingPolicy.CustomFieldsPaths),
            };

            entityData.EntityFields.AddRange(jsonData.QueryMappedValuesFromRoot(mappingPolicy.EntityFieldsRootPaths));
            entityData.ComposerFields.AddRange(jsonData.QueryMappedValuesFromRoot(mappingPolicy.ComposerFieldsRootPaths));
            entityData.CustomFields.AddRange(jsonData.QueryMappedValuesFromRoot(mappingPolicy.CustomFieldsRootPaths));

            if (string.IsNullOrEmpty(entityData.ParentCatalogName))
            {
                entityData.ParentCatalogName = mappingPolicy.DefaultCatalogName;
            }

            if (string.IsNullOrEmpty(entityData.ParentCategoryName))
            {
                entityData.ParentCategoryName = mappingPolicy.DefaultCategoryName;
            }

            if (arg.CommerceEntityType != null && !string.IsNullOrEmpty(entityData.EntityName))
            {
                if (arg.CommerceEntityType == typeof(Category) && !string.IsNullOrEmpty(entityData.ParentCatalogName))
                {
                    entityData.CommerceEntityId = $"{CommerceEntity.IdPrefix<Category>()}{entityData.ParentCatalogName}-{entityData.EntityName}";
                }
                else if (arg.CommerceEntityType == typeof(SellableItem))
                {
                    entityData.CommerceEntityId = $"{CommerceEntity.IdPrefix<SellableItem>()}{entityData.EntityId}";
                }
            }

            context.AddModel(entityData);

            await Task.CompletedTask;

            return(arg);
        }
Beispiel #3
0
        /// <summary>
        /// Find and return an existing Category or create a new one
        /// </summary>
        /// <param name="entityData"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        private async Task <Category> GetOrCreateCategory(CatalogEntityDataModel entityData, CommercePipelineExecutionContext context)
        {
            Category category = await _commerceCommander.Command <FindEntityCommand>().Process(context.CommerceContext, typeof(Category), entityData.CommerceEntityId) as Category;

            if (category == null)
            {
                category = await _commerceCommander.Command <CreateCategoryCommand>().Process(context.CommerceContext,
                                                                                              entityData.CatalogName,
                                                                                              entityData.EntityName,
                                                                                              entityData.EntityFields.ContainsKey("DisplayName") ? entityData.EntityFields["DisplayName"] : entityData.EntityName,
                                                                                              entityData.EntityFields.ContainsKey("Description") ? entityData.EntityFields["Description"] : string.Empty);
            }
            else
            {
                category.DisplayName = entityData.EntityFields.ContainsKey("DisplayName") ? entityData.EntityFields["DisplayName"] : entityData.EntityName;
                category.Description = entityData.EntityFields.ContainsKey("Description") ? entityData.EntityFields["Description"] : string.Empty;

                var persistResult = await _commerceCommander.Pipeline <IPersistEntityPipeline>().Run(new PersistEntityArgument(category), context);
            }

            return(await _commerceCommander.Command <FindEntityCommand>().Process(context.CommerceContext, typeof(Category), category.Id) as Category);
        }
Beispiel #4
0
        /// <summary>
        /// Main execution point
        /// </summary>
        /// <param name="arg"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public override async Task <ImportCatalogEntityArgument> Run(ImportCatalogEntityArgument arg, CommercePipelineExecutionContext context)
        {
            var mappingConfiguration = arg.MappingConfiguration;

            var jsonData = arg.Entity as JObject;

            Condition.Requires(jsonData, "Commerce Entity JSON parameter is required").IsNotNull();
            context.AddModel(new JsonDataModel(jsonData));

            var entityDataModel = context.GetModel <CatalogEntityDataModel>();

            var rootEntityFields = mappingConfiguration.FieldPaths.Where(s => !arg.RelatedEntities.ContainsKey(s.Key)).ToDictionary(k => k.Key, v => v.Value);

            var entityData = new CatalogEntityDataModel
            {
                EntityId     = jsonData.SelectValue <string>(mappingConfiguration.EntityIdPath),
                EntityName   = jsonData.SelectValue <string>(mappingConfiguration.EntityNamePath),
                CatalogName  = mappingConfiguration.CatalogName,
                EntityFields = jsonData.SelectMappedValues(rootEntityFields),
            };

            var refEntityFields = mappingConfiguration.FieldPaths.Where(s => arg.RelatedEntities.ContainsKey(s.Key)).ToDictionary(k => k.Key, v => v.Value);

            if (refEntityFields != null && refEntityFields.Count > 0 && arg.RelatedEntities != null && arg.RelatedEntities.Count > 0)
            {
                foreach (var key in arg.RelatedEntities.Keys)
                {
                    if (arg.RelatedEntities[key] != null && refEntityFields.ContainsKey(key))
                    {
                        var fieldValues = new List <string>();
                        foreach (var refEntity in arg.RelatedEntities[key])
                        {
                            if (refEntity != null)
                            {
                                var fieldValue = refEntity.SelectValue <string>(refEntityFields[key]);
                                if (fieldValue != null)
                                {
                                    fieldValues.Add(fieldValue);
                                }
                            }
                        }
                        if (fieldValues.Any())
                        {
                            entityData.EntityFields.Add(key, string.Join("|", fieldValues));
                        }
                    }
                }
            }

            if (string.IsNullOrEmpty(entityData.CatalogName))
            {
                entityData.CatalogName = mappingConfiguration.CatalogName;
            }

            entityData.ParentEntityIDs   = new List <string>();
            entityData.ParentEntityNames = new List <string>();
            if (!string.IsNullOrEmpty(mappingConfiguration.ParentEntityIdPath))
            {
                if (arg.RelatedEntities.ContainsKey("ParentEntityIdPath") && arg.RelatedEntities["ParentEntityIdPath"] != null)
                {
                    foreach (var parentEntity in arg.RelatedEntities["ParentEntityIdPath"])
                    {
                        var parentEntityId = parentEntity.SelectValue <string>(mappingConfiguration.ParentEntityIdPath);
                        if (!string.IsNullOrEmpty(parentEntityId) && parentEntityId != entityData.EntityId)
                        {
                            entityData.ParentEntityIDs.Add(parentEntityId);
                        }
                    }
                }
                else
                {
                    var parentEntityId = arg.Entity.SelectValue <string>(mappingConfiguration.ParentEntityIdPath);
                    if (!string.IsNullOrEmpty(parentEntityId) && parentEntityId != entityData.EntityId)
                    {
                        entityData.ParentEntityIDs.Add(parentEntityId);
                    }
                }

                if (arg.RelatedEntities.ContainsKey("ParentEntityNamePath") && arg.RelatedEntities["ParentEntityNamePath"] != null)
                {
                    foreach (var parentEntity in arg.RelatedEntities["ParentEntityNamePath"])
                    {
                        var parentEntityName = parentEntity.SelectValue <string>(mappingConfiguration.ParentEntityNamePath);
                        if (!string.IsNullOrEmpty(parentEntityName) && parentEntityName != entityData.EntityName)
                        {
                            entityData.ParentEntityNames.Add(parentEntityName);
                        }
                    }
                }
                else
                {
                    var parentEntityName = arg.Entity.SelectValue <string>(mappingConfiguration.ParentEntityNamePath);
                    if (!string.IsNullOrEmpty(parentEntityName) && parentEntityName != entityData.EntityName)
                    {
                        entityData.ParentEntityNames.Add(parentEntityName);
                    }
                }
            }

            context.AddModel(entityData);
            await Task.CompletedTask;

            return(arg);
        }