private void Delete(Entity channelEntity, Entity deletedEntity)
        {
            switch (deletedEntity.EntityType.Id)
            {
            case "Resource":
                Guid resourceGuid = EpiserverEntryIdentifier.EntityIdToGuid(deletedEntity.Id);
                _epiApi.DeleteResource(resourceGuid);

                break;

            case "Channel":
                _epiApi.DeleteCatalog(deletedEntity.Id);
                break;

            case "ChannelNode":
                _epiApi.DeleteCatalogNode(deletedEntity, channelEntity.Id);
                break;

            case "Item":
                if ((_config.ItemsToSkus && _config.UseThreeLevelsInCommerce) || !_config.ItemsToSkus)
                {
                    _epiApi.DeleteCatalogEntry(deletedEntity);
                }

                if (_config.ItemsToSkus)
                {
                    var entitiesToDelete = new List <string>();

                    List <XElement> skuElements = _catalogElementFactory.GenerateSkuItemElemetsFromItem(deletedEntity);

                    foreach (XElement sku in skuElements)
                    {
                        XElement skuCodElement = sku.Element("Code");
                        if (skuCodElement != null)
                        {
                            entitiesToDelete.Add(skuCodElement.Value);
                        }
                    }

                    _epiApi.DeleteSkus(entitiesToDelete);
                }

                break;

            default:
                _epiApi.DeleteCatalogEntry(deletedEntity);
                break;
            }
        }
示例#2
0
        private void DeleteEntity(Entity channelEntity, int parentEntityId, Entity targetEntity, string linkTypeId, string channelIdentifier, string folderDateTime, List <int> productParentIds = null)
        {
            var channelHelper = new ChannelHelper(_context);

            XElement removedElement = new XElement(targetEntity.EntityType.Id + "_" + targetEntity.Id);

            List <XElement> deletedElements = new List <XElement>();


            deletedElements.Add(removedElement);

            XDocument deleteXml    = new XDocument(new XElement("xml", new XAttribute("action", "deleted")));
            Entity    parentEntity = _context.ExtensionManager.DataService.GetEntity(parentEntityId, LoadLevel.DataOnly);

            if (parentEntity != null && !DeleteUtilConfig.ChannelEntities.ContainsKey(parentEntity.Id))
            {
                DeleteUtilConfig.ChannelEntities.Add(parentEntity.Id, parentEntity);
            }

            List <XElement> parentElements = channelHelper.GetParentXElements(parentEntity, DeleteUtilConfig);

            foreach (var parentElement in parentElements)
            {
                deleteXml.Root?.Add(parentElement);
            }

            deletedElements = deletedElements.GroupBy(elem => elem.Name.LocalName).Select(grp => grp.First()).ToList();

            var resources = new Resources(_context);

            foreach (XElement deletedElement in deletedElements)
            {
                if (!deletedElement.Name.LocalName.Contains('_'))
                {
                    continue;
                }

                string deletedElementEntityType = deletedElement.Name.LocalName.Split('_')[0];
                int    deletedElementEntityId;
                int.TryParse(deletedElement.Name.LocalName.Split('_')[1], out deletedElementEntityId);

                if (deletedElementEntityType == "Link")
                {
                    continue;
                }

                List <string> deletedResources = new List <string>();

                switch (deletedElementEntityType)
                {
                case "Channel":
                    _epiApi.DeleteCatalog(deletedElementEntityId, DeleteUtilConfig);
                    deletedResources = channelHelper.GetResourceIds(deletedElement, DeleteUtilConfig);
                    break;

                case "ChannelNode":
                    _epiApi.DeleteCatalogNode(deletedElementEntityId, channelEntity.Id, DeleteUtilConfig);

                    deleteXml.Root?.Add(new XElement("entry", _channelPrefixHelper.GetEPiCodeWithChannelPrefix(deletedElementEntityId, DeleteUtilConfig)));

                    Entity channelNode = targetEntity.Id == deletedElementEntityId
                                                 ? targetEntity
                                                 : _context.ExtensionManager.DataService.GetEntity(
                        deletedElementEntityId,
                        LoadLevel.DataAndLinks);

                    if (channelNode == null)
                    {
                        break;
                    }

                    if (deletedElement.Elements().Any())
                    {
                        foreach (XElement linkElement in deletedElement.Elements())
                        {
                            foreach (XElement entityElement in linkElement.Elements())
                            {
                                string elementEntityId = entityElement.Name.LocalName.Split('_')[1];

                                Entity child = _context.ExtensionManager.DataService.GetEntity(int.Parse(elementEntityId), LoadLevel.DataAndLinks);
                                Delete(channelEntity, targetEntity.Id, child, linkTypeId);
                            }
                        }
                    }
                    else
                    {
                        foreach (Link link in targetEntity.OutboundLinks)
                        {
                            Entity child = _context.ExtensionManager.DataService.GetEntity(link.Target.Id, LoadLevel.DataAndLinks);

                            Delete(channelEntity, targetEntity.Id, child, link.LinkType.Id);
                        }
                    }

                    deletedResources = channelHelper.GetResourceIds(deletedElement, DeleteUtilConfig);
                    break;

                case "Item":
                    deletedResources = channelHelper.GetResourceIds(deletedElement, DeleteUtilConfig);
                    if ((DeleteUtilConfig.ItemsToSkus && DeleteUtilConfig.UseThreeLevelsInCommerce) || !DeleteUtilConfig.ItemsToSkus)
                    {
                        _epiApi.DeleteCatalogEntry(deletedElementEntityId.ToString(CultureInfo.InvariantCulture), DeleteUtilConfig);

                        deleteXml.Root?.Add(new XElement("entry", _channelPrefixHelper.GetEPiCodeWithChannelPrefix(deletedElementEntityId, DeleteUtilConfig)));
                    }

                    if (DeleteUtilConfig.ItemsToSkus)
                    {
                        // delete skus if exist
                        List <string> entitiesToDelete = new List <string>();

                        Entity deletedEntity = null;

                        try
                        {
                            deletedEntity = _context.ExtensionManager.DataService.GetEntity(
                                deletedElementEntityId,
                                LoadLevel.DataOnly);
                        }
                        catch (Exception ex)
                        {
                            _context.Log(LogLevel.Warning, "Error when getting entity:" + ex);
                        }

                        if (deletedEntity != null)
                        {
                            List <XElement> skus = _epiElement.GenerateSkuItemElemetsFromItem(deletedEntity, DeleteUtilConfig);

                            foreach (XElement sku in skus)
                            {
                                XElement skuCodElement = sku.Element("Code");
                                if (skuCodElement != null)
                                {
                                    entitiesToDelete.Add(skuCodElement.Value);
                                }
                            }
                        }

                        foreach (string entityIdToDelete in entitiesToDelete)
                        {
                            _epiApi.DeleteCatalogEntry(entityIdToDelete, DeleteUtilConfig);

                            deleteXml.Root?.Add(new XElement("entry", _channelPrefixHelper.GetEPiCodeWithChannelPrefix(entityIdToDelete, DeleteUtilConfig)));
                        }
                    }

                    break;

                case "Resource":
                    deletedResources = new List <string> {
                        _channelPrefixHelper.GetEPiCodeWithChannelPrefix(deletedElementEntityId, DeleteUtilConfig)
                    };
                    break;

                case "Product":
                    _epiApi.DeleteCatalogEntry(deletedElementEntityId.ToString(CultureInfo.InvariantCulture), DeleteUtilConfig);
                    deletedResources = channelHelper.GetResourceIds(deletedElement, DeleteUtilConfig);

                    deleteXml.Root?.Add(new XElement("entry", _channelPrefixHelper.GetEPiCodeWithChannelPrefix(deletedElementEntityId, DeleteUtilConfig)));

                    Entity delEntity = _context.ExtensionManager.DataService.GetEntity(
                        deletedElementEntityId,
                        LoadLevel.DataAndLinks);

                    if (delEntity == null)
                    {
                        break;
                    }

                    foreach (Link link in delEntity.OutboundLinks)
                    {
                        if (link.Target.EntityType.Id == "Product")
                        {
                            if (productParentIds != null && productParentIds.Contains(link.Target.Id))
                            {
                                _context.Log(LogLevel.Information, string.Format("Entity with id {0} has already been deleted, break the chain to avoid circular relations behaviors (deadlocks)", link.Target.Id));
                                continue;
                            }

                            if (productParentIds == null)
                            {
                                productParentIds = new List <int>();
                            }

                            productParentIds.Add(delEntity.Id);
                        }

                        Entity child = _context.ExtensionManager.DataService.GetEntity(link.Target.Id, LoadLevel.DataAndLinks);

                        Delete(channelEntity, delEntity.Id, child, link.LinkType.Id, productParentIds);
                    }

                    break;

                default:

                    _epiApi.DeleteCatalogEntry(deletedElementEntityId.ToString(CultureInfo.InvariantCulture), DeleteUtilConfig);
                    deletedResources = channelHelper.GetResourceIds(deletedElement, DeleteUtilConfig);

                    deleteXml.Root?.Add(new XElement("entry", _channelPrefixHelper.GetEPiCodeWithChannelPrefix(deletedElementEntityId, DeleteUtilConfig)));

                    Entity prodEntity;
                    if (targetEntity.Id == deletedElementEntityId)
                    {
                        prodEntity = targetEntity;
                    }
                    else
                    {
                        prodEntity = _context.ExtensionManager.DataService.GetEntity(
                            deletedElementEntityId,
                            LoadLevel.DataAndLinks);
                    }

                    if (prodEntity == null)
                    {
                        break;
                    }

                    foreach (Link link in prodEntity.OutboundLinks)
                    {
                        if (link.Target.EntityType.Id == "Product")
                        {
                            if (productParentIds != null && productParentIds.Contains(link.Target.Id))
                            {
                                _context.Log(LogLevel.Information, string.Format("Entity with id {0} has already been deleted, break the chain to avoid circular relations behaviors (deadlocks)", link.Target.Id));
                                continue;
                            }

                            if (productParentIds == null)
                            {
                                productParentIds = new List <int>();
                            }

                            productParentIds.Add(prodEntity.Id);
                        }

                        Entity child = _context.ExtensionManager.DataService.GetEntity(link.Target.Id, LoadLevel.DataAndLinks);

                        Delete(channelEntity, parentEntityId, child, link.LinkType.Id);
                    }

                    break;
                }

                foreach (string resourceId in deletedResources)
                {
                    string resourceIdWithoutPrefix = resourceId.Substring(DeleteUtilConfig.ChannelIdPrefix.Length);

                    int resourceIdAsInt;

                    if (Int32.TryParse(resourceIdWithoutPrefix, out resourceIdAsInt))
                    {
                        if (_context.ExtensionManager.ChannelService.EntityExistsInChannel(channelEntity.Id, resourceIdAsInt))
                        {
                            deletedResources.Remove(resourceId);
                        }
                    }
                }

                if (deletedResources != null && deletedResources.Count != 0)
                {
                    XDocument resDoc          = resources.HandleResourceDelete(deletedResources);
                    string    folderDateTime2 = DateTime.Now.ToString("yyyyMMdd-HHmmss.fff");

                    string zipFileDelete = string.Format(
                        "resource_{0}{1}.zip",
                        folderDateTime2,
                        deletedElementEntityId);

                    DocumentFileHelper.ZipDocumentAndUploadToAzure(XmlDocumentType.Resources, resDoc, DeleteUtilConfig, folderDateTime2);
                    string zipDeleteFileNameInCloud = DeleteUtilConfig.ResourceNameInCloud;

                    foreach (string resourceIdString in deletedResources)
                    {
                        int    resourceId               = int.Parse(resourceIdString);
                        bool   sendUnlinkResource       = false;
                        string zipFileUnlink            = string.Empty;
                        string zipUnlinkFileNameInCloud = string.Empty;

                        Entity resource = _context.ExtensionManager.DataService.GetEntity(resourceId, LoadLevel.DataOnly);
                        if (resource != null)
                        {
                            // Only do this when removing an link (unlink)
                            Entity parentEnt = _context.ExtensionManager.DataService.GetEntity(parentEntityId, LoadLevel.DataOnly);
                            var    unlinkDoc = resources.HandleResourceUnlink(resource, parentEnt, DeleteUtilConfig);

                            zipFileUnlink = string.Format("resource_{0}{1}.zip", folderDateTime, deletedElementEntityId);
                            DocumentFileHelper.ZipDocumentAndUploadToAzure(XmlDocumentType.Resources, unlinkDoc, DeleteUtilConfig, folderDateTime);
                            zipUnlinkFileNameInCloud = DeleteUtilConfig.ResourceNameInCloud;
                        }

                        _context.Log(LogLevel.Debug, "Resources saved!");

                        if (DeleteUtilConfig.ActivePublicationMode.Equals(PublicationMode.Automatic))
                        {
                            _context.Log(LogLevel.Debug, "Starting automatic import!");

                            if (sendUnlinkResource && _epiApi.StartAssetImportIntoEpiServerCommerce(
                                    zipUnlinkFileNameInCloud /*Path.Combine(this.DeleteUtilConfig.ResourcesRootPath, folderDateTime, "Resources.xml")*/,
                                    Path.Combine(DeleteUtilConfig.ResourcesRootPath, folderDateTime),
                                    DeleteUtilConfig))
                            {
                                _epiApi.SendHttpPost(DeleteUtilConfig, Path.Combine(DeleteUtilConfig.ResourcesRootPath, folderDateTime, zipFileUnlink));
                            }

                            if (_epiApi.StartAssetImportIntoEpiServerCommerce(
                                    zipDeleteFileNameInCloud /*Path.Combine(this.DeleteUtilConfig.ResourcesRootPath, folderDateTime2, "Resources.xml")*/,
                                    Path.Combine(DeleteUtilConfig.ResourcesRootPath, folderDateTime2),
                                    DeleteUtilConfig))
                            {
                                _epiApi.SendHttpPost(DeleteUtilConfig, Path.Combine(DeleteUtilConfig.ResourcesRootPath, folderDateTime2, zipFileDelete));
                            }
                        }
                    }
                }
            }

            if (deleteXml.Root != null && deleteXml.Root.Elements().FirstOrDefault(e => e.Name.LocalName == "entry") != null)
            {
                if (!DocumentFileHelper.ZipDocumentAndUploadToAzure(XmlDocumentType.Catalog, deleteXml, DeleteUtilConfig, folderDateTime))
                {
                    _context.Log(LogLevel.Information, "Failed to zip and upload the catalog file to azure from delete utility DeleteEntity() method");
                }

                _context.Log(LogLevel.Debug, "catalog saved");
            }
        }