SmartAPI
Open Source .NET RQL library for RedDot CMS / OpenText WSM Management Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
IRedDotObject.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.Xml;
18 using erminas.SmartAPI.Exceptions;
19 using erminas.SmartAPI.Utils;
20 
21 namespace erminas.SmartAPI.CMS
22 {
32  internal abstract class RedDotObject : AbstractAttributeContainer, IRedDotObject
33  {
34  protected static XmlDocument XMLDoc = new XmlDocument();
35 
36  private Guid _guid = Guid.Empty;
37  protected string _name;
38 
39  protected RedDotObject(ISession session) : base(session)
40  {
41  }
42 
43  protected RedDotObject(ISession session, Guid guid) : base(session)
44  {
45  Guid = guid;
46  }
47 
55  protected RedDotObject(ISession session, XmlElement xmlElement) : base(session, xmlElement)
56  {
57  InitGuidAndName();
58  }
59 
65  public override bool Equals(object other)
66  {
67  var o = other as IRedDotObject;
68  if (o == null)
69  {
70  return false;
71  }
72  return ReferenceEquals(this, other) || o.Guid.Equals(_guid);
73  }
74 
75  public override int GetHashCode()
76  {
77  return _guid.GetHashCode();
78  }
79 
80  public Guid Guid
81  {
82  get
83  {
84  if (_guid.Equals(Guid.Empty) && _xmlElement != null)
85  {
86  InitIfPresent(ref _guid, "guid", GuidConvert);
87  }
88  return _guid;
89  }
90  internal set
91  {
92  if (_xmlElement != null)
93  {
94  _xmlElement.Attributes["guid"].Value = value.ToRQLString();
95  }
96  _guid = value;
97  }
98  }
99 
103  public static Guid GuidConvert(string str)
104  {
105  return new Guid(str);
106  }
107 
108  public virtual string Name
109  {
110  get { return _name; }
111  internal set { _name = value; }
112  }
113 
114  public override string ToString()
115  {
116  return Name + " (" + Guid.ToRQLString() + ")";
117  }
118 
127  protected internal static string GetSaveString(XmlElement xmlElement)
128  {
129  XmlAttributeCollection attributes = xmlElement.Attributes;
130  foreach (XmlAttribute curAttr in attributes)
131  {
132  if (string.IsNullOrEmpty(curAttr.Value))
133  {
134  curAttr.Value = RQL.SESSIONKEY_PLACEHOLDER;
135  }
136  }
137 
138  xmlElement.AddAttribute("action", "save");
139 
140  return xmlElement.NodeToString();
141  }
142 
149  protected static bool BoolConvert(string str)
150  {
151  switch (str.Trim())
152  {
153  case "1":
154  return true;
155  case "0":
156  return false;
157  default:
158  throw new Exception("Illegal value for bool '" + str + "', '1' or '0' expected");
159  }
160  }
161 
169  protected void EnsuredInit<T>(ref T variable, string attributeName, Func<string, T> converter)
170  {
171  string value = _xmlElement.GetAttributeValue(attributeName);
172  if (string.IsNullOrEmpty(value))
173  {
174  throw new SmartAPIException(Session.ServerLogin,
175  string.Format("Missing value for attribute {0}", attributeName));
176  }
177  variable = converter(value);
178  }
179 
180  protected void InitGuidAndName()
181  {
182  InitIfPresent(ref _guid, "guid", GuidConvert);
183  InitIfPresent(ref _name, "name", x => x);
184  }
185 
196  protected void InitIfPresent<T>(ref T variable, string attributeName, Func<string, T> converter)
197  {
198  string value = _xmlElement.GetAttributeValue(attributeName);
199  if (!string.IsNullOrEmpty(value))
200  {
201  variable = converter(value);
202  }
203  }
204 
208  protected static bool? NullableBoolConvert(string str)
209  {
210  return BoolConvert(str);
211  }
212  }
213 
214  public interface IRedDotObject
215  {
216  Guid Guid { get; }
217 
218  string Name { get; }
219  }
220 }