SmartAPI
Open Source .NET RQL library for RedDot CMS / OpenText WSM Management Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
AbstractValueElement.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.Web;
18 using System.Xml;
19 using erminas.SmartAPI.Exceptions;
20 using erminas.SmartAPI.Utils;
21 
22 namespace erminas.SmartAPI.CMS.Project.Pages.Elements
23 {
24  internal abstract class AbstractValueElement<T> : PageElement, IValueElement<T>
25  {
26  protected const string SAVE_VALUE =
27  @"<ELEMENTS action=""save""><ELT guid=""{0}"" value=""{1}"" type=""{2}""></ELT></ELEMENTS>";
28 
29  protected T _value;
30 
31  protected AbstractValueElement(IProject project, Guid guid, ILanguageVariant languageVariant)
32  : base(project, guid, languageVariant)
33  {
34  }
35 
36  protected AbstractValueElement(IProject project, XmlElement xmlElement) : base(project, xmlElement)
37  {
38  LoadXml();
39  }
40 
41  public virtual void Commit()
42  {
43  //TODO bei null/"" SESSIONKEY setzen??
44  string xmlNodeValue = GetXmlNodeValue();
45  string htmlEncode = string.IsNullOrEmpty(xmlNodeValue)
46  ? RQL.SESSIONKEY_PLACEHOLDER
47  : HttpUtility.HtmlEncode(xmlNodeValue);
48  ExecuteCommit(htmlEncode);
49  }
50 
51  public void DeleteValue()
52  {
53  Value = default(T);
54  }
55 
56  public void SetValueFromString(string value)
57  {
58  Value = string.IsNullOrEmpty(value) ? default(T) : FromString(value);
59  }
60 
61  public virtual T Value
62  {
63  get { return LazyLoad(ref _value); }
64  set { _value = value; }
65  }
66 
67  protected void ExecuteCommit(string valueToSave)
68  {
69  using (new LanguageContext(LanguageVariant))
70  {
71  XmlDocument xmlDoc =
72  Project.ExecuteRQL(string.Format(SAVE_VALUE, Guid.ToRQLString(), valueToSave, (int) ElementType));
73  if (xmlDoc.GetElementsByTagName("ELT").Count != 1 && !xmlDoc.InnerXml.Contains(Guid.ToRQLString()))
74  {
75  throw new SmartAPIException(Session.ServerLogin,
76  String.Format("Could not save element {0}", Guid.ToRQLString()));
77  }
78  }
79  }
80 
81  protected abstract T FromString(string value);
82  protected abstract T FromXmlNodeValue(string arg);
83 
84  protected virtual string GetXmlNodeValue()
85  {
86  return Equals(Value, null) ? null : Value.ToString();
87  }
88 
89  protected override sealed void LoadWholePageElement()
90  {
91  LoadXml();
92  LoadWholeValueElement();
93  }
94 
95  protected abstract void LoadWholeValueElement();
96 
97  private void LoadXml()
98  {
99  InitIfPresent(ref _value, "value", FromXmlNodeValue);
100  }
101  }
102 }