SmartAPI
Open Source .NET RQL library for RedDot CMS / OpenText WSM Management Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
IPreassignedKeywords.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.Exceptions;
21 using erminas.SmartAPI.Utils;
22 using erminas.SmartAPI.Utils.CachedCollections;
23 
24 namespace erminas.SmartAPI.CMS.Project.ContentClasses
25 {
26  public interface IPreassignedKeywords : IRDList<IKeyword>, IProjectObject
27  {
28  void Add(IKeyword keyword);
29  void AddRange(IEnumerable<IKeyword> keywordsToAdd);
30  void Clear();
31  IContentClass ContentClass { get; }
32  void Remove(IKeyword keyword);
33  void Set(IEnumerable<IKeyword> keywords);
34  }
35 
36  internal class PreassignedKeywords : RDList<IKeyword>, IPreassignedKeywords
37  {
38  private readonly ContentClass _contentClass;
39 
40  internal PreassignedKeywords(ContentClass contentClass, Caching caching) : base(caching)
41  {
42  _contentClass = contentClass;
43  RetrieveFunc = GetPreassignedKeywords;
44  }
45 
46  public void Add(IKeyword keyword)
47  {
48  AddRange(new[] {keyword});
49  }
50 
51  public void AddRange(IEnumerable<IKeyword> keywordsToAdd)
52  {
53  foreach (Keyword curKeyword in keywordsToAdd)
54  {
55  const string ASSIGN_KEYWORD =
56  @"<TEMPLATE action=""assign"" guid=""{0}""><CATEGORY guid=""{1}""/><KEYWORD guid=""{2}""/></TEMPLATE>";
57 
58  XmlDocument xmlDoc =
59  Project.ExecuteRQL(ASSIGN_KEYWORD.RQLFormat(_contentClass, curKeyword.Category, curKeyword),
60  RqlType.SessionKeyInProject);
61 
62  if (!WasKeywordActionSuccessful(xmlDoc))
63  {
64  throw new SmartAPIException(Session.ServerLogin,
65  string.Format("Could not assign keyword {0} to content class {1}",
66  curKeyword.Name, _contentClass.Name));
67  }
68  }
69  InvalidateCache();
70  }
71 
72  public void Clear()
73  {
74  RemoveRange(this);
75  }
76 
77  public IContentClass ContentClass
78  {
79  get { return _contentClass; }
80  }
81 
82  public IProject Project
83  {
84  get { return _contentClass.Project; }
85  }
86 
87  public void Remove(IKeyword keyword)
88  {
89  RemoveRange(new[] {keyword});
90  }
91 
92  public ISession Session
93  {
94  get { return _contentClass.Session; }
95  }
96 
102  public void Set(IEnumerable<IKeyword> keywords)
103  {
104  if (keywords == null)
105  {
106  keywords = new List<Keyword>();
107  }
108  else
109  {
110  keywords = keywords.ToList();
111  }
112  using (new CachingContext<IKeyword>(this, Caching.Enabled))
113  {
114  var keywordsToAdd = keywords.Except(this).ToList();
115  var keywordsToRemove = this.Except(keywords).ToList();
116 
117  if (!keywordsToRemove.Any() && !keywordsToAdd.Any())
118  {
119  return;
120  }
121 
122  AddRange(keywordsToAdd);
123  RemoveRange(keywordsToRemove);
124  }
125  }
126 
127  private List<IKeyword> GetPreassignedKeywords()
128  {
129  const string LOAD_PREASSIGNED_KEYWORDS = @"<TEMPLATE guid=""{0}""><KEYWORDS action=""load""/></TEMPLATE>";
130  XmlDocument xmlDoc = Project.ExecuteRQL(LOAD_PREASSIGNED_KEYWORDS.RQLFormat(_contentClass),
131  RqlType.SessionKeyInProject);
132 
133  IEnumerable<IKeyword> keywords = new List<Keyword>();
134  foreach (XmlElement node in xmlDoc.GetElementsByTagName("CATEGORY"))
135  {
136  var curCategory = new Category(Project, node.GetGuid()) {Name = node.GetAttributeValue("value")};
137  var newKeywords = from XmlElement curKeywordNode in xmlDoc.GetElementsByTagName("KEYWORD")
138  select
139  new Keyword(Project, curKeywordNode.GetGuid())
140  {
141  Name = curKeywordNode.GetAttributeValue("value"),
142  Category = curCategory
143  };
144  keywords = keywords.Union(newKeywords);
145  }
146  return keywords.ToList();
147  }
148 
149  private void RemoveRange(IEnumerable<IKeyword> keywordsToRemove)
150  {
151  foreach (Keyword curKeyword in keywordsToRemove)
152  {
153  const string REMOVE_KEYWORD =
154  @"<TEMPLATE action=""unlink"" guid=""{0}""><KEYWORD guid=""{1}""/></TEMPLATE>";
155 
156  var xmlDoc = Project.ExecuteRQL(REMOVE_KEYWORD.RQLFormat(_contentClass, curKeyword),
157  RqlType.SessionKeyInProject);
158 
159  if (!WasKeywordActionSuccessful(xmlDoc))
160  {
161  throw new SmartAPIException(Session.ServerLogin,
162  string.Format("Could not unlink keyword {0} from content class {1}",
163  curKeyword.Name, _contentClass.Name));
164  }
165  }
166  InvalidateCache();
167  }
168 
169  private static bool WasKeywordActionSuccessful(XmlNode node)
170  {
171  return node.InnerText.Contains("ok");
172  }
173  }
174 }