/// <summary> /// Save the viewed packaged solution to the database /// </summary> private void SaveCurrentPackagedSolution() { /* IMPORTANT * A packaged solution should not be saved if there exists * a solar collector without a container tied to it. */ using (var ctx = new AssistantContext()) { // Copy visible list of appliance instances to the list in the model var newApplianceInstanceList = AppliancesInPackagedSolution.ToList(); // Mark packaged solution as added if new or modified if old ctx.Entry(PackagedSolution).State = PackagedSolution.Id == 0 ? EntityState.Added : EntityState.Modified; // Mark removed appliance instances as deleted if (PackagedSolution.Id > 0) { var previousPackagedSolution = ctx.PackagedSolutions.Include(a => a.ApplianceInstances).FirstOrDefault(p => p.Id == PackagedSolution.Id); previousPackagedSolution?.ApplianceInstances.ToList().ForEach(instance => { if (newApplianceInstanceList.All(a => a.Id != instance.Id)) { ctx.Entry(instance).State = EntityState.Deleted; } }); } // Attach appliances and appliance instances to avoid duplicates foreach (var appInstance in newApplianceInstanceList) { ctx.Entry(appInstance.Appliance.DataSheet).State = EntityState.Unchanged; ctx.Entry(appInstance.Appliance).State = EntityState.Unchanged; ctx.Entry(appInstance).State = appInstance.Id == 0 ? EntityState.Added : EntityState.Unchanged; } // Set new appliance instance list PackagedSolution.ApplianceInstances = newApplianceInstanceList; // Set the creation date to now if (PackagedSolution.CreationDate == default(DateTime)) { PackagedSolution.CreationDate = DateTime.Now; } // Save database changes ctx.SaveChanges(); } // Set save state to true and notify print function of change IsDataSaved = true; PrintEnergyLabelCmd.NotifyCanExecuteChanged(); }
private void UpdateEei() { PackagedSolution.ApplianceInstances = AppliancesInPackagedSolution.ToList(); PackagedSolution.EnergyLabel.Clear(); PackagedSolution.UpdateEei(); if (PackagedSolution.EnergyLabel != null && PackagedSolution.EnergyLabel.Count > 1) { EeiResultsRoomHeating = PackagedSolution.EnergyLabel[0]; EeiResultsWaterHeating = PackagedSolution.EnergyLabel[1]; } else if (PackagedSolution.EnergyLabel != null && PackagedSolution.EnergyLabel.Count == 1) { EeiResultsRoomHeating = PackagedSolution.EnergyLabel?[0]; EeiResultsWaterHeating = null; } else { EeiResultsRoomHeating = null; EeiResultsWaterHeating = null; } }