209 lines
5.3 KiB
C#
209 lines
5.3 KiB
C#
///
|
|
/// Copyright (c) 2013-2015 Sensus Metering Systems
|
|
///
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.Windows.Forms;
|
|
using NHibernate;
|
|
using log4net;
|
|
using TBF.BenchControl;
|
|
using TBF.BenchControl.GenericDevices;
|
|
using TBF.Resources;
|
|
|
|
namespace TBF.UiControls
|
|
{
|
|
public class ProceduresCtrl : BaseWithListViewEx<Entities.Procedure>, ITabWithListViewEx
|
|
{
|
|
static readonly ILog log = LogManager.GetLogger(typeof(ProceduresCtrl));
|
|
|
|
/// <summary>
|
|
/// ListViewEx columns
|
|
/// </summary>
|
|
enum Column
|
|
{
|
|
Name,
|
|
Description,
|
|
ColumnsCount,
|
|
}
|
|
readonly int columnsCount = (int)Column.ColumnsCount;
|
|
|
|
ProceduresDlg parent;
|
|
Control parentControl;
|
|
Control[] editors;
|
|
|
|
ISession session;
|
|
|
|
public ProceduresCtrl()
|
|
: base()
|
|
{
|
|
Name = "ProceduresCtrl";
|
|
}
|
|
|
|
public void Initialize(ProceduresDlg parent, Control parentControl)
|
|
{
|
|
this.parent = parent;
|
|
this.parentControl = parentControl;
|
|
|
|
SubItemClicked += new SubItemEventHandler(listViewEx_SubItemClicked);
|
|
SubItemEndEditing += new SubItemEndEditingEventHandler(listViewEx_SubItemEndEditing);
|
|
|
|
/// ListViewEx columns
|
|
Columns.Add(Strings.Name, 100 * parent.Dpi / PathsDlg.Dpi100pct);
|
|
Columns.Add(Strings.DescriptionColHdr, 300 * parent.Dpi / PathsDlg.Dpi100pct);
|
|
|
|
editors = new Control[columnsCount];
|
|
///
|
|
editors[(int)Column.Name] = new TextBox(); /// Name
|
|
editors[(int)Column.Description] = new TextBox(); /// Description
|
|
///
|
|
for (int i = 0; i < editors.Length; i++) parent.Controls.Add(editors[i]);
|
|
|
|
ReloadAndRedrawAll();
|
|
}
|
|
|
|
void ReloadAndRedrawAll()
|
|
{
|
|
/// Load the procedures
|
|
session = FluentCommon.CreateSession(Database.Procedures);
|
|
MyItems = session.CreateQuery("FROM Procedure ORDER BY ItemNr")
|
|
.List<Entities.Procedure>();
|
|
|
|
base.RedrawAll();
|
|
}
|
|
|
|
void listViewEx_SubItemClicked(object sender, SubItemEventArgs e)
|
|
{
|
|
}
|
|
|
|
void listViewEx_SubItemEndEditing(object sender, SubItemEndEditingEventArgs e)
|
|
{
|
|
}
|
|
|
|
public override void DrawOne(object myItem)
|
|
{
|
|
Entities.Procedure entity = (Entities.Procedure)myItem;
|
|
|
|
ListViewItem lvi = new ListViewItem(entity.Name);
|
|
lvi.SubItems.Add(entity.Description);
|
|
lvi.Tag = entity;
|
|
|
|
Items.Add(lvi);
|
|
}
|
|
|
|
public override void UpdateOne(ListViewItem lvi, object myItem)
|
|
{
|
|
Entities.Procedure entity = (Entities.Procedure)myItem;
|
|
entity.Name = lvi.Text;
|
|
entity.Description = lvi.SubItems[(int)Column.Description].Text;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update the entities and save them to the database
|
|
/// </summary>
|
|
public new void OkBtnClicked()
|
|
{
|
|
base.OkBtnClicked();
|
|
|
|
foreach (var entity in ToBeRemovedItems)
|
|
{
|
|
FluentCommon.DeleteFromDb(session, entity);
|
|
}
|
|
foreach (var entity in MyItems)
|
|
{
|
|
FluentCommon.SaveToDb(session, entity);
|
|
}
|
|
|
|
Program.MainWnd.ReloadProcedures();
|
|
}
|
|
|
|
public void AddOne()
|
|
{
|
|
/// Find an unused procedure name
|
|
string newName;
|
|
for (int nameId = 1; true; nameId++)
|
|
{
|
|
newName = Strings.New_procedure_name + nameId.ToString();
|
|
bool notUsed = true;
|
|
foreach (var t in MyItems) if (t.Name.Equals(newName)) notUsed = false;
|
|
if (notUsed) break;
|
|
}
|
|
|
|
base.AddOne(new Entities.Procedure(newName, MyItems.Count + 1));
|
|
}
|
|
|
|
public new void RemoveSelected()
|
|
{
|
|
bool lastOneRemoved = base.RemoveSelected();
|
|
if (lastOneRemoved && parent != null)
|
|
{
|
|
parent.UpdateButtonStates(SharedButtons.SelectedItemPos.None);
|
|
}
|
|
}
|
|
|
|
public void editButton_Click(object sender, EventArgs e)
|
|
{
|
|
if (SelectedItems.Count != 1) return;
|
|
int dbId = (SelectedItems[0].Tag as Entities.Procedure).Id;
|
|
|
|
if (dbId == 0)
|
|
{
|
|
session.SaveOrUpdate(SelectedItems[0].Tag as Entities.Procedure);
|
|
}
|
|
|
|
dbId = (SelectedItems[0].Tag as Entities.Procedure).Id;
|
|
|
|
///
|
|
/// Entities.Procedure has to be loaded in a different session otherwise
|
|
/// the database would be updated in case the dialog was closed with 'Cancel' button.
|
|
///
|
|
using (ISession editSession = FluentCommon.CreateSession(Database.Procedures))
|
|
{
|
|
IList<Entities.Procedure> procedure = editSession
|
|
.CreateQuery("FROM Procedure WHERE Id = :id")
|
|
.SetParameter("id", dbId)
|
|
.List<Entities.Procedure>();
|
|
if (procedure.Count != 1) return;
|
|
|
|
ProcedureDlg dlg = new ProcedureDlg(procedure[0]);
|
|
if (dlg.ShowDialog() == DialogResult.OK)
|
|
{
|
|
parent.Unlock();
|
|
using (var transaction = editSession.BeginTransaction())
|
|
{
|
|
editSession.SaveOrUpdate(procedure[0]);
|
|
transaction.Commit();
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
ReloadAndRedrawAll();
|
|
}
|
|
|
|
public void copyButton_Click(object sender, EventArgs e)
|
|
{
|
|
if (SelectedItems.Count != 1) return;
|
|
|
|
Entities.Procedure selectedProcedure = (Entities.Procedure)SelectedItems[0].Tag;
|
|
|
|
/// Make a copy of the selected procedure
|
|
Entities.Procedure newProcedure = selectedProcedure.Clone();
|
|
newProcedure.Name = selectedProcedure.Name + Strings.New_name_copy;
|
|
newProcedure.ItemNr = MyItems.Count;
|
|
newProcedure.CreationUser = Entities.User.CurrentUser.Name;
|
|
newProcedure.CreationTime = DateTime.Now;
|
|
|
|
if ((new ProcedureDlg(newProcedure)).ShowDialog() == DialogResult.OK)
|
|
{
|
|
using (var transaction = session.BeginTransaction())
|
|
{
|
|
session.SaveOrUpdate(newProcedure);
|
|
transaction.Commit();
|
|
}
|
|
base.AddOne(newProcedure);
|
|
}
|
|
}
|
|
}
|
|
}
|