-
Notifications
You must be signed in to change notification settings - Fork 18
/
PublishMappings.cs
488 lines (440 loc) · 22.4 KB
/
PublishMappings.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
using System.Linq;
using Sdl.Web.Tridion.Common;
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Linq;
using Tridion;
using Tridion.ContentManager;
using Tridion.ContentManager.CommunicationManagement;
using Tridion.ContentManager.ContentManagement;
using Tridion.ContentManager.ContentManagement.Fields;
using Tridion.ContentManager.Templating;
using Tridion.ContentManager.Templating.Assembly;
namespace Sdl.Web.Tridion.Templates
{
/// <summary>
/// Publishes schema and region mapping information in JSON format
/// </summary>
[TcmTemplateTitle("Publish Mappings")]
public class PublishMappings : TemplateBase
{
// json content in page
private const string JsonOutputFormat = "{{\"name\":\"Publish Mappings\",\"status\":\"Success\",\"files\":[{0}]}}";
private const string InvalidCharactersPattern = @"[^A-Za-z0-9.]+";
private const string SchemasConfigName = "schemas";
//private const string MappingsConfigName = "mappings";
private const string RegionConfigName = "regions";
private const string VocabulariesConfigName = "vocabularies";
private const string VocabulariesAppDataId = "http://www.sdl.com/tridion/SemanticMapping/vocabularies";
private const string TypeOfAppDataId = "http://www.sdl.com/tridion/SemanticMapping/typeof";
private const string TypeOfAppDataStartElement = "<typeof>";
private const string TypeOfAppDataEndElement = "</typeof>";
private const string DefaultVocabularyPrefix = "tri";
private const string DefaultVocabulary = "http://www.sdl.com/web/schemas/core";
// list of known namespaces that are used in our schemas
private readonly Dictionary<string, string> _namespaces = new Dictionary<string, string>
{
{Constants.XsdPrefix, Constants.XsdNamespace},
{Constants.TcmPrefix, Constants.TcmNamespace},
{Constants.XlinkPrefix, Constants.XlinkNamespace},
{Constants.XhtmlPrefix, Constants.XhtmlNamespace},
{"tcmi","http://www.tridion.com/ContentManager/5.0/Instance"},
{"mapping", "http://www.sdl.com/tridion/SemanticMapping"}
};
//private string _moduleRoot;
public override void Transform(Engine engine, Package package)
{
Initialize(engine, package);
//The core configuration component should be the one being processed by the template
Component coreConfigComponent = GetComponent();
StructureGroup sg = GetSystemStructureGroup("mappings");
//_moduleRoot = GetModulesRoot(coreConfigComponent);
//Get all the active modules
//Dictionary<string, Component> moduleComponents = GetActiveModules(coreConfigComponent);
List<string> filesCreated = new List<string>();
filesCreated.AddRange(PublishJsonData(ReadMappingsData(), coreConfigComponent, "mapping", sg, true));
filesCreated.Add(PublishJsonData(ReadPageTemplateIncludes(), coreConfigComponent, "includes", "includes", sg));
//Publish the boostrap list, this is used by the web application to load in all other mapping files
PublishBootstrapJson(filesCreated, coreConfigComponent, sg, "mapping-");
StringBuilder publishedFiles = new StringBuilder();
foreach (string file in filesCreated)
{
if (!String.IsNullOrEmpty(file))
{
publishedFiles.AppendCommaSeparated(file);
Logger.Info("Published " + file);
}
}
// append json result to output
string json = String.Format(JsonOutputFormat, publishedFiles);
Item outputItem = package.GetByName(Package.OutputName);
if (outputItem != null)
{
package.Remove(outputItem);
string output = outputItem.GetAsString();
if (output.StartsWith("["))
{
// insert new json object
json = String.Format("{0},{1}{2}]", output.TrimEnd(']'), Environment.NewLine, json);
}
else
{
// append new json object
json = String.Format("[{0},{1}{2}]", output, Environment.NewLine, json);
}
}
package.PushItem(Package.OutputName, package.CreateStringItem(ContentType.Text, json));
}
private Dictionary<string, List<string>> ReadMappingsData()
{
bool containsDefaultVocabulary = false;
// generate a list of vocabulary prefix and name from appdata
Dictionary<string, List<string>> res = new Dictionary<string, List<string>> { { VocabulariesConfigName, new List<string>() } };
ApplicationData globalAppData = Engine.GetSession().SystemManager.LoadGlobalApplicationData(VocabulariesAppDataId);
if (globalAppData != null)
{
XElement vocabulariesXml = XElement.Parse(Encoding.Unicode.GetString(globalAppData.Data));
foreach (XElement vocabulary in vocabulariesXml.Elements())
{
string prefix = vocabulary.Attribute("prefix").Value;
res[VocabulariesConfigName].Add(String.Format("{{\"Prefix\":{0},\"Vocab\":{1}}}", JsonEncode(prefix), JsonEncode(vocabulary.Attribute("name").Value)));
if (prefix.Equals(DefaultVocabularyPrefix))
{
containsDefaultVocabulary = true;
}
}
}
// add default vocabulary if it is not there already
if (!containsDefaultVocabulary)
{
res[VocabulariesConfigName].Add(String.Format("{{\"Prefix\":{0},\"Vocab\":{1}}}", JsonEncode(DefaultVocabularyPrefix), JsonEncode(DefaultVocabulary)));
}
// generate a list of schema + id, separated by module
RepositoryItemsFilter schemaFilter = new RepositoryItemsFilter(Engine.GetSession())
{
Recursive = true,
ItemTypes = new [] { ItemType.Schema },
// NOTE: including SchemaPurpose.Metadata for Page Metadata
SchemaPurposes = new [] { SchemaPurpose.Component, SchemaPurpose.Multimedia, SchemaPurpose.Metadata },
BaseColumns = ListBaseColumns.Extended
};
res.Add(SchemasConfigName, new List<string>());
foreach (Schema schema in GetPublication().GetItems(schemaFilter).Cast<Schema>())
{
string rootElementName = schema.RootElementName;
if (String.IsNullOrEmpty(rootElementName))
{
// multimedia/metadata schemas don't have a root element name, so lets use its title without any invalid characters
rootElementName = Regex.Replace(schema.Title.Trim(), InvalidCharactersPattern, String.Empty);
}
// add schema typeof using tridion standard implementation vocabulary prefix
string typeOf = String.Format("{0}:{1}", DefaultVocabularyPrefix, rootElementName);
StringBuilder schemaSemantics = new StringBuilder();
// append schema typeof from appdata
ApplicationData appData = schema.LoadApplicationData(TypeOfAppDataId);
if (appData != null)
{
typeOf += "," + ExtractTypeOfAppData(appData);
}
schemaSemantics.Append(BuildSchemaSemanticsJson(typeOf));
// TODO: serialize schema fields xml to json in a smart way
// field: {"Name":"something","Path":"/something","IsMultiValue":true,"Semantics":[],"Fields":[]}
// field semantics: {"Prefix":"s","Entity";"Article","Property":"headline"}
StringBuilder fields = new StringBuilder();
// load namespace manager with schema namespaces
XmlNamespaceManager nsmgr = SchemaNamespaceManager(schema.Xsd.OwnerDocument.NameTable);
// build field elements from schema
string path = "/" + rootElementName;
fields.Append(BuildSchemaFieldsJson(schema, path, typeOf, nsmgr));
res[SchemasConfigName].Add(String.Format("{{\"Id\":{0},\"RootElement\":{1},\"Fields\":[{2}],\"Semantics\":[{3}]}}", JsonEncode(schema.Id.ItemId), JsonEncode(rootElementName), fields, schemaSemantics));
}
// get region mappings for all templates
Dictionary<string, List<string>> regions = BuildRegionMappings();
res.Add(RegionConfigName, new List<string>());
foreach (KeyValuePair<string, List<string>> region in regions)
{
StringBuilder allowedComponentTypes = new StringBuilder();
bool first = true;
foreach (string componentType in region.Value)
{
if (first)
{
first = false;
}
else
{
allowedComponentTypes.Append(",");
}
allowedComponentTypes.Append(componentType);
}
res[RegionConfigName].Add(String.Format("{{\"Region\":{0},\"ComponentTypes\":[{1}]}}", JsonEncode(region.Key), allowedComponentTypes));
}
return res;
}
private Dictionary<string, List<string>> BuildRegionMappings()
{
// format: region { schema, template }
Dictionary<string, List<string>> regions = new Dictionary<string, List<string>>();
ComponentTemplatesFilter templateFilter = new ComponentTemplatesFilter(Engine.GetSession()) { BaseColumns = ListBaseColumns.Extended };
foreach (ComponentTemplate template in GetPublication().GetComponentTemplates(templateFilter))
{
string regionName = GetRegionName(template);
if (!regions.ContainsKey(regionName))
{
regions.Add(regionName, new List<string>());
}
StringBuilder allowedComponentTypes = new StringBuilder();
bool first = true;
foreach (Schema schema in template.RelatedSchemas)
{
if (first)
{
first = false;
}
else
{
allowedComponentTypes.Append(",");
}
allowedComponentTypes.AppendFormat("{{\"Schema\":{0},\"Template\":{1}}}", JsonEncode(schema.Id.GetVersionlessUri().ToString()), JsonEncode(template.Id.GetVersionlessUri().ToString()));
}
// do not append empty strings (template.RelatedSchemas can be empty)
if (allowedComponentTypes.Length > 0)
{
regions[regionName].Add(allowedComponentTypes.ToString());
}
}
return regions;
}
private XmlNamespaceManager SchemaNamespaceManager(XmlNameTable nameTable)
{
// load namespace manager with schema namespaces
XmlNamespaceManager nsmgr = new XmlNamespaceManager(nameTable);
foreach (KeyValuePair<string, string> item in _namespaces)
{
nsmgr.AddNamespace(item.Key, item.Value);
}
return nsmgr;
}
// field: {"Name":"something","Path":"/something","IsMultiValue":true,"Semantics":[],"Fields":[]}
// field semantics: {"Prefix":"s","Entity":"Article","Property":"headline"}
private string BuildSchemaFieldsJson(Schema schema, string parentPath, string typeOf, XmlNamespaceManager nsmgr, bool embedded = false)
{
StringBuilder fields = new StringBuilder();
// loop over all field elements in schema
bool first = true;
string xpath = String.Format("/xsd:schema/xsd:element[@name='{0}']/xsd:complexType/xsd:sequence/xsd:element", schema.RootElementName);
if (embedded)
{
xpath = String.Format("/xsd:schema/xsd:complexType[@name='{0}']/xsd:sequence/xsd:element", schema.RootElementName);
}
foreach (XmlNode fieldNode in schema.Xsd.SelectNodes(xpath, nsmgr))
{
string fieldJson = BuildFieldJson(fieldNode, parentPath, typeOf, nsmgr);
if (first)
{
first = false;
}
else
{
fields.Append(",");
}
fields.Append(fieldJson);
}
// embedded schemas do not contain metadata fields
if (!embedded)
{
// add metadata fields
xpath = "/xsd:schema/xsd:element[@name='Metadata']/xsd:complexType/xsd:sequence/xsd:element";
foreach (XmlNode fieldNode in schema.Xsd.SelectNodes(xpath, nsmgr))
{
// change last item in parentPath to Metadata
int index = parentPath.LastIndexOf('/');
if (index != -1)
{
parentPath = parentPath.Substring(0, index) + "/Metadata";
}
string fieldJson = BuildFieldJson(fieldNode, parentPath, typeOf, nsmgr);
if (first)
{
first = false;
}
else
{
fields.Append(",");
}
fields.Append(fieldJson);
}
}
return fields.ToString();
}
// field: {"Name":"something","Path":"/something","IsMultiValue":true,"Semantics":[],"Fields":[]}
private string BuildFieldJson(XmlNode fieldNode, string parentPath, string typeOf, XmlNamespaceManager nsmgr)
{
string name = fieldNode.Attributes["name"].Value;
string path = parentPath + "/" + name;
string fieldTypeOf = typeOf;
StringBuilder fieldSemantics = new StringBuilder();
// if maxOccurs is anything else than 1, it is a multi value field
bool isMultiValue = !fieldNode.Attributes["maxOccurs"].Value.Equals("1");
// read semantic mapping from field so we can append it to the schema typeof
XmlNode typeOfNode = fieldNode.SelectSingleNode("xsd:annotation/xsd:appinfo/tcm:ExtensionXml/mapping:typeof", nsmgr);
if (typeOfNode != null)
{
fieldTypeOf = typeOf + "," + typeOfNode.InnerText;
}
// use field xml name as initial semantic mapping for field
string property = String.Format("{0}:{1}", DefaultVocabularyPrefix, fieldNode.Attributes["name"].Value);
// read semantic mapping from field and append if available
XmlNode propertyNode = fieldNode.SelectSingleNode("xsd:annotation/xsd:appinfo/tcm:ExtensionXml/mapping:property", nsmgr);
if (propertyNode != null)
{
property += "," + propertyNode.InnerText;
}
fieldSemantics.Append(BuildFieldSemanticsJson(property, fieldTypeOf));
// handle embedded fields
StringBuilder embeddedFields = new StringBuilder();
XmlNode embeddedSchemaNode = fieldNode.SelectSingleNode("xsd:annotation/xsd:appinfo/tcm:EmbeddedSchema", nsmgr);
if (embeddedSchemaNode != null)
{
string uri = embeddedSchemaNode.Attributes["href", Constants.XlinkNamespace].Value;
Schema embeddedSchema = (Schema)Engine.GetObject(uri);
string embeddedTypeOf = String.Format("{0}:{1}", DefaultVocabularyPrefix, embeddedSchema.RootElementName);
// append schema typeof from appdata for embedded schemas
ApplicationData appData = embeddedSchema.LoadApplicationData(TypeOfAppDataId);
if (appData != null)
{
embeddedTypeOf += "," + Encoding.Unicode.GetString(appData.Data);
}
embeddedFields.Append(BuildSchemaFieldsJson(embeddedSchema, path, embeddedTypeOf, nsmgr, true));
}
// TODO: handle link fields
//XmlAttribute typeAttribute = fieldNode.Attributes["type"];
//if (typeAttribute != null)
//{
// bool isSimpleLink = typeAttribute.Value.Equals("tcmi:SimpleLink");
// XmlNode allowedTargetSchemasNode = fieldNode.SelectSingleNode("xsd:annotation/xsd:appinfo/tcm:AllowedTargetSchemas", nsmgr);
// if (allowedTargetSchemasNode != null)
// {
// foreach (XmlNode allowedTargetSchemaNode in allowedTargetSchemasNode.SelectNodes("tcm:TargetSchema", nsmgr))
// {
// string uri = allowedTargetSchemaNode.Attributes["href", "http://www.w3.org/1999/xlink"].Value;
// Schema allowedTargetSchema = (Schema)MEngine.GetObject(uri);
//
// }
// }
// else
// {
// // if there are no allowed target schemas, all schemas are allowed...
// }
//}
//else
//{
// // if there is no type attribute, it is not a simple type so look for <xsd:complexType> inside the element
//}
return String.Format("{{\"Name\":{0},\"Path\":{1},\"IsMultiValue\":{2},\"Semantics\":[{3}],\"Fields\":[{4}]}}", JsonEncode(name), JsonEncode(path), JsonEncode(isMultiValue), fieldSemantics, embeddedFields);
}
// schema semantics: {"Prefix":"s","Entity":"Article"}
private string BuildSchemaSemanticsJson(string input)
{
StringBuilder semantics = new StringBuilder();
if (!String.IsNullOrEmpty(input))
{
// input = "s:Article" but can also be "s:Article,x:Something"
string[] values = input.Split(',');
bool first = true;
foreach (string value in values)
{
if (first)
{
first = false;
}
else
{
semantics.Append(",");
}
string[] parts = value.Split(':');
semantics.AppendFormat("{{\"Prefix\":{0},\"Entity\":{1}}}", JsonEncode(parts[0]), JsonEncode(parts[1]));
}
}
return semantics.ToString();
}
// field semantics: {"Prefix":"s","Entity":"Article","Property":"headline"}
private string BuildFieldSemanticsJson(string input, string entity)
{
Dictionary<string, string> entities = new Dictionary<string, string>();
StringBuilder semantics = new StringBuilder();
bool first = true;
if (!String.IsNullOrEmpty(input))
{
// entity = "s:Article" but can also be "s:Article,x:Something"
string[] values = entity.Split(',');
foreach (string value in values)
{
string[] parts = value.Split(':');
entities.Add(parts[0], parts[1]);
}
// input = "s:headline" but can also be "s:headline,x:something"
string[] properties = input.Split(',');
foreach (string value in properties)
{
string[] parts = value.Split(':');
if (entities.ContainsKey(parts[0]))
{
if (!first)
{
semantics.Append(",");
}
first = false;
semantics.AppendFormat("{{\"Prefix\":{0},\"Entity\":{1},\"Property\":{2}}}", JsonEncode(parts[0]), JsonEncode(entities[parts[0]]), JsonEncode(parts[1]));
}
}
}
return semantics.ToString();
}
protected virtual List<string> ReadPageTemplateIncludes()
{
//Generate a list of Page Templates which have includes in the metadata
List<string> res = new List<string>();
RepositoryItemsFilter templateFilter = new RepositoryItemsFilter(Engine.GetSession())
{
ItemTypes = new List<ItemType> {ItemType.PageTemplate},
Recursive = true
};
foreach (XmlElement item in GetPublication().GetListItems(templateFilter).ChildNodes)
{
string id = item.GetAttribute("ID");
PageTemplate template = (PageTemplate)Engine.GetObject(id);
if (template.MetadataSchema != null && template.Metadata != null)
{
ItemFields meta = new ItemFields(template.Metadata, template.MetadataSchema);
IEnumerable<string> values = meta.GetTextValues("includes");
if (values != null)
{
List<string> includes = new List<string>();
foreach (string val in values)
{
includes.Add(JsonEncode(val));
}
string json = String.Format("\"{0}\":[{1}]", template.Id.ItemId, String.Join(",\n", includes));
res.Add(json);
}
}
}
return res;
}
private static string ExtractTypeOfAppData(ApplicationData appData)
{
if (appData != null)
{
// appdata is supposed to be a unicode encoded string
string xmlData = Encoding.Unicode.GetString(appData.Data);
// remove start and end xml element from appdata string
return xmlData.Replace(TypeOfAppDataStartElement, String.Empty).Replace(TypeOfAppDataEndElement, String.Empty);
}
return null;
}
}
}