SmartAPI
Open Source .NET RQL library for RedDot CMS / OpenText WSM Management Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
IProjectGroups.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.ServerManagement;
21 using erminas.SmartAPI.Exceptions;
22 using erminas.SmartAPI.Utils;
23 using erminas.SmartAPI.Utils.CachedCollections;
24 
25 namespace erminas.SmartAPI.CMS.Project
26 {
27  public interface IProjectGroups : IIndexedRDList<string, IGroup>, IProjectObject
28  {
29  void Add(IGroup group);
30  void AddRange(IEnumerable<IGroup> group);
31  void Clear();
32  IGroup CreateAndAdd(string groupName, string groupEmailAdress);
33  new IProjectGroups Refreshed();
34  void Remove(IGroup group);
35  void Set(IEnumerable<IGroup> group);
36  }
37 
38  internal class ProjectGroups : NameIndexedRDList<IGroup>, IProjectGroups
39  {
40  private const string SINGLE_GROUP = @"<GROUP guid=""{0}"" />";
41  private readonly IProject _project;
42 
43  internal ProjectGroups(IProject project, Caching caching) : base(caching)
44  {
45  _project = project;
46  RetrieveFunc = GetAssignedGroups;
47  }
48 
49  public void Add(IGroup @group)
50  {
51  AddRange(new[] {@group});
52  }
53 
54  public void AddRange(IEnumerable<IGroup> groups)
55  {
56  const string ASSIGN_GROUPS =
57  @"<ADMINISTRATION action=""assign""><PROJECT guid=""{0}"">{1}</PROJECT></ADMINISTRATION>";
58 
59  var groupsList = groups as IList<IGroup> ?? groups.ToList();
60  if (!groupsList.Any())
61  {
62  return;
63  }
64  var groupsPart = groupsList.Aggregate("", (s, @group) => s + SINGLE_GROUP.RQLFormat(@group));
65 
66  var xmlDoc = Session.ExecuteRQL(ASSIGN_GROUPS.RQLFormat(Project, groupsPart));
67 
68  if (!xmlDoc.IsContainingOk())
69  {
70  var errorGroups = groupsList.Aggregate("", (s, @group) => s + @group.ToString() + ";");
71  throw new SmartAPIException(Session.ServerLogin,
72  string.Format("Could not add group(s) to {0}: {1}", Project, errorGroups));
73  }
74  InvalidateCache();
75  }
76 
77  public void Clear()
78  {
79  RemoveRange(this);
80  }
81 
82  public IGroup CreateAndAdd(string groupName, string groupEmailAdress)
83  {
84  const string CREATE_GROUP =
85  @"<ADMINISTRATION><PROJECT guid=""{0}"" action=""assign""><GROUPS action=""addnew""><GROUP name=""{1}"" email=""{2}""/></GROUPS></PROJECT></ADMINISTRATION>";
86 
87  var xmlDoc = Session.ExecuteRQL(CREATE_GROUP.SecureRQLFormat(_project, groupName, groupEmailAdress));
88 
89  var groupGuid = xmlDoc.GetSingleElement("GROUP").GetGuid();
90  InvalidateCache();
91  return GroupFactory.CreateFromGuid(Session, groupGuid);
92  }
93 
94  public IProject Project
95  {
96  get { return _project; }
97  }
98 
99  public void Remove(IGroup @group)
100  {
101  RemoveRange(new[] {@group});
102  }
103 
104  public ISession Session
105  {
106  get { return _project.Session; }
107  }
108 
109  public void Set(IEnumerable<IGroup> newGroups)
110  {
111  var curGroups = this.ToList();
112  var newGroupsList = newGroups as IList<IGroup> ?? newGroups.ToList();
113 
114  RemoveRange(curGroups.Except(newGroupsList));
115  AddRange(newGroupsList.Except(curGroups));
116  }
117 
118  private List<IGroup> GetAssignedGroups()
119  {
120  const string LIST_GROUPS = @"<PROJECT><GROUPS action=""list""/></PROJECT>";
121 
122  var xmlDoc = Session.ExecuteRQLInProjectContext(LIST_GROUPS, _project.Guid);
123  return
124  (from XmlElement curGroup in xmlDoc.GetElementsByTagName("GROUP")
125  select (IGroup) new Group(Session, curGroup)).ToList();
126  }
127 
128  IProjectGroups IProjectGroups.Refreshed()
129  {
130  return this;
131  }
132 
133  private void RemoveRange(IEnumerable<IGroup> groups)
134  {
135  const string UNASSIGN_GROUPS =
136  @"<ADMINISTRATION action=""unlink""><PROJECT guid=""{0}"">{1}</PROJECT></ADMINISTRATION>";
137 
138  var groupsList = groups as IList<IGroup> ?? groups.ToList();
139  if (!groupsList.Any())
140  {
141  return;
142  }
143  var groupsPart = groupsList.Aggregate("", (s, @group) => s + SINGLE_GROUP.RQLFormat(@group));
144 
145  var xmlDoc = Session.ExecuteRQL(UNASSIGN_GROUPS.RQLFormat(Project, groupsPart));
146  //7.5 sends empty reply
147  if (Session.ServerVersion >= new Version(9, 0) && !xmlDoc.IsContainingOk())
148  {
149  var errorGroups = groupsList.Aggregate("", (s, @group) => s + @group.ToString() + ";");
150  throw new SmartAPIException(Session.ServerLogin,
151  string.Format("Could not remove group(s) from {0}: {1}", Project,
152  errorGroups));
153  }
154  InvalidateCache();
155  }
156  }
157 }