321 lines
11 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using VEPROMS.CSLA.Library;
using DevComponents.DotNetBar;
using Volian.Base.Library;
namespace VEPROMS
{
public partial class frmRODbProperties : Form
{
private ProgressBarItem _ProgressBar = null;
public ProgressBarItem ProgressBar
{
get { return _ProgressBar; }
set { _ProgressBar = value; }
}
private void DoProgressBarRefresh(int value, int max, string text)
{
if (ProgressBar == null) return;
ProgressBar.Value = value;
ProgressBar.Maximum = max;
ProgressBar.Text = text;
Application.DoEvents();
}
private string InitialProgressBarMessage
{
set
{
if (ProgressBar == null) return;
ProgressBar.Value = 100;
ProgressBar.Maximum = 100;
ProgressBar.Text = value;
Application.DoEvents();
}
}
private string FinalProgressBarMessage
{
set
{
if (ProgressBar == null) return;
ProgressBar.Value = 100;
ProgressBar.Maximum = 100;
ProgressBar.Text = value;
Application.DoEvents();
}
}
private Point _ParentLocation;
public Point ParentLocation
{
get { return _ParentLocation; }
set { _ParentLocation = value; }
}
private RODbInfo _roDbInfo;
private string _origROName;
private string _origFolderPath;
private string _origSQLConnect = null;
private DocVersion _docVersion;
public frmRODbProperties(DocVersion docVersion, RODbInfo roDbInfo)
{
_roDbInfo = roDbInfo;
_docVersion = docVersion;
InitializeComponent();
_origROName = (_roDbInfo == null) ? null : _roDbInfo.ROName;
_origFolderPath = (_roDbInfo == null) ? null : _roDbInfo.FolderPath;
_origSQLConnect = (_roDbInfo == null) ? null : (_roDbInfo.DBConnectionString == null || _roDbInfo.DBConnectionString == "" || _roDbInfo.DBConnectionString == "cstring") ? null : _roDbInfo.DBConnectionString;
// Disable the OK button when initialized. Enable it if the user makes changes
ppBtnTestSQL.Visible = ppBtnTestSQL.Enabled = CanMigrateRoAccessToSql(roDbInfo);
ppTxtSQL.Visible = ppTxtSQL.Enabled = CanMigrateRoAccessToSql(roDbInfo);
ppLblSQL.Visible = CanMigrateRoAccessToSql(roDbInfo);
ppBtnOk.Enabled = false;
}
private bool CanMigrateRoAccessToSql(RODbInfo rODbi)
{
// C2017-003: This method is used to determine whether the sql server version can be used & if there is data.
// A command line argument 'RoInSql'. For now, this argument must be used to allow code to run for ro->sql. Later
// this will be changed so that if argument is used, the program does NOT include test button & text box to allow
// entry of sql server connection string
if (!Volian.Base.Library.VlnSettings.GetCommandFlag("RoInSql")) return false;
// The following conditions must be true in order to migrate the set the connection string.
// 1) the user must be an admin
UserInfo ui = UserInfo.GetByUserID(VlnSettings.UserID);
if (!ui.IsAdministrator()) return false;
return true;
}
private void ppBtnFldrDlg_Click(object sender, EventArgs e)
{
FolderBrowserDialog dlgROFolder = new FolderBrowserDialog();
// Initialize the starting location in the Browser window
dlgROFolder.SelectedPath = ppTxtPath.Text;
if (dlgROFolder.ShowDialog() == DialogResult.OK)
{
ppTxtPath.Text = dlgROFolder.SelectedPath;
}
}
private void ppBtnOk_Click(object sender, EventArgs e)
{
// check for modify. If the path to this rodb already exists, we've got to use that
// as the assocation. if it's a new path, i.e. a modify of the rodb record, just save current data.
// Then close & return.
if ((_origROName !=null && _origFolderPath !=null && (_origROName != ppRTxtName.Text || _origFolderPath != ppTxtPath.Text)) ||
(_origSQLConnect != null && _origSQLConnect != ppTxtSQL.Text)) // B2020-011 fix check for change in sql connection string
{
if (!Directory.Exists(ppTxtPath.Text))
{
MessageBox.Show(ppTxtPath.Text, "Invalid Directory for RO Database");
return;
}
if (!File.Exists(ppTxtPath.Text + @"\ro.fst"))
{
MessageBox.Show(ppTxtPath.Text, "Ro.Fst Doesn't Exist");
return;
}
RODb roDb = RODb.GetJustRoDb(_roDbInfo.RODbID);
if (_origROName != ppRTxtName.Text) roDb.ROName = ppRTxtName.Text;
if (_origSQLConnect == null && (ppTxtSQL.Text == null || ppTxtSQL.Text == "")) // B2020-011 fix check when connecting to an access database
roDb.DBConnectionString = "cstring";
else if (!(_origSQLConnect == "cstring" && (ppTxtSQL.Text == null || ppTxtSQL.Text == "")) && _origSQLConnect != ppTxtSQL.Text)
{
// C2017-003: ro in sql. 'cstring' in connection string represents using MS Access database
// if connect string is null, set connect string to "cstring" otherwise, use what user typed in:
// first check if connection can be made to what was entered:
bool canconnect = false;
try
{
using (SqlConnection connection = new SqlConnection(ppTxtSQL.Text))
{
try
{
connection.Open();
canconnect = (connection.State == ConnectionState.Open);
}
catch (SqlException) { }
}
}
catch (Exception ex) {}
if (canconnect)
roDb.DBConnectionString = (ppTxtSQL.Text == null || ppTxtSQL.Text == "") ? "cstring" : ppTxtSQL.Text;
else
MessageBox.Show("SQL Connection failed, connection string won't be saved.", "Cannot save connection data");
}
if (_origFolderPath != ppTxtPath.Text)
{
// see if an rodb already exists with this pathname.
RODbInfoList roDbList = RODbInfoList.Get();
foreach (RODbInfo rdi in roDbList)
{
if (ppTxtPath.Text.ToUpper() == rdi.FolderPath.ToUpper())
{
// if this has a previously defined path and a different name, do not save it.
if(ppRTxtName.Text.ToUpper() != rdi.ROName.ToUpper())
{
MessageBox.Show(string.Format("This path has been used for \"{0}\" Database",rdi.ROName), "Previously Used RO Path", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
// get the most recent rofst data for this rodb:
ROFstInfoList roFstList = ROFstInfoList.GetByRODbID(rdi.RODbID);
DateTime dts = new DateTime(1900, 1, 1);
int rofstinfoid = -1;
foreach (ROFstInfo rfi in roFstList)
{
if (rfi.DTS > dts)
{
dts = rfi.DTS;
rofstinfoid = rfi.ROFstID;
}
}
_docVersion.DocVersionAssociations[0].MyROFst = ROFst.Get(rofstinfoid);
_docVersion.Save().Dispose();
DialogResult = DialogResult.OK;
Close();
return;
}
}
roDb.FolderPath = ppTxtPath.Text;
}
try
{
roDb.Save().Dispose();
DialogResult = DialogResult.OK;
Close();
return;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
}
// this is creating a new rodb. A new rofst record will be created as well & the association set
// check for unique name/path first
RODbInfoList allrodbinfo = RODbInfoList.Get();
foreach (RODbInfo rdi in allrodbinfo)
{
if (ppRTxtName.Text == rdi.ROName)
{
MessageBox.Show("Create unique Description.", "Description is not unique");
return;
}
if (ppTxtPath.Text.ToUpper() == rdi.FolderPath.ToUpper())
{
MessageBox.Show("Specify unique Folder Path or cancel and select an existing RO Database from drop-down.", "Folder Path is not unique" );
return;
}
}
if (ppRTxtName.Text == null || ppRTxtName.Text == "")
{
MessageBox.Show("You must enter a Description.");
return;
}
if (ppTxtPath.Text == null || ppTxtPath.Text == "")
{
MessageBox.Show("You must enter a Path.");
return;
}
// check for an ro.fst existing in the selected path. If it is valid, create an rodb, rofst
// and an association to its docversion.
string rofstPath = ppTxtPath.Text + @"\ro.fst";
if (!File.Exists(rofstPath))
{
MessageBox.Show("No existing ro.fst in path " + ppTxtPath.Text + ". Check for invalid path");
return;
}
string connectstr = ppTxtSQL.Text != null && ppTxtSQL.Text != "" ? ppTxtExt.Text : "cstring";
RODb newroDb = RODb.MakeRODb(ppRTxtName.Text, ppTxtPath.Text, connectstr, null);
RODbInfo newrdi = RODbInfo.Get(newroDb.RODbID);
newroDb.Save().Dispose();
ROFst rofst = ROFstInfo.AddRoFst(newrdi, _docVersion, DoProgressBarRefresh);
DialogResult = DialogResult.OK;
Close();
}
private void frmRODbProperties_Load(object sender, EventArgs e)
{
Location = ParentLocation;
ppRTxtName.Text = (_roDbInfo == null) ? null : _roDbInfo.ROName;
ppTxtPath.Text = (_roDbInfo == null) ? null : _roDbInfo.FolderPath;
ppTxtSQL.Text = (_roDbInfo == null) ? null : (_roDbInfo.DBConnectionString == "cstring") ? null : _roDbInfo.DBConnectionString;
RODbConfig cfg = (_roDbInfo == null) ? new RODbConfig() : new RODbConfig(_roDbInfo);
// Note that the Graphic Extension data is shown in a non-editable text box here because
// the data is either retrieved from the roapp.ini - and if there can only be editted from
// the file system. Otherwise it is derived from the top node - and changed from folder
// properties on the top node. And lastly, it is the code default.
ppTxtExt.Text = cfg.GetDefaultGraphicExtension();
ppLblGraphicFileExtLoc.Text = (cfg == null) ? null : cfg.GetDefaultGraphicExtensionLocation();
ppBtnOk.Enabled = false;
if (_roDbInfo == null)
{
string newFolderPath = null;
FolderBrowserDialog dlgROFolder = new FolderBrowserDialog();
dlgROFolder.RootFolder = Environment.SpecialFolder.MyComputer;
dlgROFolder.SelectedPath = ppTxtPath.Text;
if (dlgROFolder.ShowDialog() == DialogResult.OK)
{
newFolderPath = dlgROFolder.SelectedPath;
RODbInfoList allRODBs = RODbInfoList.Get();
foreach(RODbInfo rodb in allRODBs)
{
if (newFolderPath.ToUpper() == rodb.FolderPath.ToUpper())
{
ppRTxtName.Text = rodb.ROName;
break;
}
}
ppBtnOk.Enabled = true;
}
ppTxtPath.Text = newFolderPath;
}
// Initialize the enabled status for the OK button to false.
//ppBtnOk.Enabled = false;
ppTxtPath.TextChanged += ppTxtPath_TextChanged;
}
void ppTxtPath_TextChanged(object sender, EventArgs e)
{
//The OK button should only be enabled if the path has changed
ppBtnOk.Enabled = _origFolderPath != ppTxtPath.Text;
}
private void ppTxtSQL_TextChanged(object sender, EventArgs e)
{
//The OK button should only be enabled if the path has changed
ppBtnOk.Enabled = _origSQLConnect != ppTxtSQL.Text;
}
// C2017-003: test that the sql string entered can connect to the database:
private void ppBtnTestSQL_Click(object sender, EventArgs e)
{
try
{
using (SqlConnection connection = new SqlConnection(ppTxtSQL.Text))
{
try
{
connection.Open();
if (connection.State == ConnectionState.Open)
{
MessageBox.Show("You have been successfully connected to the database!");
}
else
{
MessageBox.Show("Connection failed.");
}
}
catch (SqlException) { }
}
}
catch (Exception ex)
{
MessageBox.Show("Connection failed: " + ex);
}
}
}
}