19 using erminas.SmartAPI.Exceptions;
20 using erminas.SmartAPI.Utils;
22 namespace erminas.SmartAPI.CMS.Project.Pages.Elements
24 internal abstract class AbstractValueElement<T> : PageElement, IValueElement<T>
26 protected const string SAVE_VALUE =
27 @"<ELEMENTS action=""save""><ELT guid=""{0}"" value=""{1}"" type=""{2}""></ELT></ELEMENTS>";
31 protected AbstractValueElement(IProject project, Guid guid, ILanguageVariant languageVariant)
32 : base(project, guid, languageVariant)
36 protected AbstractValueElement(IProject project, XmlElement xmlElement) : base(project, xmlElement)
41 public virtual void Commit()
44 string xmlNodeValue = GetXmlNodeValue();
45 string htmlEncode =
string.IsNullOrEmpty(xmlNodeValue)
46 ? RQL.SESSIONKEY_PLACEHOLDER
47 : HttpUtility.HtmlEncode(xmlNodeValue);
48 ExecuteCommit(htmlEncode);
51 public void DeleteValue()
56 public void SetValueFromString(
string value)
58 Value =
string.IsNullOrEmpty(value) ?
default(T) : FromString(value);
61 public virtual T Value
63 get {
return LazyLoad(ref _value); }
64 set { _value = value; }
67 protected void ExecuteCommit(
string valueToSave)
72 Project.ExecuteRQL(
string.Format(SAVE_VALUE, Guid.ToRQLString(), valueToSave, (int)
ElementType));
73 if (xmlDoc.GetElementsByTagName(
"ELT").Count != 1 && !xmlDoc.InnerXml.Contains(Guid.ToRQLString()))
76 String.Format(
"Could not save element {0}", Guid.ToRQLString()));
81 protected abstract T FromString(
string value);
82 protected abstract T FromXmlNodeValue(
string arg);
84 protected virtual string GetXmlNodeValue()
86 return Equals(Value, null) ? null : Value.ToString();
89 protected override sealed
void LoadWholePageElement()
92 LoadWholeValueElement();
95 protected abstract void LoadWholeValueElement();
97 private void LoadXml()
99 InitIfPresent(ref _value,
"value", FromXmlNodeValue);