17 using System.Collections;
18 using System.Collections.Generic;
21 using erminas.SmartAPI.Exceptions;
22 using erminas.SmartAPI.Utils;
23 using erminas.SmartAPI.Utils.CachedCollections;
25 namespace erminas.SmartAPI.CMS.Project.ContentClasses.Elements
36 private List<IContentClass> _contentClasses;
37 private bool _isDirty =
true;
38 private List<IPageDefinition> _pageDefinitions;
43 var contentClassModifier =
new Modifier<IContentClass>(GetContentClasses,
45 Set(contentClasses, PageDefinitions), Add, Remove);
46 ContentClasses =
new Preassigned<IContentClass>(contentClassModifier);
48 var pageDefinitionModifier =
new Modifier<IPageDefinition>(GetPageDefinitions,
50 Set(ContentClasses, pageDefinitions), Add, Remove);
51 PageDefinitions =
new Preassigned<IPageDefinition>(pageDefinitionModifier);
54 public Preassigned<IContentClass> ContentClasses {
get;
private set; }
56 public void InvalidateCache()
61 public Preassigned<IPageDefinition> PageDefinitions {
get;
private set; }
65 XmlNodeList contentClasses, pageDefinitions;
66 ExecuteLoadQuery(out contentClasses, out pageDefinitions);
68 _contentClasses = ExtractContentClasses(contentClasses);
69 _pageDefinitions = ExtractPageDefinitions(pageDefinitions);
72 public void Set(IEnumerable<IContentClass> contentClasses, IEnumerable<IPageDefinition> pageDefinitions)
74 var assignmentQuery = ToAssignmentQuery(contentClasses, pageDefinitions);
75 ExecuteAssignmentQuery(assignmentQuery);
82 Set(ContentClasses, PageDefinitions.Union(
new[] {pageDefinition}));
85 private void Add(IContentClass contentClass)
87 Set(ContentClasses.Union(
new List<IContentClass> {contentClass}), PageDefinitions);
90 private void ExecuteAssignmentQuery(
string assignmentQuery)
92 var xmlDoc = Element.ContentClass.Project.ExecuteRQL(assignmentQuery);
94 if (!xmlDoc.IsContainingOk())
97 string.Format(
"Could not set presassigned content classes for {0}", Element));
101 private void ExecuteLoadQuery(out XmlNodeList contentClasses, out XmlNodeList pageDefinitions)
103 const string LOAD_PREASSIGNMENT =
104 @"<TEMPLATELIST action=""load"" linkguid=""{0}"" assigntemplates=""1"" withpagedefinitions=""1""/>";
106 var xmlDoc = Element.ContentClass.Project.ExecuteRQL(LOAD_PREASSIGNMENT.RQLFormat(Element));
107 contentClasses = xmlDoc.SelectNodes(
"//TEMPLATE[@selectinnewpage='1']");
108 pageDefinitions = xmlDoc.SelectNodes(
"//PAGEDEFINITION[@selectinnewpage='1']");
111 private List<IContentClass> ExtractContentClasses(XmlNodeList contentClasses)
113 return (from XmlElement curContentClass in contentClasses
116 new ContentClass(Element.ContentClass.Project, curContentClass.GetGuid())
118 Name = curContentClass.GetName(),
119 DescriptionInCurrentDisplayLanguage = curContentClass.GetAttributeValue(
"description")
124 private List<IPageDefinition> ExtractPageDefinitions(XmlNodeList pageDefinitions)
126 return (from XmlElement curPageDefinition in pageDefinitions
129 new ContentClass(Element.ContentClass.Project, curPageDefinition.GetGuid(
"templateguid")),
130 curPageDefinition)).Cast<IPageDefinition>().ToList();
133 private IEnumerable<IContentClass> GetContentClasses()
139 return _contentClasses;
142 private IEnumerable<IPageDefinition> GetPageDefinitions()
148 return _pageDefinitions;
151 private void Remove(IContentClass contentClass)
153 Set(ContentClasses.Except(
new[] {contentClass}), PageDefinitions);
156 private void Remove(IPageDefinition pageDefinition)
158 Set(ContentClasses, PageDefinitions.Except(
new[] {pageDefinition}));
161 private string ToAssignmentQuery(IEnumerable<IContentClass> contentClasses,
162 IEnumerable<IPageDefinition> pageDefinitions)
164 contentClasses = contentClasses ??
new List<IContentClass>();
165 pageDefinitions = pageDefinitions ??
new List<IPageDefinition>();
167 const string SINGLE_PREASSIGNMENT =
@"<TEMPLATE guid=""{0}""/>";
169 string assignments = contentClasses.Cast<IRedDotObject>()
170 .Union(pageDefinitions)
171 .Aggregate(
"", (s, o) => s + SINGLE_PREASSIGNMENT.RQLFormat(o));
173 const string SET_PREASSIGNMENT_ENVELOPE =
174 @"<TEMPLATE><ELEMENT action=""assign"" guid=""{0}""><TEMPLATES extendedrestriction=""{1}"">{2}</TEMPLATES></ELEMENT></TEMPLATE>";
175 string assignmentQuery = SET_PREASSIGNMENT_ENVELOPE.RQLFormat(Element,
false, assignments);
176 return assignmentQuery;
180 internal class Modifier<T> where T : IRedDotObject
182 internal readonly Action<T> Add;
183 internal readonly Func<IEnumerable<T>> Get;
184 internal readonly Action<T> Remove;
185 internal readonly Action<IEnumerable<T>> Set;
187 internal Modifier(Func<IEnumerable<T>>
get, Action<IEnumerable<T>>
set, Action<T> add, Action<T>
remove)
206 public void Add(T element)
208 _parent.Add(element);
211 public IEnumerator<T> GetEnumerator()
213 return _parent.Get().GetEnumerator();
216 public void Remove(T element)
218 _parent.Remove(element);
221 public void Set(IEnumerable<T> elements)
223 _parent.Set(elements);
226 IEnumerator IEnumerable.GetEnumerator()
228 return _parent.Get().GetEnumerator();