SmartAPI
Open Source .NET RQL library for RedDot CMS / OpenText WSM Management Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
ICategory.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.Web;
18 using System.Xml;
19 using erminas.SmartAPI.Exceptions;
20 using erminas.SmartAPI.Utils;
21 
22 namespace erminas.SmartAPI.CMS.Project.Keywords
23 {
25  {
29  void Commit();
30 
35  new void Delete();
36 
42  void DeleteForcibly();
43 
44  CategoryKeywords Keywords { get; }
45 
49  ILanguageVariant LanguageVariant { get; }
50 
62  void Rename(string newCategoryName);
63  }
64 
65  internal class ArbitraryCategory : ICategory
66  {
67  public static readonly ArbitraryCategory INSTANCE = new ArbitraryCategory();
68 
69  private ArbitraryCategory()
70  {
71  }
72 
73  public void Commit()
74  {
75  throw new NotImplementedException();
76  }
77 
78  public void DeleteForcibly()
79  {
80  throw new NotImplementedException();
81  }
82 
83  public void EnsureInitialization()
84  {
85  throw new NotImplementedException();
86  }
87 
88  public Guid Guid
89  {
90  get { return Guid.Empty; }
91  }
92 
93  public CategoryKeywords Keywords
94  {
95  get { throw new NotImplementedException(); }
96  }
97 
98  public ILanguageVariant LanguageVariant
99  {
100  get { throw new NotImplementedException(); }
101  }
102 
103  public string Name
104  {
105  get { throw new NotImplementedException(); }
106  }
107 
108  public IProject Project
109  {
110  get { throw new NotImplementedException(); }
111  }
112 
113  public void Refresh()
114  {
115  throw new NotImplementedException();
116  }
117 
118  public void Rename(string newCategoryName)
119  {
120  throw new NotImplementedException();
121  }
122 
123  public ISession Session
124  {
125  get { throw new NotImplementedException(); }
126  }
127 
128  void ICategory.Delete()
129  {
130  throw new NotImplementedException();
131  }
132 
133  void IDeletable.Delete()
134  {
135  throw new NotImplementedException();
136  }
137  }
138 
142  internal class Category : PartialRedDotProjectObject, ICategory
143  {
144  private ILanguageVariant _languageVariant;
145 
146  internal Category(IProject project, XmlElement xmlElement) : base(project, xmlElement)
147  {
148  Keywords = new CategoryKeywords(this);
149  LoadXml();
150  }
151 
152  public Category(IProject project, Guid guid) : base(project, guid)
153  {
154  Keywords = new CategoryKeywords(this);
155  }
156 
160  public void Commit()
161  {
162  const string SAVE_CATEGORY = @"<PROJECT><CATEGORY action=""save"" guid=""{0}"" value=""{1}""/></PROJECT>";
163  string htmlEncodedName = HttpUtility.HtmlEncode(Name);
164  var xmlDoc = Project.ExecuteRQL(string.Format(SAVE_CATEGORY, Guid.ToRQLString(), htmlEncodedName));
165 
166  const string CATEGORY_XPATH_TEMPLATE = "/IODATA/CATEGORY[@value='{0}' and @guid='{1}']";
167  string categoryXPath = CATEGORY_XPATH_TEMPLATE.RQLFormat(htmlEncodedName, this);
168  var category = xmlDoc.SelectSingleNode(categoryXPath);
169  if (category == null)
170  {
171  throw new SmartAPIException(Session.ServerLogin,
172  string.Format("Could not rename category to '{0}'", Name));
173  }
174  }
175 
180  public void Delete()
181  {
182  const string DELETE_CATEGORY = @"<CATEGORY action=""delete"" guid=""{0}"" force=""0""/>";
183 
184  var xmlDoc = Project.ExecuteRQL(DELETE_CATEGORY.RQLFormat(this), RqlType.SessionKeyInProject);
185  var category = (XmlElement) xmlDoc.SelectSingleNode("/IODATA/CATEGORY");
186 
187  if (category == null)
188  {
189  throw new SmartAPIException(Session.ServerLogin, string.Format("Could not delete category {0}", this));
190  }
191 
192  if (IsCategoryStillUsed(category))
193  {
194  throw new SmartAPIException(Session.ServerLogin,
195  string.Format(
196  "Could not delete category {0}, because a keyword is still assigned to a page",
197  this));
198  }
199 
200  Project.Categories.InvalidateCache();
201  }
202 
208  public void DeleteForcibly()
209  {
210  const string DELETE_KEYWORD = @"<CATEGORY action=""delete"" guid=""{0}"" force=""1"" password=""{1}"" />";
211 
212  var xmlDoc =
213  Project.ExecuteRQL(DELETE_KEYWORD.RQLFormat(this, Project.Session.ServerLogin.AuthData.Password),
214  RqlType.SessionKeyInProject);
215  var category = (XmlElement) xmlDoc.SelectSingleNode("/IODATA/CATEGORY");
216  //TODO execute page builder command
217  if (category == null)
218  {
219  throw new SmartAPIException(Session.ServerLogin, string.Format("Could not delete keyword {0}", this));
220  }
221 
222  Project.Categories.InvalidateCache();
223  }
224 
228  public CategoryKeywords Keywords { get; private set; }
229 
233  public ILanguageVariant LanguageVariant
234  {
235  get { return LazyLoad(ref _languageVariant); }
236  }
237 
249  public void Rename(string newCategoryName)
250  {
251  Name = newCategoryName;
252  Commit();
253  }
254 
255  protected override void LoadWholeObject()
256  {
257  LoadXml();
258  }
259 
260  protected override XmlElement RetrieveWholeObject()
261  {
262  const string LOAD_CATEGORY = @"<PROJECT><CATEGORY action=""load"" guid=""{0}""/></PROJECT>";
263  return
264  (XmlElement)
265  Project.ExecuteRQL(string.Format(LOAD_CATEGORY, Guid.ToRQLString())).GetElementsByTagName("CATEGORY")[0];
266  }
267 
268  private static bool IsCategoryStillUsed(XmlElement category)
269  {
270  return category.GetIntAttributeValue("countkeywordonpag") > 0 ||
271  category.GetIntAttributeValue("countkeywordonpge") > 0 ||
272  category.GetIntAttributeValue("countkeywordonpgeandpag") > 0;
273  }
274 
275  private void LoadXml()
276  {
277  EnsuredInit(ref _name, "value", HttpUtility.HtmlDecode);
278  InitIfPresent(ref _languageVariant, "languagevariantid", x => Project.LanguageVariants[x]);
279  }
280  }
281 }