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

@ -759,12 +759,14 @@ namespace SafeMobileLib
vehResponse resp = vehResponse.SQLerror; vehResponse resp = vehResponse.SQLerror;
NpgsqlTransaction transaction = null; NpgsqlTransaction transaction = null;
try using (NpgsqlConnection connection = new NpgsqlConnection())
{ {
using (NpgsqlConnection connection = new NpgsqlConnection())
try
{ {
connection.ConnectionString = getConnectionString(); connection.ConnectionString = getConnectionString();
connection.Open(); connection.Open();
// open a transaction // open a transaction
@ -785,12 +787,17 @@ namespace SafeMobileLib
cmd.ExecuteNonQuery(); cmd.ExecuteNonQuery();
} }
using (NpgsqlCommand cmd = new NpgsqlCommand("DELETE FROM vehicle_group where sc_id=" + scid, connection, transaction))
using (NpgsqlCommand cmd = new NpgsqlCommand("DELETE FROM messages where sc_id=" + scid, connection, transaction))
{ {
cmd.ExecuteNonQuery(); 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)) using (NpgsqlCommand cmd = new NpgsqlCommand("DELETE FROM subs_gateway where sc_id=" + scid, connection, transaction))
{ {
cmd.ExecuteNonQuery(); cmd.ExecuteNonQuery();
@ -804,20 +811,20 @@ namespace SafeMobileLib
transaction.Commit(); transaction.Commit();
resp = vehResponse.done; resp = vehResponse.done;
} }
} catch (Exception ex)
catch (Exception ex)
{
try
{ {
transaction?.Rollback(); try
} {
catch (Exception ex2) transaction?.Rollback();
{ }
Utils.WriteLine("COuld not roll back because: " + ex2.ToString()); catch (Exception ex2)
} {
Utils.WriteLine("COuld not roll back because: " + ex2.ToString());
}
Utils.WriteLine(ex.ToString(), ConsoleColor.Red); Utils.WriteLine(ex.ToString(), ConsoleColor.Red);
resp = vehResponse.SQLerror; 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 formTypeEdit = false; //false = add user mode; true = edit user mode
private bool AddRange = false; private bool AddRange = false;
Vehicles selVeh; Vehicles selVeh;
List<Group> gList; List<Group> gList;
List<Group> listCategory; List<Group> listCategory;
List<Group> listRadioTalk; List<Group> listRadioTalk;
List<Group> assignedTalkGroups; List<Group> assignedTalkGroups;
private ArrayList imgList; private ArrayList imgList;
private MainForm parrent; private MainForm parrent;
private bool isMultiEdit = false; private bool isMultiEdit = false;
@ -134,56 +136,48 @@ namespace SubscriberAndUserManager
break; break;
} }
foreach (User usr in usrList) //foreach (User usr in usrList)
{ //{
//Telerik.WinControls.Enumerations.SortStyle.None; // //Telerik.WinControls.Enumerations.SortStyle.None;
if (usr.UType != USERTYPE.Admin) // if (usr.UType != USERTYPE.Admin)
{ // {
RadListDataItem test = new RadListDataItem(usr.UserName); // cbUserforImei.Items.Add(new RadListDataItem(usr.UserName));
cbUserforImei.Items.Add(test); // }
} //}
}
cbUserforImei.Items.AddRange(usrList.Where(usr => usr.UType != USERTYPE.Admin).Select(usr => new RadListDataItem(usr.UserName)));
if (cbUserforImei.Items.Count != 0) if (cbUserforImei.Items.Count != 0)
cbUserforImei.SelectedIndex = 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(); gList = DBgrp.GetAllGroups();
RadListDataItem test2 = new RadListDataItem(MainForm.returnLNGString("none")); listCategory = gList.Where(x => x.Type == GroupType.CATEGORY.value).ToList();
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();
listRadioTalk = gList.Where(x => x.Type == GroupType.RADIOTALK.value).ToList(); listRadioTalk = gList.Where(x => x.Type == GroupType.RADIOTALK.value).ToList();
foreach (Group grp in listCategory) // populate category groups
{ cbGroups.Items.AddRange(listCategory.Select(grp => new RadListDataItem(grp.Name)));
//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);
}
if (cbGroups.Items.Count > 0) cbGroups.SelectedIndex = 0; 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; if (cbTalkGroup.Items.Count > 0) cbTalkGroup.SelectedIndex = 0;
//car icons //populate car icons
imgList = DB.get_all_car_picture(); imgList = DB.get_all_car_picture();
foreach (Car obj in imgList) imageList1 = getValidAllPictures(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);
}
cbImageList.DataSource = imgList; cbImageList.DataSource = imgList;
cbImageList.DisplayMember = "DisplayedName"; cbImageList.DisplayMember = "DisplayedName";
@ -191,15 +185,51 @@ namespace SubscriberAndUserManager
{ {
pictureBox1.Image = imageList1.Images[0]; pictureBox1.Image = imageList1.Images[0];
} }
if (cbStatus.Items.Count != 0)
cbStatus.SelectedIndex = 0; if (cbStatus.Items.Count != 0) cbStatus.SelectedIndex = 0;
if (cbUnitType.Items.Count != 0) if (cbUnitType.Items.Count != 0) cbUnitType.SelectedIndex = 0;
cbUnitType.SelectedIndex = 0;
parrent = _parrent; parrent = _parrent;
} }
private String InitialVehName = ""; 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) public static VehiclesForm GetSingleton(DBvehiclesManager DB, DBgroupsManager DBgrp, DBgatewaysManager DBgw, List<User> usrList, EditVehicleParams parms, MainForm _parent)
{ {
if (!VehiclesCollection.ContainsKey(parms.IdList)) if (!VehiclesCollection.ContainsKey(parms.IdList))
@ -277,9 +307,7 @@ namespace SubscriberAndUserManager
//Telerik.WinControls.Enumerations.SortStyle.None; //Telerik.WinControls.Enumerations.SortStyle.None;
if (usr.UType != USERTYPE.Admin) if (usr.UType != USERTYPE.Admin)
{ {
RadListDataItem test = new RadListDataItem(usr.UserName); cbUserforImei.Items.Add(new RadListDataItem(usr.UserName, usr.Id));
test.Value = usr.Id;
cbUserforImei.Items.Add(test);
} }
} }
if (cbUserforImei.Items.Count != 0) if (cbUserforImei.Items.Count != 0)
@ -289,18 +317,19 @@ namespace SubscriberAndUserManager
gList = DBgrp.GetAllGroups(); gList = DBgrp.GetAllGroups();
assignedTalkGroups = DBgrp.getAllTalkGroupsForUnits(IDList); assignedTalkGroups = DBgrp.getAllTalkGroupsForUnits(IDList);
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(); listRadioTalk = gList.Where(x => x.Type == GroupType.RADIOTALK.value).ToList();
RadListDataItem test2 = new RadListDataItem(MainForm.returnLNGString("none"));
cbGroups.Items.Add(test2); string none = MainForm.returnLNGString("none");
RadListDataItem test3 = new RadListDataItem(MainForm.returnLNGString("none")); cbGroups.Items.Add(new RadListDataItem(none));
cbTalkGroup.Items.Add(test3); cbTalkGroup.Items.Add(new RadListDataItem(none));
foreach (Group grp in listCategory)
{ // populate category groups
test2 = new RadListDataItem(grp.Name); cbGroups.Items.AddRange(listCategory.Select(grp => new RadListDataItem(grp.Name)));
cbGroups.Items.Add(test2);
}
if (cbGroups.Items.Count > 0) cbGroups.SelectedIndex = 0; if (cbGroups.Items.Count > 0) cbGroups.SelectedIndex = 0;
if (parms.Group == MainForm.returnLNGString("multiple")) if (parms.Group == MainForm.returnLNGString("multiple"))
{ {
cbGroups.Items.Add(MainForm.returnLNGString("multiple")); cbGroups.Items.Add(MainForm.returnLNGString("multiple"));
@ -309,22 +338,17 @@ namespace SubscriberAndUserManager
else else
{ {
int count = 0; int i = getIndex(listCategory, parms.Group);
foreach (Group grp in listCategory) if (i >= 0)
{ cbGroups.SelectedIndex = i;
count++;
if (grp.Name == parms.Group)
{
cbGroups.SelectedIndex = count;
}
}
} }
// populate talk groups
cbTalkGroup.Items.AddRange(listRadioTalk.Select(grp => new RadListDataItem(grp.Name)));
if (cbTalkGroup.Items.Count > 0) cbTalkGroup.SelectedIndex = 0; 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 > 0)
{ {
if (assignedTalkGroups.Count() > 1) if (assignedTalkGroups.Count() > 1)
@ -334,41 +358,18 @@ namespace SubscriberAndUserManager
} }
else else
{ {
int count = 0; int i = getIndex(listRadioTalk, assignedTalkGroups[0].Name);
foreach (Group grp in listRadioTalk) if (i >= 0)
{ cbTalkGroup.SelectedIndex = i;
count++;
if (grp.Name == assignedTalkGroups[0].Name)
{
cbTalkGroup.SelectedIndex = count;
}
}
} }
} }
//if (cbTalkGroup.Items.Count > 0) cbTalkGroup.SelectedIndex = 0; //if (cbTalkGroup.Items.Count > 0) cbTalkGroup.SelectedIndex = 0;
imgList = DB.get_all_car_picture(); imgList = DB.get_all_car_picture();
int idx = 0; imageList1 = getValidAllPictures(imgList);
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);
}
}
int idx = getImageIndex(imageList1, parms.Icon);
cbImageList.DataSource = imgList; cbImageList.DataSource = imgList;
cbImageList.DisplayMember = "DisplayedName"; cbImageList.DisplayMember = "DisplayedName";
if (parms.Icon == MainForm.returnLNGString("multiple")) if (parms.Icon == MainForm.returnLNGString("multiple"))
@ -381,6 +382,8 @@ namespace SubscriberAndUserManager
{ {
if (imageList1.Images.Count > 0) if (imageList1.Images.Count > 0)
{ {
if (idx < 0) idx = 0;
pictureBox1.Image = imageList1.Images[idx]; pictureBox1.Image = imageList1.Images[idx];
cbImageList.SelectedIndex = idx; cbImageList.SelectedIndex = idx;
} }
@ -476,19 +479,14 @@ namespace SubscriberAndUserManager
break; break;
} }
foreach (User usr in usrList)
{
//Telerik.WinControls.Enumerations.SortStyle.None; cbUserforImei.Items.AddRange(usrList.Where(usr => usr.UType != USERTYPE.Admin).Select(usr => new RadListDataItem(usr.UserName, usr.Id)));
if (usr.UType != USERTYPE.Admin)
{
RadListDataItem test = new RadListDataItem(usr.UserName);
test.Value = usr.Id;
cbUserforImei.Items.Add(test);
}
}
if (cbUserforImei.Items.Count != 0) if (cbUserforImei.Items.Count != 0)
cbUserforImei.SelectedIndex = 0; cbUserforImei.SelectedIndex = 0;
formTypeEdit = true; formTypeEdit = true;
int count = 0; int count = 0;
foreach (User user in usrList) foreach (User user in usrList)
@ -502,31 +500,23 @@ namespace SubscriberAndUserManager
count++; count++;
} }
} }
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(); gList = DBgrp.GetAllGroups();
RadListDataItem test2 = new RadListDataItem(MainForm.returnLNGString("none")); listCategory = gList.Where(x => x.Type == GroupType.CATEGORY.value).ToList();
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();
listRadioTalk = gList.Where(x => x.Type == GroupType.RADIOTALK.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 (cbGroups.Items.Count > 0) cbGroups.SelectedIndex = 0;
if (cbTalkGroup.Items.Count > 0) cbTalkGroup.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); Group g = DBgrp.GetGroupForSCID(sc_id,GroupType.CATEGORY.value);
if (g != null) if (g != null)
{ {
count = 0;
foreach (Group grp in listCategory) int i = getIndex(listCategory, g.Name);
{ if (i >= 0)
count++; cbGroups.SelectedIndex = i;
if (grp.Name == g.Name)
{
cbGroups.SelectedIndex = count;
}
}
} }
g = DBgrp.GetGroupForSCID(sc_id, GroupType.RADIOTALK.value); g = DBgrp.GetGroupForSCID(sc_id, GroupType.RADIOTALK.value);
if (g != null) if (g != null)
{ {
count = 0; int i = getIndex(listRadioTalk, g.Name);
foreach (Group grp in listRadioTalk) if (i >= 0)
{ cbTalkGroup.SelectedIndex = i;
count++;
if (grp.Name == g.Name)
{
cbTalkGroup.SelectedIndex = count;
}
}
} }
//car icons //get car icons
imgList = DB.get_all_car_picture(); imgList = DB.get_all_car_picture();
imageList1 = getValidAllPictures(imgList);
int idx = 0; int idx = getImageIndex(imageList1, selVeh.ImgID.ToString());
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 (selVeh.ImgID == obj.idx) idx = tempIDX;
}
catch (Exception ex)
{
Utils.WriteLine("\nBmp " + filePath + " exception " + ex.ToString(), ConsoleColor.Red);
}
}
cbImageList.DataSource = imgList; cbImageList.DataSource = imgList;
cbImageList.DisplayMember = "DisplayedName"; cbImageList.DisplayMember = "DisplayedName";
if (imageList1.Images.Count > 0) if (imageList1.Images.Count > 0)
{ {
if (idx < 0) idx = 0;
pictureBox1.Image = imageList1.Images[idx]; pictureBox1.Image = imageList1.Images[idx];
cbImageList.SelectedIndex = idx; cbImageList.SelectedIndex = idx;
} }
@ -655,6 +620,19 @@ namespace SubscriberAndUserManager
parrent = _parent; 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) private String TestUniqueIDinRange(Int32 start, Int32 stop)
{ {
String toreturn = ""; String toreturn = "";

View File

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