SmartAPI
Open Source .NET RQL library for RedDot CMS / OpenText WSM Management Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
IContentClassElements.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.Globalization;
19 using System.Linq;
20 using System.Xml;
21 using erminas.SmartAPI.CMS.Project.ContentClasses.Elements;
22 using erminas.SmartAPI.Exceptions;
23 using erminas.SmartAPI.Utils;
24 using erminas.SmartAPI.Utils.CachedCollections;
25 using log4net;
26 
27 namespace erminas.SmartAPI.CMS.Project.ContentClasses
28 {
29  internal class ContentClassElements : NameIndexedRDList<IContentClassElement>, IContentClassElements
30  {
31  private static readonly ILog LOGGER = LogManager.GetLogger(typeof (ContentClassElements));
32  private readonly ContentClass _contentClass;
33 
34  internal ContentClassElements(ContentClass contentClass, Caching caching) : base(caching)
35  {
36  _contentClass = contentClass;
37  RetrieveFunc = GetContentClassElements;
38  }
39 
40  public IContentClass ContentClass
41  {
42  get { return _contentClass; }
43  }
44 
45  public IProject Project
46  {
47  get { return _contentClass.Project; }
48  }
49 
50  public void Remove(Guid guid)
51  {
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())
55  {
56  throw new SmartAPIException(Session.ServerLogin,
57  string.Format("Could not remove element {0} from content class {1} ",
58  guid.ToRQLString(), this));
59  }
60  InvalidateCache();
61  }
62 
73  public void Remove(string elementName)
74  {
75  IContentClassElement contentClassElementToRemove;
76  if (TryGetByName(elementName, out contentClassElementToRemove))
77  {
78  throw new ArgumentException("Element '" + elementName + "' could not be found in content class '" +
79  _contentClass.Name + "'");
80  }
81  Remove(contentClassElementToRemove.Guid);
82  }
83 
84  public ISession Session
85  {
86  get { return _contentClass.Session; }
87  }
88 
89  private IContentClassElement CreateElement(XmlElement curElementNode)
90  {
91  try
92  {
93  return ContentClassElement.CreateElement(ContentClass, curElementNode);
94  } catch (Exception e)
95  {
96  var typeStr = GetElementTypeName(curElementNode);
97  var elementName = curElementNode.GetAttributeValue("eltname");
98  var str = "Could not create element '" + elementName + "' of type '" + typeStr + "'";
99 
100  LOGGER.Error(str + ": " + e.Message);
101  var ex = new SmartAPIException(Session.ServerLogin, str, e);
102  ex.Data["elementNode"] = curElementNode.OuterXml;
103 
104  throw ex;
105  }
106  }
107 
108  private List<IContentClassElement> GetContentClassElements()
109  {
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));
113 
114  var elementChildren = xmlDoc.GetSingleElement("ELEMENTS").GetElementsByTagName("ELEMENT");
115  return (from XmlElement curElementNode in elementChildren select CreateElement(curElementNode)).ToList();
116  }
117 
118  private static string GetElementTypeName(XmlElement curElementNode)
119  {
120  var elttypeStr = curElementNode.GetAttributeValue("elttype") ??
121  ((int) ElementType.None).ToString(CultureInfo.InvariantCulture);
122  int typeValue;
123  var typeStr = int.TryParse(elttypeStr, out typeValue) ? ((ElementType) typeValue).ToString() : "unknown";
124  return typeStr;
125  }
126  }
127 
128  public interface IContentClassElements : IProjectObject, IIndexedRDList<string, IContentClassElement>
129  {
130  IContentClass ContentClass { get; }
131 
136  void Remove(string elementName);
137 
138  void Remove(Guid elementGuid);
139  }
140 }