public EditPlace() { EditPlaceModel context = new EditPlaceModel(); ClearEntries(context); context.ListPlace = new ObservableCollection <Place>(); this.DataContext = context; GetPlace(); InitializeComponent(); }
public ActionResult Edit(Guid id, Guid?versionId) { try { var place = this.entityService.Get <Place>(id); if (place == null) { TempData["error"] = Locale.PlaceNotFound; return(RedirectToAction("Index")); } if (place.Tags.Any(t => t.TagKey == Constants.ImportedDataTag && t.Value?.ToLower() == "true")) { this.TempData["warning"] = Locale.RecordMustBeVerifiedBeforeEditing; return(RedirectToAction("ViewPlace", new { id, versionId })); } var relationships = new List <EntityRelationship>(); // get relationships where I am the source of type parent relationships.AddRange(entityRelationshipService.GetEntityRelationshipsBySource(place.Key.Value, EntityRelationshipTypeKeys.Parent)); // get relationships where I am the target of type dedicated service delivery location relationships.AddRange(entityRelationshipService.GetEntityRelationshipsByTarget(place.Key.Value, EntityRelationshipTypeKeys.DedicatedServiceDeliveryLocation)); // get relationships where I am the target of type parent relationships.AddRange(entityRelationshipService.GetEntityRelationshipsByTarget(place.Key.Value, EntityRelationshipTypeKeys.Parent)); // set the relationships where I am the parent to relationship type of child // this is for display purposes only foreach (var relationship in relationships.Where(r => r.RelationshipTypeKey == EntityRelationshipTypeKeys.Parent && r.TargetEntityKey == place.Key)) { relationship.RelationshipType = this.conceptService.GetConcept(EntityRelationshipTypeKeys.Child, true); } place.Relationships = relationships.Intersect(place.Relationships, new EntityRelationshipComparer()).ToList(); var model = new EditPlaceModel(place) { TypeConcepts = this.placeConceptService.GetPlaceTypeConcepts(healthFacilityMnemonic, placeTypeMnemonic).ToSelectList(this.HttpContext.GetCurrentLanguage(), t => t.Key == place.TypeConceptKey).ToList(), UpdatedBy = this.GetUserEntityBySecurityUserKey(place.CreatedByKey.Value)?.GetFullName(NameUseKeys.OfficialRecord) }; return(View(model)); } catch (Exception e) { Trace.TraceError($"Unable to retrieve place: { e }"); this.TempData["error"] = Locale.UnexpectedErrorMessage; } return(RedirectToAction("Index")); }
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) { EditPlaceModel context = this.DataContext as EditPlaceModel; if (!string.IsNullOrWhiteSpace(context.City) || !string.IsNullOrWhiteSpace(context.State) || !string.IsNullOrWhiteSpace(context.Country)) { string message = Application.Current.FindResource("EditPlace.CodeBehind.WarningClose.Message").ToString(); string caption = Application.Current.FindResource("EditPlace.CodeBehind.WarningClose.Caption").ToString(); MessageBoxResult result = CustomMessageBox.ShowYesNo(message, caption, CustomMessageBoxButton.Yes, CustomMessageBoxButton.No, MessageBoxImage.Warning); //MessageBoxResult result = MessageBox.Show(message, caption, MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.No); e.Cancel = result == MessageBoxResult.No; } }
public IActionResult EditPlace(EditPlaceInfo placeInfo) { var place = _mapper.Map <PlaceInfo>(placeInfo); var result = _placesService.UpdatePlace(place); if (!result.Success) { var model = new EditPlaceModel(); FillBaseModel(model); model.PlaceInfo = placeInfo; return(View(model)); } return(RedirectToAction("Places", new { id = placeInfo.Id })); }
public IActionResult EditPlace(int id) { var maybePlace = _placesService.GetPlace(id); if (maybePlace.Success) { var model = new EditPlaceModel(); model.PlaceInfo = _mapper.Map <EditPlaceInfo>(maybePlace.Value); FillBaseModel(model); return(View(model)); } else { return(RedirectToAction("Places")); } }
/// <summary> /// Provide edit function for the selected place /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void PlaceList_SelectionChanged(object sender, SelectionChangedEventArgs e) { EditPlaceModel context = this.DataContext as EditPlaceModel; Place place = PlaceList.SelectedItem as Place; if (place != null) { context.LabelCity = Application.Current.FindResource("EditPlace.Label.EditCity").ToString(); context.LabelState = Application.Current.FindResource("EditPlace.Label.EditState").ToString(); context.LabelCountry = Application.Current.FindResource("EditPlace.Label.EditCountry").ToString(); context.Id = place.Id; context.City = place.City; context.State = place.State; context.Country = place.Country; } }
private void ClearEntries(EditPlaceModel context) { PropertyInfo[] properties = context.GetType().GetProperties(); foreach (var propertyInfo in properties) { if (propertyInfo.PropertyType == typeof(string)) { propertyInfo.SetValue(context, string.Empty, null); } if (propertyInfo.PropertyType == typeof(int)) { propertyInfo.SetValue(context, 0, null); } } context.LabelCity = Application.Current.FindResource("EditPlace.Label.City").ToString(); context.LabelState = Application.Current.FindResource("EditPlace.Label.State").ToString(); context.LabelCountry = Application.Current.FindResource("EditPlace.Label.Country").ToString(); CommandManager.InvalidateRequerySuggested(); }
private void GetPlace() { EditPlaceModel context = this.DataContext as EditPlaceModel; context.ListPlace.Clear(); SqlMethods.SqlConnect(out SQLiteConnection con); var selectCommand = con.CreateCommand(); selectCommand.CommandText = "SELECT Id, City, State, Country FROM Places ORDER BY City"; SQLiteDataReader r = selectCommand.ExecuteReader(); while (r.Read()) { Place place = new Place(); int tempNum; int.TryParse(Convert.ToString(r["Id"]), out tempNum); place.Id = tempNum; place.City = Convert.ToString(r["City"]); place.State = Convert.ToString(r["State"]); place.Country = Convert.ToString(r["Country"]); context.ListPlace.Add(place); } r.Close(); }
public ActionResult Edit(EditPlaceModel model) { try { if (model.HasOnlyYearOrPopulation()) { if (string.IsNullOrWhiteSpace(model.Year)) { ModelState.AddModelError("Year", Locale.TargetYearRequired); } if (model.TargetPopulation == null) { ModelState.AddModelError("TargetPopulation", Locale.TargetPopulationRequired); } } if (!string.IsNullOrWhiteSpace(model.Year)) { if (model.ConvertToPopulationYear() == 0) { ModelState.AddModelError("Year", Locale.PopulationYearInvalidFormat); } } if (ModelState.IsValid) { var bundle = this.ImsiClient.Query <Place>(p => p.Key == model.Id && p.ObsoletionTime == null, 0, null, true); bundle.Reconstitute(); var place = bundle.Item.OfType <Place>().FirstOrDefault(p => p.Key == model.Id && p.ObsoletionTime == null); if (place == null) { TempData["error"] = Locale.PlaceNotFound; return(RedirectToAction("Index")); } // repopulate incase the update fails model.Identifiers = place.Identifiers.Select(i => new EntityIdentifierModel(i.Key.Value, place.Key.Value)).ToList(); model.Relationships = place.Relationships.Select(r => new EntityRelationshipModel(r)).ToList(); model.TypeConcepts = this.placeConceptService.GetPlaceTypeConcepts(healthFacilityMnemonic, placeTypeMnemonic).ToSelectList(this.HttpContext.GetCurrentLanguage(), t => t.Key == place.TypeConceptKey).ToList(); var placeToUpdate = model.ToPlace(place); if (model.SubmitYearAndPopulation()) { placeToUpdate.Extensions.RemoveAll(e => e.ExtensionType.Name == Constants.TargetPopulationUrl); var entityExtension = new EntityExtension { ExtensionType = new ExtensionType(Constants.TargetPopulationUrl, typeof(DictionaryExtensionHandler)) { Key = Constants.TargetPopulationExtensionTypeKey }, ExtensionValue = new TargetPopulation(model.ConvertPopulationToULong(), model.ConvertToPopulationYear()) }; placeToUpdate.Extensions.Add(entityExtension); } placeToUpdate.CreatedByKey = Guid.Parse(this.User.Identity.GetUserId()); var updatedPlace = this.UpdateEntity <Place>(placeToUpdate); TempData["success"] = Locale.PlaceSuccessfullyUpdated; return(RedirectToAction("ViewPlace", new { id = updatedPlace.Key, versionId = updatedPlace.VersionKey })); } } catch (Exception e) { Trace.TraceError($"Unable to update place: {e}"); } this.TempData["error"] = Locale.UnableToUpdatePlace; return(View(model)); }
private void SavePlaceCommand_Executed(object sender, ExecutedRoutedEventArgs e) { EditPlaceModel context = this.DataContext as EditPlaceModel; SqlMethods.SqlConnect(out SQLiteConnection con); SQLiteCommand selectCommand = con.CreateCommand(); selectCommand.CommandText = $"SELECT City FROM Places WHERE City=@City"; selectCommand.Parameters.AddWithValue("City", context.City); SQLiteDataReader r = selectCommand.ExecuteReader(); selectCommand.Parameters.Clear(); if (!r.Read()) { // to add new place if (context.Id == 0) { SQLiteCommand insertCommand = con.CreateCommand(); if (!string.IsNullOrWhiteSpace(context.State) && !string.IsNullOrWhiteSpace(context.Country)) { insertCommand.CommandText = "INSERT INTO Places (City,State,Country) VALUES (@City,@State,@Country)"; insertCommand.Parameters.AddWithValue("City", context.City); insertCommand.Parameters.AddWithValue("State", context.State); insertCommand.Parameters.AddWithValue("Country", context.Country); } else if (!string.IsNullOrWhiteSpace(context.State) && string.IsNullOrWhiteSpace(context.Country)) { insertCommand.CommandText = "INSERT INTO Places (City,State) VALUES (@City,@State)"; insertCommand.Parameters.AddWithValue("City", context.City); insertCommand.Parameters.AddWithValue("State", context.State); } else if (string.IsNullOrWhiteSpace(context.State) && !string.IsNullOrWhiteSpace(context.Country)) { insertCommand.CommandText = "INSERT INTO Places (City,Country) VALUES (@City,@Country)"; insertCommand.Parameters.AddWithValue("City", context.City); insertCommand.Parameters.AddWithValue("Country", context.Country); } else { insertCommand.CommandText = "INSERT INTO Places (City) VALUES (@City)"; insertCommand.Parameters.AddWithValue("City", context.City); } insertCommand.ExecuteNonQuery(); insertCommand.Parameters.Clear(); } // to update place's State and country else { SQLiteCommand updateCommand = con.CreateCommand(); updateCommand.CommandText = $"UPDATE Places SET City=@City, State=@State, Country=Country WHERE Id=@Id"; updateCommand.Parameters.AddWithValue("Country", context.Country); updateCommand.Parameters.AddWithValue("State", context.State); updateCommand.Parameters.AddWithValue("City", context.City); updateCommand.Parameters.AddWithValue("Id", context.Id); updateCommand.ExecuteNonQuery(); updateCommand.Parameters.Clear(); } } else { // to update place's city if (context.Id != 0) { SQLiteCommand updateCommand = con.CreateCommand(); updateCommand.CommandText = $"UPDATE Places SET City=@City, State=@State, Country=@Country WHERE Id=@Id"; updateCommand.Parameters.AddWithValue("Country", context.Country); updateCommand.Parameters.AddWithValue("State", context.State); updateCommand.Parameters.AddWithValue("City", context.City); updateCommand.Parameters.AddWithValue("Id", context.Id); updateCommand.ExecuteNonQuery(); updateCommand.Parameters.Clear(); } else { string message = Application.Current.FindResource("EditPlace.CodeBehind.ErrorSave.Message").ToString(); string caption = Application.Current.FindResource("EditPlace.CodeBehind.ErrorSave.Caption").ToString(); MessageBoxResult result = CustomMessageBox.ShowOK(message, caption, CustomMessageBoxButton.OK, MessageBoxImage.Error); //MessageBoxResult result = MessageBox.Show(message, caption, MessageBoxButton.OK, MessageBoxImage.Error); } } r.Close(); con.Close(); ClearEntries(context); GetPlace(); }
private void SavePlaceCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e) { EditPlaceModel context = this.DataContext as EditPlaceModel; e.CanExecute = !string.IsNullOrWhiteSpace(context.City); }