SmartAPI
Open Source .NET RQL library for RedDot CMS / OpenText WSM Management Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
IAssetManagerFiles.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.Security;
21 using erminas.SmartAPI.CMS.ServerManagement;
22 using erminas.SmartAPI.Exceptions;
23 using erminas.SmartAPI.Utils;
24 using erminas.SmartAPI.Utils.CachedCollections;
25 
26 namespace erminas.SmartAPI.CMS.Project.Folder
27 {
28  public interface IAssetManagerFiles : IFiles
29  {
30  new IAssetManagerFolder Folder { get; }
31 
39  [VersionIsGreaterThanOrEqual(10, VersionName = "Version 10")]
40  ReadOnlyCollection<IFile> GetByAttribute(FileComparisonAttribute attribute, FileComparisonOperator @operator,
41  int value);
42 
43  ReadOnlyCollection<IFile> GetByAuthor(IUser user);
44  ReadOnlyCollection<IFile> GetByLastModifier(IUser user);
45  void UpdateThumbnailAndFileInformation(string filename);
46  void UpdateThumbnailAndFileInformationForAll();
47  void UpdateThumbnailAndFileInformationRange(IEnumerable<string> filenames);
48  }
49 
50  internal class AssetManagerFiles : Files, IAssetManagerFiles
51  {
52  internal AssetManagerFiles(IAssetManagerFolder folder, Caching caching) : base(folder, caching)
53  {
54  RetrieveFunc = GetFiles;
55  }
56 
57  public new IAssetManagerFolder Folder
58  {
59  get { return (IAssetManagerFolder) base.Folder; }
60  }
61 
62  [VersionIsGreaterThanOrEqual(10, VersionName = "Version 10")]
63  public ReadOnlyCollection<IFile> GetByAttribute(FileComparisonAttribute attribute,
64  FileComparisonOperator @operator, int value)
65  {
66  VersionVerifier.EnsureVersion(Session);
67 
68  const string FILTER_FILES_BY_COMMAND =
69  @"<MEDIA><FOLDER guid=""{0}"" subdirguid=""{0}""><FILES action=""list"" view=""thumbnail"" sectioncount=""30"" maxfilesize=""0"" command=""{1}"" op=""{2}"" value=""{3}"" startcount=""1"" orderby=""name""/></FOLDER></MEDIA>";
70 
71  var rqlString = FILTER_FILES_BY_COMMAND.RQLFormat(Folder, ComparisonAttributeToString(attribute),
72  ComparisonOperatorToString(@operator), value);
73  return RetrieveFiles(rqlString).AsReadOnly();
74  }
75 
76  public ReadOnlyCollection<IFile> GetByAuthor(IUser author)
77  {
78  const string FILTER_FILES_BY_CREATOR =
79  @"<MEDIA><FOLDER guid=""{0}"" subdirguid=""{0}""><FILES action=""list"" view=""thumbnail"" maxfilesize=""0"" createguid=""{1}"" pattern="""" startcount=""1"" orderby=""name""/></FOLDER></MEDIA>";
80 
81  var query = FILTER_FILES_BY_CREATOR.RQLFormat(Folder, author);
82  return RetrieveFiles(query).AsReadOnly();
83  }
84 
85  public ReadOnlyCollection<IFile> GetByLastModifier(IUser lastModifier)
86  {
87  const string FILTER_FILES_BY_CHANGEAUTHOR =
88  @"<MEDIA><FOLDER guid=""{0}"" subdirguid=""{0}""><FILES action=""list"" view=""thumbnail"" maxfilesize=""0"" changeguid=""{1}"" pattern="""" startcount=""1"" orderby=""name""/></FOLDER></MEDIA>";
89 
90  var query = FILTER_FILES_BY_CHANGEAUTHOR.RQLFormat(Folder, lastModifier);
91  return RetrieveFiles(query).AsReadOnly();
92  }
93 
94  public void UpdateThumbnailAndFileInformation(string filename)
95  {
96  UpdateThumbnailAndFileInformationRange(new[] {filename});
97  }
98 
99  public void UpdateThumbnailAndFileInformationForAll()
100  {
101  UpdateThumbnailAndFileInformationRange(this.Select(file => file.Name));
102  }
103 
104  public void UpdateThumbnailAndFileInformationRange(IEnumerable<string> filenames)
105  {
106  const string FILE_TO_UPDATE = @"<FILE action=""update"" sourcename=""{0}"" tempdir=""{1}""/>";
107 
108  var enumerable = filenames as IList<string> ?? filenames.ToList();
109  var rqlFiles =
110  enumerable.Select(
111  s =>
112  FILE_TO_UPDATE.SecureRQLFormat(s, Session.ServerManager.ApplicationServers.Current.TempDirectoryPath));
113  var files = string.Join(string.Empty, rqlFiles);
114 
115  const string UPDATE_FILES_IN_FOLDER = @"<MEDIA><FOLDER guid=""{0}"">{1}</FOLDER></MEDIA>";
116  var xmlDoc = Project.ExecuteRQL(UPDATE_FILES_IN_FOLDER.RQLFormat(Folder, files));
117  if (xmlDoc.GetElementsByTagName("THUMB").Count == 0)
118  {
119  throw new SmartAPIException(Session.ServerLogin,
120  string.Format(
121  "Could not update thumbnails/file information in folder {0} on: {1}",
122  Folder, string.Join(", ", enumerable)));
123  }
124  }
125 
126  protected override string GetDeleteFilesStatement(string files)
127  {
128  const string DELETE_FILES =
129  @"<MEDIA loginguid=""{0}""><FOLDER guid=""{1}"" subdirguid=""{1}"" tempdir=""{2}{0}\""><FILES action=""deletefiles"">{3}</FILES></FOLDER></MEDIA>";
130 
131  return DELETE_FILES.RQLFormat(Session.LogonGuid, Folder,
132  SecurityElement.Escape(
133  Session.ServerManager.ApplicationServers.Current.TempDirectoryPath), files);
134  }
135 
136  protected override string GetSingleFilenameTemplate()
137  {
138  return @"<FILE sourcename=""{0}"" deletereal=""1"" languagevariantid=""" +
139  Project.LanguageVariants.Main.Abbreviation + @"""/>";
140  }
141 
142  private static string ComparisonAttributeToString(FileComparisonAttribute attribute)
143  {
144  switch (attribute)
145  {
146  case FileComparisonAttribute.Width:
147  return "width";
148  case FileComparisonAttribute.Heigth:
149  return "height";
150  case FileComparisonAttribute.Size:
151  return "size";
152  case FileComparisonAttribute.Depth:
153  return "depth";
154  default:
155  throw new ArgumentException(string.Format("Unknown file attribute: {0}", attribute));
156  }
157  }
158 
159  private static string ComparisonOperatorToString(FileComparisonOperator @operator)
160  {
161  switch (@operator)
162  {
163  case FileComparisonOperator.Greater:
164  return "gt";
165  case FileComparisonOperator.Less:
166  return "lt";
167  case FileComparisonOperator.LessEqual:
168  return "le";
169  case FileComparisonOperator.GreaterEqual:
170  return "ge";
171  case FileComparisonOperator.Equal:
172  return "eq";
173  default:
174  throw new ArgumentException(string.Format("Unknown comparison operator: {0}", @operator));
175  }
176  }
177 
178  private List<IFile> GetFiles()
179  {
180  const string LIST_FILES =
181  @"<MEDIA><FOLDER guid=""{0}"" subdirguid=""{0}""><FILES action=""list"" view=""thumbnail"" maxfilesize=""0"" attributeguid="""" searchtext=""*"" pattern="""" startcount=""1"" orderby=""name""/></FOLDER></MEDIA>";
182 
183  return RetrieveFiles(LIST_FILES.RQLFormat(Folder));
184  }
185  }
186 }