SmartAPI
Open Source .NET RQL library for RedDot CMS / OpenText WSM Management Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
Pages/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.Linq;
19 using System.Xml;
20 using erminas.SmartAPI.CMS.Project.ContentClasses.Elements;
21 using erminas.SmartAPI.Utils;
22 using erminas.SmartAPI.Utils.CachedCollections;
23 
24 namespace erminas.SmartAPI.CMS.Project.Pages.Elements
25 {
26  public interface IOptionList : IValueElement<IOptionListEntry>
27  {
28  bool HasDefaultValue { get; }
29  IRDEnumerable<IOptionListEntry> PossibleValues { get; }
30  string ValueString { get; }
31  }
32 
33  public interface IOptionListEntry : IRedDotObject
34  {
35  bool IsDefault { get; }
36  string Value { get; }
37  }
38 
39  internal class OptionListEntry : IOptionListEntry
40  {
41  public OptionListEntry(Guid defaultGuid, XmlElement element)
42  {
43  Guid = element.GetGuid("guid");
44  Name = element.GetAttributeValue("description");
45  Value = element.GetAttributeValue("value");
46  IsDefault = defaultGuid == Guid;
47  }
48 
49  public Guid Guid { get; private set; }
50  public bool IsDefault { get; private set; }
51  public string Name { get; private set; }
52  public string Value { get; private set; }
53  }
54 
55  [PageElementType(ElementType.OptionList)]
56  internal class OptionList : PageElement, IOptionList
57  {
58  private List<IOptionListEntry> _entries;
59  private IOptionListEntry _value;
60 
61  public OptionList(IProject project, Guid guid, ILanguageVariant languageVariant)
62  : base(project, guid, languageVariant)
63  {
64  }
65 
66  internal OptionList(IProject project, XmlElement xmlElement) : base(project, xmlElement)
67  {
68  if (xmlElement.GetElementsByTagName("SELECTIONS").Count != 1)
69  {
70  IsInitialized = false;
71  XmlElement = null;
72  }
73  else
74  {
75  LoadXml();
76  }
77  }
78 
79  public void Commit()
80  {
81  const string SAVE_VALUE =
82  @"<ELEMENTS translationmode=""0"" action=""save"" reddotcacheguid="""" ><ELT guid=""{0}"" extendedinfo="""" reddotcacheguid="""" value=""{1}"" ></ELT></ELEMENTS>";
83  using (new LanguageContext(LanguageVariant))
84  {
85  string value = _value == null ? RQL.SESSIONKEY_PLACEHOLDER : _value.Guid.ToRQLString();
86  string query = SAVE_VALUE.RQLFormat(this, value);
87  Project.ExecuteRQL(query);
88  }
89  }
90 
91  public bool HasDefaultValue
92  {
93  get { return Value != null && string.IsNullOrEmpty(XmlElement.GetAttributeValue("value")); }
94  }
95 
96  public IRDEnumerable<IOptionListEntry> PossibleValues
97  {
98  get { return LazyLoad(ref _entries).ToRDEnumerable(); }
99  }
100 
101  public IOptionListEntry Value
102  {
103  get { return LazyLoad(ref _value); }
104  set { _value = value; }
105  }
106 
107  public string ValueString
108  {
109  get { return Value == null ? null : Value.Value; }
110  }
111 
112  protected override sealed void LoadWholePageElement()
113  {
114  LoadXml();
115  }
116 
117  protected override sealed XmlElement RetrieveWholeObject()
118  {
119  using (new LanguageContext(LanguageVariant))
120  {
121  const string RETRIEVE_OPTION_LIST =
122  @"<ELT action=""load"" subelements=""1"" guid=""{0}"" ><SELECTIONS action=""list""/></ELT>";
123 
124  return
125  (XmlElement)
126  Project.ExecuteRQL(string.Format(RETRIEVE_OPTION_LIST, Guid.ToRQLString()))
127  .GetElementsByTagName("ELT")[0];
128  }
129  }
130 
131  private void LoadXml()
132  {
133  Guid defaultGuid;
134  _xmlElement.TryGetGuid("eltdefaultselectionguid", out defaultGuid);
135 
136  XmlNodeList elements = _xmlElement.GetElementsByTagName("SELECTION");
137  _entries =
138  (from XmlElement curElement in elements
139  select (IOptionListEntry) new OptionListEntry(defaultGuid, curElement)).ToList();
140 
141  Guid selectedGuid;
142  _value = _xmlElement.TryGetGuid("value", out selectedGuid)
143  ? _entries.FirstOrDefault(entry => entry.Guid == selectedGuid)
144  : _entries.FirstOrDefault(entry => entry.IsDefault);
145  }
146  }
147 }