protected void btnImport_OnClick(object sender, EventArgs e) { if (fileUploadCtl.HasFile) { var fileName = Path.GetFileName(fileUploadCtl.FileName); fileUploadCtl.PostedFile.SaveAs(Server.MapPath(@"../ImportedList/" + fileName)); var lines = File.ReadAllText(Server.MapPath(@"../ImportedList/" + fileName)); var listOfUrls = lines.Split(','); foreach (var url in listOfUrls) { string type = string.Empty; switch (url.Substring(url.LastIndexOf('.'), 4).ToLower()) { case ".zip": type = "Zip"; break; case ".xml": type = "Xml"; break; } var sourceUrl = new SourceURL { Url = url, Type = type, EntryIP = CommonFunctions.GetIpAddress(), EntryDate = DateTime.Today, IsActive = true, EntryId = Convert.ToString(Session["USER_KEY"]), }; try { using (var dataContext = new EPGDataModel()) { dataContext.SourceURLs.Add(sourceUrl); dataContext.SaveChanges(); sourceUrl.ActiveChannels = GetActiveChannels(sourceUrl.Url, sourceUrl.Srno); dataContext.SaveChanges(); } } catch (Exception ex) { lblMsg.Text = "One or more Urls in text files already exsists in database."; } } lblMsg.Text = "List Imported successfully!"; BindGrid(); } }
protected void OnClick(object sender, EventArgs e) { using (var dataContext = new EPGDataModel()) { if (!string.IsNullOrEmpty(hidRight.Value)) { var activeIds = hidRight.Value.Substring(0, hidRight.Value.Length - 1).Split(','); foreach (var item in activeIds) { var id = Convert.ToInt64(item); var dbEntry = dataContext.ActiveChannels.FirstOrDefault(c => c.Srno.Equals(id)); if (dbEntry != null) { dbEntry.IsActive = true; dataContext.Entry(dbEntry).State = EntityState.Modified; } } dataContext.SaveChanges(); PopulateInactive(Convert.ToInt64(ddlSourceXml.SelectedValue)); PopulateActive(Convert.ToInt64(ddlSourceXml.SelectedValue)); lblMsg.Text = "Selection Saved!"; } if (!string.IsNullOrEmpty(hidLeft.Value)) { var inActiveIds = hidLeft.Value.Substring(0, hidLeft.Value.Length - 1).Split(','); foreach (var item in inActiveIds) { var id = Convert.ToInt64(item); var dbEntry = dataContext.ActiveChannels.FirstOrDefault(c => c.Srno.Equals(id)); if (dbEntry != null) { dbEntry.IsActive = false; dataContext.Entry(dbEntry).State = EntityState.Modified; } } dataContext.SaveChanges(); PopulateInactive(Convert.ToInt64(ddlSourceXml.SelectedValue)); PopulateActive(Convert.ToInt64(ddlSourceXml.SelectedValue)); lblMsg.Text = "Selection Saved!"; } } }
protected void Insert(object sender, EventArgs e) { using (var dataContext = new EPGDataModel()) { var dbEntry = dataContext.Users.Where(u => u.UserName.ToLower().Equals(txtName.Text.ToLower())).FirstOrDefault(); if (dbEntry != null) { lblMsg.Text = "User " + txtName.Text + " already exsists!"; return; } var user = new User { UserName = txtName.Text, UserEmailAddress = txtUserEmailAddress.Text, Password = txtPassword.Text, UserType = ddlUserType.SelectedValue, IsActive = true, DateCreated = DateTime.Today, }; dataContext.Users.Add(user); dataContext.SaveChanges(); } BindGrid(); lblMsg.Text = "User " + txtName.Text + " added successfully!"; ClearForm(); }
protected void gvUsers_OnRowDeleting(object sender, GridViewDeleteEventArgs e) { var userId = Convert.ToString(gvUsers.DataKeys[e.RowIndex].Values[0]); using (var dataContext = new EPGDataModel()) { var user = dataContext.Users.Find(userId); dataContext.Users.Remove(user); dataContext.SaveChanges(); } BindGrid(); }
protected void gvXMLSource_OnRowDeleting(object sender, GridViewDeleteEventArgs e) { var srno = Convert.ToInt64(gvXMLSource.DataKeys[e.RowIndex].Values[0]); using (var dataContext = new EPGDataModel()) { var source = dataContext.SourceURLs.Find(srno); dataContext.ActiveChannels.RemoveRange(source.ActiveChannels); dataContext.SourceURLs.Remove(source); dataContext.SaveChanges(); } BindGrid(); }
protected void btnAdd_OnClick(object sender, EventArgs e) { grid.InnerHtml = ""; lblMsg.Text = ""; using (var dataContext = new EPGDataModel()) { var source = new SourceURL { Url = txtSourceURLAdd.Text, Type = ddlSourceType.SelectedValue, IsActive = true, EntryDate = DateTime.Today, EntryId = Convert.ToString(Session["USER_KEY"]), EntryIP = CommonFunctions.GetIpAddress() }; dataContext.SourceURLs.Add(source); dataContext.SaveChanges(); source.ActiveChannels = GetActiveChannels(source.Url, source.Srno); dataContext.SaveChanges(); } lblMsg.Text = "Record added successfully!"; BindGrid(); }
protected void gvUsers_OnRowUpdating(object sender, GridViewUpdateEventArgs e) { var row = gvUsers.Rows[e.RowIndex]; var userId = Convert.ToString(gvUsers.DataKeys[e.RowIndex].Values[0]); var userName = (row.FindControl("txtName") as TextBox).Text; var email = (row.FindControl("txtUserEmailAddress") as TextBox).Text; var pass = (row.FindControl("txtPassword") as TextBox).Text; var type = (row.FindControl("ddlUserType") as DropDownList).SelectedValue; using (var dataContext = new EPGDataModel()) { var user = dataContext.Users.Find(userName); user.UserEmailAddress = email; user.Password = pass; user.PasswordSalt = pass; user.UserType = type; try { dataContext.Entry(user).State = EntityState.Modified; dataContext.SaveChanges(); } catch (DbEntityValidationException ex) { foreach (var eve in ex.EntityValidationErrors) { Console.WriteLine( "Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } } gvUsers.EditIndex = -1; BindGrid(); }
protected void gvXMLSource_OnRowUpdating(object sender, GridViewUpdateEventArgs e) { var row = gvXMLSource.Rows[e.RowIndex]; var srno = Convert.ToInt32(gvXMLSource.DataKeys[e.RowIndex].Values[0]); var sourceUrl = (row.FindControl("txtSourceURL") as TextBox).Text; var sourceType = (row.FindControl("ddlGridSourceType") as DropDownList).SelectedValue; using (var dataContext = new EPGDataModel()) { var source = dataContext.SourceURLs.Find(srno); source.Url = sourceUrl; source.Type = sourceType; source.EntryDate = DateTime.Today; source.IsActive = true; try { dataContext.Entry(source).State = EntityState.Modified; dataContext.SaveChanges(); } catch (DbEntityValidationException ex) { foreach (var eve in ex.EntityValidationErrors) { Console.WriteLine( "Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } } gvXMLSource.EditIndex = -1; BindGrid(); }
public static void ProcessXml() { XmlConfigurator.Configure(); SourceXmlList.Clear(); OutputXmlList.Clear(); using (var dataContext = new EPGDataModel()) { List <SourceURL> listToProcess = new List <SourceURL>(); try { logger.Info("1.> Getting List of Source from Database"); listToProcess = dataContext.SourceURLs.Where(s => s.IsActive).ToList(); logger.Info("2.> Retrived List of Source from Database. Total Sources - " + listToProcess.Count); } catch (Exception exDataException) { logger.Error("Error occured while retriving List of Source from Database. Error Message - " + exDataException.Message); if (exDataException.InnerException != null) { logger.Error("Inner Exception - " + exDataException.InnerException.Message); } } foreach (var source in listToProcess) { try { logger.Info("ravi"); logger.Info("// Proccessing Source - " + source.Url); var extension = source.Type.ToLower().Equals("zip") ? ".zip" : ".xml"; logger.Info("// File extension is - " + extension); var fileName = DownloadFile(source.Url, extension); logger.Info("// File Name extension is - " + fileName); if (!string.IsNullOrEmpty(fileName)) { if (Path.GetExtension(fileName).Contains(".zip")) //Zip Archive. { logger.Info("// Source is a zip archive."); //extract xmls from zip archive. ExtractFileToDirectory(fileName, SourceXmlFolderPath); logger.Info("// Çompleted extraction of zip archive. Total files extracted - " + SourceXmlList.Count); } else if (Path.GetExtension(fileName).Contains(".xml")) //Single Xml. { logger.Info("// Source is a xml file."); //ClearDirectory(Environment.CurrentDirectory + @"\SourceXmlFiles\"); try { logger.Info("// Checking if file " + Path.GetFileName(fileName) + " alredy exists in Source Xml Folder."); if (File.Exists(SourceXmlFolderPath + Path.GetFileName(fileName))) { File.Delete(SourceXmlFolderPath + Path.GetFileName(fileName)); } logger.Info("// Copying File " + Path.GetFileName(fileName) + " to Source Xml Folder."); File.Copy(fileName, SourceXmlFolderPath + Path.GetFileName(fileName)); logger.Info("// Completed Copying File " + Path.GetFileName(fileName) + " to Source Xml Folder."); SourceXmlList.Add(SourceXmlFolderPath + Path.GetFileName(fileName)); logger.Info("// Çompleted Adding file to SourceXmlList - Total Count is - " + SourceXmlList.Count); } catch (Exception singleXmlException) { logger.Error("Error occured while copying single xml. Error - " + singleXmlException.Message); if (singleXmlException.InnerException != null) { logger.Error("Inner Exception Error - " + singleXmlException.InnerException.Message); } } } foreach (var xmlFile in SourceXmlList) { var sourceUri = new Uri(source.Url); var newChannelName = string.Empty; var newOffset = 0; try { newChannelName = HttpUtility.ParseQueryString(sourceUri.Query).Get("channelname"); int.TryParse(HttpUtility.ParseQueryString(sourceUri.Query).Get("offset"), out newOffset); } catch (Exception) { // ignored } logger.Info("Calling GenerateOutputXml(" + xmlFile + ", " + newChannelName + ", " + newOffset + ")"); OutputXmlList.AddRange(ParserEngine.GenerateOutputXml(xmlFile, source.Srno, out _startDate, out _stopDate, newChannelName, newOffset)); //GenerateOutputXml(xmlFile, newChannelName, newOffset); logger.Info("Completed GenerateOutputXml(" + xmlFile + ", " + newChannelName + ", " + newOffset + ")"); } } SourceXmlList.Clear(); } catch (Exception) { } } } Console.WriteLine("Xml Generation Completed"); if (OutputXmlList != null) { if (OutputXmlList.Count > 0) { logger.Info("Updating Database..."); Console.WriteLine("Updating Database"); foreach (var outFile in OutputXmlList) { var fileDetails = outFile.Split(','); try { using (var dataContext = new EPGDataModel()) { var fileName = Path.GetFileName(fileDetails[0]); var dbEntry = dataContext.XmlImports.Where( x => x.XmlFileName.Trim().ToLower().Equals(fileName.Trim().ToLower())).ToList(); try { if (dbEntry != null) { if (dbEntry.Count > 0) { dataContext.XmlImports.RemoveRange(dbEntry); dataContext.SaveChanges(); } } } catch (Exception deleteException) { logger.Error("Error occured while Deleting. Error Message - " + deleteException.Message); if (deleteException.InnerException != null) { logger.Error("Error InnerException - " + deleteException.InnerException.Message); } } try { var xmlImport = new XmlImport { Url = "../Output/" + Path.GetFileName(fileDetails[0]), EpgStartDt = _startDate, EpgEndDt = _stopDate, ImportDate = DateTime.Now, XmlFileName = Path.GetFileName(fileDetails[0]), Url2 = !string.IsNullOrEmpty(fileDetails[3]) ? fileDetails[3] : "", SourceUrl = !string.IsNullOrEmpty(fileDetails[4]) ? fileDetails[4] : "" }; dataContext.XmlImports.Add(xmlImport); dataContext.SaveChanges(); } catch (Exception xmlDataImportException) { logger.Error("Error occured while adding new entry to Database. Error Message - " + xmlDataImportException.Message); if (xmlDataImportException.InnerException != null) { logger.Error("Error InnerException - " + xmlDataImportException.InnerException.Message); } } } } catch (Exception updateException) { logger.Error("Error occured while running UpdatE Database function. Error Message - " + updateException.Message); if (updateException.InnerException != null) { logger.Error("Error InnerException - " + updateException.InnerException.Message); } } } Console.WriteLine("Done Updating Database"); logger.Info("Done Updating Database"); } } }
protected void btnGetenerate_OnClick(object sender, EventArgs e) { _sourceXmlList.Clear(); _outputXmlList.Clear(); _additionalOutputXmlList.Clear(); lblMsg.Text = ""; grid.InnerHtml = ""; foreach (GridViewRow row in gvXMLSource.Rows) { var chk = (row.FindControl("chkSelect") as CheckBox); if (chk != null && chk.Checked) { var srno = Convert.ToInt32(gvXMLSource.DataKeys[row.RowIndex].Values[0]); using (var dataContext = new EPGDataModel()) { var source = dataContext.SourceURLs.Find(srno); if (source != null) { var extension = source.Type.ToLower().Equals("zip") ? ".zip" : ".xml"; var fileName = DownloadFile(source.Url, extension); //MODIFY HERE if (!string.IsNullOrEmpty(fileName)) { if (Path.GetExtension(fileName).Contains(".zip")) //Zip Archive. { //extract xmls from zip archive. ExtractFileToDirectory(fileName, Server.MapPath(@"~/SourceXmlFiles/")); } else if (Path.GetExtension(fileName).Contains(".xml")) //Single Xml. { //ClearDirectory(Server.MapPath(@"~/SourceXmlFiles/")); if (File.Exists(Server.MapPath(@"~/SourceXmlFiles/" + Path.GetFileName(fileName)))) { File.Delete(Server.MapPath(@"~/SourceXmlFiles/" + Path.GetFileName(fileName))); } File.Copy(Server.MapPath(fileName), Server.MapPath(@"~/SourceXmlFiles/" + Path.GetFileName(fileName))); _sourceXmlList.Add(Server.MapPath(@"~/SourceXmlFiles/" + Path.GetFileName(fileName))); } foreach (var xmlFile in _sourceXmlList) { var sourceUri = new Uri(source.Url); var newChannelName = string.Empty; var newOffset = 0; try { newChannelName = HttpUtility.ParseQueryString(sourceUri.Query).Get("channelname"); int.TryParse(HttpUtility.ParseQueryString(sourceUri.Query).Get("offset"), out newOffset); } catch (Exception) { // ignored } _outputXmlList.AddRange(ParserEngine.GenerateOutputXml(xmlFile, source.Srno, out _startDate, out _stopDate, newChannelName, newOffset)); //GenerateOutputXml(xmlFile,source.Srno, newChannelName, newOffset); } } } } } _sourceXmlList.Clear(); } if (_outputXmlList != null) { if (_outputXmlList.Count > 0) { grid.InnerHtml += "<span style='text-align:center;' class='alert-danger'>Generated Output Xml(s)</span>"; grid.InnerHtml += "<table style='width:50%;' class='table table-striped table-bordered table-hover table-condensed table-mptt'><th class='sortable'>Srno</th><th class='sortable'>Output Xml (click to view xml)</th><th class='sortable'>EPG Start Date</th><th class='sortable'>EPG End Date</th><tbody>"; var iCount = 1; foreach (var outFile in _outputXmlList) { var fileDetails = outFile.Split(','); grid.InnerHtml += "<tr><td>" + iCount + "</td><td><a href='XmlTransformation.aspx?file=" + fileDetails[0] + "' target='_blank' class='btn-link'> " + Path.GetFileName(fileDetails[0]) + "</a></td>"; grid.InnerHtml += "<td>" + fileDetails[1] + "</td><td>" + fileDetails[2] + "</td></tr>"; try { using (var dataContext = new EPGDataModel()) { var fileName = Path.GetFileName(fileDetails[0]); var dbEntry = dataContext.XmlImports.Where( x => x.XmlFileName.Trim().ToLower().Equals(fileName.Trim().ToLower())).ToList(); if (dbEntry != null) { if (dbEntry.Count > 0) { dataContext.Database.ExecuteSqlCommand( "DELETE FROM XmlImport WHERE XmlFileName='" + fileName + "'"); } } var xmlImport = new XmlImport { Url = "../Output/" + Path.GetFileName(fileDetails[0]), EpgStartDt = _startDate, EpgEndDt = _stopDate, ImportDate = DateTime.Now, XmlFileName = Path.GetFileName(fileDetails[0]), Url2 = !string.IsNullOrEmpty(fileDetails[3])?fileDetails[3]:"", SourceUrl = !string.IsNullOrEmpty(fileDetails[4]) ? fileDetails[4] : "", }; dataContext.XmlImports.Add(xmlImport); dataContext.SaveChanges(); } } catch (Exception) { } iCount++; } grid.InnerHtml += "</tbody></table>"; lblMsg.Text = "Xml(s) generated successfully!"; } } }