Skip to main content

A DD4T.net Implementation - Taxonomy Factory

I have noticed that in all my recent projects, there is a strong need for an easy and well performing way of handling taxonomies. Functionality ranges from being able to read a Keyword in a taxonomy, to traverse the child/parent relationships, to read metadata on a Keyword and also to read related Components tagged against a Keyword.

In order to provide these features, I implemented a DD4T factory that deals with Tridion taxonomies. Namely, I created the ITaxonomyFactory that defines certain method signatures. I'm using Ninject to bind the interface to an actual implementing class, so using this in DD4T context is trivial.

public interface ITaxonomyFactory
{
    MyKeyword GetTaxonomy(string taxonomyXmlNameOrUri);
    MyKeyword GetKeywordByUri(MyKeyword root, string uri);
}

The factory class uses the TaxonomyFactory and TaxonomyRelationManager to interact with the data published to the Content Delivery database.

Method GetTaxonomy takes either a Taxonomy XML name, as defined in Tridion CMS (which is resolved using the labels mechanism described in post Everything's a Label) or TcmUri of a Tridion Category.

The method uses the CacheWrapper described in post A Simple TTL Cache, in order to store either a root Keyword object or a null value for a given amount of time.

The returned value, MyKeyword, is a specialization object of DD4T.ContentModel.IKeyword, which will be described in a follow-up post.

public class MyTaxonomyFactory : ITaxonomyFactory
{
    [Inject]
    public virtual ICacheWrapper CacheWrapper { get; set; }

    private TaxonomyFactory taxonomyFactory;
    private TaxonomyRelationManager manager;

    public MyTaxonomyFactory()
    {
        taxonomyFactory = new TaxonomyFactory();
        manager = new TaxonomyRelationManager();
    }

    public MyKeyword GetTaxonomy(string taxonomyXmlNameOrUri)
    {
        MyKeyword result;
        object cacheElement;
        string taxonomyUri = DD4TResourceProvider.GetTaxonomyLabel(taxonomyXmlNameOrUri);
        string key = "category" + taxonomyUri;

        if (CacheWrapper.TryGet(key, out cacheElement))
        {
            result = cacheElement as MyKeyword;
        }
        else
        {
            tridion.Keyword tridionKeyword = taxonomyFactory.GetTaxonomyKeywords(taxonomyUri);
            if (tridionKeyword == null)
            {
                result = null;
                CacheWrapper.Insert(key, false, 1);
            }
            else
            {
                TaxonomyConverter taxonomyConverter = new TaxonomyConverter();
                result = taxonomyConverter.ConvertToDD4T(tridionKeyword);
                CacheWrapper.Insert(key, result, 60);
            }
        }

        return result;
    }

Method GetKeywordByUri is a simple traversal method starting from a keyword in the taxonomy and searching depth-first for a keyword whose TcmUri matches a TcmUri we look for. The implementation makes use of a fancy delegate method, through which we can easily supply different comparison logic. The KeywordComparer returns a boolean result from comparing two Keywords. In the GetKeywordByUri method, we simply provide a comparer that only checks the keywords TcmUri. This mechanism is very flexible and easy to extend for other types of comparisons, for example by keyword title, key or even some custom meta field.

private delegate bool KeywordComparer(MyKeyword keyword);

public MyKeyword GetKeywordByUri(MyKeyword root, string uri)
{
    return SearchKeyword(root, delegate(MyKeyword keyword)
    {
        return keyword != null && keyword.Id.Equals(uri);
    });
}

private MyKeyword SearchKeyword(MyKeyword root, KeywordComparer keywordComparer)
{
    if (keywordComparer(root))
    {
        return root;
    }

    foreach (MyKeyword childKeyword in root.ChildKeywords)
    {
        MyKeyword result = SearchKeyword(childKeyword, keywordComparer);
        if (result != null)
        {
            return result;
        }
    }

    return null;
}

As an example, the following code snippet reads a taxonomy and a given keyword in it:

    ITaxonomyFactory factory = 
            DependencyResolver.Current.GetService<ITaxonomyFactory>();
    MyKeyword root = factory.GetTaxonomy("Product-Catalog");
    MyKeyword product = factory.GetKeywordByUri(root, "tcm:123-456-1024");


Comments

Popular posts from this blog

Running sp_updatestats on AWS RDS database

Part of the maintenance tasks that I perform on a MSSQL Content Manager database is to run stored procedure sp_updatestats . exec sp_updatestats However, that is not supported on an AWS RDS instance. The error message below indicates that only the sa  account can perform this: Msg 15247 , Level 16 , State 1 , Procedure sp_updatestats, Line 15 [Batch Start Line 0 ] User does not have permission to perform this action. Instead there are several posts that suggest using UPDATE STATISTICS instead: https://dba.stackexchange.com/questions/145982/sp-updatestats-vs-update-statistics I stumbled upon the following post from 2008 (!!!), https://social.msdn.microsoft.com/Forums/sqlserver/en-US/186e3db0-fe37-4c31-b017-8e7c24d19697/spupdatestats-fails-to-run-with-permission-error-under-dbopriveleged-user , which describes a way to wrap the call to sp_updatestats and execute it under a different user: create procedure dbo.sp_updstats with execute as 'dbo' as

Content Delivery Monitoring in AWS with CloudWatch

This post describes a way of monitoring a Tridion 9 combined Deployer by sending the health checks into a custom metric in CloudWatch in AWS. The same approach can also be used for other Content Delivery services. Once the metric is available in CloudWatch, we can create alarms in case the service errors out or becomes unresponsive. The overall architecture is as follows: Content Delivery service sends heartbeat (or exposes HTTP endpoint) for monitoring Monitoring Agent checks heartbeat (or HTTP health check) regularly and stores health state AWS lambda function: runs regularly reads the health state from Monitoring Agent pushes custom metrics into CloudWatch I am running the Deployer ( installation docs ) and Monitoring Agent ( installation docs ) on a t2.medium EC2 instance running CentOS on which I also installed the Systems Manager Agent (SSM Agent) ( installation docs ). In my case I have a combined Deployer that I want to monitor. This consists of an Endpoint and a

Debugging a Tridion 2011 Event System

OK, so you wrote your Tridion Event System. Now it's time to debug it. I know this is a hypothetical situtation -- your code never needs any kind of debugging ;) but indulge me... Recently, Alvin Reyes ( @nivlong ) blogged about being difficult to know how exactly to debug a Tridion Event System. More exactly, the question was " What process do I attach to for debugging even system code? ". Unfortunately, there is no simple or generic answer for it. Different events are fired by different Tridion CM modules. These modules run as different programs (or services) or run inside other programs (e.g. IIS). This means that you will need to monitor (or debug) different processes, based on which events your code handles. So the usual suspects are: dllhost.exe (or dllhost3g.exe ) - running as the MTSUser is the SDL Tridion Content Manager COM+ application and it fires events on generic TOM objects (e.g. events based on Tridion.ContentManager.Extensibility.Events.CrudEven