Advertisement
Guest User

Untitled

a guest
Dec 14th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.93 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Specialized;
  4. using System.Configuration;
  5. using System.Data;
  6. using System.Data.SqlClient;
  7. using System.Diagnostics;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Net;
  11. using System.Xml;
  12. using Microsoft.SharePoint.Client;
  13. using OfficeOpenXml;
  14. using Utilities;
  15. using File = System.IO.File;
  16. using Dapper;
  17. namespace Unicer.ImportExcelRetail
  18. {
  19. public class SharepointClient
  20. {
  21. private string host { get; set; }
  22. private string user { get; set; }
  23. private string pass { get; set; }
  24. private string ItemLimit { get; set; }
  25. private string listName { get; set; }
  26.  
  27. private string importAlways { get; set; }
  28. private static bool SharepointOnline(string url, string username, string password)
  29. {
  30. try
  31. {
  32.  
  33.  
  34. using (ClientContext context = new ClientContext(url))
  35. {
  36. var networkCred = new NetworkCredential(username, password);
  37. context.Credentials = new SharePointOnlineCredentials(username, networkCred.SecurePassword);
  38. Web web = context.Web;
  39. List docs = web.Lists.GetByTitle("Fotos");
  40. }
  41. return true;
  42. }
  43. catch (Exception)
  44. {
  45. return false;
  46. }
  47. }
  48.  
  49. public int GetLastFileModified(string url, string username, string password, string listName)
  50. {
  51. int itemCount = 0;
  52. using (var context = new ClientContext(url))
  53. {
  54.  
  55. var networkCred = new NetworkCredential(username, password);
  56. context.Credentials = new SharePointOnlineCredentials(username, networkCred.SecurePassword);
  57. var site = context.Web;
  58. context.Load(site, s => s.ListTemplates);
  59. context.ExecutingWebRequest += (s, e) =>
  60. {
  61. e.WebRequestExecutor.WebRequest.Proxy.Credentials =
  62. new NetworkCredential("porto\\60348", "unicer.12345");
  63.  
  64. };
  65.  
  66. List oList = context.Web.Lists.GetByTitle(listName);
  67. context.ExecuteQuery();
  68. CamlQuery query = new CamlQuery();
  69. ListItemCollection collListItem = oList.GetItems(query);
  70.  
  71. context.Load(collListItem);
  72.  
  73. context.ExecuteQuery();
  74. itemCount = collListItem.Count;
  75.  
  76. }
  77. return itemCount;
  78. }
  79.  
  80. public Tuple<DateTime, byte[]> GetLastFile(string url, string username, string password, string listName)
  81. {
  82. int itemCount = 0;
  83. string titleName = string.Empty;
  84. using (var context = new ClientContext(url))
  85. {
  86.  
  87. var networkCred = new NetworkCredential(username, password);
  88. context.Credentials = new SharePointOnlineCredentials(username, networkCred.SecurePassword);
  89. var site = context.Web;
  90. context.Load(site, s => s.ListTemplates);
  91.  
  92. IEnumerable<List> allLists;
  93.  
  94. var query = from list in context.Web.Lists
  95. select list;
  96.  
  97. allLists = context.LoadQuery(query);
  98. context.ExecuteQuery();
  99. List oList = allLists.Where(x => x.Title.Contains(listName)).OrderByDescending(x => x.Created)
  100. .FirstOrDefault();
  101.  
  102. if (oList != null)
  103. {
  104. CamlQuery camlQuery = new CamlQuery();
  105. var q = new CamlQuery() { ViewXml = "<View><Query><OrderBy><FieldRef Name='Modified' Ascending='False' /></OrderBy></Query></View>" };
  106. ListItemCollection collListItem = oList.GetItems(camlQuery);
  107. context.Load(collListItem, items => items.Include(
  108. item => item["Modified"],
  109. item => item["FileRef"]
  110. ));
  111. context.ExecuteQuery();
  112.  
  113. ListItem lastFile = collListItem.First();
  114.  
  115. ClientResult<Stream> streamResult = lastFile.File.OpenBinaryStream();
  116. context.ExecuteQuery();
  117. Tuple<DateTime, byte[]> tuple =
  118. new Tuple<DateTime, byte[]>(DateTime.Parse(lastFile["Modified"].ToString()), ReadFully(streamResult.Value));
  119.  
  120. return tuple;
  121. }
  122. }
  123. return null;
  124. }
  125.  
  126. public static byte[] ReadFully(Stream input)
  127. {
  128. byte[] buffer = new byte[16 * 1024];
  129. using (MemoryStream ms = new MemoryStream())
  130. {
  131. int read;
  132. while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
  133. {
  134. ms.Write(buffer, 0, read);
  135. }
  136. return ms.ToArray();
  137. }
  138. }
  139.  
  140.  
  141. public void Run()
  142. {
  143. // get FTPClient config values
  144. NameValueCollection config = ConfigurationManager.GetSection("ImportSettings") as NameValueCollection;
  145. host = config["SharepointURL"];
  146. user = config["SharepointUser"];
  147. pass = config["SharepointPassword"];
  148. listName = config["ListName"];
  149. //check connection to sharepoint online
  150. bool isSharepointOnline = SharepointOnline(host, user, pass);
  151. //bool isSqlServerOnline = CheckSqlServer();
  152. bool isSqlServerOnline = true;
  153. //Start read from sql
  154. Log(string.Format("Start importing file to:{0}", host));
  155. if (isSharepointOnline && isSqlServerOnline) // all good
  156. {
  157. Tuple<DateTime, byte[]> file = GetLastFile(host, user, pass, listName);
  158.  
  159. if (file != null && file.Item2.Length > 0)
  160. {
  161. Stream stream = new MemoryStream(file.Item2);
  162. DataTable dt = new DataTable();
  163. Log(string.Format("Found Excel File with size {0} and date {1}", file.Item2.Length, file.Item1));
  164. using (StreamReader sr = new StreamReader(stream))
  165. {
  166. // read the underlying stream and convert to a DataTable using ToDataTable extension method
  167. dt = sr.BaseStream.ToDataTable(HasHeader: true);
  168. }
  169.  
  170. Log(string.Format("Start importing {0} rows to sql", dt.Rows.Count));
  171.  
  172. bool error = InsertSqlData(dt, file.Item1);
  173. if (!error) Log(string.Format("Sucess importing {0} rows to sql", dt.Rows.Count));
  174.  
  175. }
  176.  
  177.  
  178. }
  179. }
  180.  
  181. private bool InsertSqlData(DataTable dt, DateTime date)
  182. {
  183. bool error = false;
  184. try
  185. {
  186.  
  187.  
  188. using (IDbConnection connection = GetOpenConnection())
  189. {
  190. string sqlQuery = @"INSERT INTO [UniwayRetail].[dbo].[RuturasOSA]
  191. ([Cadeia]
  192. ,[CodLoja]
  193. ,[Loja]
  194. ,[Vendedor]
  195. ,[Segmento]
  196. ,[Artigo]
  197. ,[EAN]
  198. ,[SKUS]
  199. ,[Stock]
  200. ,[DiasCobertura]
  201. ,[RegData])
  202. VALUES
  203. (@cadeia,@CodLoja,@Loja,@Vendedor,@Segmento,@Artigo,@EAN,@SKUS,@Stock,@DiasCobertura,@RegData)
  204. ";
  205.  
  206. string sqlQueryDelete = string.Format(@"DELETE [UniwayRetail].[dbo].[RuturasOSA]
  207. where RegData >='{0}-{1}-{2}'",date.Year,date.Month,date.Day);
  208. connection.Execute(sqlQueryDelete);
  209.  
  210.  
  211. foreach (DataRow dataRow in dt.Rows)
  212. {
  213. if (dataRow["Cod. Loja"] != null && !string.IsNullOrEmpty(dataRow["Cod. Loja"].ToString()) && dataRow["EAN"] != null && !string.IsNullOrEmpty(dataRow["EAN"].ToString()))
  214. connection.Execute(sqlQuery,
  215. new
  216. {
  217. Cadeia = dataRow["Cadeia"],
  218. CodLoja = dataRow["Cod. Loja"],
  219. Loja = dataRow["Loja"],
  220. Vendedor = dataRow["Vendedor"],
  221. Segmento = dataRow["Segmento"],
  222. Artigo = dataRow["Artigo"],
  223. EAN = dataRow["EAN"],
  224. SKUS = dataRow["SKUs"],
  225. Stock = dataRow["Stock"],
  226. DiasCobertura = dataRow["Dias Cobertura"],
  227. RegData = date.Date,
  228. });
  229. else
  230. {
  231. Log(string.Format("Missing mandatory fields in Line:{0}", string.Join(",", dataRow.ItemArray)));
  232. }
  233. }
  234. }
  235. }
  236. catch (Exception ex)
  237. {
  238. error = true;
  239. Log(string.Format("Error in DB:{0}", ex.Message));
  240. }
  241. return error;
  242. }
  243.  
  244.  
  245. public static IEnumerable<List<T>> SplitList<T>(List<T> locations, int nSize = 4900)
  246. {
  247. for (int i = 0; i < locations.Count; i += nSize)
  248. {
  249. yield return locations.GetRange(i, Math.Min(nSize, locations.Count - i));
  250. }
  251. }
  252.  
  253. private dynamic ReadSQLData(bool onlyImported = true)
  254. {
  255. string queryAndImported = string.Empty;
  256. if (onlyImported)
  257. {
  258. queryAndImported = @"AND (f.Imported IS NULL OR f.Imported = 0)";
  259. }
  260. string date = DateTime.Now.AddYears(-1).ToString("MM/dd/yyyy");
  261. IEnumerable<dynamic> results = null;
  262. const string sql =
  263. @"SELECT vh.VisitaId,u.Nome,c.Name,vh.DataVisita,f.TipoFoto,f.FotoId from Visitas_Header vh
  264. inner JOIN Utilizadores u ON u.UserId=vh.UtilizadorId
  265. inner JOIN Clientes c ON SUBSTRING(c.CustomerId, PATINDEX('%[^0 ]%', c.CustomerId + ' '), LEN(c.CustomerId)) = vh.ClienteId
  266. inner join Fotos f ON f.VisitaId = vh.VisitaId
  267. WHERE vh.DataVisita < '{0}' {1}
  268. ORDER BY vh.DataVisita asc
  269. ";
  270.  
  271. try
  272. {
  273. using (IDbConnection connection = GetOpenConnection())
  274. {
  275. results = connection.Query<dynamic>(string.Format(sql, date, queryAndImported));
  276. Log(string.Format("Found {0} lines in DB:", results.Count()));
  277. }
  278. }
  279. catch (Exception ex)
  280. {
  281. Log(string.Format("Error in DB:{0}", ex.Message));
  282. }
  283. return results;
  284. }
  285.  
  286. private byte[] ReadImageBytes(int idFoto)
  287. {
  288.  
  289. byte[] results = null;
  290. const string sql = @"
  291. SELECT f.Foto from Fotos f
  292. WHERE f.FotoId = {0}
  293. ";
  294.  
  295. try
  296. {
  297. using (IDbConnection connection = GetOpenConnection())
  298. {
  299. results = connection.Query<byte[]>(string.Format(sql, idFoto)).FirstOrDefault();
  300.  
  301. }
  302. }
  303. catch (Exception ex)
  304. {
  305. Log(string.Format("Error in DB:{0}", ex.Message));
  306. }
  307. return results;
  308. }
  309.  
  310. public static IDbConnection GetOpenConnection()
  311. {
  312. // var connection = new SqlConnection(@"Data Source = UB-SQLDEV-01;Initial Catalog=UniwayRetail;User Id=UniwayRetail;Password=zxcvbnm123$%&/;");
  313. var connectionString = ConfigurationManager.ConnectionStrings["UniwayRetail"].ConnectionString;
  314. var connection = new SqlConnection(connectionString);
  315. //SetDialect(SimpleCRUD.Dialect.SQLServer);
  316. return connection;
  317. }
  318.  
  319.  
  320. public void Log(string logtext)
  321. {
  322. NameValueCollection config = ConfigurationManager.GetSection("ImportSettings") as NameValueCollection;
  323. string _logpath = config["Logs"];
  324. //string logfolder = Utility.RemoveFilenameFromFilepath(_logpath);
  325. string logfolder = _logpath;
  326. logfolder = AppDomain.CurrentDomain.BaseDirectory;
  327. string timeString = DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss");
  328.  
  329. using (StreamWriter w = File.AppendText(logfolder + "Logs.txt"))
  330. {
  331. w.WriteLine(timeString + " " + logtext);
  332. }
  333. Console.WriteLine(logtext);
  334. }
  335.  
  336. private bool CheckSqlServer()
  337. {
  338. try
  339. {
  340. using (IDbConnection connection = GetOpenConnection())
  341. {
  342. ConnectionState conState = connection.State;
  343.  
  344. if (conState == ConnectionState.Closed || conState == ConnectionState.Broken)
  345. {
  346. Log(string.Format("Could not connect to DB:{0}", connection.ConnectionString));
  347. return false;
  348. }
  349. else
  350. {
  351. return true;
  352. }
  353.  
  354. }
  355. }
  356. catch (Exception ex)
  357. {
  358. Log(string.Format("Could not connect to DB:{0}", ex.Message));
  359. return false;
  360. }
  361. }
  362.  
  363.  
  364. }
  365.  
  366. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement