16 using System.Collections.Generic;
19 using erminas.SmartAPI.CMS.Project.Keywords;
20 using erminas.SmartAPI.Utils;
21 using erminas.SmartAPI.Utils.CachedCollections;
23 namespace erminas.SmartAPI.CMS.Project.Pages
25 internal class PageAssignedKeywords : RDList<IKeyword>, IAssignedKeywords
27 private readonly IPage _page;
29 internal PageAssignedKeywords(IPage page,
Caching caching) : base(caching)
32 RetrieveFunc = GetKeywords;
37 if (ContainsGuid(keyword.
Guid))
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);
50 public void AddRange(IEnumerable<IKeyword> keywords)
52 Set(this.Union(keywords));
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));
69 public void Set(IEnumerable<IKeyword> newKeywords)
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"" />";
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));
79 string toAdd = newKeywordsAsList.Except(
this).Aggregate(
"", (x, y) => x + ADD_SINGLE_KEYWORD.RQLFormat(y));
81 if (
string.IsNullOrEmpty(toRemove) &&
string.IsNullOrEmpty(toAdd))
86 _page.Project.ExecuteRQL(SET_KEYWORDS.RQLFormat(_page, toRemove + toAdd),
RqlType.SessionKeyInProject);
91 private List<IKeyword> GetKeywords()
93 const string LOAD_KEYWORDS =
@"<PROJECT><PAGE guid=""{0}""><KEYWORDS action=""load"" /></PAGE></PROJECT>";
94 var xmlDoc = _page.Project.ExecuteRQL(LOAD_KEYWORDS.RQLFormat(_page));
96 (from XmlElement curNode in xmlDoc.GetElementsByTagName(
"KEYWORD")
97 select (
IKeyword) new Keyword(_page.Project, curNode)).ToList();