Jerome Lefebvre

Localizing the AF Example kits - Part 3 - Reset to Template

Blog Post created by Jerome Lefebvre on Aug 31, 2016

This is an ongoing series introducing the various code examples I and others have created in the goal of localizing the various AF example kits.

Series:

Part 0: URI Builder data reference in AF 2016

Part 1: Localizing the AF Example kits - Part 1 - Exporting a database

Part 2: Localizing the AF Example kits - Part 2 – Fixing analysis mapping after import

 

It is very common practice when working in PSE to make several changes in templates and then going to various element attributes and applying the “reset to template” to template to capture those changes. If you have only a few elements and attributes, this is not a problem. If you have a lot of elements and attributes, then you might learn the more advance technique of doing an attribute search.

 

You can then do a search for all the attributes that you do want to have updated. So, the easiest way to do so is to simply grab everything (with the limit of 1000 increase appropriately)

But, then you typically do not want to update values that might have been manually entered in an attribute, this usually the case for configuration items. Thus, you sort by configuration item and only select all elements that are not configuration items.

This can be error prone and you can end up resetting attributes that you did not wish to reset and lost valuable information.

Thus, it may be better to automate this reset to template feature with some AF SDK. There was already a blog post that detailed this procedure by Han Yong Lee. This code does not diverge to much from that post, but it also grabs child all child attributes.

 

Let say I have one child attribute, then the reset to template method is aptly called: ResetToTemplate.

 

attr.ResetToTemplate();

 

Now, if you want to reset an attribute and all of its child attribute, you can apply this method and loop over all children attributes. As those could also have children attributes, you write a simple recursive method:

 

public static void resetAttribute(AFAttribute attr)
{
 attr.ResetToTemplate();
 foreach(AFAttribute childAttr in attr.Attributes)
        resetAttribute(childAttr);
}

Instead of applying these types of recursive methods, you would be better off using a FindAttribute method. But, as this is not a task that I do frequently or need to be particular quick, I took the liberty of writing the method in this way.

Now, let’s add in the check to see if an attribute is a configuration item that I do not want to update or if it is a PI Point that I want to update.

 

public static void resetAttribute(AFAttribute attr)
{
 if (!attr.IsConfigurationItem)
        attr.ResetToTemplate();
 if (attr.DataReference != null && attr.DataReferencePlugIn.Name == "PI Point")
        attr.DataReference.CreateConfig();
 foreach(AFAttribute childAttr in attr.Attributes)
        resetAttribute(childAttr);
}

Now, it a regular scenario, I would need to be more careful about writing this method. AFAttribute.ResetToTemplate returns a Boolean that holds the success or failure of that operation. It can also raise events such as InvalidOperationException in the case a user and this attribute checked out. The AFDataReference.CreateConfig can raise more errors, such as in the case the specified PI Data Archive can’t be reached. But, for the use of the localization of AF Example kits, those errors do not tend to occur, thus, I have not coded against them.

 

To complete this script, I would then also recursively iterate over all elements in the database to grab all attributes.

 

The full code is found on GitHub: AF-Example-Kit-Utilities/ResetToTemplate at master · JeromeLefebvre/AF-Example-Kit-Utilities · GitHub 

 

This was worked done with Joon Hyung Im

Outcomes