SmartAPI
Open Source .NET RQL library for RedDot CMS / OpenText WSM Management Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
ITemplateVariants.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;
17 using System.Collections.Generic;
18 using System.Linq;
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 TemplateVariants : NameIndexedRDList<ITemplateVariant>, ITemplateVariants
27  {
28  private readonly IContentClass _contentClass;
29 
30  internal TemplateVariants(IContentClass contentClass, Caching caching) : base(caching)
31  {
32  _contentClass = contentClass;
33  RetrieveFunc = GetTemplateVariants;
34  }
35 
36  public IContentClass ContentClass
37  {
38  get { return _contentClass; }
39  }
40 
41  public void CreateTemplateVariant(TemplateVariantCreationOptions options)
42  {
43  if (!options.IsValid)
44  {
45  throw new SmartAPIException(Session.ServerLogin, "Missing Name value for template creation");
46  }
47 
48  const string CREATE_TEMPLATE_VARIANT = @"<TEMPLATE action=""assign"" guid=""{0}"">
49 <TEMPLATEVARIANTS action=""addnew"">
50 <TEMPLATEVARIANT name=""{1}"" description="""" code=""{2}"" fileextension=""{3}"" insertstylesheetinpage=""{4}"" nostartendmarkers=""{5}"" containerpagereference=""{6}"">{2}</TEMPLATEVARIANT>
51 </TEMPLATEVARIANTS>
52 </TEMPLATE>";
53 
54  var command = CREATE_TEMPLATE_VARIANT.SecureRQLFormat(ContentClass, options.Name, options.Data,
55  options.FileExtension, options.IsStylesheetIncludedInHeader, !options.ContainsAreaMarksInPage,
56  options.HasContainerPageReference);
57 
58  Project.ExecuteRQL(command, RqlType.SessionKeyInProject);
59 
60  InvalidateCache();
61  }
62 
63  public IProject Project
64  {
65  get { return _contentClass.Project; }
66  }
67 
68  public ISession Session
69  {
70  get { return _contentClass.Session; }
71  }
72 
73  private List<ITemplateVariant> GetTemplateVariants()
74  {
75  const string LIST_CC_TEMPLATES =
76  @"<PROJECT><TEMPLATE guid=""{0}""><TEMPLATEVARIANTS action=""list"" withstylesheets=""0""/></TEMPLATE></PROJECT>";
77  var xmlDoc = Project.ExecuteRQL(LIST_CC_TEMPLATES.RQLFormat(_contentClass));
78  var variants = xmlDoc.GetElementsByTagName("TEMPLATEVARIANT");
79  return
80  (from XmlElement curVariant in variants
81  select (ITemplateVariant) new TemplateVariant(_contentClass, curVariant)).ToList();
82  }
83  }
84 
86  {
87  public TemplateVariantCreationOptions(string name)
88  {
89  Name = name;
90  }
91 
92  public string Name { get; set; }
93  public string FileExtension { get; set; }
94  public bool IsStylesheetIncludedInHeader { get; set; }
95  public bool HasContainerPageReference { get; set; }
96  public bool ContainsAreaMarksInPage { get; set; }
97  public bool IsValid { get { return Name != null; }}
98  public string Data { get; set; }
99  }
100 
101  public interface ITemplateVariants : IIndexedRDList<string, ITemplateVariant>, IProjectObject
102  {
103  IContentClass ContentClass { get; }
104  void CreateTemplateVariant(TemplateVariantCreationOptions options);
105  }
106 }