SmartAPI
Open Source .NET RQL library for RedDot CMS / OpenText WSM Management Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
IContentClassFolderSharing.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.Utils;
20 using erminas.SmartAPI.Utils.CachedCollections;
21 
22 namespace erminas.SmartAPI.CMS.Project.Folder
23 {
24  internal class ContentClassFolderSharing : NameIndexedRDList<IProject>, IContentClassFolderSharing
25  {
26  private const string SHARING =
27  @"<SHAREDFOLDER shared=""{0}"" action=""save"" guid=""{1}"" ><PROJECTS>{2}</PROJECTS></SHAREDFOLDER>";
28 
29  private const string SINGLE_PROJECT = @"<PROJECT guid=""{0}"" sharedrights=""{1}"" />";
30  private readonly ContentClassFolder _contentClassFolder;
31 
32  internal ContentClassFolderSharing(ContentClassFolder contentClassFolder, Caching caching) : base(caching)
33  {
34  _contentClassFolder = contentClassFolder;
35  RetrieveFunc = GetSharedToProjects;
36  }
37 
38  public void Add(IProject project)
39  {
40  AddRange(new[] {project});
41  }
42 
43  public void AddRange(IEnumerable<IProject> projects)
44  {
45  const bool IS_SHARED = true;
46  var projectsRql = projects.Aggregate("", (s, project) => s + SINGLE_PROJECT.RQLFormat(project, IS_SHARED));
47 
48  var query = SHARING.RQLFormat(IS_SHARED, _contentClassFolder, projectsRql);
49  Project.ExecuteRQL(query, RqlType.SessionKeyInProject);
50 
51  InvalidateCache();
52  }
53 
54  public IContentClassFolder ContentClassFolder
55  {
56  get { return _contentClassFolder; }
57  }
58 
59  public IProject Project
60  {
61  get { return _contentClassFolder.Project; }
62  }
63 
64  public void Remove(IProject project)
65  {
66  var isStillSharing = Count > 1 || (Count == 1 && !this.First().Equals(project));
67  Project.ExecuteRQL(
68  SHARING.RQLFormat(isStillSharing, _contentClassFolder, SINGLE_PROJECT.RQLFormat(project, 0)),
69  RqlType.SessionKeyInProject);
70 
71  InvalidateCache();
72  }
73 
74  public ISession Session
75  {
76  get { return _contentClassFolder.Session; }
77  }
78 
79  private List<IProject> GetSharedToProjects()
80  {
81  const string LOAD_SHARED = @"<SHAREDFOLDER action=""load"" guid=""{0}""/>";
82 
83  var xmlDoc = Project.ExecuteRQL(LOAD_SHARED.RQLFormat(_contentClassFolder), RqlType.SessionKeyInProject);
84  var projectNodes = xmlDoc.SelectNodes("//PROJECT[@sharedrights=1]");
85 
86  //server manager can access all projects, others might only be able to see the name/guid
87  if (Session.CurrentUser.ModuleAssignment.IsServerManager)
88  {
89  return
90  (from XmlElement curProject in projectNodes
91  select Session.ServerManager.Projects.GetByGuid(curProject.GetGuid())).ToList();
92  }
93 
94  return (from XmlElement curProject in projectNodes
95  select (IProject) new Project(Session, curProject.GetGuid()) {Name = curProject.GetName()}).ToList();
96  }
97  }
98 
99  public interface IContentClassFolderSharing : IIndexedRDList<string, IProject>, IProjectObject
100  {
101  void Add(IProject project);
102  void AddRange(IEnumerable<IProject> project);
103  IContentClassFolder ContentClassFolder { get; }
104  void Remove(IProject project);
105  }
106 }