227 lines
9.1 KiB
C#
227 lines
9.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using NHibernate;
|
|
using log4net;
|
|
|
|
namespace SharedDatabase
|
|
{
|
|
public class WPlaceRegistrationMgmt
|
|
{
|
|
static readonly ILog log = LogManager.GetLogger(typeof(WPlaceRegistrationMgmt));
|
|
|
|
string workplace; /// Workplace ID string
|
|
string user; /// User name
|
|
string ipAddress; /// IP address of the workplace (not implemented now)
|
|
string processName; /// Process name
|
|
string workstepName; /// Workstep name
|
|
DateTime valiUntil; /// End of validity of this registration
|
|
|
|
/// <summary>
|
|
/// Constructor without registering a workplace. Use UpdateRegistration() to register a workplace.
|
|
/// </summary>
|
|
public WPlaceRegistrationMgmt()
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Constructor that registers a workplace.
|
|
/// </summary>
|
|
/// <param name="session">MySQL DB session</param>
|
|
/// <param name="workplace">Workplace name</param>
|
|
/// <param name="user">User name</param>
|
|
/// <param name="ipAddress">IP address</param>
|
|
/// <param name="processName">Process name</param>
|
|
/// <param name="workstepName">Workstep name</param>
|
|
/// <param name="valiUntil">Registration is valid until this DateTime</param>
|
|
public WPlaceRegistrationMgmt(ISession session, string workplace, string user, string ipAddress, string processName, string workstepName, DateTime valiUntil)
|
|
{
|
|
if (RegisterWorkplace(session, workplace, user, ipAddress, processName, workstepName, valiUntil))
|
|
{
|
|
this.workplace = workplace;
|
|
this.user = user;
|
|
this.ipAddress = ipAddress;
|
|
this.processName = processName;
|
|
this.workstepName = workstepName;
|
|
this.valiUntil = valiUntil;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Compares associated information and registers a workplace if anything changed.
|
|
/// </summary>
|
|
/// <param name="session">MySQL DB session</param>
|
|
/// <param name="workplace">Workplace name</param>
|
|
/// <param name="user">User name</param>
|
|
/// <param name="ipAddress">IP address</param>
|
|
/// <param name="processName">Process name</param>
|
|
/// <param name="workstepName">Workstep name</param>
|
|
/// <param name="valiUntil">Registration is valid until this DateTime</param>
|
|
/// <returns>true when successful</returns>
|
|
public bool UpdateRegistration(ISession session, string workplace, string user, string ipAddress, string processName, string workstepName, DateTime valiUntil)
|
|
{
|
|
if (this.workplace != workplace)
|
|
{
|
|
/// Workplace name changed -> Unregister and register with a new name
|
|
if (!UnregisterWorkplace(session, this.workplace)) return false;
|
|
|
|
if (RegisterWorkplace(session, workplace, user, ipAddress, processName, workstepName, valiUntil))
|
|
{
|
|
this.workplace = workplace;
|
|
this.user = user;
|
|
this.ipAddress = ipAddress;
|
|
this.processName = processName;
|
|
this.workstepName = workstepName;
|
|
this.valiUntil = valiUntil;
|
|
return true; /// Registration successful
|
|
}
|
|
else
|
|
{
|
|
return false; /// Registration failed
|
|
}
|
|
}
|
|
else if (this.user != user || this.ipAddress != ipAddress || this.processName != processName || this.workstepName != workstepName)
|
|
{
|
|
/// Anythig else changed -> Update registration
|
|
if (RegisterWorkplace(session, workplace, user, ipAddress, processName, workstepName, valiUntil))
|
|
{
|
|
this.workplace = workplace;
|
|
this.user = user;
|
|
this.ipAddress = ipAddress;
|
|
this.processName = processName;
|
|
this.workstepName = workstepName;
|
|
this.valiUntil = valiUntil;
|
|
return true; /// Registration successful
|
|
}
|
|
else
|
|
{
|
|
return false; /// Registration failed
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return true; /// Already registered with identical associated info
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Unregisters a workplace.
|
|
/// </summary>
|
|
/// <param name="session">MySQL DB session</param>
|
|
/// <returns>true when successful</returns>
|
|
public bool UnregisterWorkplace(ISession session)
|
|
{
|
|
if (UnregisterWorkplace(session, this.workplace))
|
|
{
|
|
this.workplace = string.Empty;
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Register a workplace. Provide associated information.
|
|
/// </summary>
|
|
/// <param name="session">MySQL DB session</param>
|
|
/// <param name="workplace">Workplace name</param>
|
|
/// <param name="user">User name</param>
|
|
/// <param name="ipAddress">IP address</param>
|
|
/// <param name="processName">Process name</param>
|
|
/// <param name="workstepName">Workstep name</param>
|
|
/// <param name="valiUntil">Registration is valid until this DateTime</param>
|
|
/// <returns>true when successful</returns>
|
|
static bool RegisterWorkplace(ISession session, string workplace, string user, string ipAddress, string processName, string workstepName, DateTime valiUntil)
|
|
{
|
|
try
|
|
{
|
|
IList<Entities.WorkplaceRegistration> wpRegs = session
|
|
.QueryOver<Entities.WorkplaceRegistration>()
|
|
.Where(x => (x.Workplace == workplace))
|
|
.List();
|
|
|
|
if (wpRegs.Count == 0)
|
|
{
|
|
Entities.WorkplaceRegistration wpReg = new Entities.WorkplaceRegistration(workplace, user, ipAddress, processName, workstepName);
|
|
wpRegs.Add(wpReg);
|
|
session.SaveOrUpdate(wpReg);
|
|
log.InfoFormat("RegisterWorkplace(., {0}, {1}, ...) successful (new)", workplace, user);
|
|
return true;
|
|
}
|
|
else if (wpRegs.Count == 1)
|
|
{
|
|
Entities.WorkplaceRegistration wpReg = wpRegs[0];
|
|
|
|
wpReg.Active = true;
|
|
wpReg.TimeStamp = DateTime.Now;
|
|
wpReg.ValidUntil = DateTime.Now + new TimeSpan(8, 0, 0);
|
|
wpReg.UserName = user;
|
|
wpReg.IPAddress = ipAddress;
|
|
wpReg.ProcessName = processName;
|
|
wpReg.WorkstepName = workstepName;
|
|
|
|
session.SaveOrUpdate(wpReg);
|
|
session.Flush();
|
|
log.InfoFormat("RegisterWorkplace(., {0}, {1}, ...) successful", workplace, user);
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
log.ErrorFormat("RegisterWorkplace(., {0}, {1}, ...) failed", workplace, user);
|
|
return false;
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
log.ErrorFormat("RegisterWorkplace(., {0}, {1}, ...) exception: {2}", workplace, user, e.Message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Unregister a workplace.
|
|
/// </summary>
|
|
/// <param name="session">MySQL DB session</param>
|
|
/// <param name="workplace">Workplace name</param>
|
|
/// <returns>true when successful</returns>
|
|
public static bool UnregisterWorkplace(ISession session, string workplace)
|
|
{
|
|
if (workplace == null) return true; /// no workplace => unregistration successful
|
|
|
|
try
|
|
{
|
|
IList<Entities.WorkplaceRegistration> wpRegs = session
|
|
.QueryOver<Entities.WorkplaceRegistration>()
|
|
.Where(x => (x.Workplace == workplace))
|
|
.List();
|
|
|
|
if (wpRegs.Count == 1)
|
|
{
|
|
Entities.WorkplaceRegistration wpReg = wpRegs[0];
|
|
|
|
wpReg.Active = false;
|
|
wpReg.TimeStamp = DateTime.Now;
|
|
|
|
session.SaveOrUpdate(wpReg);
|
|
session.Flush();
|
|
log.InfoFormat("UnregisterWorkplace(., {0}) successful", workplace);
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
log.ErrorFormat("UnregisterWorkplace(., {0}) failed", workplace);
|
|
return false;
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
log.ErrorFormat("UnregisterWorkplace(., {0}) exception: {1}", workplace, e.Message);
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
}
|