SmartAPI
Open Source .NET RQL library for RedDot CMS / OpenText WSM Management Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
ICategoryKeywords.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.Web;
19 using System.Xml;
20 using erminas.SmartAPI.Exceptions;
21 using erminas.SmartAPI.Utils;
22 using erminas.SmartAPI.Utils.CachedCollections;
23 
24 namespace erminas.SmartAPI.CMS.Project.Keywords
25 {
26  public interface ICategoryKeywords : IRDList<IKeyword>, IProjectObject
27  {
28  ICategory Category { get; }
29  IKeyword CreateOrGet(string keywordName);
30  void Delete(string keywordName);
31  void DeleteForcibly(string keywordName);
32  }
33 
40  public class CategoryKeywords : RDList<IKeyword>, ICategoryKeywords
41  {
42  internal CategoryKeywords(ICategory category) : base(Caching.Enabled)
43  {
44  Project = category.Project;
45  Category = category;
46  RetrieveFunc = GetKeywords;
47  }
48 
49  public ICategory Category { get; private set; }
50 
51  public IKeyword CreateOrGet(string keywordName)
52  {
53  const string SAVE_KEYWORD = @"<CATEGORY guid=""{0}""><KEYWORD action=""save"" value=""{1}""/></CATEGORY>";
54 
55  var xmlDoc =
56  Category.Project.ExecuteRQL(SAVE_KEYWORD.RQLFormat(Category, HttpUtility.HtmlEncode(keywordName)),
57  RqlType.SessionKeyInProject);
58  var keyword = (XmlElement) xmlDoc.SelectSingleNode("/IODATA/KEYWORD");
59  if (keyword == null)
60  {
61  throw new SmartAPIException(Session.ServerLogin,
62  string.Format("Could not create the keyword '{0}'", keywordName));
63  }
64 
65  InvalidateCache();
66  return new Keyword(Category.Project, keyword);
67  }
68 
69  public void Delete(string keywordName)
70  {
71  IKeyword keyword;
72  if (!TryGetByName(keywordName, out keyword))
73  {
74  return;
75  }
76 
77  keyword.Delete();
78  InvalidateCache();
79  }
80 
81  public void DeleteForcibly(string keywordName)
82  {
83  IKeyword keyword;
84  if (!TryGetByName(keywordName, out keyword))
85  {
86  return;
87  }
88 
89  keyword.DeleteForcibly();
90  InvalidateCache();
91  }
92 
93  public IProject Project { get; private set; }
94 
95  public ISession Session
96  {
97  get { return Project.Session; }
98  }
99 
100  private List<IKeyword> GetKeywords()
101  {
102  const string LIST_KEYWORDS =
103  @"<PROJECT><CATEGORY guid=""{0}""><KEYWORDS action=""load"" /></CATEGORY></PROJECT>";
104  XmlDocument xmlDoc = Category.Project.ExecuteRQL(LIST_KEYWORDS.RQLFormat(Category));
105  XmlNodeList xmlNodes = xmlDoc.GetElementsByTagName("KEYWORD");
106 
107  var kategoryKeyword = new List<Keyword>
108  {
109  new Keyword(Category.Project, Category.Guid) {Name = "[category]", Category = Category}
110  };
111  return
112  (from XmlElement curNode in xmlNodes
113  select (IKeyword) new Keyword(Category.Project, curNode) {Category = Category}).Union(kategoryKeyword)
114  .ToList();
115  }
116  }
117 }