SmartAPI
Open Source .NET RQL library for RedDot CMS / OpenText WSM Management Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
ILanguageVariants.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
24 {
25  public interface ILanguageVariants : IIndexedRDList<string, ILanguageVariant>, IProjectObject
26  {
27  ILanguageVariant Current { get; set; }
28  ILanguageVariant Main { get; }
29  }
30 
31  internal class LanguageVariants : IndexedRDList<string, ILanguageVariant>, ILanguageVariants
32  {
33  private readonly IProject _project;
34  private ILanguageVariant _currentLanguageVariant;
35 
36  internal LanguageVariants(IProject project, Caching caching) : base(variant => variant.Abbreviation, caching)
37  {
38  _project = project;
39  RetrieveFunc = GetLanguageVariants;
40  }
41 
45  public ILanguageVariant Current
46  {
47  get
48  {
49  EnsureListIsLoaded();
50  return _currentLanguageVariant;
51  }
52  set
53  {
54  if (_currentLanguageVariant == value)
55  {
56  return;
57  }
58  const string SELECT_LANGUAGE = @"<LANGUAGEVARIANT action=""setactive"" guid=""{0}""/>";
59  XmlDocument xmlDoc = _project.ExecuteRQL(SELECT_LANGUAGE.RQLFormat(value.Guid.ToRQLString()),
60  RqlType.SessionKeyInProject);
61  if (!xmlDoc.IsContainingOk())
62  {
63  throw new SmartAPIException(Session.ServerLogin,
64  string.Format("Could not load language variant '{0}' for project {1}",
65  value.Abbreviation, this));
66  }
67  if (_currentLanguageVariant != null)
68  {
69  ((LanguageVariant) _currentLanguageVariant).IsCurrentLanguageVariant = false;
70  }
71  ((LanguageVariant) value).IsCurrentLanguageVariant = true;
72  _currentLanguageVariant = value;
73  }
74  }
75 
76  public ILanguageVariant Main
77  {
78  get { return this.First(variant => variant.IsMainLanguage); }
79  }
80 
81  public IProject Project
82  {
83  get { return _project; }
84  }
85 
86  public ISession Session
87  {
88  get { return _project.Session; }
89  }
90 
91  private List<ILanguageVariant> GetLanguageVariants()
92  {
93  const string LIST_LANGUAGE_VARIANTS =
94  @"<PROJECT projectguid=""{0}""><LANGUAGEVARIANTS action=""list""/></PROJECT>";
95  XmlDocument xmlDoc = Project.ExecuteRQL(LIST_LANGUAGE_VARIANTS.RQLFormat(Project));
96  XmlNodeList xmlNodes = xmlDoc.GetElementsByTagName("LANGUAGEVARIANT");
97  var languageVariants = new List<ILanguageVariant>();
98 
99  foreach (XmlElement curNode in xmlNodes)
100  {
101  var variant = new LanguageVariant(Project, curNode);
102  languageVariants.Add(variant);
103  if (variant.IsCurrentLanguageVariant)
104  {
105  _currentLanguageVariant = variant;
106  }
107  }
108 
109  return languageVariants;
110  }
111  }
112 }