/// <summary> /// Updates or adds resourcebox to system /// </summary> /// <param name="resourcebox">Resourcebox to be updated or added</param> /// <param name="add">True if the resourcebox is to be added</param> /// <returns>0 - If all Ok, else a database error</returns> /// #1.04 - New index for week list (week number) public int setResourceBox(ResourceBox resourcebox, bool add) { try { // Save the current id-list SortedList oldids = new SortedList(resourcebox.IdsNextTo); int fel = db_.setResourceBox(resourcebox, add); if (fel != 0) { return(fel); } if (add == true) { // Add the resourcebox to the list this.resourceBoxes_.Add(resourcebox.Id, resourcebox); SelectedWeek selweek = new SelectedWeek(resourcebox.StartTime); // #1.04 - start if (!weeks_.ContainsKey(selweek.Week)) { weeks_[selweek.Week] = new SortedList(); } ((SortedList)weeks_[selweek.Week]).Add(resourcebox.Id, resourcebox); // #1.04 - stop ((ActivePersonResource)this.activePersonResources_[resourcebox.Name]).AddObserver(resourcebox); } // Update alla resourceboxes with the old id-list fel = this.updateIdsNextTo(oldids); // Update id-list of the resourcebox itself resourcebox.updateIdsNextTo(db_); // Update alla resourceboxes with the new id-list fel += this.updateIdsNextTo(resourcebox.IdsNextTo); resourcebox.Notify(); if (fel != 0) { return(fel); } return(0); } catch { // If there was en error update alla resourceboxes this.updateResourceBoxes(); } return(ErrorHandler.ERR_UNEXPECTED_RESOURCEBOX_ADD); }
/// <summary> /// Removes a resourcebox from system. /// </summary> /// <param name="resourcebox">Resourcebox to remove</param> /// <returns>0 - If all Ok, else a database error</returns> /// #1.04 - New index for week list (week number) public int removeResourceBox(ResourceBox resourcebox) { // If the resourcebox just is locked, then just zero it, by setting end and startime to the same thing if (resourcebox.Locked == true) { resourcebox.EndTime = resourcebox.LockedStartTime; resourcebox.StartTime = resourcebox.LockedStartTime; this.setResourceBox(resourcebox, false); resourcebox.Notify(); return(0); } else { try { // Get old id-list SortedList oldids = new SortedList(resourcebox.IdsNextTo); int fel = db_.removeResourceBox(resourcebox); if (fel != 0) { return(fel); } // Update with old id-list fel = this.updateIdsNextTo(oldids); // HACK: är denna rad nödvändig? fel += this.updateIdsNextTo(resourcebox.IdsNextTo); this.resourceBoxes_.Remove(resourcebox.Id); // Remove for week-lists (this week and next) SelectedWeek selweek = new SelectedWeek(resourcebox.StartTime); // #1.04 - start if (weeks_.ContainsKey(selweek.Week)) { ((SortedList)weeks_[selweek.Week]).Remove(resourcebox.Id); } selweek = new SelectedWeek(selweek.DateOfFirstDayInWeek.AddDays(7)); if (weeks_.ContainsKey(selweek.Week)) { ((SortedList)weeks_[selweek.Week]).Remove(resourcebox.Id); } // #1.04 - stop // Hide graphics resourcebox.StartTime = new DateTime(0); resourcebox.EndTime = new DateTime(0); resourcebox.Notify(); if (fel != 0) { return(fel); } return(0); } catch { this.updateResourceBoxes(); } return(ErrorHandler.ERR_UNEXPECTED_RESOURCEBOX_REM); } }