SmartAPI
Open Source .NET RQL library for RedDot CMS / OpenText WSM Management Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
ICategories.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.Collections.Generic;
17 using System.Linq;
18 using System.Xml;
19 using erminas.SmartAPI.Exceptions;
20 using erminas.SmartAPI.Utils;
21 using erminas.SmartAPI.Utils.CachedCollections;
22 
23 namespace erminas.SmartAPI.CMS.Project.Keywords
24 {
25  public interface ICategories : IRDList<ICategory>, IProjectObject
26  {
27  ICategory CreateOrGet(string categoryName);
28  void Delete(string categoryName);
29  }
30 
38  public class Categories : RDList<ICategory>, ICategories
39  {
40  private readonly IProject _project;
41 
42  internal Categories(IProject project) : base(Caching.Enabled)
43  {
44  RetrieveFunc = GetCategories;
45  _project = project;
46  }
47 
48  public ICategory CreateOrGet(string categoryName)
49  {
50  const string ADD_CATEGORY = @"<PROJECT><CATEGORY action=""addnew"" value=""{0}""/></PROJECT>";
51  var xmlDoc = _project.ExecuteRQL(ADD_CATEGORY.RQLFormat(categoryName));
52 
53  var category = (XmlElement) xmlDoc.SelectSingleNode("//CATEGORY");
54  if (category == null)
55  {
56  throw new SmartAPIException(Session.ServerLogin,
57  string.Format("Could not create the category {0} in project {1}",
58  categoryName, _project));
59  }
60 
61  InvalidateCache();
62  return new Category(_project, category);
63  }
64 
65  public void Delete(string categoryName)
66  {
67  ICategory category;
68  if (TryGetByName(categoryName, out category))
69  {
70  category.Delete();
71  }
72  }
73 
74  public IProject Project
75  {
76  get { return _project; }
77  }
78 
79  public ISession Session
80  {
81  get { return _project.Session; }
82  }
83 
84  private List<ICategory> GetCategories()
85  {
86  const string LIST_CATEGORIES = @"<PROJECT><CATEGORIES action=""list"" /></PROJECT>";
87  XmlDocument xmlDoc = _project.ExecuteRQL(LIST_CATEGORIES);
88  XmlNodeList xmlNodes = xmlDoc.GetElementsByTagName("CATEGORY");
89  return (from XmlElement curNode in xmlNodes select (ICategory) new Category(_project, curNode)).ToList();
90  }
91  }
92 }