Skip to content

Commit

Permalink
SRQ-15639 : Fixed issue with publish mappings and includes
Browse files Browse the repository at this point in the history
  • Loading branch information
majiccode committed Feb 17, 2021
1 parent 86088ff commit 5e6aacf
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 11 deletions.
38 changes: 36 additions & 2 deletions Sdl.Web.Tridion.Templates/Common/TemplateBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public abstract class TemplateBase : ITemplate
"</?tcdl:ComponentPresentation[^>]*>", RegexOptions.Compiled);

private TemplatingLogger _logger;
private InternalLogger _internalLogger;
private Session _session;
private Engine _engine;
private Package _package;
Expand Down Expand Up @@ -74,7 +75,17 @@ protected Package Package
/// </summary>
protected Publication Publication
{
get { return _publication ?? (_publication = GetPublication()); }
get
{
if (_publication == null)
{
InternalLogger.Debug("Getting publication...");
_publication = GetPublication();
InternalLogger.Debug($" Found publication id='{_publication.Id}', title='{Publication.Title}', webDavUrl='{Publication.WebDavUrl}'");
}

return _publication;
}
set
{
// Allows dependency injection for unit test purposes.
Expand All @@ -87,7 +98,26 @@ protected Publication Publication
/// </summary>
protected Session Session
{
get { return _session ?? (_session = Engine.GetSession()); }
get
{
if (_session == null)
{
InternalLogger.Debug("Getting session from Engine..");
_session = Engine.GetSession();

if (_session == null)
{
InternalLogger.Debug(" strange, session was null!");
}
else
{
InternalLogger.Debug("Session details:");
InternalLogger.Debug($" webDavUrl prefix : {_session.WebDavUrlPrefix}");
}
}

return _session;
}
set
{
// Allows dependency injection for unit test purposes.
Expand All @@ -100,6 +130,10 @@ protected Session Session
/// </summary>
protected TemplatingLogger Logger => _logger ?? (_logger = TemplatingLogger.GetLogger(GetType()));

protected InternalLogger InternalLogger => _internalLogger ??
(_internalLogger =
new InternalLogger(TemplatingLogger.GetLogger(GetType())));

/// <summary>
/// Attempts to return value of a parameter
/// </summary>
Expand Down
42 changes: 42 additions & 0 deletions Sdl.Web.Tridion.Templates/Log.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.IO;
using Tridion.ContentManager.Templating;

namespace Sdl.Web.Tridion
{
public class InternalLogger
{
private readonly TemplatingLogger _log;
private readonly string _logFile = null;

public InternalLogger(TemplatingLogger log)
{
_log = log;
try
{
_logFile = Environment.GetEnvironmentVariable("DXA_LOGGING");
}
catch
{
// invalid so ignore
}
}

public void Debug(string msg)
{
try
{
_log?.Debug(msg);
if (string.IsNullOrEmpty(_logFile)) return;
using (var sw = File.AppendText(_logFile))
{
sw.WriteLine(msg);
}
}
catch
{
// ignore
}
}
}
}
2 changes: 1 addition & 1 deletion Sdl.Web.Tridion.Templates/Sdl.Web.Tridion.Templates.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
Expand Down Expand Up @@ -95,6 +94,7 @@
<Compile Include="Common\TemplateBase.cs" />
<Compile Include="Common\Utility.cs" />
<Compile Include="ContextExpressionUtils.cs" />
<Compile Include="Log.cs" />
<Compile Include="StringExtensions.cs" />
<Compile Include="Templates\GenerateSitemap.cs" />
<Compile Include="Templates\PublishConfiguration.cs" />
Expand Down
42 changes: 34 additions & 8 deletions Sdl.Web.Tridion.Templates/Templates/PublishMappings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public class PublishMappings : TemplateBase

public PublishMappings()
{
InternalLogger.Debug("PublishMappings TBB constructed");
foreach (KeyValuePair<string, string> ns in _namespaces)
{
_namespaceManager.AddNamespace(ns.Key, ns.Value);
Expand All @@ -69,6 +70,7 @@ public PublishMappings()

public override void Transform(Engine engine, Package package)
{
InternalLogger.Debug("Transform called");
Initialize(engine, package);

package.TryGetParameter("retrofitMode", out _retrofitMode, Logger);
Expand All @@ -88,6 +90,7 @@ public override void Transform(Engine engine, Package package)
AddBootstrapJsonBinary(binaries, inputComponent, mappingsStructureGroup, "mapping");

OutputSummary("Publish Mappings", binaries.Select(b => b?.Url));
InternalLogger.Debug("Transform completed");
}

private Binary PublishSemanticVocabularies(StructureGroup structureGroup, Component relatedComponent)
Expand Down Expand Up @@ -271,18 +274,28 @@ private Binary PublishXpmRegionConfiguration(StructureGroup structureGroup, Comp

private Binary PublishPageIncludes(StructureGroup structureGroup, Component relatedComponent)
{
InternalLogger.Debug($"PublishPageIncludes(structureGroup='{structureGroup.Title}, {structureGroup.Id}', relatedComponent='{relatedComponent.Title}, {relatedComponent.Id}')");
IDictionary<string, string[]> pageIncludes = new Dictionary<string, string[]>();

RepositoryItemsFilter pageTemplatesFilter = new RepositoryItemsFilter(Session)
{
ItemTypes = new[] { ItemType.PageTemplate },
Recursive = true
};

InternalLogger.Debug("Getting all page templates from publication...");
IEnumerable<PageTemplate> pageTemplates = Publication.GetItems(pageTemplatesFilter).Cast<PageTemplate>();
InternalLogger.Debug($"Found {pageTemplates.Count()} page templates... Checking page template metadata if it exists...");
foreach (PageTemplate pt in pageTemplates.Where(pt => pt.MetadataSchema != null && pt.Metadata != null))
{
InternalLogger.Debug($" page template '{pt.Title}' with id '{pt.Id}' contains metadata with metadata schema '{pt.MetadataSchema.Title}', id = '{pt.MetadataSchema.Id}'...");
ItemFields ptMetadataFields = new ItemFields(pt.Metadata, pt.MetadataSchema);

InternalLogger.Debug($"Looking for metadata field values with fieldname 'includes'..");
var includeFields = ptMetadataFields.GetTextValues("includes");
foreach (var includeField in includeFields)
{
InternalLogger.Debug($"Found include with value = {includeField} !");
}
string[] includes = ptMetadataFields.GetTextValues("includes").Select(id => GetPublishPath(id)).ToArray();
pageIncludes.Add(pt.Id.ItemId.ToString(), includes);
}
Expand All @@ -292,17 +305,30 @@ private Binary PublishPageIncludes(StructureGroup structureGroup, Component rela

private string GetPublishPath(string pageId)
{
string result;
if (TcmUri.IsValid(pageId) || pageId.StartsWith("/webdav/"))
try
{
Page page = (Page)Session.GetObject(pageId);
result = page.PublishLocationUrl.Substring(1);
InternalLogger.Debug($"GetPublishPath(pageId = {pageId}) called");
string result;
if (TcmUri.IsValid(pageId) || pageId.StartsWith("/webdav/", StringComparison.InvariantCultureIgnoreCase))
{
InternalLogger.Debug(" PageId is a valid tcm uri or begins with /webdav/ -- attempting to get page object from session");
Page page = (Page) Session.GetObject(pageId);
result = page.PublishLocationUrl.Substring(1);
InternalLogger.Debug($" Page object from session = {result}");
}
else
{
InternalLogger.Debug($" Using pageId '{pageId}'");
result = pageId;
}
return result;
}
else
catch
{
result = pageId;
InternalLogger.Debug($" Using pageId '{pageId}'");
}
return result;

return pageId;
}

private SemanticSchemaData GetSemanticSchema(Schema schema)
Expand Down
Binary file modified cms/content/sites9/all-publications/Binaries/2-70-2048.tbbasm
Binary file not shown.
Binary file modified cms/content/sites9/all-publications/Binaries/2-80-2048.tbbasm
Binary file not shown.
Binary file modified cms/content/sites9/framework-only/Binaries/2-70-2048.tbbasm
Binary file not shown.
Binary file modified cms/content/sites9/framework-only/Binaries/2-80-2048.tbbasm
Binary file not shown.
Binary file modified cms/content/sites9/master-only/Binaries/2-70-2048.tbbasm
Binary file not shown.
Binary file modified cms/content/sites9/master-only/Binaries/2-80-2048.tbbasm
Binary file not shown.

0 comments on commit 5e6aacf

Please sign in to comment.