Skip to main content

A DD4T.net Implementation - Navigation (part 3)

Check out the previous posts about the NavigationFactory: Navigation and Navigation (part 2). I presented a way to deserialize the navigation XML into an object model and then serve it through a factory class that uses caching.

In this post, I will present a simple way of locating a certain node in the navigation object model. Locating a NavigationItem is done by comparing it in some way to a given identifier. The most natural way is to retrieve an item by its TcmUri.

In order to do so, I defined the following method on the INavigationFactory interface:

    NavigationItem GetItemById(Navigation navigation, string tcmUri);

The implementation of this method traverses the Navigation object passed as parameter and applies a Comparator to it, hoping to identify the given tcmUri.

The method calls helper method FindItem that performs the recursive traversal of the Navigation object model.

public NavigationItem GetItemById(Navigation navigation, string pageUri)
{
    if (navigation == null)
    {
        return null;
    }

    IComparator comparator = new UriComparator() { Uri = pageUri };
    foreach (NavigationItem item in navigation.Items)
    {
        NavigationItem result = FindItem(item, comparator);
        if (result != null)
        {
            return result;
        }
    }

    return null;
}

The UriComparator class is an implementation of the custom interface IComparator that compares the given Uri property to the TcmUri property of the NavigationItem we are currently testing.

private interface IComparator
{
    bool Compare(NavigationItem item);
}

private class UriComparator : IComparator
{
    public string Uri { get; set; }

    public bool Compare(NavigationItem item)
    {
        return Uri != null && item != null && Uri.Equals(item.TcmUri);
    }
}

Finally, the FindItem method calls the actual comparator's Compare method and performs the depth-first recursive traversal of the NavigationItem passed to it.

private NavigationItem FindItem(NavigationItem item, IComparator comparator)
{
    if (item == null)
    {
        return null;
    }

    if (comparator.Compare(item))
    {
        return item;
    }

    if (item.ChildItems != null)
    {
        foreach (NavigationItem child in item.ChildItems)
        {
            NavigationItem result = FindItem(child, comparator);
            if (result != null)
            {
                return result;
            }
        }
    }

    return null;
}

As an example, the calling code uses the NavigationFactory.GetItemById() in the following way:

INavigationFactory navigationFactory =
        DependencyResolver.Current.GetService<INavigationFactory>();
NavigationItem item = navigationFactory.GetItemById(Navigation, "tcm:1-2-64");


Comments

Popular posts from this blog

Toolkit - Dynamic Content Queries

This post if part of a series about the  File System Toolkit  - a custom content delivery API for SDL Tridion. This post presents the Dynamic Content Query capability. The requirements for the Toolkit API are that it should be able to provide CustomMeta queries, pagination, and sorting -- all on the file system, without the use third party tools (database, search engines, indexers, etc). Therefore I had to implement a simple database engine and indexer -- which is described in more detail in post Writing My Own Database Engine . The querying logic does not make use of cache. This means the query logic is executed every time. When models are requested, the models are however retrieved using the ModelFactory and those are cached. Query Class This is the main class for dynamic content queries. It is the entry point into the execution logic of a query. The class takes as parameter a Criterion (presented below) which triggers the execution of query in all sub-criteria of a Criterio

A DD4T.net Implementation - Custom Binary Publisher

The default way to publish binaries in DD4T is implemented in class DD4T.Templates.Base.Utils.BinaryPublisher and uses method RenderedItem.AddBinary(Component) . This produces binaries that have their TCM URI as suffix in their filename. In my recent project, we had a requirement that binary file names should be clean (without the TCM URI suffix). Therefore, it was time to modify the way DD4T was publishing binaries. The method in charge with publishing binaries is called PublishItem and is defined in class BinaryPublisher . I therefore extended the BinaryPublisher and overrode method PublishItem. public class CustomBinaryPublisher : BinaryPublisher { private Template currentTemplate; private TcmUri structureGroupUri; In its simplest form, method PublishItem just takes the item and passes it to the AddBinary. In order to accomplish the requirement, we must specify a filename while publishing. This is the file name part of the binary path of Component.BinaryConten

Scaling Policies

This post is part of a bigger topic Autoscaling Publishers in AWS . In a previous post we talked about the Auto Scaling Groups , but we didn't go into details on the Scaling Policies. This is the purpose of this blog post. As defined earlier, the Scaling Policies define the rules according to which the group size is increased or decreased. These rules are based on instance metrics (e.g. CPU), CloudWatch custom metrics, or even CloudWatch alarms and their states and values. We defined a Scaling Policy with Steps, called 'increase_group_size', which is triggered first by the CloudWatch Alarm 'Publish_Alarm' defined earlier. Also depending on the size of the monitored CloudWatch custom metric 'Waiting for Publish', the Scaling Policy with Steps can add a difference number of instances to the group. The scaling policy sets the number of instances in group to 1 if there are between 1000 and 2000 items Waiting for Publish in the queue. It also sets the