SmartAPI
Open Source .NET RQL library for RedDot CMS / OpenText WSM Management Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
IPartialRedDotObject.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.Runtime.CompilerServices;
19 using System.Xml;
20 using erminas.SmartAPI.CMS.Project;
21 using erminas.SmartAPI.CMS.Project.ContentClasses;
22 using erminas.SmartAPI.Utils;
23 
24 namespace erminas.SmartAPI.CMS
25 {
27  {
28  void EnsureInitialization();
29  void Refresh();
30  }
31 
33  {
34  }
35 
36  //public interface ILanguageDependentPartialRedDotObject : IPartialRedDotProjectObject,
37  // ILanguageDependentXmlBasedObject
38  //{
39 
40  //}
41 
68  internal abstract class PartialRedDotObject : RedDotObject, IPartialRedDotObject
69  {
76  protected PartialRedDotObject(ISession session, XmlElement xmlElement) : base(session, xmlElement)
77  {
78  IsInitialized = true;
79  }
80 
87  protected PartialRedDotObject(ISession session, Guid guid) : base(session, guid)
88  {
89  IsInitialized = false;
90  }
91 
92  protected PartialRedDotObject(ISession session) : base(session)
93  {
94  IsInitialized = false;
95  }
96 
100  public bool IsInitialized { get; set; }
101 
108  protected T LazyLoad<T>(ref T value)
109  {
110  if (IsInitialized || !Equals(value, default(T)))
111  {
112  return value;
113  }
114  Refresh();
115  return value;
116  }
117 
118  protected abstract void LoadWholeObject();
119 
123  protected abstract XmlElement RetrieveWholeObject();
124 
125  #region IPartialRedDotObject Members
126 
127  public void EnsureInitialization()
128  {
129  if (!IsInitialized)
130  {
131  Refresh();
132  }
133  }
134 
135  public override sealed string Name
136  {
137  get { return LazyLoad(ref _name); }
138  internal set { base.Name = value; }
139  }
140 
141  public virtual void Refresh()
142  {
143  XmlElement = (XmlElement) RetrieveWholeObject().Clone();
144  InitGuidAndName();
145  LoadWholeObject();
146 
147  IsInitialized = true;
148  }
149 
150  public override sealed XmlElement XmlElement
151  {
152  get
153  {
154  EnsureInitialization();
155  return base.XmlElement;
156  }
157  protected internal set
158  {
159  base.XmlElement = value;
160  _readWriteWrapper = new XmlReadWriteWrapper(_xmlElement, new Dictionary<string, string>());
161  }
162  }
163 
164  protected override sealed T GetAttributeValue<T>([CallerMemberName] string callerName = "")
165  {
166  EnsureInitialization();
167 // ReSharper disable ExplicitCallerInfoArgument
168  return base.GetAttributeValue<T>(callerName);
169 // ReSharper restore ExplicitCallerInfoArgument
170  }
171 
172  protected override sealed void SetAttributeValue<T>(T value, [CallerMemberName] string callerName = "")
173  {
174  EnsureInitialization();
175 // ReSharper disable ExplicitCallerInfoArgument
176  base.SetAttributeValue(value, callerName);
177 // ReSharper restore ExplicitCallerInfoArgument
178  }
179 
180  #endregion
181  }
182 
183  internal abstract class LanguageDependentPartialRedDotProjectObject : PartialRedDotProjectObject,
184  ILanguageDependentXmlBasedObject
185  {
186  //protected readonly Dictionary<string, object> _languageDependendValues = new Dictionary<string, object>();
187 
188  //private readonly Dictionary<string, XmlElement> _languageDependentWriteXmlElements =
189  // new Dictionary<string, XmlElement>();
190 
191  //private readonly Dictionary<string, XmlElement> _languageDependentXmlElements =
192  // new Dictionary<string, XmlElement>();
193 
194  private readonly Dictionary<string, XmlReadWriteWrapper> _languageDepdentXmlElements =
195  new Dictionary<string, XmlReadWriteWrapper>();
196 
197  protected LanguageDependentPartialRedDotProjectObject(IProject project, XmlElement xmlElement)
198  : base(project, xmlElement)
199  {
200  }
201 
202  protected LanguageDependentPartialRedDotProjectObject(IProject project, Guid guid) : base(project, guid)
203  {
204  }
205 
206  protected LanguageDependentPartialRedDotProjectObject(IProject project) : base(project)
207  {
208  }
209 
210  public IXmlReadWriteWrapper GetElementForLanguage(string languageAbbreviation)
211  {
212  var element = _languageDepdentXmlElements.GetOrAdd(languageAbbreviation, () =>
213  {
214  using (new LanguageContext(Project.LanguageVariants[languageAbbreviation]))
215  {
216  var read = (XmlElement) RetrieveWholeObject().Clone();
217  return new XmlReadWriteWrapper(read, _readWriteWrapper.WrittenValues);
218  }
219  });
220 
221  return element;
222  }
223 
224  public override void Refresh()
225  {
226  _languageDepdentXmlElements.Clear();
227  base.Refresh();
228  }
229 
230  //public XmlElement XmlElementForCurrentLanguage
231  //{
232  // get { return GetXmlElementForLanguage(Project.LanguageVariants.Current.Abbreviation); }
233  //}
234 
235  //public XmlElement XmlElementForMainLanguage
236  //{
237  // get { return GetXmlElementForLanguage(Project.LanguageVariants.Main.Abbreviation); }
238  //}
239 
240  //private static XmlElement GetWriteMergedIntoRead(XmlElement writeElement, XmlElement readElement)
241  //{
242  // var mergedElement = (XmlElement) readElement.Clone();
243  // foreach (XmlAttribute curAttribute in writeElement.Attributes)
244  // {
245  // mergedElement.SetAttributeValue(curAttribute.Name, curAttribute.Value);
246  // }
247 
248  // return mergedElement;
249  //}
250  }
251 }