PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/mojoPortal.Business/FileAttachment.cs

#
C# | 400 lines | 229 code | 74 blank | 97 comment | 6 complexity | f68638dea7fafd3494c7755958a41212 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause, CPL-1.0, CC-BY-SA-3.0, GPL-2.0
  1. // Author: Joe Audette
  2. // Created: 2009-03-08
  3. // Last Modified: 2011-08-23
  4. //
  5. // The use and distribution terms for this software are covered by the
  6. // Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
  7. // which can be found in the file CPL.TXT at the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by
  9. // the terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Data;
  15. using mojoPortal.Data;
  16. namespace mojoPortal.Business
  17. {
  18. public class FileAttachment
  19. {
  20. #region Constructors
  21. public FileAttachment()
  22. { }
  23. public FileAttachment(Guid rowGuid)
  24. {
  25. GetFileAttachment(rowGuid);
  26. }
  27. #endregion
  28. #region Private Properties
  29. private Guid rowGuid = Guid.Empty;
  30. private Guid siteGuid = Guid.Empty;
  31. private Guid moduleGuid = Guid.Empty;
  32. private Guid itemGuid = Guid.Empty;
  33. private Guid specialGuid1 = Guid.Empty;
  34. private Guid specialGuid2 = Guid.Empty;
  35. private string serverFileName = string.Empty;
  36. private string fileName = string.Empty;
  37. private DateTime createdUtc = DateTime.UtcNow;
  38. private Guid createdBy = Guid.Empty;
  39. #endregion
  40. #region Public Properties
  41. public Guid RowGuid
  42. {
  43. get { return rowGuid; }
  44. set { rowGuid = value; }
  45. }
  46. public Guid SiteGuid
  47. {
  48. get { return siteGuid; }
  49. set { siteGuid = value; }
  50. }
  51. public Guid ModuleGuid
  52. {
  53. get { return moduleGuid; }
  54. set { moduleGuid = value; }
  55. }
  56. public Guid ItemGuid
  57. {
  58. get { return itemGuid; }
  59. set { itemGuid = value; }
  60. }
  61. public Guid SpecialGuid1
  62. {
  63. get { return specialGuid1; }
  64. set { specialGuid1 = value; }
  65. }
  66. public Guid SpecialGuid2
  67. {
  68. get { return specialGuid2; }
  69. set { specialGuid2 = value; }
  70. }
  71. public string ServerFileName
  72. {
  73. get { return serverFileName; }
  74. set { serverFileName = value; }
  75. }
  76. public string FileName
  77. {
  78. get { return fileName; }
  79. set { fileName = value; }
  80. }
  81. public DateTime CreatedUtc
  82. {
  83. get { return createdUtc; }
  84. set { createdUtc = value; }
  85. }
  86. public Guid CreatedBy
  87. {
  88. get { return createdBy; }
  89. set { createdBy = value; }
  90. }
  91. #endregion
  92. #region Private Methods
  93. /// <summary>
  94. /// Gets an instance of FileAttachment.
  95. /// </summary>
  96. /// <param name="rowGuid"> rowGuid </param>
  97. private void GetFileAttachment(Guid rowGuid)
  98. {
  99. using (IDataReader reader = DBFileAttachment.GetOne(rowGuid))
  100. {
  101. PopulateFromReader(reader);
  102. }
  103. }
  104. private void PopulateFromReader(IDataReader reader)
  105. {
  106. if (reader.Read())
  107. {
  108. this.rowGuid = new Guid(reader["RowGuid"].ToString());
  109. this.siteGuid = new Guid(reader["SiteGuid"].ToString());
  110. this.moduleGuid = new Guid(reader["ModuleGuid"].ToString());
  111. this.itemGuid = new Guid(reader["ItemGuid"].ToString());
  112. this.specialGuid1 = new Guid(reader["SpecialGuid1"].ToString());
  113. this.specialGuid2 = new Guid(reader["SpecialGuid2"].ToString());
  114. this.serverFileName = reader["ServerFileName"].ToString();
  115. this.fileName = reader["FileName"].ToString();
  116. this.createdUtc = Convert.ToDateTime(reader["CreatedUtc"]);
  117. this.createdBy = new Guid(reader["CreatedBy"].ToString());
  118. }
  119. }
  120. /// <summary>
  121. /// Persists a new instance of FileAttachment. Returns true on success.
  122. /// </summary>
  123. /// <returns></returns>
  124. private bool Create()
  125. {
  126. this.rowGuid = Guid.NewGuid();
  127. if (this.serverFileName.Length == 0)
  128. {
  129. this.serverFileName = rowGuid.ToString() + ".config";
  130. }
  131. int rowsAffected = DBFileAttachment.Create(
  132. this.rowGuid,
  133. this.siteGuid,
  134. this.moduleGuid,
  135. this.itemGuid,
  136. this.specialGuid1,
  137. this.specialGuid2,
  138. this.serverFileName,
  139. this.fileName,
  140. this.createdUtc,
  141. this.createdBy);
  142. return (rowsAffected > 0);
  143. }
  144. /// <summary>
  145. /// Updates this instance of FileAttachment. Returns true on success.
  146. /// </summary>
  147. /// <returns>bool</returns>
  148. private bool Update()
  149. {
  150. return DBFileAttachment.Update(
  151. this.rowGuid,
  152. this.serverFileName,
  153. this.fileName);
  154. }
  155. #endregion
  156. #region Public Methods
  157. /// <summary>
  158. /// Saves this instance of FileAttachment. Returns true on success.
  159. /// </summary>
  160. /// <returns>bool</returns>
  161. public bool Save()
  162. {
  163. if (this.rowGuid != Guid.Empty)
  164. {
  165. return Update();
  166. }
  167. else
  168. {
  169. return Create();
  170. }
  171. }
  172. #endregion
  173. #region Static Methods
  174. /// <summary>
  175. /// Deletes an instance of FileAttachment. Returns true on success.
  176. /// </summary>
  177. /// <param name="rowGuid"> rowGuid </param>
  178. /// <returns>bool</returns>
  179. public static bool Delete(Guid rowGuid)
  180. {
  181. return DBFileAttachment.Delete(rowGuid);
  182. }
  183. /// <summary>
  184. /// Deletes rows from the mp_FileAttachment table. Returns true if row deleted.
  185. /// </summary>
  186. /// <param name="rowGuid"> rowGuid </param>
  187. /// <returns>bool</returns>
  188. public static bool DeleteBySite(Guid siteGuid)
  189. {
  190. return DBFileAttachment.DeleteBySite(siteGuid);
  191. }
  192. /// <summary>
  193. /// Deletes rows from the mp_FileAttachment table. Returns true if row deleted.
  194. /// </summary>
  195. /// <param name="rowGuid"> rowGuid </param>
  196. /// <returns>bool</returns>
  197. public static bool DeleteByModule(Guid moduleGuid)
  198. {
  199. return DBFileAttachment.DeleteByModule(moduleGuid);
  200. }
  201. /// <summary>
  202. /// Deletes rows from the mp_FileAttachment table. Returns true if row deleted.
  203. /// </summary>
  204. /// <param name="rowGuid"> rowGuid </param>
  205. /// <returns>bool</returns>
  206. public static bool DeleteByItem(Guid itemGuid)
  207. {
  208. return DBFileAttachment.DeleteByItem(itemGuid);
  209. }
  210. //public static void DeleteAttachmentFiles(List<FileAttachment> attachments, string basePath)
  211. //{
  212. // if (attachments == null) { return; }
  213. // if(string.IsNullOrEmpty(basePath)) { return;}
  214. // foreach (FileAttachment f in attachments)
  215. // {
  216. // if(File.Exists(Path.Combine(basePath, f.ServerFileName)))
  217. // {
  218. // File.Delete(Path.Combine(basePath, f.ServerFileName));
  219. // }
  220. // }
  221. //}
  222. //public static void DeleteAttachmentFile(FileAttachment attachment, string basePath)
  223. //{
  224. // if (attachment == null) { return; }
  225. // if(string.IsNullOrEmpty(basePath)) { return;}
  226. // if(File.Exists(Path.Combine(basePath, attachment.ServerFileName)))
  227. // {
  228. // File.Delete(Path.Combine(basePath, attachment.ServerFileName));
  229. // }
  230. //}
  231. private static List<FileAttachment> LoadListFromReader(IDataReader reader)
  232. {
  233. List<FileAttachment> fileAttachmentList = new List<FileAttachment>();
  234. try
  235. {
  236. while (reader.Read())
  237. {
  238. FileAttachment fileAttachment = new FileAttachment();
  239. fileAttachment.rowGuid = new Guid(reader["RowGuid"].ToString());
  240. fileAttachment.siteGuid = new Guid(reader["SiteGuid"].ToString());
  241. fileAttachment.moduleGuid = new Guid(reader["ModuleGuid"].ToString());
  242. fileAttachment.itemGuid = new Guid(reader["ItemGuid"].ToString());
  243. fileAttachment.specialGuid1 = new Guid(reader["SpecialGuid1"].ToString());
  244. fileAttachment.specialGuid2 = new Guid(reader["SpecialGuid2"].ToString());
  245. fileAttachment.serverFileName = reader["ServerFileName"].ToString();
  246. fileAttachment.fileName = reader["FileName"].ToString();
  247. fileAttachment.createdUtc = Convert.ToDateTime(reader["CreatedUtc"]);
  248. fileAttachment.createdBy = new Guid(reader["CreatedBy"].ToString());
  249. fileAttachmentList.Add(fileAttachment);
  250. }
  251. }
  252. finally
  253. {
  254. reader.Close();
  255. }
  256. return fileAttachmentList;
  257. }
  258. /// <summary>
  259. /// Gets an IDataReader with row sfrom the mp_FileAttachment table.
  260. /// </summary>
  261. /// <param name="rowGuid"> rowGuid </param>
  262. public static IDataReader SelectByModule(Guid moduleGuid)
  263. {
  264. return DBFileAttachment.SelectByModule(moduleGuid);
  265. }
  266. /// <summary>
  267. /// Gets an IDataReader with rows from the mp_FileAttachment table.
  268. /// </summary>
  269. /// <param name="rowGuid"> rowGuid </param>
  270. public static IDataReader SelectByItem(Guid itemGuid)
  271. {
  272. return DBFileAttachment.SelectByItem(itemGuid);
  273. }
  274. /// <summary>
  275. /// Gets a List<FileAttachment> with rows from the mp_FileAttachment table.
  276. /// </summary>
  277. /// <param name="rowGuid"> rowGuid </param>
  278. public static List<FileAttachment> GetListByItem(Guid itemGuid)
  279. {
  280. using (IDataReader reader = DBFileAttachment.SelectByItem(itemGuid))
  281. {
  282. return LoadListFromReader(reader);
  283. }
  284. }
  285. /// <summary>
  286. /// Gets an IDataReader with rows from the mp_FileAttachment table.
  287. /// </summary>
  288. /// <param name="rowGuid"> rowGuid </param>
  289. public static IDataReader SelectBySpecial1(Guid specialGuid1)
  290. {
  291. return DBFileAttachment.SelectBySpecial1(specialGuid1);
  292. }
  293. /// <summary>
  294. /// Gets an IDataReader with rows from the mp_FileAttachment table.
  295. /// </summary>
  296. /// <param name="rowGuid"> rowGuid </param>
  297. public static IDataReader SelectBySpecial2(Guid specialGuid2)
  298. {
  299. return DBFileAttachment.SelectBySpecial2(specialGuid2);
  300. }
  301. #endregion
  302. #region Comparison Methods
  303. /// <summary>
  304. /// Compares 2 instances of FileAttachment.
  305. /// </summary>
  306. public static int CompareByServerFileName(FileAttachment fileAttachment1, FileAttachment fileAttachment2)
  307. {
  308. return fileAttachment1.ServerFileName.CompareTo(fileAttachment2.ServerFileName);
  309. }
  310. /// <summary>
  311. /// Compares 2 instances of FileAttachment.
  312. /// </summary>
  313. public static int CompareByFileName(FileAttachment fileAttachment1, FileAttachment fileAttachment2)
  314. {
  315. return fileAttachment1.FileName.CompareTo(fileAttachment2.FileName);
  316. }
  317. /// <summary>
  318. /// Compares 2 instances of FileAttachment.
  319. /// </summary>
  320. public static int CompareByCreatedUtc(FileAttachment fileAttachment1, FileAttachment fileAttachment2)
  321. {
  322. return fileAttachment1.CreatedUtc.CompareTo(fileAttachment2.CreatedUtc);
  323. }
  324. #endregion
  325. }
  326. }