// //Get all of the widgets for an organization public JsonResult GetWidgetsForOrganization(string organizationCTID) { //Get data var widgets = WidgetServices.GetWidgetsForOrganization(organizationCTID); //Extract var results = new List <WidgetV2>(); foreach (var widget in widgets) { var result = new WidgetV2(); try { //Extract the JSON data if it is V2 data result = JsonConvert.DeserializeObject <WidgetV2>(widget.CustomStyles, new JsonSerializerSettings() { Error = IgnoreDeserializationErrors }); if (result.Id == 0) { throw new Exception(); } } catch { //Try to convert old data to new format result = V1toV2(widget); } if (result.Id == 44) { } results.Add(result); } return(JsonResponse(results, true, "okay", null)); }
// private Widget V2toV1(WidgetV2 v2) { if (v2.RowId == Guid.Empty) { v2.RowId = Guid.NewGuid(); } //NOTE: all pertinent filters are stored in CustomStyles, not clear of use of SimpleMap?? var v1 = SimpleMap <Widget>(v2); v1.WidgetAlias = v2.UrlName; v1.OrgCTID = v2.OrganizationCTID; v1.OrganizationName = v2.OrganizationName; v1.CountryFilters = string.Join(",", v2.Locations.Countries ?? new List <string>()); v1.RegionFilters = string.Join(",", v2.Locations.Regions ?? new List <string>()); v1.CityFilters = string.Join(",", v2.Locations.Cities ?? new List <string>()); v1.OwningOrganizationIds = string.Join(",", v2.CredentialFilters.OwnedBy); v1.IncludeIfAvailableOnline = v2.Locations.IsAvailableOnline; v1.WidgetStylesUrl = v2.CustomCssUrl; v1.SearchFilters = v2.CustomJSON; v1.LogoFileName = v2.LogoFileName; //Handle file updates if (v2.LogoImage != null) { try { //Delete flagged file(s) foreach (var toDelete in v2.LogoImage.Deletes) { var match = v2.LogoImage.Files.FirstOrDefault(m => m.RowId == toDelete); if (match != null) { v2.LogoImage.Files.Remove(match); } FileReferenceServices.DeleteFile(v2.RowId, "png"); } //Save/update new/existing file(s) FileReferenceServices.SaveImageReference(v2.LogoImage.Files.FirstOrDefault(), v2.RowId, 500 * 1000, true, 500, 500); } catch (Exception ex) { //messages.Add( "Error processing files: " + ex.Message ); } } //Important, and one-way v1.CustomStyles = JsonConvert.SerializeObject(v2); return(v1); }
// public JsonResult SaveWidgetV2() { //Manually bind large JSON document var model = Helpers.BindJsonModel <WidgetV2>(Request.InputStream, "data"); //Convert var toSave = V2toV1(model); //Save the data var result = new WidgetV2(); var messages = new List <string>(); if (!string.IsNullOrWhiteSpace(toSave.WidgetAlias)) { var widgetExisting = WidgetServices.GetByAlias(toSave.WidgetAlias); if (widgetExisting != null && widgetExisting.Id > 0 && widgetExisting.Id != toSave.Id) { messages.Add("Widget Alias already exist"); return(JsonResponse(null, false, "error", messages)); } } if (!new WidgetServices().Save(toSave, ref messages)) { LoggingHelper.DoTrace(5, "WidgetController.SaveWidgetV2. Errors on save: " + string.Join("\n\r>", messages)); return(JsonResponse(null, false, "error", messages)); } //Copy key properties from newly-saved widget (such as ID) to the V2 widget var saved = WidgetServices.Get(toSave.Id); result = JsonConvert.DeserializeObject <WidgetV2>(saved.CustomStyles); result.Created = saved.Created; result.LastUpdated = saved.LastUpdated; result.RowId = saved.RowId; result.Id = saved.Id; result.CreatedById = saved.CreatedById; result.LastUpdatedById = saved.LastUpdatedById; //SimpleUpdate( saved, result ); //result.UrlName = saved.WidgetAlias; //Save again to ensure the V2 data has the correct key properties saved.CustomStyles = JsonConvert.SerializeObject(result); new WidgetServices().Save(saved, ref messages); return(JsonResponse(result, true, "okay", null)); }
// public JsonResult PreviewWithoutSaving(WidgetV2 data) { Session.Remove("PreviewSearchWidget"); Session.Add("PreviewSearchWidget", JsonConvert.SerializeObject(data)); return(JsonResponse(null, true, "okay", null)); }