17 using System.Collections.Generic;
20 using erminas.SmartAPI.CMS.Project.Keywords;
21 using erminas.SmartAPI.Utils;
22 using erminas.SmartAPI.Utils.CachedCollections;
24 namespace erminas.SmartAPI.CMS.Project.
Pages.Elements
28 private readonly AbstractKeywordAssignableMultiLinkElement _parent;
30 internal LinkAssignedKeywords(AbstractKeywordAssignableMultiLinkElement parent,
Caching caching) : base(caching)
33 RetrieveFunc = GetAssignedKeywords;
38 ExecuteAssignKeywords(
new[] {keyword});
40 ExecutePagebuilderLinkCleanup();
45 public void AddRange(IEnumerable<IKeyword> keywords)
47 ExecuteAssignKeywords(keywords);
49 ExecutePagebuilderLinkCleanup();
56 ExecuteUnassignKeywords(
this);
58 ExecutePagebuilderLinkCleanup();
65 ExecuteUnassignKeywords(
new[] {keyword});
67 ExecutePagebuilderLinkCleanup();
72 public void Set(IEnumerable<IKeyword> keywords)
74 var currentKeywords = this.ToList();
76 ExecuteUnassignKeywords(currentKeywords.Except(newKeywords));
77 ExecuteAssignKeywords(newKeywords.Except(currentKeywords));
80 private void ExecuteAssignKeywords(IEnumerable<IKeyword> keywords)
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"" />";
86 var keywordsStr = keywords.Aggregate(
"", (s, keyword) => s + SINGLE_KEYWORD.RQLFormat(keyword));
90 private void ExecutePagebuilderLinkCleanup()
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,
98 private void ExecuteUnassignKeywords(IEnumerable<IKeyword> keywords)
100 const string UNASSIGN_KEYWORD =
101 @"<LINK guid=""{0}"" action=""assign"" allkeywords=""0""><KEYWORDS>{1}</KEYWORDS></LINK>";
103 const string SINGLE_KEYWORD =
@"<KEYWORD guid=""{0}"" delete=""1"" changed=""1"" />";
105 var keywordsStr = keywords.Aggregate(
"", (s, keyword) => s + SINGLE_KEYWORD.RQLFormat(keyword));
107 _parent.Project.ExecuteRQL(UNASSIGN_KEYWORD.RQLFormat(_parent, keywordsStr),
RqlType.SessionKeyInProject);
110 private List<IKeyword> GetAssignedKeywords()
112 const string LOAD_KEYWORDS =
@"<LINK guid=""{0}""><KEYWORDS action=""load""/></LINK>";
113 var xmlDoc = _parent.Project.ExecuteRQL(LOAD_KEYWORDS.RQLFormat(_parent),
RqlType.SessionKeyInProject);
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))
123 internal abstract class AbstractKeywordAssignableMultiLinkElement : AbstractMultiLinkElement,
IKeywordAssignable
125 protected AbstractKeywordAssignableMultiLinkElement(
IProject project, Guid guid,
127 : base(project, guid, languageVariant)
132 protected AbstractKeywordAssignableMultiLinkElement(
IProject project, XmlElement xmlElement)
133 : base(project, xmlElement)
142 AssignedKeywords =
new LinkAssignedKeywords(
this,
Caching.Enabled);