Advertisement
Guest User

Untitled

a guest
Oct 16th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.99 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Data;
  6. using System.Data.Common;
  7. using Microsoft.Practices.EnterpriseLibrary.Data;
  8. using System.Collections.ObjectModel;
  9. using System.IO;
  10.  
  11. namespace CC.XDocs.Data.Documents
  12. {
  13. public class clsDDocuments
  14. {
  15. #region Private fields
  16.  
  17. static readonly string[] SizeSuffixes =
  18. { "bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
  19.  
  20. private readonly Database database;
  21.  
  22. #endregion
  23.  
  24. #region Constructor
  25.  
  26. /// <summary>
  27. /// Default constructor
  28. /// </summary>
  29. public clsDDocuments()
  30. {
  31. this.database = DatabaseFactory.CreateDatabase();
  32. }
  33.  
  34. #endregion
  35.  
  36. #region Public Methods
  37.  
  38. /// <summary>
  39. /// A method that loads all the documents from the database
  40. /// and send them a as datatable (for the GridView).
  41. /// </summary>
  42. /// <returns>Returns the DataTable with all documents.</returns>
  43. public DataTable AllDocuments()
  44. {
  45. int queriesCount = 2;
  46. StringBuilder[] sqlQuery = new StringBuilder[queriesCount];
  47.  
  48. sqlQuery[0] = new StringBuilder().AppendLine(" SELECT Documents.D500_DOCUMENTO AS[Document Name],")
  49. .AppendLine(" Files.D500_NOME_FILE AS[Filename],")
  50. .AppendLine(" Categories.DEFI_TIPO_DOCUMENTO AS[Category],")
  51. .AppendLine(" CONVERT(DATE, Documents.DATA_INSERIMENTO, 103) AS[Upload Date],")
  52. .AppendLine(" Documents.UNIV_UTENTE_INS AS[User]")
  53. .AppendLine(" FROM XD_DD01_DOCUMENTI AS Documents")
  54. .AppendLine(" JOIN XD_DD02_FILE AS Files")
  55. .AppendLine(" ON Files.UNIV_FILE = Documents.UNIV_FILE")
  56. .AppendLine(" JOIN XD_DC01_TIPI_DOCUMENTO AS Categories")
  57. .AppendLine(" ON Categories.TIPB_DOCUMENTO = Documents.TIPB_DOCUMENTO");
  58.  
  59. DbCommand command = this.database.GetSqlStringCommand(sqlQuery[0].ToString());
  60.  
  61. sqlQuery[1] = new StringBuilder()
  62. .AppendLine("SELECT D500_PATH FROM XD_DD02_FILE AS f JOIN XD_DD01_DOCUMENTI AS d ON d.UNIV_FILE = f.UNIV_FILE");
  63.  
  64. DbCommand pathCommand = this.database.GetSqlStringCommand(sqlQuery[1].ToString());
  65.  
  66. // Gets the file paths needed for getting the file size
  67. DataRowCollection filePathsRows = this.database.ExecuteDataSet(pathCommand).Tables[0].Rows;
  68.  
  69.  
  70. DataTable dt = this.database.ExecuteDataSet(command).Tables[0];
  71.  
  72. // Adds the column "Dimension"
  73. DataColumn dc = new DataColumn("Dimension", typeof(string));
  74. dt.Columns.Add(dc);
  75.  
  76. // Fills the column "Dimension" with the corresponding values
  77. InsertFileSizeRows(filePathsRows, dt);
  78.  
  79. return dt;
  80. }
  81.  
  82. /// <summary>
  83. /// A method that loads all the categories from the database
  84. /// and send them a as datatable (for the DropDownList).
  85. /// </summary>
  86. /// <returns>Returns the DataTable with all categories.</returns>
  87. public DataTable AllCategories()
  88. {
  89. StringBuilder sqlQuery = new StringBuilder();
  90.  
  91. sqlQuery.AppendLine("SELECT DEFI_TIPO_DOCUMENTO FROM XD_DC01_TIPI_DOCUMENTO");
  92.  
  93. DbCommand command = this.database.GetSqlStringCommand(sqlQuery.ToString());
  94.  
  95. DataTable dt = this.database.ExecuteDataSet(command).Tables[0];
  96. return dt;
  97. }
  98.  
  99. /// <summary>
  100. /// A method that loads all the types from the database
  101. /// and send them as a datatable (for the DropDownList).
  102. /// </summary>
  103. /// <returns>Returns the DataTable with all types.</returns>
  104. public DataTable AllTypes()
  105. {
  106. StringBuilder sqlQuery = new StringBuilder();
  107.  
  108. sqlQuery.AppendLine("SELECT D500_PATH FROM XD_DD02_FILE");
  109.  
  110. DbCommand command = this.database.GetSqlStringCommand(sqlQuery.ToString());
  111.  
  112. DataTable dt = this.database.ExecuteDataSet(command).Tables[0];
  113. return dt;
  114. }
  115.  
  116. public DataTable AllUsers()
  117. {
  118. StringBuilder sqlQuery = new StringBuilder();
  119.  
  120. sqlQuery.AppendLine("SELECT DISTINCT UNIV_UTENTE_INS FROM XD_DD01_DOCUMENTI");
  121.  
  122. DbCommand command = this.database.GetSqlStringCommand(sqlQuery.ToString());
  123.  
  124. DataTable dt = this.database.ExecuteDataSet(command).Tables[0];
  125. return dt;
  126. }
  127.  
  128. //TODO: Expand the search button functionality with better query
  129. /// <summary>
  130. /// A method that loads the new DataTable from
  131. /// the user input after a search button
  132. /// </summary>
  133. /// <returns>Returns the DataTable with the categories input</returns>
  134. public DataTable SelectedCategory(string category)
  135. {
  136. int queriesCount = 2;
  137. StringBuilder[] sqlQuery = new StringBuilder[queriesCount];
  138.  
  139. sqlQuery[0] = new StringBuilder()
  140. .AppendLine(" SELECT Documents.D500_DOCUMENTO AS[Document Name],")
  141. .AppendLine(" Files.D500_NOME_FILE AS[Filename],")
  142. .AppendLine(" Categories.DEFI_TIPO_DOCUMENTO AS [Category],")
  143. .AppendLine(" CONVERT(DATE, Documents.DATA_INSERIMENTO, 103) AS[Upload Date],")
  144. .AppendLine(" Documents.UNIV_UTENTE_INS AS[User]")
  145. .AppendLine(" FROM XD_DD01_DOCUMENTI AS Documents")
  146. .AppendLine(" JOIN XD_DD02_FILE AS Files")
  147. .AppendLine(" ON Files.UNIV_FILE = Documents.UNIV_FILE")
  148. .AppendLine(" JOIN XD_DC01_TIPI_DOCUMENTO AS Categories")
  149. .AppendLine(" ON Categories.TIPB_DOCUMENTO = Documents.TIPB_DOCUMENTO")
  150. .AppendLine(" WHERE Categories.DEFI_TIPO_DOCUMENTO = @Category");
  151.  
  152. DbCommand command = this.database.GetSqlStringCommand(sqlQuery[0].ToString());
  153.  
  154. database.AddInParameter(command, "@Category", DbType.String, category);
  155.  
  156. sqlQuery[1] = new StringBuilder()
  157. .AppendLine("SELECT f.D500_PATH FROM XD_DD02_FILE AS f JOIN XD_DD01_DOCUMENTI AS d " +
  158. "ON d.UNIV_FILE = f.UNIV_FILE JOIN XD_DC01_TIPI_DOCUMENTO AS c ON d.TIPB_DOCUMENTO = c.TIPB_DOCUMENTO WHERE c.DEFI_TIPO_DOCUMENTO = @Category");
  159.  
  160. DbCommand pathCommand = this.database.GetSqlStringCommand(sqlQuery[1].ToString());
  161.  
  162. this.database.AddInParameter(pathCommand, "@Category", DbType.String, category);
  163.  
  164. // Gets the file paths needed for getting the file size
  165. DataRowCollection filePathsRows = this.database.ExecuteDataSet(pathCommand).Tables[0].Rows;
  166.  
  167. DataTable dt = this.database.ExecuteDataSet(command).Tables[0];
  168.  
  169. // Adds the column "Dimension"
  170. DataColumn dc = new DataColumn("Dimension", typeof(string));
  171. dt.Columns.Add(dc);
  172.  
  173. // Fills the column "Dimension" with the corresponding values
  174. InsertFileSizeRows(filePathsRows, dt);
  175.  
  176. return dt;
  177. }
  178.  
  179. /// <summary>
  180. /// A method that loads all the statuses from the database
  181. /// and send them as a datatable (for the PopupMenu DropDownList).
  182. /// </summary>
  183. /// <returns>Returns the DataTable with all statuses.</returns>
  184. public DataTable AllStatuses()
  185. {
  186. StringBuilder sqlQuery = new StringBuilder();
  187.  
  188. sqlQuery.AppendLine("SELECT DEFI_STATO_DOC FROM XD_DC02_STATO_DOCUMENTO");
  189.  
  190. DbCommand command = this.database.GetSqlStringCommand(sqlQuery.ToString());
  191.  
  192. DataTable dt = this.database.ExecuteDataSet(command).Tables[0];
  193. return dt;
  194. }
  195.  
  196. #endregion
  197.  
  198. #region Private Methods
  199.  
  200. /// <summary>
  201. /// A helper method for calculating the file size.
  202. /// </summary>
  203. /// <param name="value">The size of the file in bytes.</param>
  204. /// <returns>Returns the calculated size from bytes to KB/MB/GM as a string.</returns>
  205. private string SizeSuffix(Int64 value)
  206. {
  207. if (value < 0) { return "-" + SizeSuffix(-value); }
  208.  
  209. int i = 0;
  210. decimal dValue = (decimal)value;
  211. while (Math.Round(dValue / 1024) >= 1)
  212. {
  213. dValue /= 1024;
  214. i++;
  215. }
  216.  
  217. return string.Format("{0:n1} {1}", dValue, SizeSuffixes[i]);
  218. }
  219.  
  220. /// <summary>
  221. /// A method that inserts the file sizes for each document
  222. /// into the table.
  223. /// </summary>
  224. /// <param name="filePathsRows">The rows to be inserted</param>
  225. /// <param name="dt">The DataTable</param>
  226. private void InsertFileSizeRows(DataRowCollection filePathsRows, DataTable dt)
  227. {
  228. //foreach (DataRow row in filePathsRows)
  229. //{
  230. // string filePath = row.ItemArray[0].ToString();
  231. // long fileInfoSize = new FileInfo(filePath).Length;
  232. // string size = SizeSuffix(fileInfoSize);
  233.  
  234. // dt.Rows[index++]["Dimension"] = size;
  235. //}
  236. int index = 0;
  237. for (int i = 0; i < dt.Rows.Count; i++)
  238. {
  239. string filePath = filePathsRows[i].ItemArray[0].ToString();
  240. long fileInfoSize = new FileInfo(filePath).Length;
  241. string size = SizeSuffix(fileInfoSize);
  242.  
  243. dt.Rows[index++]["Dimension"] = size;
  244. }
  245. }
  246. #endregion
  247. }
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement