using EnVisage.Code; using EnVisage.Properties; using System; using System.Collections.Generic; using System.Linq; using System.Timers; using System.Web; namespace EnVisage.App_Start { public class LockerResponse { public bool Status { get; set; } public string LockedBy { get; set; } public string EntityTitle { get; set; } } public static class ContentLocker { static readonly Dictionary EntityTitles = new Dictionary { {"SystemSettings", "System Settings"}, {"Clients", "Client"}, {"CreditDepartment", "Credit Department"}, {"ExpenditureCategory", "Expenditure Category"}, {"GLAccount", "GL Account"}, {"Trainings", "Training"}, {"Vacations", "Vacation"}, {"ScenarioGroup", "Scenario Group"}, {"ExpenditureCategoryRate", "Rate"}, {"Project Statuses", "Project Status"}, {"Types", "Type"}, {"UnitOfMeasure", "Unit Of Measure"}, {"TrainingTypes", "Training Type"}, {"PeopleResource", "People Resource"} }; static Timer timer; static Dictionary> lockersList; static ContentLocker() { lockersList = new Dictionary>(); timer = new Timer(Settings.Default.CheckInterval); timer.Elapsed += checkDeadLocks; if (!timer.Enabled) timer.Start(); } static void checkDeadLocks(object sender, ElapsedEventArgs e) { foreach (var lockedElement in lockersList) { unlockDead(lockedElement.Value); } } static void unlockDead(Dictionary lockedList) { List unlockItems = new List(); foreach (var lockedElement in lockedList) { if (lockedElement.Value.GetLockInterval() >= Settings.Default.UnlockInterval) { unlockItems.Add(lockedElement.Key); } } foreach (var unlockId in unlockItems) { lockedList.Remove(unlockId); } } public static LockerResponse AddLock(String tableId, String fieldId, string owner) { if (!lockersList.ContainsKey(tableId)) { lockersList.Add(tableId, new Dictionary()); } if (lockersList[tableId].ContainsKey(fieldId) && lockersList[tableId][fieldId].Owner != owner) { return new LockerResponse { Status = false, LockedBy = lockersList[tableId][fieldId].Owner, EntityTitle = GetLockedEntityTitle(tableId) }; } if (lockersList[tableId].ContainsKey(fieldId) && lockersList[tableId][fieldId].Owner == owner) { return new LockerResponse { Status = true, EntityTitle = GetLockedEntityTitle(tableId) }; } lockersList[tableId].Add(fieldId, new LockedElement(owner)); return new LockerResponse { Status = true, EntityTitle = GetLockedEntityTitle(tableId) }; } public static bool RemoveLock(String tableId, String fieldId, string owner) { if (!lockersList.ContainsKey(tableId)) { return false; } if (!lockersList[tableId].ContainsKey(fieldId)) { return false; } if (lockersList[tableId][fieldId].Owner == owner) { lockersList[tableId].Remove(fieldId); return true; } return false; } public static bool UpdateLock(String tableId, String fieldId, string owner) { if (!lockersList.ContainsKey(tableId)) { return false; } if (!lockersList[tableId].ContainsKey(fieldId)) { return false; } if (lockersList[tableId][fieldId].Owner == owner) { lockersList[tableId][fieldId].UpdateLock(); return true; } return false; } public static string[] getLockList(String tableId) { if (lockersList.ContainsKey(tableId)) { List lockItems = new List(); foreach (var lockedElement in lockersList) { lockItems.Add(lockedElement.Key); } return lockItems.ToArray(); } return null; } public static LockerResponse IsLocked(String tableId, String fieldId, string owner) { if (lockersList.ContainsKey(tableId) && lockersList[tableId].ContainsKey(fieldId) && lockersList[tableId][fieldId].Owner != owner) { return new LockerResponse { Status = true, LockedBy = lockersList[tableId][fieldId].Owner, EntityTitle = GetLockedEntityTitle(tableId) }; } return new LockerResponse {Status = false, EntityTitle = GetLockedEntityTitle(tableId)}; } public static bool IsLock(String tableId, String fieldId, string owner) { return IsLocked(tableId, fieldId, owner).Status; } public static string GetLockedEntityTitle(string tableId) { if (EntityTitles.ContainsKey(tableId)) return EntityTitles[tableId]; return tableId; } } }