18 using erminas.SmartAPI.Exceptions;
19 using erminas.SmartAPI.Utils;
21 namespace erminas.SmartAPI.CMS
32 internal abstract class RedDotObject : AbstractAttributeContainer, IRedDotObject
34 protected static XmlDocument XMLDoc =
new XmlDocument();
36 private Guid _guid = Guid.Empty;
37 protected string _name;
39 protected RedDotObject(ISession session) : base(session)
43 protected RedDotObject(ISession session, Guid guid) : base(session)
55 protected RedDotObject(ISession session, XmlElement xmlElement) : base(session, xmlElement)
65 public override bool Equals(
object other)
67 var o = other as IRedDotObject;
72 return ReferenceEquals(
this, other) || o.Guid.Equals(_guid);
75 public override int GetHashCode()
77 return _guid.GetHashCode();
84 if (_guid.Equals(Guid.Empty) && _xmlElement != null)
86 InitIfPresent(ref _guid,
"guid", GuidConvert);
92 if (_xmlElement != null)
94 _xmlElement.Attributes[
"guid"].Value = value.ToRQLString();
103 public static Guid GuidConvert(
string str)
105 return new Guid(str);
108 public virtual string Name
110 get {
return _name; }
111 internal set { _name = value; }
114 public override string ToString()
116 return Name +
" (" + Guid.ToRQLString() +
")";
127 protected internal static string GetSaveString(XmlElement xmlElement)
129 XmlAttributeCollection attributes = xmlElement.Attributes;
130 foreach (XmlAttribute curAttr
in attributes)
132 if (
string.IsNullOrEmpty(curAttr.Value))
134 curAttr.Value = RQL.SESSIONKEY_PLACEHOLDER;
138 xmlElement.AddAttribute(
"action",
"save");
140 return xmlElement.NodeToString();
149 protected static bool BoolConvert(
string str)
158 throw new Exception(
"Illegal value for bool '" + str +
"', '1' or '0' expected");
169 protected void EnsuredInit<T>(ref T variable,
string attributeName, Func<string, T> converter)
171 string value = _xmlElement.GetAttributeValue(attributeName);
172 if (
string.IsNullOrEmpty(value))
175 string.Format(
"Missing value for attribute {0}", attributeName));
177 variable = converter(value);
180 protected void InitGuidAndName()
182 InitIfPresent(ref _guid,
"guid", GuidConvert);
183 InitIfPresent(ref _name,
"name", x => x);
196 protected void InitIfPresent<T>(ref T variable,
string attributeName, Func<string, T> converter)
198 string value = _xmlElement.GetAttributeValue(attributeName);
199 if (!
string.IsNullOrEmpty(value))
201 variable = converter(value);
208 protected static bool? NullableBoolConvert(
string str)
210 return BoolConvert(str);
214 public interface IRedDotObject