17 using System.Collections.Generic;
18 using System.Collections.ObjectModel;
21 using erminas.SmartAPI.Exceptions;
22 using erminas.SmartAPI.Utils;
23 using erminas.SmartAPI.Utils.CachedCollections;
25 namespace erminas.SmartAPI.CMS.Project.Folder
27 internal class Files : IndexedCachedList<string, IFile>, IFiles
29 private readonly IFolder _folder;
31 internal Files(IFolder folder,
Caching caching) : base(file => file.Name, caching)
34 RetrieveFunc = GetFiles;
37 public void Add(
string filename,
string directory)
39 AddRange(
new[] {
new FileSource(filename, directory)});
42 public void AddRange(IEnumerable<FileSource> sources)
44 var fileSources = sources as IList<FileSource> ?? sources.ToList();
45 if (!fileSources.Any())
49 const string SINGLE_FILE =
@"<FILE action=""save"" sourcename=""{0}"" sourcepath=""{1}""/>";
51 fileSources.Select(fileSource => SINGLE_FILE.SecureRQLFormat(fileSource.Filename, fileSource.Directory));
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)));
56 var responseText = xmlDoc.GetSingleElement(
"IODATA").InnerText;
57 if (!
string.IsNullOrWhiteSpace(responseText))
60 string.Format(
"Could not add files in folder {0}: {1}", _folder,
67 get {
return _folder; }
70 public IFile GetByName(
string name)
72 return GetByNamePattern(name).First(file => file.Name == name);
75 public ReadOnlyCollection<IFile> GetByNamePattern(
string searchText)
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();
82 public IProject Project
84 get {
return Folder.Project; }
87 public void Remove(
string filename)
89 RemoveRange(
new[] {filename});
92 public void RemoveForcibly(
string filename)
94 RemoveRangeForcibly(
new[] {filename});
97 public virtual void RemoveRange(IEnumerable<string> filenames)
99 RemoveRangeForcibly(filenames);
102 public virtual void RemoveRangeForcibly(IEnumerable<string> filenames)
104 var filenameList = filenames as IList<string> ?? filenames.ToList();
105 if (!filenameList.Any())
110 var files =
string.Join(
string.Empty,
111 filenameList.Select(s => GetSingleFilenameTemplate().SecureRQLFormat(s)));
112 var deleteFiles = GetDeleteFilesStatement(files);
115 var xmlDoc = Project.ExecuteRQL(deleteFiles);
116 if (!xmlDoc.IsContainingOk())
120 "In folder {0}, could not delete one ore more files out of: {1}",
121 _folder,
string.Join(
", ", filenameList)));
127 if (Session.ServerVersion <=
new Version(11, 2) ||
129 "ERROR in procedure Files: Object reference not set to an instance of an object."))
136 public ISession Session
138 get {
return Folder.Session; }
141 protected virtual string GetDeleteFilesStatement(
string files)
143 const string DELETE_FILES =
144 @"<MEDIA><FOLDER guid=""{0}""><FILES action=""deletefiles"">{1}</FILES></FOLDER></MEDIA>";
145 return DELETE_FILES.RQLFormat(Folder, files);
148 protected virtual string GetSingleFilenameTemplate()
150 const string SINGLE_FILE =
@"<FILE sourcename=""{0}"" currendirectory="""" checkfolder=""1""/>";
154 protected List<IFile> RetrieveFiles(
string rqlString)
156 XmlDocument xmlDoc = Project.ExecuteRQL(rqlString);
157 XmlNodeList xmlNodes = xmlDoc.GetElementsByTagName(
"FILE");
159 return (from XmlElement xmlNode in xmlNodes select (IFile)
new File(Project, xmlNode)).ToList();
162 private List<IFile> GetFiles()
164 const string LIST_FILES =
165 @"<PROJECT><TEMPLATE><ELEMENT folderguid=""{0}""><FILES action=""list"" /></ELEMENT></TEMPLATE></PROJECT>";
167 return RetrieveFiles(LIST_FILES.RQLFormat(Folder));
173 void Add(
string filename,
string directory);
174 void AddRange(IEnumerable<FileSource> sources);
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);