using System; using System.Collections.Generic; using System.Linq; using System.Text; using NHibernate; using log4net; namespace TracingDB { 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 /// /// Constructor without registering a workplace. Use UpdateRegistration() to register a workplace. /// public WPlaceRegistrationMgmt() { } /// /// Constructor that registers a workplace. /// /// MySQL DB session /// Workplace name /// User name /// IP address /// Process name /// Workstep name /// Registration is valid until this DateTime 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; } } /// /// Compares associated information and registers a workplace if anything changed. /// /// MySQL DB session /// Workplace name /// User name /// IP address /// Process name /// Workstep name /// Registration is valid until this DateTime /// true when successful 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 } } /// /// Unregisters a workplace. /// /// MySQL DB session /// true when successful public bool UnregisterWorkplace(ISession session) { if (UnregisterWorkplace(session, this.workplace)) { this.workplace = string.Empty; return true; } else { return false; } } /// /// Register a workplace. Provide associated information. /// /// MySQL DB session /// Workplace name /// User name /// IP address /// Process name /// Workstep name /// Registration is valid until this DateTime /// true when successful static bool RegisterWorkplace(ISession session, string workplace, string user, string ipAddress, string processName, string workstepName, DateTime valiUntil) { try { IList wpRegs = session .QueryOver() .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; } } /// /// Unregister a workplace. /// /// MySQL DB session /// Workplace name /// true when successful public static bool UnregisterWorkplace(ISession session, string workplace) { if (workplace == null) return true; /// no workplace => unregistration successful try { IList wpRegs = session .QueryOver() .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; } } } }