SmartAPI
Open Source .NET RQL library for RedDot CMS / OpenText WSM Management Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
IFiles.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.Collections.ObjectModel;
19 using System.Linq;
20 using System.Xml;
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 {
27  internal class Files : IndexedCachedList<string, IFile>, IFiles
28  {
29  private readonly IFolder _folder;
30 
31  internal Files(IFolder folder, Caching caching) : base(file => file.Name, caching)
32  {
33  _folder = folder;
34  RetrieveFunc = GetFiles;
35  }
36 
37  public void Add(string filename, string directory)
38  {
39  AddRange(new[] {new FileSource(filename, directory)});
40  }
41 
42  public void AddRange(IEnumerable<FileSource> sources)
43  {
44  var fileSources = sources as IList<FileSource> ?? sources.ToList();
45  if (!fileSources.Any())
46  {
47  return;
48  }
49  const string SINGLE_FILE = @"<FILE action=""save"" sourcename=""{0}"" sourcepath=""{1}""/>";
50  var filesToSave =
51  fileSources.Select(fileSource => SINGLE_FILE.SecureRQLFormat(fileSource.Filename, fileSource.Directory));
52 
53  const string SAVE_FILES = @"<PROJECT><FOLDER guid=""{0}"">{1}</FOLDER></PROJECT>";
54  var xmlDoc = Project.ExecuteRQL(SAVE_FILES.RQLFormat(_folder, string.Join(string.Empty, filesToSave)));
55 
56  var responseText = xmlDoc.GetSingleElement("IODATA").InnerText;
57  if (!string.IsNullOrWhiteSpace(responseText))
58  {
59  throw new SmartAPIException(Session.ServerLogin,
60  string.Format("Could not add files in folder {0}: {1}", _folder,
61  responseText));
62  }
63  }
64 
65  public IFolder Folder
66  {
67  get { return _folder; }
68  }
69 
70  public IFile GetByName(string name)
71  {
72  return GetByNamePattern(name).First(file => file.Name == name);
73  }
74 
75  public ReadOnlyCollection<IFile> GetByNamePattern(string searchText)
76  {
77  const string LIST_FILES_BY_NAME_PATTERN =
78  @"<PROJECT><TEMPLATE><ELEMENT folderguid=""{0}""><FILES action=""list"" searchtext=""{1}"" /></ELEMENT></TEMPLATE></PROJECT>";
79  return RetrieveFiles(LIST_FILES_BY_NAME_PATTERN.SecureRQLFormat(Folder, searchText)).AsReadOnly();
80  }
81 
82  public IProject Project
83  {
84  get { return Folder.Project; }
85  }
86 
87  public void Remove(string filename)
88  {
89  RemoveRange(new[] {filename});
90  }
91 
92  public void RemoveForcibly(string filename)
93  {
94  RemoveRangeForcibly(new[] {filename});
95  }
96 
97  public virtual void RemoveRange(IEnumerable<string> filenames)
98  {
99  RemoveRangeForcibly(filenames);
100  }
101 
102  public virtual void RemoveRangeForcibly(IEnumerable<string> filenames)
103  {
104  var filenameList = filenames as IList<string> ?? filenames.ToList();
105  if (!filenameList.Any())
106  {
107  return;
108  }
109 
110  var files = string.Join(string.Empty,
111  filenameList.Select(s => GetSingleFilenameTemplate().SecureRQLFormat(s)));
112  var deleteFiles = GetDeleteFilesStatement(files);
113  try
114  {
115  var xmlDoc = Project.ExecuteRQL(deleteFiles);
116  if (!xmlDoc.IsContainingOk())
117  {
118  throw new SmartAPIException(Session.ServerLogin,
119  string.Format(
120  "In folder {0}, could not delete one ore more files out of: {1}",
121  _folder, string.Join(", ", filenameList)));
122  }
123  }
124  catch (RQLException e)
125  {
126  //TODO check auf dem aelteren 11er server
127  if (Session.ServerVersion <= new Version(11, 2) ||
128  !e.Message.Contains(
129  "ERROR in procedure Files: Object reference not set to an instance of an object."))
130  {
131  throw;
132  }
133  }
134  }
135 
136  public ISession Session
137  {
138  get { return Folder.Session; }
139  }
140 
141  protected virtual string GetDeleteFilesStatement(string files)
142  {
143  const string DELETE_FILES =
144  @"<MEDIA><FOLDER guid=""{0}""><FILES action=""deletefiles"">{1}</FILES></FOLDER></MEDIA>";
145  return DELETE_FILES.RQLFormat(Folder, files);
146  }
147 
148  protected virtual string GetSingleFilenameTemplate()
149  {
150  const string SINGLE_FILE = @"<FILE sourcename=""{0}"" currendirectory="""" checkfolder=""1""/>";
151  return SINGLE_FILE;
152  }
153 
154  protected List<IFile> RetrieveFiles(string rqlString)
155  {
156  XmlDocument xmlDoc = Project.ExecuteRQL(rqlString);
157  XmlNodeList xmlNodes = xmlDoc.GetElementsByTagName("FILE");
158 
159  return (from XmlElement xmlNode in xmlNodes select (IFile) new File(Project, xmlNode)).ToList();
160  }
161 
162  private List<IFile> GetFiles()
163  {
164  const string LIST_FILES =
165  @"<PROJECT><TEMPLATE><ELEMENT folderguid=""{0}""><FILES action=""list"" /></ELEMENT></TEMPLATE></PROJECT>";
166 
167  return RetrieveFiles(LIST_FILES.RQLFormat(Folder));
168  }
169  }
170 
171  public interface IFiles : IIndexedCachedList<string, IFile>, IProjectObject
172  {
173  void Add(string filename, string directory);
174  void AddRange(IEnumerable<FileSource> sources);
175  IFolder Folder { get; }
176  ReadOnlyCollection<IFile> GetByNamePattern(string searchText);
177  void Remove(string filename);
178  void RemoveForcibly(string filename);
179  void RemoveRange(IEnumerable<string> filenames);
180  void RemoveRangeForcibly(IEnumerable<string> filenames);
181  }
182 }