SmartAPI
Open Source .NET RQL library for RedDot CMS / OpenText WSM Management Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
IFile.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.Xml;
18 using erminas.SmartAPI.Utils;
19 
20 namespace erminas.SmartAPI.CMS.Project.Folder
21 {
22  public interface IFile : IProjectObject, IDeletable
23  {
24  FileAttributes Attributes { get; }
25 
29  IFolder Folder { get; }
30 
34  string Name { get; }
35 
36  int ReferenceCount();
37 
38  Guid? ThumbnailGuid { get; }
39  string ThumbnailPath { get; }
40  }
41 
42  internal class File : IFile
43  {
47  private readonly IFolder _folder;
48 
52  private readonly string _name;
53 
54  private readonly IProject _project;
55  private readonly Guid? _thumbnailGuid;
56  private readonly XmlElement _xmlElement;
57 
58  internal File(IProject project, XmlElement xmlElement)
59  {
60  _project = project;
61  _xmlElement = xmlElement;
62  _name = xmlElement.GetAttributeValue("name");
63  var folderGuid = xmlElement.GetGuid("folderguid");
64  _folder = project.Folders.AllIncludingSubFolders.GetByGuid(folderGuid);
65  Guid guid;
66  if (_xmlElement.TryGetGuid("thumbguid", out guid))
67  {
68  _thumbnailGuid = guid;
69  }
70  if (IsAssetWithThumbnail)
71  {
72  //older versions do not contain the thumbnailpath attribute, so it has to be constructed
73  ThumbnailPath = xmlElement.GetAttributeValue("thumbnailpath") ?? CreateThumbnailPath();
74  }
75  }
76 
77  public File(IFolder folder, string fileName)
78  {
79  _project = folder.Project;
80  _folder = folder;
81  _name = fileName;
82  }
83 
84  public FileAttributes Attributes
85  {
86  get
87  {
88  const string LIST_FILE_ATTRIBUTES =
89  @"<MEDIA><FOLDER guid=""{0}""><FILE sourcename=""{1}""><FILEATTRIBUTES action=""list""/></FILE></FOLDER></MEDIA>";
90 
91  XmlDocument xmlDoc = Project.ExecuteRQL(LIST_FILE_ATTRIBUTES.RQLFormat(_folder, _name));
92 
93  var node = (XmlElement) xmlDoc.GetElementsByTagName("EXTERNALATTRIBUTES")[0];
94  return new FileAttributes(_folder, node);
95  }
96  }
97 
98  public void Delete()
99  {
100  //TODO ist ENG okay, oder sollte man z.b. die standardsprache waehlen
101  const string DELETE_FILE =
102  "<MEDIA><FOLDER guid=\"{0}\" ><FILES action=\"deletefiles\"><FILE sourcename=\"{1}\" languagevariantid=\"ENG\" checkfolder=\"1\"/></FILES></FOLDER></MEDIA>";
103 
104  _project.ExecuteRQL(string.Format(DELETE_FILE, _folder.Guid.ToRQLString(), _name));
105  }
106 
107  public override bool Equals(object obj)
108  {
109  if (ReferenceEquals(null, obj))
110  {
111  return false;
112  }
113  if (ReferenceEquals(this, obj))
114  {
115  return true;
116  }
117  return obj.GetType() == GetType() && Equals((File) obj);
118  }
119 
123  public IFolder Folder
124  {
125  get { return _folder; }
126  }
127 
128  public override int GetHashCode()
129  {
130  unchecked
131  {
132  return ((_folder != null ? _folder.GetHashCode() : 0)*397) ^ (_name != null ? _name.GetHashCode() : 0);
133  }
134  }
135 
139  public string Name
140  {
141  get { return _name; }
142  }
143 
144  public IProject Project
145  {
146  get { return _project; }
147  }
148 
149  public int ReferenceCount()
150  {
151  const string GET_REFERENCES =
152  "<PROJECT><TRANSFER action=\"checkimage\" getreferences=\"1\" sync=\"1\" folderguid=\"{0}\" filename=\"{1}\" /></PROJECT>";
153 
154  XmlDocument xmlDoc = _project.ExecuteRQL(string.Format(GET_REFERENCES, _folder.Guid.ToRQLString(), _name));
155  return xmlDoc.GetElementsByTagName("REFERENCE").Count;
156  }
157 
158  public ISession Session
159  {
160  get { return Project.Session; }
161  }
162 
163  public Guid? ThumbnailGuid
164  {
165  get { return _thumbnailGuid; }
166  }
167 
168  public string ThumbnailPath { get; private set; }
169 
170  public override string ToString()
171  {
172  string path = "";
173  if (_folder is IAssetManagerFolder)
174  {
175  var assetFolder = (IAssetManagerFolder) _folder;
176  if (assetFolder.IsSubFolder)
177  {
178  path = assetFolder.ParentFolder.Name + "/";
179  }
180  }
181  return _folder != null ? path + _folder.Name + "/" + Name : "";
182  }
183 
184  protected bool Equals(File other)
185  {
186  return Equals(_folder, other._folder) && string.Equals(_name, other._name);
187  }
188 
189  private string CreateThumbnailPath()
190  {
191  return @"THUMBNAIL\{0}\{1}\{2}.JPG".RQLFormat(_project, _folder, _thumbnailGuid.Value);
192  }
193 
194  private bool IsAssetWithThumbnail
195  {
196  get { return _thumbnailGuid != null; }
197  }
198  }
199 }