SmartAPI
Open Source .NET RQL library for RedDot CMS / OpenText WSM Management Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
PageAssignedKeywords.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.CMS.Project.Keywords;
20 using erminas.SmartAPI.Utils;
21 using erminas.SmartAPI.Utils.CachedCollections;
22 
23 namespace erminas.SmartAPI.CMS.Project.Pages
24 {
25  internal class PageAssignedKeywords : RDList<IKeyword>, IAssignedKeywords
26  {
27  private readonly IPage _page;
28 
29  internal PageAssignedKeywords(IPage page, Caching caching) : base(caching)
30  {
31  _page = page;
32  RetrieveFunc = GetKeywords;
33  }
34 
35  public void Add(IKeyword keyword)
36  {
37  if (ContainsGuid(keyword.Guid))
38  {
39  return;
40  }
41 
42  const string ADD_KEYWORD =
43  @"<PAGE guid=""{0}"" action=""assign""><KEYWORDS><KEYWORD guid=""{1}"" changed=""1"" /></KEYWORDS></PAGE>";
44  _page.Project.ExecuteRQL(ADD_KEYWORD.RQLFormat(_page, keyword), RqlType.SessionKeyInProject);
45  //server sends empty reply
46 
47  InvalidateCache();
48  }
49 
50  public void AddRange(IEnumerable<IKeyword> keywords)
51  {
52  Set(this.Union(keywords));
53  }
54 
55  public void Clear()
56  {
57  Set(new IKeyword[0]);
58  }
59 
60  public void Remove(IKeyword keyword)
61  {
62  const string DELETE_KEYWORD =
63  @"<PROJECT><PAGE guid=""{0}"" action=""unlink""><KEYWORD guid=""{1}"" /></PAGE></PROJECT>";
64  _page.Project.ExecuteRQL(DELETE_KEYWORD.RQLFormat(_page, keyword));
65 
66  InvalidateCache();
67  }
68 
69  public void Set(IEnumerable<IKeyword> newKeywords)
70  {
71  const string SET_KEYWORDS = @"<PAGE guid=""{0}"" action=""assign""><KEYWORDS>{1}</KEYWORDS></PAGE>";
72  const string REMOVE_SINGLE_KEYWORD = @"<KEYWORD guid=""{0}"" delete=""1"" changed=""1"" />";
73  const string ADD_SINGLE_KEYWORD = @"<KEYWORD guid=""{0}"" changed=""1"" />";
74 
75  var newKeywordsAsList = newKeywords as IList<IKeyword> ?? newKeywords.ToList();
76  string toRemove = this.Except(newKeywordsAsList)
77  .Aggregate("", (x, y) => x + REMOVE_SINGLE_KEYWORD.RQLFormat(y));
78 
79  string toAdd = newKeywordsAsList.Except(this).Aggregate("", (x, y) => x + ADD_SINGLE_KEYWORD.RQLFormat(y));
80 
81  if (string.IsNullOrEmpty(toRemove) && string.IsNullOrEmpty(toAdd))
82  {
83  return;
84  }
85 
86  _page.Project.ExecuteRQL(SET_KEYWORDS.RQLFormat(_page, toRemove + toAdd), RqlType.SessionKeyInProject);
87 
88  InvalidateCache();
89  }
90 
91  private List<IKeyword> GetKeywords()
92  {
93  const string LOAD_KEYWORDS = @"<PROJECT><PAGE guid=""{0}""><KEYWORDS action=""load"" /></PAGE></PROJECT>";
94  var xmlDoc = _page.Project.ExecuteRQL(LOAD_KEYWORDS.RQLFormat(_page));
95  return
96  (from XmlElement curNode in xmlDoc.GetElementsByTagName("KEYWORD")
97  select (IKeyword) new Keyword(_page.Project, curNode)).ToList();
98  }
99  }
100 }