17 using System.Collections.Generic;
18 using System.Globalization;
21 using erminas.SmartAPI.CMS.Project.ContentClasses.Elements;
22 using erminas.SmartAPI.Exceptions;
23 using erminas.SmartAPI.Utils;
24 using erminas.SmartAPI.Utils.CachedCollections;
27 namespace erminas.SmartAPI.CMS.Project.ContentClasses
29 internal class ContentClassElements : NameIndexedRDList<IContentClassElement>, IContentClassElements
31 private static readonly ILog LOGGER = LogManager.GetLogger(typeof (ContentClassElements));
32 private readonly ContentClass _contentClass;
34 internal ContentClassElements(ContentClass contentClass,
Caching caching) : base(caching)
36 _contentClass = contentClass;
37 RetrieveFunc = GetContentClassElements;
40 public IContentClass ContentClass
42 get {
return _contentClass; }
45 public IProject Project
47 get {
return _contentClass.Project; }
50 public void Remove(Guid guid)
52 const string REMOVE_ELEMENT =
@"<TEMPLATE><ELEMENT action=""delete"" guid=""{0}""/></TEMPLATE>";
53 XmlDocument xmlDoc = Project.ExecuteRQL(REMOVE_ELEMENT.RQLFormat(guid),
RqlType.SessionKeyInProject);
54 if (!xmlDoc.IsContainingOk())
57 string.Format(
"Could not remove element {0} from content class {1} ",
58 guid.ToRQLString(),
this));
73 public void Remove(
string elementName)
76 if (TryGetByName(elementName, out contentClassElementToRemove))
78 throw new ArgumentException(
"Element '" + elementName +
"' could not be found in content class '" +
79 _contentClass.Name +
"'");
81 Remove(contentClassElementToRemove.Guid);
84 public ISession Session
86 get {
return _contentClass.Session; }
93 return ContentClassElement.CreateElement(ContentClass, curElementNode);
96 var typeStr = GetElementTypeName(curElementNode);
97 var elementName = curElementNode.GetAttributeValue(
"eltname");
98 var str =
"Could not create element '" + elementName +
"' of type '" + typeStr +
"'";
100 LOGGER.Error(str +
": " + e.Message);
102 ex.Data[
"elementNode"] = curElementNode.OuterXml;
108 private List<IContentClassElement> GetContentClassElements()
110 const string LOAD_CC_ELEMENTS =
111 @"<PROJECT><TEMPLATE action=""load"" guid=""{0}""><ELEMENTS childnodesasattributes=""1"" action=""load""/><TEMPLATEVARIANTS action=""list""/></TEMPLATE></PROJECT>";
112 var xmlDoc = _contentClass.Project.ExecuteRQL(LOAD_CC_ELEMENTS.RQLFormat(_contentClass));
114 var elementChildren = xmlDoc.GetSingleElement(
"ELEMENTS").GetElementsByTagName(
"ELEMENT");
115 return (from XmlElement curElementNode in elementChildren select CreateElement(curElementNode)).ToList();
118 private static string GetElementTypeName(XmlElement curElementNode)
120 var elttypeStr = curElementNode.GetAttributeValue(
"elttype") ??
121 ((int)
ElementType.None).ToString(CultureInfo.InvariantCulture);
123 var typeStr =
int.TryParse(elttypeStr, out typeValue) ? ((
ElementType) typeValue).ToString() :
"unknown";
136 void Remove(
string elementName);
138 void Remove(Guid elementGuid);