Example #1
0
        /// <summary>
        /// For Publish and UnPublish, remove all items from the website structure Publication from the list.
        /// Website structure Publication URI is read from the config file.
        /// </summary>
        /// <param name="item">Item to be resolved (e.g. a page, structure group, template)</param>
        /// <param name="instruction">Resolve instruction</param>
        /// <param name="context">Publish context</param>
        /// <param name="resolvedItems">List of items that are currently to be rendered and published (added by previous resolvers in the chain)</param>
        public void Resolve(IdentifiableObject item, ResolveInstruction instruction, PublishContext context, Tridion.Collections.ISet <ResolvedItem> resolvedItems)
        {
            List <ResolvedItem> itemsToRemove = new List <ResolvedItem>();
            StringBuilder       infoMessage   = new StringBuilder();

            infoMessage.AppendLine(string.Format("Removed the following items from a {0} Transaction to {1}:", instruction.Purpose, context.PublicationTarget.Title));

            // check for items from website structure publication (these do not need to be published or unpublished)
            foreach (ResolvedItem resolvedItem in resolvedItems)
            {
                // mark all items from website structure publication for removal
                if (resolvedItem.Item.Id.PublicationId == _websiteStructurePublicationUri.ItemId)
                {
                    itemsToRemove.Add(resolvedItem);
                }
            }

            // remove all items that we need to discard
            foreach (ResolvedItem itemToRemove in itemsToRemove)
            {
                infoMessage.AppendLine(string.Format("{0}: {1} ({2})", itemToRemove.Item.Id.ItemType, itemToRemove.Item.Title, itemToRemove.Item.Id));
                resolvedItems.Remove(itemToRemove);
            }
            if (itemsToRemove.Count > 0)
            {
                // log info mesage about which items have been removed (optionally this can be logged as a warning to stand out in the logfile)
                Logger.Write(infoMessage.ToString(), "ChildPublicationsOnlyResolver", LoggingCategory.General, TraceEventType.Information);
            }
        }
        /// <summary>
        /// For Publish and UnPublish, remove all items from the website structure Publication from the list.
        /// Website structure Publication URI is read from the config file.
        /// </summary>
        /// <param name="item">Item to be resolved (e.g. a page, structure group, template)</param>
        /// <param name="instruction">Resolve instruction</param>
        /// <param name="context">Publish context</param>
        /// <param name="resolvedItems">List of items that are currently to be rendered and published (added by previous resolvers in the chain)</param>
        public void Resolve(IdentifiableObject item, ResolveInstruction instruction, PublishContext context, Tridion.Collections.ISet<ResolvedItem> resolvedItems)
        {
            List<ResolvedItem> itemsToRemove = new List<ResolvedItem>();
            StringBuilder infoMessage = new StringBuilder();
            infoMessage.AppendLine(string.Format("Removed the following items from a {0} Transaction to {1}:", instruction.Purpose, context.PublicationTarget.Title));

            // check for items from website structure publication (these do not need to be published or unpublished) 
            foreach (ResolvedItem resolvedItem in resolvedItems)
            {
                // mark all items from website structure publication for removal
                if (resolvedItem.Item.Id.PublicationId == _websiteStructurePublicationUri.ItemId)
                {
                    itemsToRemove.Add(resolvedItem);
                }
            }

            // remove all items that we need to discard
            foreach (ResolvedItem itemToRemove in itemsToRemove)
            {
                infoMessage.AppendLine(string.Format("{0}: {1} ({2})", itemToRemove.Item.Id.ItemType, itemToRemove.Item.Title, itemToRemove.Item.Id));
                resolvedItems.Remove(itemToRemove);
            }
            if (itemsToRemove.Count > 0)
            {
                // log info mesage about which items have been removed (optionally this can be logged as a warning to stand out in the logfile)
                Logger.Write(infoMessage.ToString(), "ChildPublicationsOnlyResolver", LoggingCategory.General, TraceEventType.Information);
            }
        }
        /// <summary>
        /// Implement the resolver function to control the resolving
        /// </summary>
        /// <param name="item">the item published</param>
        /// <param name="instruction">Resolve Instructiuons</param>
        /// <param name="context">Publish Context</param>
        /// <param name="resolvedItems">Collection of Resolved Items</param>
        public void Resolve(IdentifiableObject item, ResolveInstruction instruction, PublishContext context, Tridion.Collections.ISet<ResolvedItem> resolvedItems)
        {
            // Array of schemas which are supposed to publish independently with out resolved items
            string[] lookupSchemas = { "Schema Title1", "Schema Title2" };

            // If published item is a component and it's type belongs to look up schemas array, start the process
            if (item is Component)
            {
                Component comp1 = (Component)item;
                if (lookupSchemas.Contains(comp1.Schema.Title))
                {
                    // Temp collection of resolved items
                    List<ResolvedItem> tempItems = new List<ResolvedItem>();

                    // Loop through resolved items
                    foreach (var resolvedItem in resolvedItems)
                    {
                        // If the resolved item is the published item
                        // Allow it to be published by adding to temp items collection
                        if (resolvedItem.Item is Component)
                        {
                            Component compResolved = (Component)resolvedItem.Item;
                            if (resolvedItem.Item.Id == item.Id)
                            {
                                tempItems.Add(resolvedItem);
                            }
                        }
                    }

                    // Delete all resolved items
                    resolvedItems.Clear();

                    // Add temp items(needs to be published) to resolvedItems collection
                    foreach (ResolvedItem tempResolvedItem in tempItems)
                    {
                        resolvedItems.Add(tempResolvedItem);
                    }

                }
            }
        }
        public void Resolve(IdentifiableObject item, ResolveInstruction instruction, PublishContext context,
                            ISet <ResolvedItem> resolvedItems)
        {
            if (instruction.Purpose != ResolvePurpose.Publish && instruction.Purpose != ResolvePurpose.RePublish)
            {
                return;
            }
            var sourceItem         = (RepositoryLocalObject)item;
            var contextPublication = (Publication)sourceItem.ContextRepository;
            var filter             = new ComponentTemplatesFilter(item.Session)
            {
                AllowedOnPage = false,
                BaseColumns   = ListBaseColumns.IdAndTitle
            };
            const string dataPresentationTemplateTitle = "Generate Data Presentation";
            var          dataPresentationTemplate      = contextPublication.GetComponentTemplates(filter).FirstOrDefault(
                ct => ct.Title == dataPresentationTemplateTitle);
            var resolvedItemList = resolvedItems.ToArray();

            foreach (var resolvedItem in resolvedItemList)
            {
                if (!(resolvedItem.Item is Page))
                {
                    continue;
                }
                var page = (Page)resolvedItem.Item;
                if (page.ComponentPresentations.Count <= 0)
                {
                    continue;
                }
                // for each component lets resolve it with our data presentation template
                foreach (var cp in page.ComponentPresentations.Where(
                             cp =>
                             dataPresentationTemplate != null &&
                             dataPresentationTemplate.RelatedSchemas.Contains(cp.Component.Schema)))
                {
                    resolvedItems.Add(new ResolvedItem(cp.Component, dataPresentationTemplate));
                }
            }
        }
        /// <summary>
        /// Implement the resolver function to control the resolving
        /// </summary>
        /// <param name="item">the item published</param>
        /// <param name="instruction">Resolve Instructiuons</param>
        /// <param name="context">Publish Context</param>
        /// <param name="resolvedItems">Collection of Resolved Items</param>
        public void Resolve(IdentifiableObject item, ResolveInstruction instruction, PublishContext context, Tridion.Collections.ISet <ResolvedItem> resolvedItems)
        {
            // Array of schemas which are supposed to publish independently with out resolved items
            string[] lookupSchemas = { "Schema Title1", "Schema Title2" };

            // If published item is a component and it's type belongs to look up schemas array, start the process
            if (item is Component)
            {
                Component comp1 = (Component)item;
                if (lookupSchemas.Contains(comp1.Schema.Title))
                {
                    // Temp collection of resolved items
                    List <ResolvedItem> tempItems = new List <ResolvedItem>();

                    // Loop through resolved items
                    foreach (var resolvedItem in resolvedItems)
                    {
                        // If the resolved item is the published item
                        // Allow it to be published by adding to temp items collection
                        if (resolvedItem.Item is Component)
                        {
                            Component compResolved = (Component)resolvedItem.Item;
                            if (resolvedItem.Item.Id == item.Id)
                            {
                                tempItems.Add(resolvedItem);
                            }
                        }
                    }

                    // Delete all resolved items
                    resolvedItems.Clear();

                    // Add temp items(needs to be published) to resolvedItems collection
                    foreach (ResolvedItem tempResolvedItem in tempItems)
                    {
                        resolvedItems.Add(tempResolvedItem);
                    }
                }
            }
        }
        /// <summary>
        /// Entry point for resolver.
        /// </summary>
        /// <param name="item">Item to resolve</param>
        /// <param name="instruction">Resolve instruction</param>
        /// <param name="context">Publish context</param>
        /// <param name="resolvedItems">Final items to resolve</param>
        public void Resolve(IdentifiableObject item, ResolveInstruction instruction, PublishContext context,
                            Tridion.Collections.ISet <ResolvedItem> resolvedItems)
        {
            if (instruction.Purpose != ResolvePurpose.Publish && instruction.Purpose != ResolvePurpose.RePublish ||
                _maxRecurseDepth == 0)
            {
                return;
            }
            _log.Debug("DXA Custom Resolver started...");
            _log.Debug("Loading global app data:");
            const string appId = "dxa:CustomResolver";

            try
            {
                var appData = context.Session.SystemManager.LoadGlobalApplicationData(appId);
                if (appData != null)
                {
                    _log.Debug("Found configuration for custom resolver in global app data. Attempting to parse.");
                    string xml = string.Empty;
                    if (appData.TypeId?.StartsWith("XmlElement:") ?? false)
                    {
                        _log.Debug($"Found configuration using XElement appData type.");
                        string      @string     = Encoding.UTF8.GetString(appData.Data);
                        XmlDocument xmlDocument = new XmlDocument();
                        xmlDocument.LoadXml(@string);
                        xml = xmlDocument.DocumentElement?.OuterXml;
                    }
                    if (string.IsNullOrEmpty(xml))
                    {
                        _log.Debug("Assuming string content for xml.");
                        xml = Encoding.Unicode.GetString(appData.Data);
                    }
                    _log.Debug($"xml={xml}");
                    XElement parsedXml = XElement.Parse(xml);
                    foreach (XElement xe in parsedXml.Elements())
                    {
                        if (!xe.Name.LocalName.Equals("RecurseDepth"))
                        {
                            continue;
                        }
                        _log.Debug($"Found RecurseDepth value of '{xe.Value}'.");
                        _maxRecurseDepth = int.Parse(xe.Value);
                        break; // only one setting at moment
                    }
                }
                else
                {
                    _log.Debug(
                        $"Custom resolver configuration not found in global app data for '{appId}'. Using default settings.");
                    _maxRecurseDepth = -1;
                }
            }
            catch (Exception e)
            {
                _log.Debug(
                    $"Exception occured when reading global app data for application id '{appId}'. Using default settings.");
                _log.Debug(e.Message);
                _maxRecurseDepth = -1;
            }

            _log.Debug($"Using _maxRecurseDepth={_maxRecurseDepth}");

            try
            {
                var sourceItem = (RepositoryLocalObject)item;

                _log.Debug($"Analyzing source item of type: {item.GetType().Name} with id: {item.Id} and title: {item.Title}");

                var contextPublication = (Publication)sourceItem.ContextRepository;
                var filter             = new ComponentTemplatesFilter(item.Session)
                {
                    AllowedOnPage = false,
                    BaseColumns   = ListBaseColumns.IdAndTitle
                };
                const string dataPresentationTemplateTitle = "Generate Data Presentation";
                var          dataPresentationTemplate      = contextPublication.GetComponentTemplates(filter).FirstOrDefault(
                    ct => ct.Title == dataPresentationTemplateTitle);
                if (dataPresentationTemplate == null)
                {
                    _log.Debug("No 'Generate Data Presentation' component template found. Skipping custom resolver.");
                    return;
                }
                var       resolved = new HashSet <IdentifiableObject>();
                Stopwatch t        = new Stopwatch();
                t.Start();

                HashSet <TcmUri> alreadyResolved = new HashSet <TcmUri>();
                foreach (var x in resolvedItems)
                {
                    alreadyResolved.Add(x.Item.Id);
                }

                List <ResolvedItem> resolvedItemsCopy = new List <ResolvedItem>(resolvedItems);
                foreach (var resolvedItem in resolvedItemsCopy)
                {
                    foreach (var dataPresentation in ResolveDataPresentations(resolvedItem.Item, dataPresentationTemplate, resolved, 0))
                    {
                        if (alreadyResolved.Contains(dataPresentation.Item.Id))
                        {
                            _log.Debug($"  > Already resolved item '{dataPresentation.Item.Title}' with id: {dataPresentation.Item.Id}");
                            continue;
                        }
                        _log.Debug($"  > Resolved item '{dataPresentation.Item.Title}' with id: {dataPresentation.Item.Id}");
                        alreadyResolved.Add(dataPresentation.Item.Id);
                        resolvedItems.Add(dataPresentation);
                    }
                }

                t.Stop();
                _log.Debug($"DXA Custom Resolver took {t.ElapsedMilliseconds}ms to complete.");
            }
            catch (Exception e)
            {
                _log.Debug($"Exception occured: {e.Message}");
                _log.Debug($"Stacktrace: {e.StackTrace}");
                throw;
            }
        }
Example #7
0
 // This Resolve method executes after the default resolver has done its job and produced a set of resolved items.
 public void Resolve(IdentifiableObject item, ResolveInstruction instruction, PublishContext context, ISet <ResolvedItem> resolvedItems)
 {
     //resolvedItems.Add(...);
 }