SmartAPI
Open Source .NET RQL library for RedDot CMS / OpenText WSM Management Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
IContentClassFolder.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.Project.ContentClasses;
21 using erminas.SmartAPI.Exceptions;
22 using erminas.SmartAPI.Utils;
23 using erminas.SmartAPI.Utils.CachedCollections;
24 
25 namespace erminas.SmartAPI.CMS.Project.Folder
26 {
28  {
32  IIndexedRDList<string, IContentClass> ContentClasses { get; }
33 
34  bool IsBroken { get; }
35 
36  bool IsSharedToOtherProjects { get; }
37  IContentClassFolder SharedFrom { get; }
38  IContentClassFolderSharing SharedTo { get; }
39  }
40 
44  internal sealed class ContentClassFolder : RedDotProjectObject, IContentClassFolder
45  {
46  private IProject _project;
47  private Lazy<IContentClassFolder> _sharedFrom;
48 
49  internal ContentClassFolder(IProject project, Guid guid) : base(project, guid)
50  {
51  Init(project);
52  }
53 
54  internal ContentClassFolder(IProject project, XmlElement xmlElement) : base(project, xmlElement)
55  {
56  Init(project);
57  }
58 
62  public IIndexedRDList<string, IContentClass> ContentClasses { get; private set; }
63 
64  public bool IsBroken { get; internal set; }
65 
66  public bool IsSharedToOtherProjects
67  {
68  get { return SharedTo.Any(); }
69  }
70 
71  public IContentClassFolder SharedFrom
72  {
73  get { return _sharedFrom.Value; }
74  }
75 
76  public IContentClassFolderSharing SharedTo { get; private set; }
77 
78  private List<IContentClass> GetContentClasses()
79  {
80  // RQL for listing all content classes of a folder.
81  // One Parameter: Folder Guid: {0}
82  const string LIST_CC_OF_FOLDER = @"<TEMPLATES folderguid=""{0}"" action=""list""/>";
83 
84  XMLDoc = _project.ExecuteRQL(string.Format(LIST_CC_OF_FOLDER, Guid.ToRQLString()));
85 
86  return (from XmlElement curNode in XMLDoc.GetElementsByTagName("TEMPLATE")
87  select (IContentClass) new ContentClass(_project, curNode)).ToList();
88  }
89 
90  private IContentClassFolder GetSharedFrom()
91  {
92  const string LOAD_FOLDER = @"<PROJECT><FOLDER action=""load"" guid=""{0}""/></PROJECT>";
93  var xmlDoc = Project.ExecuteRQL(LOAD_FOLDER.RQLFormat(this));
94  XmlElement = xmlDoc.GetSingleElement("FOLDER");
95  Guid sharedProjectGuid, sharedFolderGuid;
96  if (XmlElement.TryGetGuid("linkedprojectguid", out sharedProjectGuid) &&
97  XmlElement.TryGetGuid("linkedfolderguid", out sharedFolderGuid))
98  {
99  if (IsBroken)
100  {
101  throw new BrokenContentClassFolderSharingException(Session.ServerLogin, this, sharedProjectGuid,
102  sharedFolderGuid);
103  }
104  if (Session.CurrentUser.ModuleAssignment.IsServerManager)
105  {
106  IProject project = Session.ServerManager.Projects.GetByGuid(sharedProjectGuid);
107  return GetSharedFromFolder(project, sharedFolderGuid);
108  }
109  if (Session.ServerManager.Projects.ForCurrentUser.ContainsGuid(sharedProjectGuid))
110  {
111  IProject project = Session.ServerManager.Projects.ForCurrentUser.GetByGuid(sharedProjectGuid);
112  return GetSharedFromFolder(project, sharedFolderGuid);
113  }
114  var sharedProject = new Project(Session, sharedProjectGuid);
115  return new ContentClassFolder(sharedProject, sharedFolderGuid);
116  }
117  return null;
118  }
119 
120  private static IContentClassFolder GetSharedFromFolder(IProject project, Guid sharedFolderGuid)
121  {
122  return
123  project.ContentClassFolders.Union(project.ContentClassFolders.Broken)
124  .First(x => x.Guid == sharedFolderGuid);
125  }
126 
127  private void Init(IProject project)
128  {
129  ContentClasses = new NameIndexedRDList<IContentClass>(GetContentClasses, Caching.Enabled);
130  SharedTo = new ContentClassFolderSharing(this, Caching.Enabled);
131  _project = project;
132  _sharedFrom = new Lazy<IContentClassFolder>(GetSharedFrom);
133  }
134  }
135 }