SmartAPI
Open Source .NET RQL library for RedDot CMS / OpenText WSM Management Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
IUserProjects.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.Utils;
22 using erminas.SmartAPI.Utils.CachedCollections;
23 
24 namespace erminas.SmartAPI.CMS.Project
25 {
26  public interface IUserProjects : IIndexedCachedList<string, IUserProjectAssignment>, ISessionObject
27  {
28  IUserProjectAssignment AddOrSet(IProject project, UserRole role, ExtendedUserRoles extendedRoles);
29  bool ContainsProject(IProject project);
30  bool ContainsProjectGuid(Guid projectGuid);
31  bool ContainsProjectName(string projectName);
32  IUserProjectAssignment GetByProject(IProject project);
33  IUserProjectAssignment GetByProjectGuid(Guid projectGuid);
34  IUserProjectAssignment GetByProjectName(string projectName);
35  IUserProjectAssignment this[IProject project] { get; }
36  void Remove(IProject project);
37  bool TryGetByProject(IProject project, out IUserProjectAssignment assignment);
38  bool TryGetByProjectGuid(Guid projectGuid, out IUserProjectAssignment assignment);
39  bool TryGetByProjectName(string projectName, out IUserProjectAssignment assignment);
40  IUser User { get; }
41  }
42 
43  internal class UserProjects : IndexedCachedList<string, IUserProjectAssignment>, IUserProjects
44  {
45  private readonly IUser _user;
46 
47  internal UserProjects(IUser user, Caching caching) : base(assignment => assignment.Project.Name, caching)
48  {
49  _user = user;
50  RetrieveFunc = GetProjectAssignments;
51  }
52 
53  public IUserProjectAssignment AddOrSet(IProject project, UserRole role, ExtendedUserRoles extendedRoles)
54  {
55  return UserProjectAssignment.Create(_user, project, role, extendedRoles);
56  }
57 
58  public bool ContainsProject(IProject project)
59  {
60  return ContainsProjectGuid(project.Guid);
61  }
62 
63  public bool ContainsProjectGuid(Guid projectGuid)
64  {
65  return this.Any(assignment => assignment.Project.Guid == projectGuid);
66  }
67 
68  public bool ContainsProjectName(string projectName)
69  {
70  return ContainsKey(projectName);
71  }
72 
73  public IUserProjectAssignment GetByProject(IProject project)
74  {
75  return this[project];
76  }
77 
78  public IUserProjectAssignment GetByProjectGuid(Guid projectGuid)
79  {
80  return this.First(assignment => assignment.Project.Guid == projectGuid);
81  }
82 
83  public IUserProjectAssignment GetByProjectName(string projectName)
84  {
85  return this[projectName];
86  }
87 
88  public IUserProjectAssignment this[IProject project]
89  {
90  get { return GetByProjectGuid(project.Guid); }
91  }
92 
93  public void Remove(IProject project)
94  {
95  UserProjectAssignment.Delete(project, User);
96  }
97 
98  public ISession Session
99  {
100  get { return _user.Session; }
101  }
102 
103  public bool TryGetByProject(IProject project, out IUserProjectAssignment assignment)
104  {
105  return TryGetByProjectName(project.Name, out assignment);
106  }
107 
108  public bool TryGetByProjectGuid(Guid projectGuid, out IUserProjectAssignment assignment)
109  {
110  assignment = this.FirstOrDefault(projectAssignment => projectAssignment.Project.Guid == projectGuid);
111  return assignment != null;
112  }
113 
114  public bool TryGetByProjectName(string projectName, out IUserProjectAssignment assignment)
115  {
116  return TryGet(projectName, out assignment);
117  }
118 
119  public IUser User
120  {
121  get { return _user; }
122  }
123 
124  private List<IUserProjectAssignment> GetProjectAssignments()
125  {
126  const string LIST_USER_PROJECTS =
127  @"<ADMINISTRATION><USER guid=""{0}""><PROJECTS action=""list"" extendedinfo=""1""/></USER></ADMINISTRATION>";
128 
129  var xmlDoc = Session.ExecuteRQL(LIST_USER_PROJECTS.RQLFormat(User));
130  return (from XmlElement assignmentElement in xmlDoc.GetElementsByTagName("PROJECT")
131  select (IUserProjectAssignment) new UserProjectAssignment(_user, assignmentElement)).ToList();
132  }
133  }
134 }