SmartAPI
Open Source .NET RQL library for RedDot CMS / OpenText WSM Management Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
AbstractKeywordAssignableMultiLinkElement.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.Linq;
19 using System.Xml;
20 using erminas.SmartAPI.CMS.Project.Keywords;
21 using erminas.SmartAPI.Utils;
22 using erminas.SmartAPI.Utils.CachedCollections;
23 
24 namespace erminas.SmartAPI.CMS.Project.Pages.Elements
25 {
26  internal class LinkAssignedKeywords : RDList<IKeyword>, IAssignedKeywords
27  {
28  private readonly AbstractKeywordAssignableMultiLinkElement _parent;
29 
30  internal LinkAssignedKeywords(AbstractKeywordAssignableMultiLinkElement parent, Caching caching) : base(caching)
31  {
32  _parent = parent;
33  RetrieveFunc = GetAssignedKeywords;
34  }
35 
36  public void Add(IKeyword keyword)
37  {
38  ExecuteAssignKeywords(new[] {keyword});
39 
40  ExecutePagebuilderLinkCleanup();
41 
42  InvalidateCache();
43  }
44 
45  public void AddRange(IEnumerable<IKeyword> keywords)
46  {
47  ExecuteAssignKeywords(keywords);
48 
49  ExecutePagebuilderLinkCleanup();
50 
51  InvalidateCache();
52  }
53 
54  public void Clear()
55  {
56  ExecuteUnassignKeywords(this);
57 
58  ExecutePagebuilderLinkCleanup();
59 
60  InvalidateCache();
61  }
62 
63  public void Remove(IKeyword keyword)
64  {
65  ExecuteUnassignKeywords(new[] {keyword});
66 
67  ExecutePagebuilderLinkCleanup();
68 
69  InvalidateCache();
70  }
71 
72  public void Set(IEnumerable<IKeyword> keywords)
73  {
74  var currentKeywords = this.ToList();
75  var newKeywords = keywords as IList<IKeyword> ?? keywords.ToList();
76  ExecuteUnassignKeywords(currentKeywords.Except(newKeywords));
77  ExecuteAssignKeywords(newKeywords.Except(currentKeywords));
78  }
79 
80  private void ExecuteAssignKeywords(IEnumerable<IKeyword> keywords)
81  {
82  const string ASSING_KEYWORD =
83  @"<LINK guid=""{0}"" action=""assign"" allkeywords=""0""><KEYWORDS>{1}</KEYWORDS></LINK>";
84  const string SINGLE_KEYWORD = @"<KEYWORD guid=""{0}"" changed=""1"" />";
85 
86  var keywordsStr = keywords.Aggregate("", (s, keyword) => s + SINGLE_KEYWORD.RQLFormat(keyword));
87  _parent.Project.ExecuteRQL(ASSING_KEYWORD.RQLFormat(_parent, keywordsStr), RqlType.SessionKeyInProject);
88  }
89 
90  private void ExecutePagebuilderLinkCleanup()
91  {
92  const string PAGEBUILDER_LINK =
93  @"<PAGEBUILDER><LINKING sessionkey=""{0}""><LINKS><LINK guid=""{1}""/></LINKS><PAGES><PAGE sessionkey=""{0}"" guid=""{2}""/></PAGES></LINKING></PAGEBUILDER>";
94  _parent.Project.ExecuteRQL(PAGEBUILDER_LINK.RQLFormat(_parent.Project.Session.SessionKey, _parent,
95  _parent.Page));
96  }
97 
98  private void ExecuteUnassignKeywords(IEnumerable<IKeyword> keywords)
99  {
100  const string UNASSIGN_KEYWORD =
101  @"<LINK guid=""{0}"" action=""assign"" allkeywords=""0""><KEYWORDS>{1}</KEYWORDS></LINK>";
102 
103  const string SINGLE_KEYWORD = @"<KEYWORD guid=""{0}"" delete=""1"" changed=""1"" />";
104 
105  var keywordsStr = keywords.Aggregate("", (s, keyword) => s + SINGLE_KEYWORD.RQLFormat(keyword));
106 
107  _parent.Project.ExecuteRQL(UNASSIGN_KEYWORD.RQLFormat(_parent, keywordsStr), RqlType.SessionKeyInProject);
108  }
109 
110  private List<IKeyword> GetAssignedKeywords()
111  {
112  const string LOAD_KEYWORDS = @"<LINK guid=""{0}""><KEYWORDS action=""load""/></LINK>";
113  var xmlDoc = _parent.Project.ExecuteRQL(LOAD_KEYWORDS.RQLFormat(_parent), RqlType.SessionKeyInProject);
114 
115  var keywords = xmlDoc.SelectNodes("/IODATA/CATEGORIES/CATEGORY/KEYWORDS/KEYWORD");
116  return keywords == null
117  ? new List<IKeyword>()
118  : (from XmlElement keyword in keywords select (IKeyword) new Keyword(_parent.Project, keyword))
119  .ToList();
120  }
121  }
122 
123  internal abstract class AbstractKeywordAssignableMultiLinkElement : AbstractMultiLinkElement, IKeywordAssignable
124  {
125  protected AbstractKeywordAssignableMultiLinkElement(IProject project, Guid guid,
126  ILanguageVariant languageVariant)
127  : base(project, guid, languageVariant)
128  {
129  Init();
130  }
131 
132  protected AbstractKeywordAssignableMultiLinkElement(IProject project, XmlElement xmlElement)
133  : base(project, xmlElement)
134  {
135  Init();
136  }
137 
138  public IAssignedKeywords AssignedKeywords { get; private set; }
139 
140  private void Init()
141  {
142  AssignedKeywords = new LinkAssignedKeywords(this, Caching.Enabled);
143  }
144  }
145 }