SmartAPI
Open Source .NET RQL library for RedDot CMS / OpenText WSM Management Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
SubFolders.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.Exceptions;
20 using erminas.SmartAPI.Utils;
21 using erminas.SmartAPI.Utils.CachedCollections;
22 
23 namespace erminas.SmartAPI.CMS.Project.Folder
24 {
25  internal class SubFolders : NameIndexedRDList<IAssetManagerFolder>, ISubFolders
26  {
27  private readonly IAssetManagerFolder _folder;
28 
29  internal SubFolders(IAssetManagerFolder folder, Caching caching) : base(caching)
30  {
31  _folder = folder;
32  RetrieveFunc = GetSubFolders;
33  }
34 
35  public override void InvalidateCache()
36  {
37  ((Project) Project).AllFoldersXmlDocument = null;
38  base.InvalidateCache();
39  }
40 
41  public IAssetManagerFolder ParentFolder
42  {
43  get { return _folder; }
44  }
45 
46  public IProject Project
47  {
48  get { return _folder.Project; }
49  }
50 
51  public new ISubFolders Refreshed()
52  {
53  Refresh();
54  return this;
55  }
56 
57  public ISession Session
58  {
59  get { return _folder.Session; }
60  }
61 
62  private List<IAssetManagerFolder> GetSubFolders()
63  {
64  if (((Project) Project).AllFoldersXmlDocument == null)
65  {
66  const string LOAD_FOLDERS =
67  @"<PROJECT><FOLDERS action=""list"" foldertype=""0"" withsubfolders=""1""/></PROJECT>";
68  ((Project) Project).AllFoldersXmlDocument = Project.ExecuteRQL(LOAD_FOLDERS);
69  }
70 
71  var parentFolderXPath = "//FOLDERS/FOLDER[@guid='{0}']".RQLFormat(ParentFolder);
72  var parentNode = ((Project) Project).AllFoldersXmlDocument.SelectSingleNode(parentFolderXPath);
73  if (parentNode == null)
74  {
75  throw new SmartAPIException(Session.ServerLogin,
76  string.Format("Could not load subfolders of {0}", _folder));
77  }
78 
79  var subFolders = parentNode.SelectNodes(".//SUBFOLDER");
80  return
81  (from XmlElement curSubNode in subFolders
82  select (IAssetManagerFolder) new AssetManagerFolder(_folder, curSubNode)).ToList();
83  }
84  }
85 }