using System; using System.Collections.Generic; using System.Text; using System.IO; namespace WmData { /// /// The WmDataFile object is a two purpose object: /// 1) it provides meta data of a certain file, like creation date, size, type, etc. /// 2) it provides acces to the real binary file data in the SQL backend /// public class WmDataFile : IComparable { protected int id; protected DateTime created; protected DateTime lastAccessed; protected DateTime lastUpdated; protected string filename; protected string fileExtension; protected int contentLength; protected string contentType; protected WmDataFileSupport fileSupport; protected static WmDataFileSortBy sortBy; public WmDataFile(WmDataFileSupport fileSupport){ this.fileSupport = fileSupport; // used for retrieval of the bits and byes (binary data) } public int Id { get { return id; } set { id = value; } } public DateTime Created { get { return created; } set { created = value; } } public DateTime LastAccessed { get { return lastAccessed; } set { lastAccessed = value; } } public DateTime LastUpdated { get { return lastUpdated; } set { lastUpdated = value; } } public string Filename { get { return filename; } set { filename = value; } } public string FileExtension { get { return fileExtension; } set { fileExtension = value; } } public int ContentLength { get { return contentLength; } set { contentLength = value; } } public string ContentType { get { return contentType; } set { contentType = value; } } public byte[] BinaryData { get { // first check if this meta object was already stored in the database backend if (id == 0) { throw new Exception("WmDataFile: The object instance with meta data must be added to SQL backend before reading binary data"); } byte[] binaryData = fileSupport.readBinaryData(this); return binaryData; } set { // first check if this meta object was already stored in the database backend if (id == 0) { throw new Exception("WmDataFile: The object instance with meta data must be added to SQL backend before adding binary data");} fileSupport.saveBinaryData(this, value); } } public void saveAs(string path) { FileStream fileWriter = new FileStream(path, FileMode.Create); byte[] binaryData = BinaryData; fileWriter.Write(binaryData, 0, binaryData.Length); fileWriter.Close(); } public static WmDataFileSortBy SortBy { get { return sortBy; } set { sortBy = value; } } public int CompareTo(object obj) { if (!(obj is WmDataFile)) { throw new InvalidCastException("This object is not of type WmDataFile"); } WmDataFile other = (WmDataFile)obj; switch (sortBy) { case WmDataFileSortBy.ID: return this.Id.CompareTo(other.Id); case WmDataFileSortBy.CREATED: return this.Created.CompareTo(other.Created); case WmDataFileSortBy.LASTACCESSED: return this.LastAccessed.CompareTo(other.LastAccessed); case WmDataFileSortBy.LASTUPDATED: return this.LastUpdated.CompareTo(other.LastUpdated); case WmDataFileSortBy.SIZE: return this.ContentLength.CompareTo(other.ContentLength); default: goto case WmDataFileSortBy.ID; } } } public enum WmDataFileSortBy { ID,CREATED,LASTACCESSED,LASTUPDATED,FILENAME,SIZE } }