16 using System.Collections.Generic;
20 using erminas.SmartAPI.Exceptions;
21 using erminas.SmartAPI.Utils;
22 using erminas.SmartAPI.Utils.CachedCollections;
24 namespace erminas.SmartAPI.CMS.Project.ContentClasses
26 internal class ProjectVariantAssignment : IProjectVariantAssignment
28 private readonly IContentClass _contentClass;
30 private readonly IProjectVariant _projectVariant;
31 private readonly ITemplateVariant _templateVariant;
33 internal ProjectVariantAssignment(IContentClass contentClass, IProjectVariant projectVariant,
34 ITemplateVariant templateVariant)
36 _contentClass = contentClass;
37 _projectVariant = projectVariant;
38 _templateVariant = templateVariant;
41 public IContentClass ContentClass
43 get {
return _contentClass; }
46 public IProjectVariant ProjectVariant
48 get {
return _projectVariant; }
51 public ITemplateVariant TemplateVariant
53 get {
return _templateVariant; }
56 public bool IsNotUsingTidy {
get;
internal set; }
57 public bool IsPublishing {
get;
internal set; }
65 bool IsNotUsingTidy {
get; }
66 bool IsPublishing {
get; }
73 public bool IsNotUsingTidy {
get;
set; }
74 public bool IsPublishing {
get;
set; }
77 internal class ProjectVariantAssignments : IndexedCachedList<IProjectVariant, IProjectVariantAssignment>,
78 IProjectVariantAssignments
80 private readonly IContentClass _contentClass;
82 internal ProjectVariantAssignments(IContentClass contentClass,
Caching caching)
83 : base(assignment => assignment.ProjectVariant,
Caching.Enabled)
85 _contentClass = contentClass;
86 RetrieveFunc = GetAssignments;
89 public void Assign(ITemplateVariant templateVariant, ProjectVariantAssignmentSettings settings)
91 Assign(
new Dictionary<ITemplateVariant, ProjectVariantAssignmentSettings> { { templateVariant, settings } });
94 public void Assign(ILookup<ITemplateVariant, ProjectVariantAssignmentSettings> assignments)
96 const string ASSIGN_PROJECT_VARIANT =
97 @"<TEMPLATE guid=""{0}""><TEMPLATEVARIANTS>{1}</TEMPLATEVARIANTS></TEMPLATE>";
98 const string SINGLE_ASSIGNMENT =
99 @"<TEMPLATEVARIANT guid=""{0}""><PROJECTVARIANTS action=""assign"">{1}</PROJECTVARIANTS></TEMPLATEVARIANT>";
100 const string SINGLE_PROJECT_VARIANT =
101 @"<PROJECTVARIANT donotgenerate=""{1}"" donotusetidy=""{2}"" guid=""{0}"" />";
104 var builder =
new StringBuilder();
105 foreach (var curEntry
in assignments)
107 var projectVariants = curEntry.Aggregate(
"", (s, variant) => s + SINGLE_PROJECT_VARIANT.RQLFormat(variant.ProjectVariant, !variant.IsPublishing, variant.IsNotUsingTidy));
108 builder.Append(SINGLE_ASSIGNMENT.RQLFormat(curEntry.Key, projectVariants));
111 Project.ExecuteRQL(ASSIGN_PROJECT_VARIANT.RQLFormat(_contentClass, builder),
RqlType.SessionKeyInProject);
115 public void Assign(IDictionary<ITemplateVariant, ProjectVariantAssignmentSettings> assignments)
117 Assign(assignments.ToLookup(x=>x.Key, x=>x.Value));
120 public IContentClass ContentClass
122 get {
return _contentClass; }
125 public IProject Project
127 get {
return _contentClass.Project; }
130 public ISession Session
132 get {
return _contentClass.Session; }
138 private List<IProjectVariantAssignment> GetAssignments()
140 const string PROJECT_VARIANT_ASSIGNMENT =
141 @"<TEMPLATE guid=""{0}"" ><TEMPLATEVARIANTS withstylesheets=""1"" action=""projectvariantslist"" /></TEMPLATE>";
145 var xmlDoc = Project.ExecuteRQL(PROJECT_VARIANT_ASSIGNMENT.RQLFormat(_contentClass),
147 using (
new CachingContext<IProjectVariant>(Project.ProjectVariants,
Caching.Enabled))
149 return (from XmlElement curElement in xmlDoc.GetElementsByTagName(
"TEMPLATEVARIANT")
151 (IProjectVariantAssignment)
152 new ProjectVariantAssignment(_contentClass,
153 Project.ProjectVariants.GetByGuid(
154 curElement.GetGuid("projectvariantguid")),
155 new TemplateVariant(_contentClass, curElement.GetGuid()))
157 IsNotUsingTidy = curElement.GetBoolAttributeValue(
"donotusetidy").GetValueOrDefault(),
158 IsPublishing = !curElement.GetBoolAttributeValue(
"donotgenerate").GetValueOrDefault()
164 throw new SmartAPIException(Session.ServerLogin,
"Could not get project variant assignments", e);
168 IEnumerable<IProjectVariantAssignment> IProjectVariantAssignments.this[ITemplateVariant templateVariant]
173 (from curAssignment in
this
174 where curAssignment.TemplateVariant.Equals(templateVariant)
175 select curAssignment).ToList();
181 IIndexedCachedList<IProjectVariant, IProjectVariantAssignment>
185 void Assign(ILookup<ITemplateVariant, ProjectVariantAssignmentSettings> assignments);