|
using System;
using System.Data;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
using System.Threading;
public partial class _Default : Page
{
bool bImportAllowed = false;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string[,] ReqFiles = null;
DataTable dt = GetAvailableFiles(out ReqFiles);
ViewState["AvailableFiles"] = dt;
if (dt.Rows.Count == 0)
{
this.lblFiles.Text = Resources.EventResources.lblFileAvailableErr + " - " + App.virDirPath + "\\ImportFiles";
this.lblFiles.ForeColor = System.Drawing.Color.Red;
this.lblFiles.CssClass = "ErrorPrompt";
this.btnImport.Enabled = false;
string files = "";
for (int i = 0; i < ReqFiles.GetLength(0); i++)
{
files += ReqFiles[i, 0] + ", ";
}
}
else
{
this.lblFiles.Text = Resources.EventResources.lblFileAvailable;
}
gvFiles.DataSource = dt;
gvFiles.DataBind();
DataTable dtInfo = FillLastImportSyncInfo();
gvImortSyncInfo.DataSource = dtInfo;
gvImortSyncInfo.DataBind();
//warning prompt
bool bNewImportandNotSynched = CheckNewImportandNotSynched(dtInfo, dt);
if (bNewImportandNotSynched)
{
btnImport.Attributes.Add("onclick", "confirmImport('" + Resources.EventResources.alertImport + "');");
}
else
{
btnImport.Attributes.Add("onClick", "showDiv();");
//Set the flag for SynchedOrNotNew
this.hiddenSynchedOrNotNew.Value = "1";
}
btnSync.Attributes.Add("onClick", "showDiv();");
}
else
{
bImportAllowed = ChkforConfirmation();
// Import is clicked with confirmation, so reset the flag.
if (bImportAllowed)
{
btnImport.Attributes.Add("onClick", "showDiv();");
}
}
}
private bool ChkforConfirmation()
{
bool result = false;
bool synchedOrNotNew = false;
bool importConfirmed = false;
if (Request.Form["hiddenImportConfirmed"] != null)
{
importConfirmed = (Request.Form["hiddenImportConfirmed"].Trim() == "1");
}
if (importConfirmed)
{
// Import is clicked with confirmation, so reset the flag.
this.hiddenImportConfirmed.Value = "0";
}
if (Request.Form["hiddenSynchedOrNotNew"] != null)
{
synchedOrNotNew = (Request.Form["hiddenSynchedOrNotNew"].Trim() == "1");
}
result = importConfirmed || synchedOrNotNew;
return result;
}
protected void btnImport_Click(object sender, EventArgs e)
{
try
{
if ((Page.IsValid) && bImportAllowed)
{
bool agencyImportSucc = true, facilityImportSucc = true,
locationImportSucc = true;
DataTable dt = (DataTable)ViewState["AvailableFiles"];
foreach (DataRow row in dt.Rows)
{
if (row["FileName"].ToString().StartsWith("Locations"))
{
//Code for importing locations...
//check for success & set the flag
locationImportSucc = true;
if (locationImportSucc)
{
//update location entries for import
FileHandler.UpdateImportEntry(row["FileName"].ToString());
}
}
else if (row["FileName"].ToString().StartsWith("Agencies"))
{
//Code for importing agencies...
//check for success & set the flag
agencyImportSucc = true;
if (agencyImportSucc)
{
//update agency entries for import
FileHandler.UpdateImportEntry(row["FileName"].ToString());
}
}
else if (row["FileName"].ToString().StartsWith("Facilities"))
{
//Code for importing facilities...
//check for success & set the flag
facilityImportSucc = true;
if (facilityImportSucc)
{
//update facility entries for import
FileHandler.UpdateImportEntry(row["FileName"].ToString());
}
}
}
bool importDone = (locationImportSucc || agencyImportSucc || facilityImportSucc);
//Simulating delay
Thread.Sleep(4000);
if (importDone)
{
DataTable dtInfo = FillLastImportSyncInfo();
gvImortSyncInfo.DataSource = dtInfo;
gvImortSyncInfo.DataBind();
this.hiddenSynchedOrNotNew.Value = "1"; //Once import done it is no longer new
}
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
protected void btnSync_Click(object sender, EventArgs e)
{
try
{
DataTable dtImportInfo = FillLastImportSyncInfo();
string lastImportfile = String.Empty;
if (dtImportInfo.Rows.Count > 0)
{
foreach (DataRow row in dtImportInfo.Rows)
{
//update entries from import to sync
FileHandler.UpdateSyncEntry(row["LastImportFileName"].ToString());
}
}
//Simulating delay
Thread.Sleep(4000);
DataTable dtSyncInfo = FillLastImportSyncInfo();
gvImortSyncInfo.DataSource = dtSyncInfo;
gvImortSyncInfo.DataBind();
this.btnImport.Enabled = false; //Disable the import button
this.btnSync.Enabled = false; //Disable the sync button
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
private DataTable GetAvailableFiles(out string[,] ReqFiles)
{
// Last chronological (by looking at the file name attribute only) file name
// will be picked up from /Importfiles folder and then compared with LastSync file as stored in Web.config - if it is chronologically greater
// than LastSync file name it will appear on the list, otherwise it will not. Only the latest not synched file can be imported.
DataTable dtFiles = new DataTable();
dtFiles.TableName = "Files";
dtFiles.Columns.Add(new DataColumn("FileName", typeof(string)));
dtFiles.Columns.Add(new DataColumn("FilePath", typeof(string)));
ReqFiles = new string[3, 2]; //this is set as per files in different categories, in this example we set as 3 categories
//Locations, Agencies & Facilities
ReqFiles[0, 0] = "Locations" + FileHandler.GetLatestFileDate("Locations") + ".csv";
ReqFiles[0, 1] = App.virDirPath + "\\ImportFiles\\Locations" + FileHandler.GetLatestFileDate("Locations") + ".csv";
ReqFiles[1, 0] = "Agencies" + FileHandler.GetLatestFileDate("Agencies") + ".csv";
ReqFiles[1, 1] = App.virDirPath + "\\ImportFiles\\Agencies" + FileHandler.GetLatestFileDate("Agencies") + ".csv";
ReqFiles[2, 0] = "Facilities" + FileHandler.GetLatestFileDate("Facilities") + ".csv";
ReqFiles[2, 1] = App.virDirPath + "\\ImportFiles\\Facilities" + FileHandler.GetLatestFileDate("Facilities") + ".csv";
DataTable dt = GetSyncDetails();
for (int i = 0; i < ReqFiles.GetLength(0); i++)
{
if (File.Exists(ReqFiles[i, 1].ToString()))
{
// Now check whether this last filename is chronologically greater than last synched filename
if (FileHandler.CompareImportFilewithLastSynced(ReqFiles[i, 0].ToString(), dt))
{
DataRow dr = dtFiles.NewRow();
dr["FileName"] = ReqFiles[i, 0];
dr["FilePath"] = ReqFiles[i, 1];
dtFiles.Rows.Add(dr);
}
}
}
return dtFiles;
}
private DataTable GetSyncDetails()
{
DataTable dtSyncInfo = new DataTable();
dtSyncInfo.TableName = "SyncInfo";
dtSyncInfo.Columns.Add(new DataColumn("Last_Imported_File", typeof(string)));
dtSyncInfo.Columns.Add(new DataColumn("Last_Modified_DateTime", typeof(DateTime)));
dtSyncInfo.Columns.Add(new DataColumn("Last_Sync_File", typeof(string)));
dtSyncInfo.Columns.Add(new DataColumn("Last_Sync_DateTime", typeof(DateTime)));
dtSyncInfo.Columns.Add(new DataColumn("Category", typeof(string)));
DataRow dr = dtSyncInfo.NewRow();
dr["Last_Imported_File"] = ConfigManager.GetWebConfig("LastImportLocationFile");
string tempStr = ConfigManager.GetWebConfig("LastImportLocationDateTime");
if (tempStr.Length > 0)
{
dr["Last_Modified_DateTime"] = DateTime.Parse(tempStr);
}
dr["Last_Sync_File"] = ConfigManager.GetWebConfig("LastSyncLocationFile");
tempStr = ConfigManager.GetWebConfig("LastSyncLocationDateTime");
if (tempStr.Length > 0)
{
dr["Last_Sync_DateTime"] = DateTime.Parse(tempStr);
}
dr["Category"] = "Locations";
dtSyncInfo.Rows.Add(dr);
dr = dtSyncInfo.NewRow();
dr["Last_Imported_File"] = ConfigManager.GetWebConfig("LastImportAgencyFile");
tempStr = ConfigManager.GetWebConfig("LastImportAgencyDateTime");
if (tempStr.Length > 0)
{
dr["Last_Modified_DateTime"] = DateTime.Parse(tempStr);
}
dr["Last_Sync_File"] = ConfigManager.GetWebConfig("LastSyncAgencyFile");
tempStr = ConfigManager.GetWebConfig("LastSyncAgencyDateTime");
if (tempStr.Length > 0)
{
dr["Last_Sync_DateTime"] = DateTime.Parse(tempStr);
}
dr["Category"] = "Agencies";
dtSyncInfo.Rows.Add(dr);
dr = dtSyncInfo.NewRow();
dr["Last_Imported_File"] = ConfigManager.GetWebConfig("LastImportFacilityFile");
tempStr = ConfigManager.GetWebConfig("LastImportFacilityDateTime");
if (tempStr.Length > 0)
{
dr["Last_Modified_DateTime"] = DateTime.Parse(tempStr);
}
dr["Last_Sync_File"] = ConfigManager.GetWebConfig("LastSyncFacilityFile");
tempStr = ConfigManager.GetWebConfig("LastSyncFacilityDateTime");
if (tempStr.Length > 0)
{
dr["Last_Sync_DateTime"] = DateTime.Parse(tempStr);
}
dr["Category"] = "Facilities";
dtSyncInfo.Rows.Add(dr);
return dtSyncInfo;
}
private DataTable FillLastImportSyncInfo()
{
DataTable dtInfo = new DataTable();
dtInfo.TableName = "LastImportSyncInfo";
dtInfo.Columns.Add(new DataColumn("LastImportFileName", typeof(string)));
dtInfo.Columns.Add(new DataColumn("LastImportDateTime", typeof(DateTime)));
dtInfo.Columns.Add(new DataColumn("LastSyncFileName", typeof(string)));
dtInfo.Columns.Add(new DataColumn("LastSyncDateTime", typeof(DateTime)));
DataTable dt = GetSyncDetails();
if (dt.Rows.Count > 0)
{
foreach (DataRow row in dt.Rows)
{
DataRow dr = dtInfo.NewRow();
dr["LastImportFileName"] = row["Last_Imported_File"].ToString();
dr["LastImportDateTime"] = DateTime.Parse(row["Last_Modified_DateTime"].ToString());
dr["LastSyncFileName"] = row["Last_Sync_File"].ToString();
if (row["Last_Sync_DateTime"].ToString().Length > 0)
{
dr["LastSyncDateTime"] = DateTime.Parse(row["Last_Sync_DateTime"].ToString());
}
dtInfo.Rows.Add(dr);
}
this.lblImortSyncInfo.Visible = true;
}
else
{
this.lblImortSyncInfo.Visible = false;
}
return dtInfo;
}
private bool CheckNewImportandNotSynched(DataTable dtInfo, DataTable dtFiles)
// User can work with the same set of files many times before synch. So if the
// new set of files match with old imported set there should be no prompt.
// returns true if prompt warning is required.
{
bool bResult = false;
bool[] bFileMatchedArray = new bool[8]; //size is set to import file numbers
bool bAllFilesMatched = true;
int i = 0;
foreach (DataRow rowFile in dtFiles.Rows)
{
foreach (DataRow rowLastImportFile in dtInfo.Rows)
{
bFileMatchedArray[i] = false;
if (((rowLastImportFile["LastImportFileName"].ToString().ToLower()).Equals(rowFile["FileName"].ToString().ToLower())))
{
bFileMatchedArray[i] = true;
break;
}
}
bAllFilesMatched = bAllFilesMatched && bFileMatchedArray[i];
i++;
}
if (!bAllFilesMatched)
{
bResult = false;
foreach (DataRow row in dtInfo.Rows)
{
if (!((row["LastImportFileName"].ToString().ToLower()).Equals(row["LastSyncFileName"].ToString().ToLower())))
{
bResult = true;
break;
}
}
}
else
{
bResult = false;
}
return bResult;
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Started my career with Tata Institute of Fundamental Research as Scientific Officer and worked in Project group.
Over 20 years of work experience in C++, C#, ASP.NET, SQL etc. Currently working as a Free Lance Software Consultant, Pune, India.