SmartAPI
Open Source .NET RQL library for RedDot CMS / OpenText WSM Management Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
IProjectVariantAssignments.cs
Go to the documentation of this file.
1 // SmartAPI - .Net programmatic access to RedDot servers
2 //
3 // Copyright (C) 2013 erminas GbR
4 //
5 // This program is free software: you can redistribute it and/or modify it
6 // under the terms of the GNU General Public License as published by the Free Software Foundation,
7 // either version 3 of the License, or (at your option) any later version.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 // See the GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License along with this program.
14 // If not, see <http://www.gnu.org/licenses/>.
15 
16 using System.Collections.Generic;
17 using System.Linq;
18 using System.Text;
19 using System.Xml;
20 using erminas.SmartAPI.Exceptions;
21 using erminas.SmartAPI.Utils;
22 using erminas.SmartAPI.Utils.CachedCollections;
23 
24 namespace erminas.SmartAPI.CMS.Project.ContentClasses
25 {
26  internal class ProjectVariantAssignment : IProjectVariantAssignment
27  {
28  private readonly IContentClass _contentClass;
29 
30  private readonly IProjectVariant _projectVariant;
31  private readonly ITemplateVariant _templateVariant;
32 
33  internal ProjectVariantAssignment(IContentClass contentClass, IProjectVariant projectVariant,
34  ITemplateVariant templateVariant)
35  {
36  _contentClass = contentClass;
37  _projectVariant = projectVariant;
38  _templateVariant = templateVariant;
39  }
40 
41  public IContentClass ContentClass
42  {
43  get { return _contentClass; }
44  }
45 
46  public IProjectVariant ProjectVariant
47  {
48  get { return _projectVariant; }
49  }
50 
51  public ITemplateVariant TemplateVariant
52  {
53  get { return _templateVariant; }
54  }
55 
56  public bool IsNotUsingTidy { get; internal set; }
57  public bool IsPublishing { get; internal set; }
58  }
59 
60  public interface IProjectVariantAssignment
61  {
62  IContentClass ContentClass { get; }
63  IProjectVariant ProjectVariant { get; }
64  ITemplateVariant TemplateVariant { get; }
65  bool IsNotUsingTidy { get; }
66  bool IsPublishing { get; }
67  }
68 
70  {
71  public IProjectVariant ProjectVariant { get; set; }
72  public ITemplateVariant TemplateVariant { get; set; }
73  public bool IsNotUsingTidy { get; set; }
74  public bool IsPublishing { get; set; }
75  }
76 
77  internal class ProjectVariantAssignments : IndexedCachedList<IProjectVariant, IProjectVariantAssignment>,
78  IProjectVariantAssignments
79  {
80  private readonly IContentClass _contentClass;
81 
82  internal ProjectVariantAssignments(IContentClass contentClass, Caching caching)
83  : base(assignment => assignment.ProjectVariant, Caching.Enabled)
84  {
85  _contentClass = contentClass;
86  RetrieveFunc = GetAssignments;
87  }
88 
89  public void Assign(ITemplateVariant templateVariant, ProjectVariantAssignmentSettings settings)
90  {
91  Assign(new Dictionary<ITemplateVariant, ProjectVariantAssignmentSettings> { { templateVariant, settings } });
92  }
93 
94  public void Assign(ILookup<ITemplateVariant, ProjectVariantAssignmentSettings> assignments)
95  {
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}"" />";
102 
103 
104  var builder = new StringBuilder();
105  foreach (var curEntry in assignments)
106  {
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));
109  }
110 
111  Project.ExecuteRQL(ASSIGN_PROJECT_VARIANT.RQLFormat(_contentClass, builder), RqlType.SessionKeyInProject);
112  InvalidateCache();
113  }
114 
115  public void Assign(IDictionary<ITemplateVariant, ProjectVariantAssignmentSettings> assignments)
116  {
117  Assign(assignments.ToLookup(x=>x.Key, x=>x.Value));
118  }
119 
120  public IContentClass ContentClass
121  {
122  get { return _contentClass; }
123  }
124 
125  public IProject Project
126  {
127  get { return _contentClass.Project; }
128  }
129 
130  public ISession Session
131  {
132  get { return _contentClass.Session; }
133  }
134 
138  private List<IProjectVariantAssignment> GetAssignments()
139  {
140  const string PROJECT_VARIANT_ASSIGNMENT =
141  @"<TEMPLATE guid=""{0}"" ><TEMPLATEVARIANTS withstylesheets=""1"" action=""projectvariantslist"" /></TEMPLATE>";
142 
143  try
144  {
145  var xmlDoc = Project.ExecuteRQL(PROJECT_VARIANT_ASSIGNMENT.RQLFormat(_contentClass),
146  RqlType.SessionKeyInProject);
147  using (new CachingContext<IProjectVariant>(Project.ProjectVariants, Caching.Enabled))
148  {
149  return (from XmlElement curElement in xmlDoc.GetElementsByTagName("TEMPLATEVARIANT")
150  select
151  (IProjectVariantAssignment)
152  new ProjectVariantAssignment(_contentClass,
153  Project.ProjectVariants.GetByGuid(
154  curElement.GetGuid("projectvariantguid")),
155  new TemplateVariant(_contentClass, curElement.GetGuid()))
156  {
157  IsNotUsingTidy = curElement.GetBoolAttributeValue("donotusetidy").GetValueOrDefault(),
158  IsPublishing = !curElement.GetBoolAttributeValue("donotgenerate").GetValueOrDefault()
159  })
160  .ToList();
161  }
162  } catch (RQLException e)
163  {
164  throw new SmartAPIException(Session.ServerLogin, "Could not get project variant assignments", e);
165  }
166  }
167 
168  IEnumerable<IProjectVariantAssignment> IProjectVariantAssignments.this[ITemplateVariant templateVariant]
169  {
170  get
171  {
172  return
173  (from curAssignment in this
174  where curAssignment.TemplateVariant.Equals(templateVariant)
175  select curAssignment).ToList();
176  }
177  }
178  }
179 
181  IIndexedCachedList<IProjectVariant, IProjectVariantAssignment>
182  {
183  void Assign(ITemplateVariant templateVariant, ProjectVariantAssignmentSettings projectVariant);
184  //void Assign(IDictionary<ITemplateVariant, ProjectVariantAssignmentSettings> assignments);
185  void Assign(ILookup<ITemplateVariant, ProjectVariantAssignmentSettings> assignments);
186  IContentClass ContentClass { get; }
187 
188  IEnumerable<IProjectVariantAssignment> this[ITemplateVariant templateVariant] { get; }
189  }
190 }