SmartAPI
Open Source .NET RQL library for RedDot CMS / OpenText WSM Management Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
AbstractAttributeContainer.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.Linq;
19 using System.Reflection;
20 using System.Runtime.CompilerServices;
21 using System.Xml;
22 using erminas.SmartAPI.CMS.Project;
23 using erminas.SmartAPI.Exceptions;
24 
25 namespace erminas.SmartAPI.CMS
26 {
27  public interface IAttributeAssignment
28  {
29  void AssignAllLanguageIndependentRedDotAttributes<T>(T source, T target);
30  void AssignAllRedDotAttributesForLanguage<T>(T source, T target, string language) where T : IProjectObject;
31  }
32 
34  {
35  public void AssignAllLanguageIndependentRedDotAttributes<T>(T source, T target)
36  {
37  var properties =
38  typeof (T).GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy);
39  foreach (var curProperty in properties)
40  {
41  if (!curProperty.CanRead || !curProperty.CanWrite)
42  {
43  continue;
44  }
45 
46  var attribute = GetRedDotAttribute(curProperty);
47  if (attribute == null || attribute.IsReadOnly)
48  {
49  continue;
50  }
51  try
52  {
53  CopyValue(source, target, curProperty);
54  } catch (Exception e)
55  {
56  throw new AttributeChangeException(curProperty.Name, e);
57  }
58  }
59  }
60 
61  public void AssignAllRedDotAttributesForLanguage<T>(T source, T target, string language)
62  where T : IProjectObject
63  {
64  var properties =
65  typeof (T).GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy);
66  foreach (var curProperty in properties)
67  {
68  if (!curProperty.CanRead)
69  {
70  continue;
71  }
72 
73  var attribute = GetRedDotAttribute(curProperty);
74  if (attribute == null || attribute.IsReadOnly)
75  {
76  continue;
77  }
78  try
79  {
80  CopyValueForLanguage(source, target, curProperty, language);
81  } catch (Exception e)
82  {
83  throw new AttributeChangeException(curProperty.Name, e);
84  }
85  }
86  }
87 
88  private static void CopyLanguageDependendValue<T>(T source, T target, PropertyInfo propertyInfo, string language)
89  where T : IProjectObject
90  {
91  var sourceValue = propertyInfo.GetValue(source, null);
92  var targetValue = propertyInfo.GetValue(target, null);
93 
94  var sourceLanguageIndexer = GetLanguageIndexer(sourceValue);
95  var targetLanguageIndexer = GetLanguageIndexer(targetValue);
96 
97  var value = sourceLanguageIndexer.GetValue(sourceLanguageIndexer, new object[] {language});
98 
99  targetLanguageIndexer.SetValue(targetLanguageIndexer, value, new object[] {language});
100  }
101 
102  private static void CopyValue<T>(T source, T target, PropertyInfo curProperty)
103  {
104  const bool DONT_SHOW_NON_PUBLIC_ACCESSORS = false;
105  var getMethod = curProperty.GetGetMethod(DONT_SHOW_NON_PUBLIC_ACCESSORS);
106  var setMethod = curProperty.GetSetMethod(DONT_SHOW_NON_PUBLIC_ACCESSORS);
107 
108  var value = getMethod.Invoke(source, new object[0]);
109 
110  setMethod.Invoke(target, new[] {value});
111  }
112 
113  private static void CopyValueForLanguage<T>(T source, T target, PropertyInfo propertyInfo, string language)
114  where T : IProjectObject
115  {
116  if (typeof (ILanguageDependentValueBase).IsAssignableFrom(propertyInfo.PropertyType))
117  {
118  CopyLanguageDependendValue(source, target, propertyInfo, language);
119  return;
120  }
121  CopyValue(source, target, propertyInfo);
122  }
123 
124  private static PropertyInfo GetLanguageIndexer(object @object)
125  {
126  return @object.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public).First(info =>
127  {
128  var param = info.GetIndexParameters().FirstOrDefault();
129  return param != null && param.ParameterType == typeof (string);
130  });
131  }
132 
133  private static RedDotAttribute GetRedDotAttribute(PropertyInfo curProperty)
134  {
135  const bool USE_INHERITED = true;
136  var attributes =
137  curProperty.GetCustomAttributes(typeof (RedDotAttribute), USE_INHERITED).Cast<RedDotAttribute>();
138  return attributes.FirstOrDefault();
139  }
140  }
141 
142  internal class AttributeChangeException : Exception
143  {
144  private readonly string _attributeName;
145 
146  public AttributeChangeException(string attributeName, Exception exception)
147  : base(string.Format("Could not change attribute {0}", attributeName), exception)
148  {
149  _attributeName = attributeName;
150  }
151 
152  public string AttributeName
153  {
154  get { return _attributeName; }
155  }
156  }
157 
158  internal abstract class AbstractAttributeContainer : ISessionObject
159  {
160  private readonly ISession _session;
161  protected XmlReadWriteWrapper _readWriteWrapper;
162  protected XmlElement _xmlElement;
163 
164  internal AbstractAttributeContainer(ISession session)
165  {
166  _session = session;
167  }
168 
169  internal XmlReadWriteWrapper XmlReadWriteWrapper { get { return _readWriteWrapper; } }
170 
171  internal AbstractAttributeContainer(ISession session, XmlElement xmlElement)
172  {
173  _session = session;
174  if (xmlElement == null)
175  {
176  throw new SmartAPIInternalException("XmlElement is null");
177  }
178  _xmlElement = xmlElement;
179  _readWriteWrapper = new XmlReadWriteWrapper(_xmlElement, new Dictionary<string, string>());
180  }
181 
182  public virtual ISession Session
183  {
184  get { return _session; }
185  }
186 
187  public virtual XmlElement XmlElement
188  {
189  get { return _xmlElement; }
190  protected internal set
191  {
192  _xmlElement = value;
193  _readWriteWrapper = new XmlReadWriteWrapper(_xmlElement, new Dictionary<string, string>());
194  }
195  }
196 
197  protected virtual T GetAttributeValue<T>([CallerMemberName] string callerName = "")
198  {
199  var project = this as IProjectObject;
200  var property = GetProperty(callerName);
201  var attribute = GetRedDotAttributeOfCallerMember(callerName);
202 
203  //TODO language depenece in die entsprechende klasse packen
204  if (IsLanguageDependentProperty(property))
205  {
206  return GetLanguageDependentProperty<T>(attribute, property);
207  }
208 
209  return attribute.ReadFrom<T>(project, _readWriteWrapper);
210  }
211 
212  protected RedDotAttribute GetRedDotAttributeOfCallerMember(string callerName)
213  {
214  const bool USE_INHERITED_ATTRIBUTES = true;
215 
216  var property = GetProperty(callerName);
217  return
218  (RedDotAttribute)
219  property.GetCustomAttributes(typeof (RedDotAttribute), USE_INHERITED_ATTRIBUTES).First();
220  }
221 
222  protected virtual void SetAttributeValue<T>(T value, [CallerMemberName] string callerName = "")
223  {
224  var project = this as IProjectObject;
225  var attribute = GetRedDotAttributeOfCallerMember(callerName);
226 
227  attribute.WriteTo(project, _readWriteWrapper, value);
228  }
229 
230  private T GetLanguageDependentProperty<T>(RedDotAttribute attribute, PropertyInfo property)
231  {
232  try
233  {
234  var contentType = typeof (T).GetGenericArguments()[0];
235  var realType = typeof (LanguageDependentValue<>).MakeGenericType(contentType);
236  var constructor =
237  realType.GetConstructor(new[] {typeof (IPartialRedDotProjectObject), typeof (RedDotAttribute)});
238 
239 // ReSharper disable PossibleNullReferenceException
240  return (T) constructor.Invoke(new object[] {this, attribute});
241 // ReSharper restore PossibleNullReferenceException
242  } catch (Exception e)
243  {
244  throw new SmartAPIInternalException(
245  string.Format("Internal error in construction of language dependent property {0}", property.Name), e);
246  }
247  }
248 
249  private PropertyInfo GetProperty(string callerName)
250  {
251  return GetType()
252  .GetProperty(callerName,
253  BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public |
254  BindingFlags.FlattenHierarchy);
255  }
256 
257  private static bool IsLanguageDependentProperty(PropertyInfo property)
258  {
259  Func<Type, bool> isLanguageDependent =
260  x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof (ILanguageDependentValue<>);
261  return isLanguageDependent(property.PropertyType) ||
262  property.PropertyType.GetInterfaces().Any(isLanguageDependent);
263  }
264  }
265 }