This week's TBB is Read Configuration Items TBB. It is a very generic TBB that reads values from a multi-value Name-Value-Pair embeddable field and creates Package items for each pair. The TBB does a bit more, for example localizes a TcmUri to the current Publication context and pushes also the Publication properties PublicationPath and PublicationUrl.
Where the GenericUtils methods are:
Schemas
Global Configuration Schema
Name
|
Global Configuration
|
||
Description
|
Use as container of Name-Value pairs
|
||
Root Element Name
|
GlobalConfiguration
|
||
Type
|
System
|
||
Field Name
|
Type
|
Mandatory
|
Properties
|
SystemValues
|
Embedded
|
No
|
Multi-value,
Schema “Name-Value Pair”
|
Name-Value Pair Schema
Name
|
Name-Value Pair
|
||
Description
|
Used to hold Name/Value
configuration pairs
|
||
Root Element Name
|
NameValuePair
|
||
Type
|
System
|
||
Field Name
|
Type
|
Mandatory
|
Properties
|
Name
|
Text
|
Yes
|
Single
line, no-repeat
|
Value
|
Text
|
No
|
Single
line, no-repeat
|
Read Configuration Items Parameter Schema
Name
|
Read Configuration Items
Parameter Schema
|
||
Description
|
Read Configuration Items
Parameter Schema
|
||
Type
|
Parameter Schema
|
||
Field Name
|
Type
|
Mandatory
|
Properties
|
ConfigurationComponentFieldName
|
Text
|
No
|
Read Configuration Items TBB
Name
|
Read Configuration Items TBB
|
Type
|
· Template in .NET Assembly
|
Description
|
Used to:
· Read the configuration Component from the
Publication metadata;
· Create package items for each found
configuration name/value pair;
Notes:
This is a very generic TBB that reads all fields from the Configuration Component.
The logic reads the field name “ConfigurationComponentFieldName”
from the TBB Parameter. It reads then the Configuration Component linked from the
Publication metadata field specified by “ConfigurationComponentFieldName”.
From the Configuration Component, read all fields and
create package items for each field. Name of package item is the name of the
field in the Component (sanitized to remove the whitespace) and the value is
the value of the field. It uses the Name/Value Pair Schema to read and create
all values from the System Values Configuration Component.
|
Parameters
|
Uses Parameter Schema “Read
Configuration Items Parameter Schema” to read the field name from the
Publication metadata Schema that contains the Component Link to the
Configuration Component
|
Applicable to
|
Component Templates and Page
Templates
|
The Code
[TcmTemplateTitle("Read Configuration Items TBB")]
public class ReadConfigurationItems
: ITemplate {
TemplatingLogger log = TemplatingLogger.GetLogger(typeof(ReadConfigurationItems));
public void
Transform(Engine engine, Package package) {
GenericUtils
utils = new GenericUtils();
Publication
publication = utils.GetContextPublication(engine, package);
if
(publication.Metadata == null ||
publication.MetadataSchema == null) {
log.Debug(string.Format("Publication '{0}' does not have a Metadata
Schema", publication.Title));
return;
}
ItemFields
pubMetaFields = new ItemFields(publication.Metadata,
publication.MetadataSchema);
ComponentLinkField
configLinkField = pubMetaFields["Configuration"]
as ComponentLinkField;
foreach
(Component configComponent in configLinkField.Values) {
log.Debug("Identified
Configuration Component " + configComponent.WebDavUrl);
CreateItemsFromConfiguration(configComponent, package, publication);
}
AddPublicationUrlPath(package,
publication);
}
private void
AddPublicationUrlPath(Package package, Publication publication) {
string
path = publication.PublicationPath;
if
(path.EndsWith("/") ||
path.EndsWith("\\")) {
path = path.Substring(0,
path.Length - 1);
}
package.PushItem("PublicationPath",
package.CreateStringItem(ContentType.Text,
path));
path = publication.PublicationUrl;
if
(path.EndsWith("/") ||
path.EndsWith("\\")) {
path = path.Substring(0,
path.Length - 1);
}
package.PushItem("PublicationUrl",
package.CreateStringItem(ContentType.Text,
path));
}
private void
CreateItemsFromConfiguration(Component
component, Package package, Publication publication) {
GenericUtils
utils = new GenericUtils();
ItemFields
fields = new ItemFields(component.Content,
component.Schema);
foreach
(ItemField field in
fields) {
if
(field is ComponentLinkField)
{
string
name = Regex.Replace(field.Name, "\\W", string.Empty);
Component
value = ((ComponentLinkField)field).Value;
Item
item = package.GetByName(name);
if
(item == null) {
log.Debug(string.Format("Add
Component {0} = {1} to package", name, value.Id));
package.PushItem(name,
package.CreateTridionItem(ContentType.Component,
value));
} else
{
log.Debug(string.Format("Update Component {0} = {1} in package",
name, value.Id));
item.SetAsXmlDocument(value.Content.OwnerDocument);
}
} else
if (field is EmbeddedSchemaField && field.Name == "SystemValues") {
foreach
(ItemFields embeddedFields in ((EmbeddedSchemaField)field).Values)
{
string
name = Regex.Replace(((TextField)embeddedFields["Name"]).Value,
"\\W", string.Empty);
string
value = ((TextField)embeddedFields["Value"]).Value;
value =
utils.LocalizeUri(value, publication.Id.ItemId);
Item
item = package.GetByName(name);
if
(item == null) {
log.Debug(string.Format("Add
{0} = {1} to package", name, value));
package.PushItem(name,
package.CreateStringItem(ContentType.Text,
value));
} else
{
log.Debug(string.Format("Update
{0} = {1} in package", name, value));
item.SetAsString(value);
}
}
}
}
}
}
Where the GenericUtils methods are:
public Publication
GetContextPublication(Engine engine, Package package) {
Item
item = GetPageOrComponentItem(package);
RepositoryLocalObject
respositoryLocalObject = engine.GetObject(item) as
RepositoryLocalObject;
return
respositoryLocalObject.ContextRepository as Publication;
}
public string
LocalizeUri(string tcmUri, int publicationId) {
if (TcmUri.IsValid(tcmUri)) {
TcmUri
uri = new TcmUri(tcmUri);
tcmUri = new
TcmUri(uri.ItemId, uri.ItemType,
publicationId).ToString();
}
return
tcmUri;
}
public string
LocalizeUri(Engine engine, string tcmUri) {
if (TcmUri.IsValid(tcmUri)) {
return
engine.LocalizeUri(new TcmUri(tcmUri));
} else {
return
tcmUri;
}
}
Comments