17 using System.Collections.Generic;
18 using System.Globalization;
19 using System.Reflection;
21 using erminas.SmartAPI.CMS.Project.ContentClasses.Elements;
22 using erminas.SmartAPI.Exceptions;
23 using erminas.SmartAPI.Utils;
25 namespace erminas.SmartAPI.CMS.Project.Pages.Elements
45 return PageElement.CreateElement(project, elementGuid, languageVariant);
60 internal abstract class PageElement : PartialRedDotProjectObject, IPageElement
62 private const string RETRIEVE_PAGE_ELEMENT =
@"<ELT action=""load"" guid=""{0}""/>";
64 private static readonly Dictionary<ElementType, Type> TYPES =
new Dictionary<ElementType, Type>();
72 foreach (Type curType
in typeof (PageElement).Assembly.GetTypes())
74 foreach (
object curAttr
in curType.GetCustomAttributes(typeof (PageElementType),
false))
77 curType.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null,
78 new[] {typeof (Project), typeof (XmlElement)}, null) == null)
81 string.Format(
"{0} does not contain a constructor (Project, XmlElement)", curType.Name));
83 var type = ((PageElementType) curAttr).Type;
84 if (TYPES.ContainsKey(type))
87 type, TYPES[type].Name, curType.Name));
89 TYPES.Add(type, curType);
94 protected PageElement(IProject project, Guid guid, ILanguageVariant languageVariant) : base(project, guid)
96 LanguageVariant = languageVariant;
99 protected PageElement(IProject project, XmlElement xmlElement) : base(project, xmlElement)
110 public static IPageElement CreateElement(IProject project, XmlElement xmlElement)
112 var typeValue = (
ElementType)
int.Parse(xmlElement.GetAttributeValue(
"elttype"));
114 if (!TYPES.TryGetValue(typeValue, out type))
116 throw new ArgumentException(
string.Format(
"Unknown element type: {0}", typeValue));
120 Activator.CreateInstance(type, BindingFlags.NonPublic | BindingFlags.Instance, null,
121 new object[] {project, xmlElement}, CultureInfo.InvariantCulture);
131 public static IPageElement CreateElement(IProject project, Guid elementGuid, ILanguageVariant languageVariant)
135 XmlDocument xmlDoc = project.ExecuteRQL(
string.Format(RETRIEVE_PAGE_ELEMENT, elementGuid.ToRQLString()));
136 var xmlNode = (XmlElement) xmlDoc.GetElementsByTagName(
"ELT")[0];
137 return CreateElement(project, xmlNode);
147 object[] types = GetType().GetCustomAttributes(typeof (PageElementType),
false);
148 if (types.Length != 1)
153 Type = ((PageElementType) types[0]).Type;
157 set { Type = value; }
160 public ILanguageVariant LanguageVariant
162 get {
return _languageVariant; }
163 private set { _languageVariant = value; }
168 get {
return LazyLoad(ref _page); }
171 public static void RegisterType(
ElementType typeValue, Type type)
173 if (!typeof (PageElement).IsAssignableFrom(type))
176 throw new ArgumentException(String.Format(
"TypeId is not a subclass of {0}", typeof (PageElement).Name));
179 if (TYPES.ContainsKey(typeValue))
181 throw new ArgumentException(
"There is already a type registered for " + typeValue);
184 TYPES.Add(typeValue, type);
187 protected override sealed
void LoadWholeObject()
190 LoadWholePageElement();
193 protected abstract void LoadWholePageElement();
195 protected override XmlElement RetrieveWholeObject()
201 Project.ExecuteRQL(
string.Format(RETRIEVE_PAGE_ELEMENT, Guid.ToRQLString()))
202 .GetElementsByTagName(
"ELT")[0];
206 private void LoadXml()
209 EnsuredInit(ref _languageVariant,
"languagevariantid", Project.LanguageVariants.Get);
210 EnsuredInit(ref _page,
"pageguid", x =>
new Page(Project, GuidConvert(x), LanguageVariant));