Files
GotecHaftmittel/.svn/pristine/f8/f88894d30a86adb402adb39f642998cedab09230.svn-base
2025-10-31 12:50:24 +01:00

198 lines
5.8 KiB
Plaintext

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Globalization;
using System.Text;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Threading;
using System.Windows.Markup;
using System.Data.Odbc;
using System.Data.SqlClient;
using Haftmittel.Properties;
namespace Haftmittel
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private int tow = 0;
private int ope = 0;
private string connstr = string.Empty;
public MainWindow()
{
try
{
string[] cmd = Environment.GetCommandLineArgs();
if (cmd.Count() < 2)
{
MessageBox.Show("Za mało parametrów");
this.Close();
return;
}
if (OdczytajPlik(cmd[1]))
{
if (string.IsNullOrWhiteSpace(connstr))
{
connstr = Settings.Default.ConnectionString;
}
else
{
connstr = przerobConnectionString(connstr);
}
DataContext = new ClassHaftmittel(tow, ope, connstr);
}
else
{
this.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Thread.CurrentThread.CurrentUICulture = new CultureInfo(Settings.Default.Language);
this.Language = XmlLanguage.GetLanguage(Thread.CurrentThread.CurrentUICulture.Name);
try
{
InitializeComponent();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
/// <summary>
/// odczytuje plik CSV wysłany z XLa
/// </summary>
/// <param name="nazwa"></param>
private bool OdczytajPlik(string nazwa)
{
try
{
StreamReader reader = File.OpenText(nazwa);
string line;
while ((line = reader.ReadLine()) != null)
{
string[] items = line.Split(';');
// pierwszy parametr to GID towaru, drugi operatora
tow = Convert.ToInt32(items[1]);
ope = Convert.ToInt32(items[2]);
for (int licz = 3; licz < items.Length - 1; licz++)
{
if (!string.IsNullOrWhiteSpace(items[licz]))
{
connstr += @items[licz] + ";";
}
}
connstr = connstr.Replace("\"", "");
}
return true;
}
catch (Exception)
{
MessageBox.Show(string.Format("Problemy z otwarciem lub zawartością pliku {0}", nazwa));
return false;
}
}
private void Image_MouseDown_1(object sender, MouseButtonEventArgs e)
{
// (DataContext as ClassHaftmittel).SDS_Obrazek1Change();
if ((sender as Image).Opacity == 1)
{
(sender as Image).Opacity = 0.2;
}
else
{
(sender as Image).Opacity = 1;
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (DataContext != null)
{
if ((DataContext as ClassHaftmittel).IsChanged)
{
if (MessageBox.Show("Niezapisane dane. Zakończyć?", "Koniec", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{
e.Cancel = false;
}
else
{
e.Cancel = true;
}
}
}
}
private void ComboBox_KeyDown(object sender, KeyEventArgs e)
{
ObservableCollection<ClassSkladnik> lc = ((sender as ComboBox).ItemsSource as ObservableCollection<ClassSkladnik>);
ClassSkladnik c = lc.Where(x => x.Kod.ToUpper().StartsWith(e.Key.ToString().Last().ToString())).FirstOrDefault();
if (c != null)
{
(sender as ComboBox).SelectedItem = c;
}
}
private string przerobConnectionString(string connstr)
{
if (!string.IsNullOrWhiteSpace(connstr))
{
var odbc = new OdbcConnectionStringBuilder(connstr);
var sql = new SqlConnectionStringBuilder()
{
DataSource = odbc["server"].ToString(),
InitialCatalog = odbc["database"].ToString(),
UserID = odbc["uid"].ToString(),
Password = odbc["pwd"].ToString(),
PersistSecurityInfo = (odbc["trusted_connection"].ToString() == "true")
};
return sql.ConnectionString;
}
else
{
throw new Exception("Problem z dostępem do bazy");
}
}
}
}