16 using System.Collections.Generic;
19 using erminas.SmartAPI.CMS.Project.Keywords;
20 using erminas.SmartAPI.Exceptions;
21 using erminas.SmartAPI.Utils;
22 using erminas.SmartAPI.Utils.CachedCollections;
24 namespace erminas.SmartAPI.CMS.Project.ContentClasses
29 void AddRange(IEnumerable<IKeyword> keywordsToAdd);
33 void Set(IEnumerable<IKeyword> keywords);
36 internal class PreassignedKeywords : RDList<IKeyword>, IPreassignedKeywords
38 private readonly ContentClass _contentClass;
40 internal PreassignedKeywords(ContentClass contentClass,
Caching caching) : base(caching)
42 _contentClass = contentClass;
43 RetrieveFunc = GetPreassignedKeywords;
48 AddRange(
new[] {keyword});
51 public void AddRange(IEnumerable<IKeyword> keywordsToAdd)
53 foreach (Keyword curKeyword
in keywordsToAdd)
55 const string ASSIGN_KEYWORD =
56 @"<TEMPLATE action=""assign"" guid=""{0}""><CATEGORY guid=""{1}""/><KEYWORD guid=""{2}""/></TEMPLATE>";
59 Project.ExecuteRQL(ASSIGN_KEYWORD.RQLFormat(_contentClass, curKeyword.Category, curKeyword),
62 if (!WasKeywordActionSuccessful(xmlDoc))
65 string.Format(
"Could not assign keyword {0} to content class {1}",
66 curKeyword.Name, _contentClass.Name));
77 public IContentClass ContentClass
79 get {
return _contentClass; }
82 public IProject Project
84 get {
return _contentClass.Project; }
89 RemoveRange(
new[] {keyword});
92 public ISession Session
94 get {
return _contentClass.Session; }
102 public void Set(IEnumerable<IKeyword> keywords)
104 if (keywords == null)
106 keywords =
new List<Keyword>();
110 keywords = keywords.ToList();
112 using (
new CachingContext<IKeyword>(
this,
Caching.Enabled))
114 var keywordsToAdd = keywords.Except(
this).ToList();
115 var keywordsToRemove = this.Except(keywords).ToList();
117 if (!keywordsToRemove.Any() && !keywordsToAdd.Any())
122 AddRange(keywordsToAdd);
123 RemoveRange(keywordsToRemove);
127 private List<IKeyword> GetPreassignedKeywords()
129 const string LOAD_PREASSIGNED_KEYWORDS =
@"<TEMPLATE guid=""{0}""><KEYWORDS action=""load""/></TEMPLATE>";
130 XmlDocument xmlDoc = Project.ExecuteRQL(LOAD_PREASSIGNED_KEYWORDS.RQLFormat(_contentClass),
133 IEnumerable<IKeyword> keywords =
new List<Keyword>();
134 foreach (XmlElement node
in xmlDoc.GetElementsByTagName(
"CATEGORY"))
136 var curCategory =
new Category(Project, node.GetGuid()) {Name = node.GetAttributeValue(
"value")};
137 var newKeywords = from XmlElement curKeywordNode in xmlDoc.GetElementsByTagName(
"KEYWORD")
139 new Keyword(Project, curKeywordNode.GetGuid())
141 Name = curKeywordNode.GetAttributeValue(
"value"),
142 Category = curCategory
144 keywords = keywords.Union(newKeywords);
146 return keywords.ToList();
149 private void RemoveRange(IEnumerable<IKeyword> keywordsToRemove)
151 foreach (Keyword curKeyword
in keywordsToRemove)
153 const string REMOVE_KEYWORD =
154 @"<TEMPLATE action=""unlink"" guid=""{0}""><KEYWORD guid=""{1}""/></TEMPLATE>";
156 var xmlDoc = Project.ExecuteRQL(REMOVE_KEYWORD.RQLFormat(_contentClass, curKeyword),
159 if (!WasKeywordActionSuccessful(xmlDoc))
162 string.Format(
"Could not unlink keyword {0} from content class {1}",
163 curKeyword.Name, _contentClass.Name));
169 private static bool WasKeywordActionSuccessful(XmlNode node)
171 return node.InnerText.Contains(
"ok");