SmartAPI
Open Source .NET RQL library for RedDot CMS / OpenText WSM Management Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
ILanguageDependentValue.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.Linq;
17 using erminas.SmartAPI.CMS.Project;
18 using erminas.SmartAPI.CMS.Project.ContentClasses;
19 using erminas.SmartAPI.Exceptions;
20 
21 namespace erminas.SmartAPI.CMS
22 {
23  public interface ILanguageDependentValueBase
24  {
26  }
27 
28  public interface ILanguageDependentReadValue<out T> : ILanguageDependentValueBase
29  {
30  T ForCurrentLanguage { get; }
31  T ForMainLanguage { get; }
32  T this[ILanguageVariant languageVariant] { get; }
33  T this[string languageAbbreviation] { get; }
34  }
35 
36  public interface ILanguageDependentValue<T> : ILanguageDependentReadValue<T>
37  {
38  T ForAllLanguages { set; }
39  new T ForCurrentLanguage { get; set; }
40  new T ForMainLanguage { get; set; }
41  new T this[ILanguageVariant languageVariant] { get; set; }
42  new T this[string languageAbbreviation] { get; set; }
43  void SetAllLanguagesFrom(ILanguageDependentValue<T> other);
44  }
45 
46  internal abstract class AbstractLanguageDependendReadValue<T> : ILanguageDependentReadValue<T>
47  {
48  protected AbstractLanguageDependendReadValue(IPartialRedDotProjectObject parent)
49  {
50  Parent = parent;
51  }
52 
53  public T ForCurrentLanguage
54  {
55  get { return this[Parent.Project.LanguageVariants.Current]; }
56  }
57 
58  public T ForMainLanguage
59  {
60  get { return this[Parent.Project.LanguageVariants.Main]; }
61  }
62 
63  public T this[ILanguageVariant languageVariant]
64  {
65  get { return this[languageVariant.Abbreviation]; }
66  }
67 
68  public abstract T this[string languageAbbreviation] { get; }
69  public IPartialRedDotProjectObject Parent { get; private set; }
70  }
71 
72  internal class LanguageDependentValue<T> : ILanguageDependentValue<T>
73  {
74  private readonly RedDotAttribute _attribute;
75  private readonly IPartialRedDotProjectObject _parent;
76 
77  public LanguageDependentValue(IPartialRedDotProjectObject parent, RedDotAttribute attribute)
78  {
79  _parent = parent;
80  _attribute = attribute;
81  }
82 
83  public T ForAllLanguages
84  {
85  set
86  {
87  foreach (var curLanguage in _parent.Project.LanguageVariants)
88  {
89  this[curLanguage] = value;
90  }
91  }
92  }
93 
94  public T ForCurrentLanguage
95  {
96  get { return this[_parent.Project.LanguageVariants.Current]; }
97  set { this[_parent.Project.LanguageVariants.Current] = value; }
98  }
99 
100  public T ForMainLanguage
101  {
102  get { return this[_parent.Project.LanguageVariants.Main]; }
103  set { this[_parent.Project.LanguageVariants.Main] = value; }
104  }
105 
106  public T this[ILanguageVariant languageVariant]
107  {
108  get { return this[languageVariant.Abbreviation]; }
109  set { this[languageVariant.Abbreviation] = value; }
110  }
111 
112  public virtual T this[string languageAbbreviation]
113  {
114  get
115  {
116  var xmlElement = ((ILanguageDependentXmlBasedObject) _parent).GetElementForLanguage(languageAbbreviation);
117  return _attribute.ReadFrom<T>(_parent, xmlElement);
118  }
119  set
120  {
121  var element = ((ILanguageDependentXmlBasedObject) _parent).GetElementForLanguage(languageAbbreviation);
122  _attribute.WriteTo(_parent, element, value);
123  }
124  }
125 
126  public IPartialRedDotProjectObject Parent
127  {
128  get { return _parent; }
129  }
130 
131  public void SetAllLanguagesFrom(ILanguageDependentValue<T> value)
132  {
133  CheckLanguageVariantCompatibility(value);
134  foreach (var language in Parent.Project.LanguageVariants)
135  {
136  this[language] = value[language];
137  }
138  }
139 
140  private void CheckLanguageVariantCompatibility(ILanguageDependentValue<T> value)
141  {
142  if (_parent.Session == value.Parent.Session && _parent.Project.Equals(value.Parent.Project))
143  {
144  return;
145  }
146  if (
147  Parent.Project.LanguageVariants.Any(
148  lang => !value.Parent.Project.LanguageVariants.ContainsName(lang.Name)))
149  {
150  throw new SmartAPIException(Parent.Session.ServerLogin,
151  string.Format(
152  "Unable to assign values for all languages from project {0} to project {1} as the language variants are incompatible",
153  value.Parent.Project, _parent.Project));
154  }
155  }
156  }
157 }