/// <summary>
        /// Called when the user clicks Delete. The IModel is parsed from the controls in the EditView and is then attempted to be deleted from the database
        /// </summary>
        public void HandleDeleteButtonClick()
        {
            IModel model         = null;
            var    controlValues = this.ViewControlsToDictionary(EditView.GetControls());

            model = Utils.ParseWinFormsToIModel(EditView.Model, controlValues, QueryType.REMOVE);
            if (model != null)
            {
                this.Delete(model);
                this.isExistingObjectInDatabase = false;
            }
        }
        /// <summary>
        /// Called when the user clicks the Save button. Parses the controls in the EditView to an IModel and saves/updates it in the database
        /// </summary>
        /// <param name="oldIdentifyingAttributes">If the IModel has changed their identifying attribute, these are the old/current identifying attributes</param>
        public void HandleSaveButtonClick(Dictionary <string, object> oldIdentifyingAttributes = null)
        {
            if (this.IdentifyingValuesAreNotEmpty())
            {
                IModel model         = null;
                var    controlValues = this.ViewControlsToDictionary(EditView.GetControls());
                if (controlValues != null)
                {
                    model = Utils.ParseWinFormsToIModel(EditView.Model, controlValues, QueryType.ADD);
                    if (model != null && model.GetIdentifyingAttributes().First().Value != null)
                    {
                        if (model is Building)
                        {
                            DateTime opening, closing;
                            opening = ((Building)model).Avail_start;
                            closing = ((Building)model).Avail_end;
                            if (opening > closing)
                            {
                                this.UpdateResponseLabel("Öppningstid kan inte vara senare än stängningstid");
                                return;
                            }
                        }

                        //Special case for booking, since it cannot overlap another booking
                        //But if it's an update to an existing item, we're not doing the check
                        if (model is Booking)
                        {
                            Booking parsedBooking = (Booking)model;
                            if (parsedBooking.RoomId == null || parsedBooking.PersonId == null || parsedBooking.Start_time == default(DateTime) || parsedBooking.End_time == default(DateTime))
                            {
                                this.UpdateResponseLabel("Något av följande fält är tomt, vänligen fyll i alla obligatoriska fält (Person, Rum, Starttid, Sluttid");
                                return;
                            }
                            if (isExistingObjectInDatabase == false)
                            {
                                DAL  d          = new DAL(this);
                                bool isBookable = d.IsRoomBookableOnDate(parsedBooking.RoomId, parsedBooking.Start_time, parsedBooking.End_time);
                                if (isBookable == false)
                                {
                                    this.UpdateResponseLabel("Rummet är redan bokad denna tid, vänligen välj en annan");
                                    return;
                                }
                            }
                            DateTime startDate = parsedBooking.Start_time;
                            DateTime endDate   = parsedBooking.End_time;
                            startDate = new DateTime(startDate.Year, startDate.Month, startDate.Day, startDate.Hour, 0, 0);
                            endDate   = new DateTime(endDate.Year, endDate.Month, endDate.Day, endDate.Hour, 0, 0);
                            ((Booking)model).Start_time = startDate;
                            ((Booking)model).End_time   = endDate;
                        }


                        // SAVING OBJECT

                        DAL    dal          = new DAL(this);
                        int    affectedRows = 0;
                        string dbMethod     = "";
                        if (isExistingObjectInDatabase)
                        {
                            if (oldIdentifyingAttributes != null && oldIdentifyingAttributes.Count > 0)
                            {
                                affectedRows = dal.Update(model, oldIdentifyingAttributes);
                            }
                            else
                            {
                                affectedRows = dal.Update(model);
                            }
                            dbMethod = "uppdaterad";
                        }
                        else
                        {
                            affectedRows = dal.Add(model);
                            dbMethod     = "tillagd";
                        }
                        string displayName = Utils.ConvertAttributeNameToDisplayName(model, model.GetType().Name);
                        displayName = displayName[0].ToString().ToUpper() + displayName.Substring(1);
                        if (affectedRows > 0)
                        {
                            this.UpdateResponseLabel(string.Format("{0} {1}", displayName, dbMethod));
                            this.isExistingObjectInDatabase = true;
                            this.SetAllControlsDisabledExceptClose();
                        }
                        else if (affectedRows != -1)
                        { //-1 is error from DAL
                            this.UpdateResponseLabel(string.Format("Ingen {0} {1}", displayName, dbMethod));
                        }

                        // END OF SAVING

                        if (affectedRows > 0 && this.ViewHasListOfIModels)
                        {
                            // Foreach the keys in the originalvalues (there can be multiple lists/checklistboxes)
                            foreach (var changedStatusForType in this.changedStatusOnReferencingModels)
                            {
                                Type referencedType = changedStatusForType.Key;
                                Dictionary <IModel, bool> changedStatus = changedStatusForType.Value;
                                Dictionary <IModel, bool> initialStatus = this.InitialStatusOnReferencingModels.ContainsKey(referencedType) ? this.InitialStatusOnReferencingModels[referencedType] : null;

                                List <IModel> toBeAdded = new List <IModel>();
                                List <IModel> toDeleteOrUpdateToNull = new List <IModel>();

                                bool doOrdinaryAddAndDelete = false;

                                var intersected = initialStatus.Keys.Intersect(changedStatus.Keys).ToList();
                                foreach (var intersectedModel in intersected)
                                {
                                    if (initialStatus[intersectedModel] == true && changedStatus[intersectedModel] == false)
                                    {
                                        toDeleteOrUpdateToNull.Add(intersectedModel);
                                    }
                                    else if (initialStatus[intersectedModel] == false && changedStatus[intersectedModel] == true)
                                    {
                                        toBeAdded.Add(intersectedModel);
                                    }
                                }
                                // If it's an associationtable, and a changedStatus-dict contains elements
                                // delete all associated-table-objects with ID from each IDs
                                if (model is Room && changedStatus.Any() && changedStatus.First().Key is Resource)
                                {
                                    toBeAdded = toBeAdded.Select(x => (IModel) new Room_Resource(((Room)model).Id, ((Resource)x).Id)).ToList();
                                    toDeleteOrUpdateToNull = toDeleteOrUpdateToNull.Select(x => (IModel) new Room_Resource(((Room)model).Id, ((Resource)x).Id)).ToList();
                                    doOrdinaryAddAndDelete = true;
                                }

                                dal = new DAL(this);
                                int added = 0, updatedOrRemoved = 0;

                                if (doOrdinaryAddAndDelete && (toBeAdded.Any() || toDeleteOrUpdateToNull.Any()))
                                {
                                    foreach (IModel add in toBeAdded)
                                    {
                                        added += dal.Add(add);
                                    }
                                    foreach (IModel remove in toDeleteOrUpdateToNull)
                                    {
                                        updatedOrRemoved += dal.Remove(remove);
                                    }
                                }
                                else if (!doOrdinaryAddAndDelete)
                                {
                                    if (toBeAdded.Any())
                                    {
                                        added = dal.ConnectOrNullReferencedIModelsToIModelToQuery(toBeAdded, model, true);
                                    }
                                    if (toDeleteOrUpdateToNull.Any())
                                    {
                                        updatedOrRemoved = dal.ConnectOrNullReferencedIModelsToIModelToQuery(toDeleteOrUpdateToNull, model, false);
                                    }
                                }
                            }
                        }
                    }

                    else
                    {
                        this.UpdateResponseLabel(string.Format("Identifierande attribut ({0}) kan ej vara tomt", string.Join(", ", this.identifyingAttributesValues.Keys)));
                    }
                }
            }
        }