SmartAPI
Open Source .NET RQL library for RedDot CMS / OpenText WSM Management Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
ContentClasses/Elements/IOptionList.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.Collections.ObjectModel;
19 using System.Linq;
20 using System.Xml;
21 using erminas.SmartAPI.CMS.Converter;
22 using erminas.SmartAPI.Utils;
23 
24 namespace erminas.SmartAPI.CMS.Project.ContentClasses.Elements
25 {
26  public enum SortMode
27  {
28  Ascending = 2,
29  Descending = 3
30  }
31 
33  {
34  IContentClassElement ChildElementOf { get; set; }
35  ILanguageDependentValue<string> DefaultValueString { get; }
36  string DescriptionInCurrentDisplayLanguage { get; set; }
37 
38  IEnumerable<IOptionListSelection> Entries { get; }
39 
40  //string EntriesAsString { get; set; }
41 
42  bool HasLanguageDependendNames { get; set; }
43  bool HasLanguageDependendValues { get; set; }
44  bool IsAllowingOtherValues { get; set; }
45  ILanguageDependentValue<string> SampleText { get; }
46  SortMode SortMode { get; set; }
47  }
48 
49  public interface IOptionListEntry
50  {
51  ILanguageVariant LanguageVariant { get; }
52  string Name { get; }
53  string Value { get; }
54  }
55 
56  internal class OptionListEntry : IOptionListEntry
57  {
58  public ILanguageVariant LanguageVariant { get; internal set; }
59  public string Name { get; internal set; }
60  public string Value { get; internal set; }
61  }
62 
64  {
65  IEnumerable<IOptionListEntry> Entries { get; }
66  Guid Guid { get; }
67  IOptionList OptionList { get; }
68  }
69 
70  internal sealed class OptionListSelection : IOptionListSelection
71  {
72  private readonly IOptionList _optionList;
73 
74  public OptionListSelection(IOptionList optionList, XmlElement element)
75  {
76  _optionList = optionList;
77  Guid = element.GetGuid();
78  Entries = GetEntries(element);
79  }
80 
81  public IEnumerable<IOptionListEntry> Entries { get; private set; }
82  public Guid Guid { get; private set; }
83 
84  public IOptionList OptionList
85  {
86  get { return _optionList; }
87  }
88 
89  public IProject Project
90  {
91  get { return OptionList.Project; }
92  }
93 
94  public ISession Session
95  {
96  get { return OptionList.Session; }
97  }
98 
99  private ReadOnlyCollection<OptionListEntry> GetEntries(XmlElement element)
100  {
101  return (from XmlElement curEntry in element.GetElementsByTagName("ITEM")
102  select
103  new OptionListEntry
104  {
105  LanguageVariant = Project.LanguageVariants[curEntry.GetAttributeValue("languageid")],
106  Name = curEntry.GetName(),
107  Value = curEntry.InnerText
108  }).ToList().AsReadOnly();
109  }
110  }
111 
112  internal class OptionList : ContentClassContentElement, IOptionList
113  {
114  private const string ELTDEFAULTVALUE = "eltdefaultvalue";
115 
116  internal OptionList(IContentClass contentClass, XmlElement xmlElement) : base(contentClass, xmlElement)
117  {
118  if (xmlElement.SelectSingleNode(@"//SELECTIONS") == null)
119  {
120  IsInitialized = false;
121  }
122 // ReSharper disable ObjectCreationAsStatement
123  //TODO neu implementieren:
124  //new OptionListSelectionAttribute(this, "eltoptionlistdata", xmlElement);
125 // ReSharper restore ObjectCreationAsStatement
126  }
127 
128  [RedDot("eltparentelementguid", ConverterType = typeof (ContentClassElementConverter))]
129  public IContentClassElement ChildElementOf
130  {
131  get { return GetAttributeValue<IContentClassElement>(); }
132  set { SetAttributeValue(value); }
133  }
134 
135  [RedDot(ELTDEFAULTVALUE)]
136  public ILanguageDependentValue<string> DefaultValueString
137  {
138  get { return GetAttributeValue<ILanguageDependentValue<string>>(); }
139  }
140 
141  [RedDot("eltrddescription")]
142  public string DescriptionInCurrentDisplayLanguage
143  {
144  get { return GetAttributeValue<string>(); }
145  set { SetAttributeValue(value); }
146  }
147 
148  public IEnumerable<IOptionListSelection> Entries
149  {
150  get
151  {
152  EnsureInitialization();
153  return (from XmlElement curEntry in _xmlElement.GetElementsByTagName("SELECTION")
154  select (IOptionListSelection) new OptionListSelection(this, curEntry)).ToList().AsReadOnly();
155  }
156  }
157 
158  //[RedDot("eltoptionlistdata")]
159  //public string EntriesAsString
160  //{
161  // get { return GetAttributeValue<string>(); }
162  // set { SetAttributeValue(value); }
163  //}
164 
165  [RedDot("eltlanguagedependentname")]
166  public bool HasLanguageDependendNames
167  {
168  get { return GetAttributeValue<bool>(); }
169  set { SetAttributeValue(value); }
170  }
171 
172  [RedDot("eltlanguagedependentvalue")]
173  public bool HasLanguageDependendValues
174  {
175  get { return GetAttributeValue<bool>(); }
176  set { SetAttributeValue(value); }
177  }
178 
179  [RedDot("eltuserdefinedallowed")]
180  public bool IsAllowingOtherValues
181  {
182  get { return GetAttributeValue<bool>(); }
183  set { SetAttributeValue(value); }
184  }
185 
186  [RedDot("eltrdexample")]
187  public ILanguageDependentValue<string> SampleText
188  {
189  get { return GetAttributeValue<ILanguageDependentValue<string>>(); }
190  }
191 
192  [RedDot("eltorderby", ConverterType = typeof (EnumConverter<SortMode>))]
193  public SortMode SortMode
194  {
195  get { return GetAttributeValue<SortMode>(); }
196  set { SetAttributeValue(value); }
197  }
198 
199  protected override XmlElement RetrieveWholeObject()
200  {
201  const string LOAD_OBJECT =
202  @"<TEMPLATE><ELEMENT action=""load"" guid=""{0}""><SELECTIONS action=""load"" guid=""{0}""/></ELEMENT></TEMPLATE>";
203 
204  return
205  Project.ExecuteRQL(LOAD_OBJECT.RQLFormat(this), RqlType.SessionKeyInProject).GetSingleElement("ELEMENT");
206  }
207  }
208 }