refactor code

This commit is contained in:
Laurențiu Constantin 2024-04-18 18:10:37 +03:00
parent 2dc4a05cf2
commit 9084fa4381
3 changed files with 182 additions and 194 deletions

View File

@ -760,11 +760,13 @@ namespace SafeMobileLib
vehResponse resp = vehResponse.SQLerror;
NpgsqlTransaction transaction = null;
try
using (NpgsqlConnection connection = new NpgsqlConnection())
{
using (NpgsqlConnection connection = new NpgsqlConnection())
try
{
connection.ConnectionString = getConnectionString();
connection.Open();
// open a transaction
@ -785,12 +787,17 @@ namespace SafeMobileLib
cmd.ExecuteNonQuery();
}
using (NpgsqlCommand cmd = new NpgsqlCommand("DELETE FROM messages where sc_id=" + scid, connection, transaction))
using (NpgsqlCommand cmd = new NpgsqlCommand("DELETE FROM vehicle_group where sc_id=" + scid, connection, transaction))
{
cmd.ExecuteNonQuery();
}
using (NpgsqlCommand cmd = new NpgsqlCommand("DELETE FROM geozoneinout where sc_id=" + scid, connection, transaction))
{
cmd.ExecuteNonQuery();
}
using (NpgsqlCommand cmd = new NpgsqlCommand("DELETE FROM subs_gateway where sc_id=" + scid, connection, transaction))
{
cmd.ExecuteNonQuery();
@ -804,20 +811,20 @@ namespace SafeMobileLib
transaction.Commit();
resp = vehResponse.done;
}
}
catch (Exception ex)
{
try
catch (Exception ex)
{
transaction?.Rollback();
}
catch (Exception ex2)
{
Utils.WriteLine("COuld not roll back because: " + ex2.ToString());
}
try
{
transaction?.Rollback();
}
catch (Exception ex2)
{
Utils.WriteLine("COuld not roll back because: " + ex2.ToString());
}
Utils.WriteLine(ex.ToString(), ConsoleColor.Red);
resp = vehResponse.SQLerror;
Utils.WriteLine(ex.ToString(), ConsoleColor.Red);
resp = vehResponse.SQLerror;
}
}

View File

@ -28,10 +28,12 @@ namespace SubscriberAndUserManager
private bool formTypeEdit = false; //false = add user mode; true = edit user mode
private bool AddRange = false;
Vehicles selVeh;
List<Group> gList;
List<Group> listCategory;
List<Group> listRadioTalk;
List<Group> assignedTalkGroups;
private ArrayList imgList;
private MainForm parrent;
private bool isMultiEdit = false;
@ -134,56 +136,48 @@ namespace SubscriberAndUserManager
break;
}
foreach (User usr in usrList)
{
//Telerik.WinControls.Enumerations.SortStyle.None;
if (usr.UType != USERTYPE.Admin)
{
RadListDataItem test = new RadListDataItem(usr.UserName);
cbUserforImei.Items.Add(test);
}
}
//foreach (User usr in usrList)
//{
// //Telerik.WinControls.Enumerations.SortStyle.None;
// if (usr.UType != USERTYPE.Admin)
// {
// cbUserforImei.Items.Add(new RadListDataItem(usr.UserName));
// }
//}
cbUserforImei.Items.AddRange(usrList.Where(usr => usr.UType != USERTYPE.Admin).Select(usr => new RadListDataItem(usr.UserName)));
if (cbUserforImei.Items.Count != 0)
cbUserforImei.SelectedIndex = 0;
string none = MainForm.returnLNGString("none");
cbGroups.Items.Add(new RadListDataItem(none));
cbTalkGroup.Items.Add(new RadListDataItem(none));
cbGatewayAndRadioGW.Items.Add(new RadListDataItem(none));
gList = DBgrp.GetAllGroups();
RadListDataItem test2 = new RadListDataItem(MainForm.returnLNGString("none"));
cbGroups.Items.Add(test2);
RadListDataItem test3 = new RadListDataItem(MainForm.returnLNGString("none"));
cbTalkGroup.Items.Add(test3);
RadListDataItem test4 = new RadListDataItem(MainForm.returnLNGString("none"));
cbGatewayAndRadioGW.Items.Add(test4);
listCategory = gList.Where(x => x.Type == GroupType.CATEGORY.value).ToList();
listCategory = gList.Where(x => x.Type == GroupType.CATEGORY.value).ToList();
listRadioTalk = gList.Where(x => x.Type == GroupType.RADIOTALK.value).ToList();
foreach (Group grp in listCategory)
{
//Telerik.WinControls.Enumerations.SortStyle.None;
test2 = new RadListDataItem(grp.Name);
cbGroups.Items.Add(test2);
}
foreach (Group grp in listRadioTalk)
{
test2 = new RadListDataItem(grp.Name);
cbTalkGroup.Items.Add(test2);
}
// populate category groups
cbGroups.Items.AddRange(listCategory.Select(grp => new RadListDataItem(grp.Name)));
if (cbGroups.Items.Count > 0) cbGroups.SelectedIndex = 0;
// populate talk groups
cbTalkGroup.Items.AddRange(listRadioTalk.Select(grp => new RadListDataItem(grp.Name)));
if (cbTalkGroup.Items.Count > 0) cbTalkGroup.SelectedIndex = 0;
//car icons
//populate car icons
imgList = DB.get_all_car_picture();
foreach (Car obj in imgList)
{
string filePath = System.Windows.Forms.Application.StartupPath + @"\resource\cars\"
+ (SubscriberAndUserManager.MainForm.iconThemeType == IconTheme.CLASSIC ? "" : "list_")
+ SubscriberAndUserManager.MainForm.iconThemeType + "_" + obj.iconPattern + ".png";
Bitmap bmp = new Bitmap(filePath);
Image tmp2 = bmp;
imageList1.Images.Add(tmp2);
}
imageList1 = getValidAllPictures(imgList);
cbImageList.DataSource = imgList;
cbImageList.DisplayMember = "DisplayedName";
@ -191,15 +185,51 @@ namespace SubscriberAndUserManager
{
pictureBox1.Image = imageList1.Images[0];
}
if (cbStatus.Items.Count != 0)
cbStatus.SelectedIndex = 0;
if (cbUnitType.Items.Count != 0)
cbUnitType.SelectedIndex = 0;
if (cbStatus.Items.Count != 0) cbStatus.SelectedIndex = 0;
if (cbUnitType.Items.Count != 0) cbUnitType.SelectedIndex = 0;
parrent = _parrent;
}
private String InitialVehName = "";
private ImageList getValidAllPictures(ArrayList imgList)
{
ImageList imageList = new ImageList();
foreach (Car obj in imgList)
{
string filePath = System.Windows.Forms.Application.StartupPath + @"\resource\cars\"
+ (SubscriberAndUserManager.MainForm.iconThemeType == IconTheme.CLASSIC ? "" : "list_")
+ SubscriberAndUserManager.MainForm.iconThemeType + "_" + obj.iconPattern + ".png";
try
{
Bitmap bmp = new Bitmap(filePath);
Image tmp2 = bmp;
imageList.Images.Add(obj.idx.ToString(), tmp2);
}
catch (Exception ex)
{
Utils.WriteLine("\nBmp " + filePath + " exception " + ex.ToString(), ConsoleColor.Red);
}
}
return imageList;
}
private int getImageIndex(ImageList imageList, string key)
{
if (imageList.Images.ContainsKey(key))
return imageList.Images.IndexOfKey(key);
return -1;
}
public static VehiclesForm GetSingleton(DBvehiclesManager DB, DBgroupsManager DBgrp, DBgatewaysManager DBgw, List<User> usrList, EditVehicleParams parms, MainForm _parent)
{
if (!VehiclesCollection.ContainsKey(parms.IdList))
@ -277,9 +307,7 @@ namespace SubscriberAndUserManager
//Telerik.WinControls.Enumerations.SortStyle.None;
if (usr.UType != USERTYPE.Admin)
{
RadListDataItem test = new RadListDataItem(usr.UserName);
test.Value = usr.Id;
cbUserforImei.Items.Add(test);
cbUserforImei.Items.Add(new RadListDataItem(usr.UserName, usr.Id));
}
}
if (cbUserforImei.Items.Count != 0)
@ -289,18 +317,19 @@ namespace SubscriberAndUserManager
gList = DBgrp.GetAllGroups();
assignedTalkGroups = DBgrp.getAllTalkGroupsForUnits(IDList);
listCategory = gList.Where(x => x.Type == GroupType.CATEGORY.value).ToList();
listRadioTalk = gList.Where(x => x.Type == GroupType.RADIOTALK.value).ToList();
RadListDataItem test2 = new RadListDataItem(MainForm.returnLNGString("none"));
cbGroups.Items.Add(test2);
RadListDataItem test3 = new RadListDataItem(MainForm.returnLNGString("none"));
cbTalkGroup.Items.Add(test3);
foreach (Group grp in listCategory)
{
test2 = new RadListDataItem(grp.Name);
cbGroups.Items.Add(test2);
}
string none = MainForm.returnLNGString("none");
cbGroups.Items.Add(new RadListDataItem(none));
cbTalkGroup.Items.Add(new RadListDataItem(none));
// populate category groups
cbGroups.Items.AddRange(listCategory.Select(grp => new RadListDataItem(grp.Name)));
if (cbGroups.Items.Count > 0) cbGroups.SelectedIndex = 0;
if (parms.Group == MainForm.returnLNGString("multiple"))
{
cbGroups.Items.Add(MainForm.returnLNGString("multiple"));
@ -309,22 +338,17 @@ namespace SubscriberAndUserManager
else
{
int count = 0;
foreach (Group grp in listCategory)
{
count++;
if (grp.Name == parms.Group)
{
cbGroups.SelectedIndex = count;
}
}
int i = getIndex(listCategory, parms.Group);
if (i >= 0)
cbGroups.SelectedIndex = i;
}
// populate talk groups
cbTalkGroup.Items.AddRange(listRadioTalk.Select(grp => new RadListDataItem(grp.Name)));
if (cbTalkGroup.Items.Count > 0) cbTalkGroup.SelectedIndex = 0;
foreach (Group grp in listRadioTalk)
{
test3 = new RadListDataItem(grp.Name);
cbTalkGroup.Items.Add(test3);
}
if (assignedTalkGroups.Count > 0)
{
if (assignedTalkGroups.Count() > 1)
@ -334,40 +358,17 @@ namespace SubscriberAndUserManager
}
else
{
int count = 0;
foreach (Group grp in listRadioTalk)
{
count++;
if (grp.Name == assignedTalkGroups[0].Name)
{
cbTalkGroup.SelectedIndex = count;
}
}
int i = getIndex(listRadioTalk, assignedTalkGroups[0].Name);
if (i >= 0)
cbTalkGroup.SelectedIndex = i;
}
}
//if (cbTalkGroup.Items.Count > 0) cbTalkGroup.SelectedIndex = 0;
imgList = DB.get_all_car_picture();
int idx = 0;
int tempIDX = -1;
foreach (Car obj in imgList)
{
string filePath = System.Windows.Forms.Application.StartupPath + @"\resource\cars\"
+ (SubscriberAndUserManager.MainForm.iconThemeType == IconTheme.CLASSIC ? "" : "list_")
+ SubscriberAndUserManager.MainForm.iconThemeType + "_" + obj.iconPattern + ".png";
try
{
tempIDX++;
Bitmap bmp = new Bitmap(filePath);
Image tmp2 = bmp;
imageList1.Images.Add(tmp2);
if (parms.Icon != MainForm.returnLNGString("multiple")) { if (Convert.ToInt32(parms.Icon) == obj.idx) idx = tempIDX; }
}
catch(Exception ex)
{
Utils.WriteLine("\nBmp " + filePath + " exception " + ex.ToString(), ConsoleColor.Red);
}
}
imageList1 = getValidAllPictures(imgList);
int idx = getImageIndex(imageList1, parms.Icon);
cbImageList.DataSource = imgList;
cbImageList.DisplayMember = "DisplayedName";
@ -381,6 +382,8 @@ namespace SubscriberAndUserManager
{
if (imageList1.Images.Count > 0)
{
if (idx < 0) idx = 0;
pictureBox1.Image = imageList1.Images[idx];
cbImageList.SelectedIndex = idx;
}
@ -476,19 +479,14 @@ namespace SubscriberAndUserManager
break;
}
foreach (User usr in usrList)
{
//Telerik.WinControls.Enumerations.SortStyle.None;
if (usr.UType != USERTYPE.Admin)
{
RadListDataItem test = new RadListDataItem(usr.UserName);
test.Value = usr.Id;
cbUserforImei.Items.Add(test);
}
}
cbUserforImei.Items.AddRange(usrList.Where(usr => usr.UType != USERTYPE.Admin).Select(usr => new RadListDataItem(usr.UserName, usr.Id)));
if (cbUserforImei.Items.Count != 0)
cbUserforImei.SelectedIndex = 0;
formTypeEdit = true;
int count = 0;
foreach (User user in usrList)
@ -504,28 +502,20 @@ namespace SubscriberAndUserManager
}
gList = DBgrp.GetAllGroups();
RadListDataItem test2 = new RadListDataItem(MainForm.returnLNGString("none"));
cbGroups.Items.Add(test2);
RadListDataItem test3 = new RadListDataItem(MainForm.returnLNGString("none"));
cbTalkGroup.Items.Add(test3);
RadListDataItem test4 = new RadListDataItem(MainForm.returnLNGString("none"));
cbGatewayAndRadioGW.Items.Add(test4);
listCategory = gList.Where(x => x.Type == GroupType.CATEGORY.value).ToList();
string none = MainForm.returnLNGString("none");
cbGroups.Items.Add(new RadListDataItem(none));
cbTalkGroup.Items.Add(new RadListDataItem(none));
cbGatewayAndRadioGW.Items.Add(new RadListDataItem(none));
gList = DBgrp.GetAllGroups();
listCategory = gList.Where(x => x.Type == GroupType.CATEGORY.value).ToList();
listRadioTalk = gList.Where(x => x.Type == GroupType.RADIOTALK.value).ToList();
foreach (Group grp in listCategory)
{
test2 = new RadListDataItem(grp.Name);
cbGroups.Items.Add(test2);
}
foreach (Group grp in listRadioTalk)
{
test2 = new RadListDataItem(grp.Name);
cbTalkGroup.Items.Add(test2);
}
cbGroups.Items.AddRange(listCategory.Select(grp => new RadListDataItem(grp.Name)));
cbTalkGroup.Items.AddRange(listRadioTalk.Select(grp => new RadListDataItem(grp.Name)));
if (cbGroups.Items.Count > 0) cbGroups.SelectedIndex = 0;
if (cbTalkGroup.Items.Count > 0) cbTalkGroup.SelectedIndex = 0;
@ -535,60 +525,35 @@ namespace SubscriberAndUserManager
Group g = DBgrp.GetGroupForSCID(sc_id,GroupType.CATEGORY.value);
if (g != null)
{
count = 0;
foreach (Group grp in listCategory)
{
count++;
if (grp.Name == g.Name)
{
cbGroups.SelectedIndex = count;
}
}
int i = getIndex(listCategory, g.Name);
if (i >= 0)
cbGroups.SelectedIndex = i;
}
g = DBgrp.GetGroupForSCID(sc_id, GroupType.RADIOTALK.value);
if (g != null)
{
count = 0;
foreach (Group grp in listRadioTalk)
{
count++;
if (grp.Name == g.Name)
{
cbTalkGroup.SelectedIndex = count;
}
}
int i = getIndex(listRadioTalk, g.Name);
if (i >= 0)
cbTalkGroup.SelectedIndex = i;
}
//car icons
//get car icons
imgList = DB.get_all_car_picture();
imageList1 = getValidAllPictures(imgList);
int idx = 0;
int tempIDX= -1;
foreach (Car obj in imgList)
{
int idx = getImageIndex(imageList1, selVeh.ImgID.ToString());
string filePath = System.Windows.Forms.Application.StartupPath + @"\resource\cars\"
+ (SubscriberAndUserManager.MainForm.iconThemeType == IconTheme.CLASSIC ? "" : "list_")
+ SubscriberAndUserManager.MainForm.iconThemeType + "_" + obj.iconPattern + ".png";
try
{
tempIDX++;
Bitmap bmp = new Bitmap(filePath);
Image tmp2 = bmp;
imageList1.Images.Add(tmp2);
if (selVeh.ImgID == obj.idx) idx = tempIDX;
}
catch (Exception ex)
{
Utils.WriteLine("\nBmp " + filePath + " exception " + ex.ToString(), ConsoleColor.Red);
}
}
cbImageList.DataSource = imgList;
cbImageList.DisplayMember = "DisplayedName";
if (imageList1.Images.Count > 0)
{
if (idx < 0) idx = 0;
pictureBox1.Image = imageList1.Images[idx];
cbImageList.SelectedIndex = idx;
}
@ -655,6 +620,19 @@ namespace SubscriberAndUserManager
parrent = _parent;
}
private int getIndex(List<Group> list, string name)
{
for (int i = 0; i < list.Count; i++)
{
if (list[i].Name.Equals(name))
return i;
}
return -1;
}
private String TestUniqueIDinRange(Int32 start, Int32 stop)
{
String toreturn = "";

View File

@ -167,13 +167,16 @@ namespace SubscriberAndUserManager
MainForm.VehNameList.Clear();
MainForm.ImeiList.Clear();
GridViewUnitsList.DataSource = vehs;
foreach (Vehicles obj in vehs)
{
MainForm.VehNameList.Add(obj.VehName);
MainForm.ImeiList.Add(obj.Imei);
}
GridViewUnitsList.BeginUpdate();
GridViewUnitsList.DataSource = vehs;
if (vehs.Count > 0)
selectedVeh = vehs[0];
//car icons
@ -199,14 +202,11 @@ namespace SubscriberAndUserManager
try
{
if (veh.IsStolen == 0)
{
GridViewUnitsList.Rows[count].Cells["gvIsStolen"].Value = MainForm.returnLNGString("no");
}
else
{
GridViewUnitsList.Rows[count].Cells["gvIsStolen"].Value = MainForm.returnLNGString("yes");
}
string msg = veh.IsStolen == 0 ? "no" : "yes";
GridViewUnitsList.Rows[count].Cells["gvIsStolen"].Value = MainForm.returnLNGString(msg);
}
catch (Exception ex)
{
@ -214,6 +214,9 @@ namespace SubscriberAndUserManager
}
count++;
}
GridViewUnitsList.EndUpdate();
if (vehs.Count == 0)
radPageContacts.Item.Visibility = radPageGroups.Item.Visibility = radPageCanBeCalled.Item.Visibility = Telerik.WinControls.ElementVisibility.Collapsed;