public LockBatch GetCurrentLocks(string loginName) { LockBatch batch = new LockBatch(); lock(locks) { LockItem cpLockItem = new LockItem(); cpLockItem.ItemType = ResourceMap.ModelTypeToItemType<CollectionPoint>(); cpLockItem.IDsToLock = locks.GetLockedIdsForItemType(cpLockItem.ItemType, loginName, LockMode.Locked); LockItem cLocks = new LockItem(); cLocks.ItemType = ResourceMap.ModelTypeToItemType<Customer>(); cLocks.IDsToLock = locks.GetLockedIdsForItemType(cLocks.ItemType, loginName, LockMode.Locked); batch.ItemsToLock = new List<LockItem>(new LockItem[] { cpLockItem, cLocks }); } return batch; }
public LockBatch GetItemsToLock(int id) { Console.WriteLine("Customer Strategy called."); LockBatch batch = new LockBatch(); LockItem lockItem = new LockItem(); lockItem.ItemType = ItemType.CollectionPoint; lockItem.IDsToLock = new List<int>(new int[] { 1, 2, 3 }); LockItem lockItem2 = new LockItem(); lockItem2.ItemType = ItemType.Customer; List<int> aLotOfIds = new List<int>(); for (int i = 1; i < 30; i++) { aLotOfIds.Add(i); } lockItem2.IDsToLock = aLotOfIds; batch.ItemsToLock = new List<LockItem>(new LockItem[] { lockItem, lockItem2 }); return batch; }
public LockBatch GetItemsToLock(int id) { Console.WriteLine("CollectionPoint lock strategy for id: {0} called.", id); ResourceCache currentItems = tm.Cache; List<CollectionPoint> allCollectionPoints = currentItems.GetAllItems<CollectionPoint>(); List<Customer> allCustomers = currentItems.GetAllItems<Customer>(); LockItem cpLockItem = new LockItem(); cpLockItem.ItemType = ItemType.CollectionPoint; cpLockItem.IDsToLock = new List<int>(); LockItem customerLockItem = new LockItem(); customerLockItem.ItemType = ItemType.Customer; customerLockItem.IDsToLock = new List<int>(); LockBatch batch = new LockBatch(); CollectionPoint targetItem = currentItems.GetItem<CollectionPoint>(id); if(targetItem != null) { batch.ItemsToLock = new List<LockItem>(new LockItem[] { cpLockItem, customerLockItem }); cpLockItem.IDsToLock.Add(id); //root id of the lock request // get the customers List<Customer> targetCustomers = targetItem.Customers; // add the customers of target item first foreach(Customer currentCustomer in targetCustomers) { customerLockItem.IDsToLock.Add(currentCustomer.Id); } // go over the other CollectionPoints and get the dependencies int oldCustomerCount = -1; //TODO: do we need to observe oldCollectionPoint count too? do { oldCustomerCount = customerLockItem.IDsToLock.Count; foreach(CollectionPoint currentCollectionPoint in allCollectionPoints) { if(cpLockItem.IDsToLock.Contains(currentCollectionPoint.Id) == false) { bool dependencyExists = false; List<int> customerIdsOfCurrentCP = new List<int>(); //check if this collection point contains at least one customer with //which is allready marked as locked foreach(Customer currentCustomer in currentCollectionPoint.Customers) { customerIdsOfCurrentCP.Add(currentCustomer.Id); if(customerLockItem.IDsToLock.Contains(currentCustomer.Id)) { dependencyExists = true; } } if(dependencyExists == true) { // if new dependency found we need to add the CollectionPoint // and the customer not allready marked to be locked foreach(int customerId in customerIdsOfCurrentCP) { if(customerLockItem.IDsToLock.Contains(customerId) == false) { customerLockItem.IDsToLock.Add(customerId); } } cpLockItem.IDsToLock.Add(currentCollectionPoint.Id); } } } } while (oldCustomerCount.Equals(customerLockItem.IDsToLock.Count) == false); } else { Console.WriteLine("CollectionPoint root item: {0} isn't cached by the data source!", id); batch.ItemsToLock = null; } return batch; }
private bool ReleaseLocks(String login, LockMode mode, LockItem item) { bool allLocksReleased = true; foreach(int id in item.IDsToLock) { List<LockData> currentLocks = locks.GetLocksForItem(id, item.ItemType); LockData release = new LockData(login, mode); if(currentLocks.Contains(release)) { currentLocks.Remove(release); locks.UpdateLocksForItem(id, item.ItemType, currentLocks); } else { //this should never be the case! allLocksReleased = false; } } return allLocksReleased; }
private bool SetLocks(String login, LockBatch batch, LockMode mode, LockItem item) { foreach (int id in item.IDsToLock) { List<LockData> currentLocks = locks.GetLocksForItem(id, item.ItemType); LockData newLock = new LockData(login, mode); // for future write-lock development // we have to pass currentLocks as a whole to assure that ll // for this user are present in currentLocks // write locks can only be set if the ll for this user are set foreach(LockData lockData in currentLocks) { if(newLock.Equals(lockData) == false && !LockMatrix.IsModeCompatible(lockData.mode, mode)) { Console.WriteLine("Lock conflict, rollback locks for user: {0}", login); Unlock(login, batch, mode); return false; } } if(currentLocks.Contains(newLock) == false) { currentLocks.Add(newLock); locks.UpdateLocksForItem(id, item.ItemType, currentLocks); } } return true; }