17 using System.Collections.Generic;
20 using erminas.SmartAPI.CMS.ServerManagement;
21 using erminas.SmartAPI.Exceptions;
22 using erminas.SmartAPI.Utils;
23 using erminas.SmartAPI.Utils.CachedCollections;
25 namespace erminas.SmartAPI.CMS.Project
30 void AddRange(IEnumerable<IGroup> group);
32 IGroup CreateAndAdd(
string groupName,
string groupEmailAdress);
35 void Set(IEnumerable<IGroup> group);
38 internal class ProjectGroups : NameIndexedRDList<IGroup>,
IProjectGroups
40 private const string SINGLE_GROUP =
@"<GROUP guid=""{0}"" />";
43 internal ProjectGroups(
IProject project,
Caching caching) : base(caching)
46 RetrieveFunc = GetAssignedGroups;
49 public void Add(
IGroup @group)
51 AddRange(
new[] {@group});
54 public void AddRange(IEnumerable<IGroup> groups)
56 const string ASSIGN_GROUPS =
57 @"<ADMINISTRATION action=""assign""><PROJECT guid=""{0}"">{1}</PROJECT></ADMINISTRATION>";
59 var groupsList = groups as IList<IGroup> ?? groups.ToList();
60 if (!groupsList.Any())
64 var groupsPart = groupsList.Aggregate(
"", (s, @group) => s + SINGLE_GROUP.RQLFormat(@group));
66 var xmlDoc = Session.
ExecuteRQL(ASSIGN_GROUPS.RQLFormat(Project, groupsPart));
68 if (!xmlDoc.IsContainingOk())
70 var errorGroups = groupsList.Aggregate(
"", (s, @group) => s + @group.ToString() +
";");
72 string.Format(
"Could not add group(s) to {0}: {1}", Project, errorGroups));
82 public IGroup CreateAndAdd(
string groupName,
string groupEmailAdress)
84 const string CREATE_GROUP =
85 @"<ADMINISTRATION><PROJECT guid=""{0}"" action=""assign""><GROUPS action=""addnew""><GROUP name=""{1}"" email=""{2}""/></GROUPS></PROJECT></ADMINISTRATION>";
87 var xmlDoc = Session.ExecuteRQL(CREATE_GROUP.SecureRQLFormat(_project, groupName, groupEmailAdress));
89 var groupGuid = xmlDoc.GetSingleElement(
"GROUP").GetGuid();
91 return GroupFactory.CreateFromGuid(Session, groupGuid);
94 public IProject Project
96 get {
return _project; }
99 public void Remove(
IGroup @group)
101 RemoveRange(
new[] {@group});
104 public ISession Session
106 get {
return _project.Session; }
109 public void Set(IEnumerable<IGroup> newGroups)
111 var curGroups = this.ToList();
112 var newGroupsList = newGroups as IList<IGroup> ?? newGroups.ToList();
114 RemoveRange(curGroups.Except(newGroupsList));
115 AddRange(newGroupsList.Except(curGroups));
118 private List<IGroup> GetAssignedGroups()
120 const string LIST_GROUPS =
@"<PROJECT><GROUPS action=""list""/></PROJECT>";
122 var xmlDoc = Session.ExecuteRQLInProjectContext(LIST_GROUPS, _project.Guid);
124 (from XmlElement curGroup in xmlDoc.GetElementsByTagName(
"GROUP")
125 select (
IGroup) new Group(Session, curGroup)).ToList();
128 IProjectGroups IProjectGroups.Refreshed()
133 private void RemoveRange(IEnumerable<IGroup> groups)
135 const string UNASSIGN_GROUPS =
136 @"<ADMINISTRATION action=""unlink""><PROJECT guid=""{0}"">{1}</PROJECT></ADMINISTRATION>";
138 var groupsList = groups as IList<IGroup> ?? groups.ToList();
139 if (!groupsList.Any())
143 var groupsPart = groupsList.Aggregate(
"", (s, @group) => s + SINGLE_GROUP.RQLFormat(@group));
145 var xmlDoc = Session.ExecuteRQL(UNASSIGN_GROUPS.RQLFormat(Project, groupsPart));
147 if (Session.ServerVersion >=
new Version(9, 0) && !xmlDoc.IsContainingOk())
149 var errorGroups = groupsList.Aggregate(
"", (s, @group) => s + @group.ToString() +
";");
151 string.Format(
"Could not remove group(s) from {0}: {1}", Project,