Skip to content

Commit 5e6aacf

Browse files
committed
SRQ-15639 : Fixed issue with publish mappings and includes
1 parent 86088ff commit 5e6aacf

File tree

10 files changed

+113
-11
lines changed

10 files changed

+113
-11
lines changed

Sdl.Web.Tridion.Templates/Common/TemplateBase.cs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public abstract class TemplateBase : ITemplate
3131
"</?tcdl:ComponentPresentation[^>]*>", RegexOptions.Compiled);
3232

3333
private TemplatingLogger _logger;
34+
private InternalLogger _internalLogger;
3435
private Session _session;
3536
private Engine _engine;
3637
private Package _package;
@@ -74,7 +75,17 @@ protected Package Package
7475
/// </summary>
7576
protected Publication Publication
7677
{
77-
get { return _publication ?? (_publication = GetPublication()); }
78+
get
79+
{
80+
if (_publication == null)
81+
{
82+
InternalLogger.Debug("Getting publication...");
83+
_publication = GetPublication();
84+
InternalLogger.Debug($" Found publication id='{_publication.Id}', title='{Publication.Title}', webDavUrl='{Publication.WebDavUrl}'");
85+
}
86+
87+
return _publication;
88+
}
7889
set
7990
{
8091
// Allows dependency injection for unit test purposes.
@@ -87,7 +98,26 @@ protected Publication Publication
8798
/// </summary>
8899
protected Session Session
89100
{
90-
get { return _session ?? (_session = Engine.GetSession()); }
101+
get
102+
{
103+
if (_session == null)
104+
{
105+
InternalLogger.Debug("Getting session from Engine..");
106+
_session = Engine.GetSession();
107+
108+
if (_session == null)
109+
{
110+
InternalLogger.Debug(" strange, session was null!");
111+
}
112+
else
113+
{
114+
InternalLogger.Debug("Session details:");
115+
InternalLogger.Debug($" webDavUrl prefix : {_session.WebDavUrlPrefix}");
116+
}
117+
}
118+
119+
return _session;
120+
}
91121
set
92122
{
93123
// Allows dependency injection for unit test purposes.
@@ -100,6 +130,10 @@ protected Session Session
100130
/// </summary>
101131
protected TemplatingLogger Logger => _logger ?? (_logger = TemplatingLogger.GetLogger(GetType()));
102132

133+
protected InternalLogger InternalLogger => _internalLogger ??
134+
(_internalLogger =
135+
new InternalLogger(TemplatingLogger.GetLogger(GetType())));
136+
103137
/// <summary>
104138
/// Attempts to return value of a parameter
105139
/// </summary>

Sdl.Web.Tridion.Templates/Log.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using System.IO;
3+
using Tridion.ContentManager.Templating;
4+
5+
namespace Sdl.Web.Tridion
6+
{
7+
public class InternalLogger
8+
{
9+
private readonly TemplatingLogger _log;
10+
private readonly string _logFile = null;
11+
12+
public InternalLogger(TemplatingLogger log)
13+
{
14+
_log = log;
15+
try
16+
{
17+
_logFile = Environment.GetEnvironmentVariable("DXA_LOGGING");
18+
}
19+
catch
20+
{
21+
// invalid so ignore
22+
}
23+
}
24+
25+
public void Debug(string msg)
26+
{
27+
try
28+
{
29+
_log?.Debug(msg);
30+
if (string.IsNullOrEmpty(_logFile)) return;
31+
using (var sw = File.AppendText(_logFile))
32+
{
33+
sw.WriteLine(msg);
34+
}
35+
}
36+
catch
37+
{
38+
// ignore
39+
}
40+
}
41+
}
42+
}

Sdl.Web.Tridion.Templates/Sdl.Web.Tridion.Templates.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3-
43
<PropertyGroup>
54
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
65
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
@@ -95,6 +94,7 @@
9594
<Compile Include="Common\TemplateBase.cs" />
9695
<Compile Include="Common\Utility.cs" />
9796
<Compile Include="ContextExpressionUtils.cs" />
97+
<Compile Include="Log.cs" />
9898
<Compile Include="StringExtensions.cs" />
9999
<Compile Include="Templates\GenerateSitemap.cs" />
100100
<Compile Include="Templates\PublishConfiguration.cs" />

Sdl.Web.Tridion.Templates/Templates/PublishMappings.cs

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public class PublishMappings : TemplateBase
6161

6262
public PublishMappings()
6363
{
64+
InternalLogger.Debug("PublishMappings TBB constructed");
6465
foreach (KeyValuePair<string, string> ns in _namespaces)
6566
{
6667
_namespaceManager.AddNamespace(ns.Key, ns.Value);
@@ -69,6 +70,7 @@ public PublishMappings()
6970

7071
public override void Transform(Engine engine, Package package)
7172
{
73+
InternalLogger.Debug("Transform called");
7274
Initialize(engine, package);
7375

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

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

9396
private Binary PublishSemanticVocabularies(StructureGroup structureGroup, Component relatedComponent)
@@ -271,18 +274,28 @@ private Binary PublishXpmRegionConfiguration(StructureGroup structureGroup, Comp
271274

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

276280
RepositoryItemsFilter pageTemplatesFilter = new RepositoryItemsFilter(Session)
277281
{
278282
ItemTypes = new[] { ItemType.PageTemplate },
279283
Recursive = true
280284
};
281-
285+
InternalLogger.Debug("Getting all page templates from publication...");
282286
IEnumerable<PageTemplate> pageTemplates = Publication.GetItems(pageTemplatesFilter).Cast<PageTemplate>();
287+
InternalLogger.Debug($"Found {pageTemplates.Count()} page templates... Checking page template metadata if it exists...");
283288
foreach (PageTemplate pt in pageTemplates.Where(pt => pt.MetadataSchema != null && pt.Metadata != null))
284289
{
290+
InternalLogger.Debug($" page template '{pt.Title}' with id '{pt.Id}' contains metadata with metadata schema '{pt.MetadataSchema.Title}', id = '{pt.MetadataSchema.Id}'...");
285291
ItemFields ptMetadataFields = new ItemFields(pt.Metadata, pt.MetadataSchema);
292+
293+
InternalLogger.Debug($"Looking for metadata field values with fieldname 'includes'..");
294+
var includeFields = ptMetadataFields.GetTextValues("includes");
295+
foreach (var includeField in includeFields)
296+
{
297+
InternalLogger.Debug($"Found include with value = {includeField} !");
298+
}
286299
string[] includes = ptMetadataFields.GetTextValues("includes").Select(id => GetPublishPath(id)).ToArray();
287300
pageIncludes.Add(pt.Id.ItemId.ToString(), includes);
288301
}
@@ -292,17 +305,30 @@ private Binary PublishPageIncludes(StructureGroup structureGroup, Component rela
292305

293306
private string GetPublishPath(string pageId)
294307
{
295-
string result;
296-
if (TcmUri.IsValid(pageId) || pageId.StartsWith("/webdav/"))
308+
try
297309
{
298-
Page page = (Page)Session.GetObject(pageId);
299-
result = page.PublishLocationUrl.Substring(1);
310+
InternalLogger.Debug($"GetPublishPath(pageId = {pageId}) called");
311+
string result;
312+
if (TcmUri.IsValid(pageId) || pageId.StartsWith("/webdav/", StringComparison.InvariantCultureIgnoreCase))
313+
{
314+
InternalLogger.Debug(" PageId is a valid tcm uri or begins with /webdav/ -- attempting to get page object from session");
315+
Page page = (Page) Session.GetObject(pageId);
316+
result = page.PublishLocationUrl.Substring(1);
317+
InternalLogger.Debug($" Page object from session = {result}");
318+
}
319+
else
320+
{
321+
InternalLogger.Debug($" Using pageId '{pageId}'");
322+
result = pageId;
323+
}
324+
return result;
300325
}
301-
else
326+
catch
302327
{
303-
result = pageId;
328+
InternalLogger.Debug($" Using pageId '{pageId}'");
304329
}
305-
return result;
330+
331+
return pageId;
306332
}
307333

308334
private SemanticSchemaData GetSemanticSchema(Schema schema)
3.5 KB
Binary file not shown.
3.5 KB
Binary file not shown.
3.5 KB
Binary file not shown.
3.5 KB
Binary file not shown.
3.5 KB
Binary file not shown.
3.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)