SmartAPI
Open Source .NET RQL library for RedDot CMS / OpenText WSM Management Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
IPublicationSetting.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.Xml;
19 using erminas.SmartAPI.Exceptions;
20 using erminas.SmartAPI.Utils;
21 using erminas.SmartAPI.Utils.CachedCollections;
22 
23 namespace erminas.SmartAPI.CMS.Project.Publication
24 {
26  {
27  IIndexedRDList<string, IPublicationFolderSetting> ExportFolderSettings { get; }
28  ILanguageVariant LanguageVariant { get; }
29  IProjectVariant ProjectVariant { get; }
30  IPublicationPackage PublicationPackage { get;}
31  IEnumerable<IPublicationTarget> PublishingTargets { get; }
32  void SetPublishingTargetsAndCommit(List<IPublicationTarget> newTargets);
33  }
34 
35  internal class PublicationSetting : RedDotProjectObject, IPublicationSetting
36  {
37  private readonly NameIndexedRDList<IPublicationFolderSetting> _exportFolderSettings;
38  private List<IPublicationTarget> _publishingTargets;
39  private readonly IPublicationSettings _publicationSettings;
40 
41  internal PublicationSetting(IPublicationSettings settings, XmlElement xmlElement)
42  : base(settings.Project, xmlElement)
43  {
44  _exportFolderSettings = new NameIndexedRDList<IPublicationFolderSetting>(LoadExportFolderSettings,
45  Caching.Enabled);
46  _publicationSettings = settings;
47  LoadXml();
48  }
49 
50  public IIndexedRDList<string, IPublicationFolderSetting> ExportFolderSettings
51  {
52  get { return _exportFolderSettings; }
53  }
54 
55  public ILanguageVariant LanguageVariant { get; private set; }
56  public IProjectVariant ProjectVariant { get; private set; }
57 
58  public IPublicationPackage PublicationPackage { get { return _publicationSettings.PublicationPackage; } }
59 
60  public IEnumerable<IPublicationTarget> PublishingTargets
61  {
62  get { return _publishingTargets.ToList(); }
63  }
64 
65  public void SetPublishingTargetsAndCommit(List<IPublicationTarget> newTargets)
66  {
67  const string SAVE_EXPORT_TARGETS =
68  @"<PROJECT><EXPORTSETTING guid=""{0}""><EXPORTTARGETS action=""save"">{1}</EXPORTTARGETS></EXPORTSETTING></PROJECT>";
69  const string SINGLE_EXPORT_TARGET = @"<EXPORTTARGET guid=""{0}"" selected=""{1}"" />";
70 
71  string targets = newTargets.Aggregate("",
72  (current, curTarget) =>
73  current +
74  string.Format(SINGLE_EXPORT_TARGET, curTarget.Guid.ToRQLString(), "1"));
75  string removeTargets = _publishingTargets.Where(x => newTargets.All(y => y.Guid != x.Guid))
76  .Aggregate("",
77  (current, curTarget) =>
78  current +
79  string.Format(SINGLE_EXPORT_TARGET,
80  curTarget.Guid.ToRQLString(), "0"));
81 
82  XmlDocument xmlDoc =
83  PublicationPackage.Project.ExecuteRQL(string.Format(SAVE_EXPORT_TARGETS, Guid.ToRQLString(),
84  targets + removeTargets));
85 
86  if (!xmlDoc.InnerXml.Contains("ok"))
87  {
88  throw new SmartAPIException(Session.ServerLogin,
89  string.Format("Could not set publishing targets for {0}", this));
90  }
91  _exportFolderSettings.InvalidateCache();
92  }
93 
94  private List<IPublicationFolderSetting> LoadExportFolderSettings()
95  {
96  const string LIST_EXPORT_FOLDER_SETTINGS =
97  @"<TREESEGMENT type=""project.1710"" action=""load"" guid=""{0}"" descent=""project"" parentguid=""{1}""/>";
98  XmlDocument xmlDoc =
99  PublicationPackage.Project.ExecuteRQL(string.Format(LIST_EXPORT_FOLDER_SETTINGS, Guid.ToRQLString(),
100  PublicationPackage.Guid.ToRQLString()));
101 
102  return (from XmlElement curSegment in xmlDoc.GetElementsByTagName("SEGMENT")
103  select
104  (IPublicationFolderSetting)
105  new PublicationFolderSetting(this, curSegment.GetGuid())
106  {
107  Name = curSegment.GetAttributeValue("value")
108  }).ToList();
109  }
110 
111  private void LoadXml()
112  {
113  ProjectVariant = ProjectVariantFactory.CreateFromGuid(PublicationPackage.Project,
114  _xmlElement.GetGuid("projectvariantguid"));
115 
116  _name = _xmlElement.GetAttributeValue("projectvariantname") + "/" +
117  _xmlElement.GetAttributeValue("languagevariantname");
118  LanguageVariant =
119  PublicationPackage.Project.LanguageVariants.GetByGuid(_xmlElement.GetGuid("languagevariantguid"));
120  XmlNodeList exportTargets = (XmlElement).GetElementsByTagName("EXPORTTARGET");
121  _publishingTargets = (from XmlElement curTarget in exportTargets
122  select (IPublicationTarget) new PublicationTarget(Project, curTarget.GetGuid()))
123  .ToList();
124  }
125 
126  public void Delete()
127  {
128  const string DELETE = @"<PROJECT><EXPORTSETTING action=""deletesetting"" packetguid=""{0}"" guid=""{1}""/></PROJECT>";
129  Project.ExecuteRQL(DELETE.RQLFormat(PublicationPackage, this));
130 
131  _publicationSettings.InvalidateCache();
132  }
133  }
134 }