private void LoadWidgities() { var blockEntityTypeId = EntityTypeCache.Get(EntityTypeId).Id; var widgityCache = WidgityCache.GetForEntity(blockEntityTypeId, EntityGuid); Widgities = widgityCache.Select(w => w.GetEntity()).OrderBy(w => w.Order).ToList(); WidgityItems = new Dictionary <Guid, List <WidgityItem> >(); EntityTypeId = EntityTypeId; EntityGuid = EntityGuid; Mode = Mode; foreach (var widgity in widgityCache) { var widgityItems = widgity.WidgityItems; WidgityItems[widgity.Guid] = widgityItems.Select(wi => wi.GetEntity()).ToList(); } SaveState(); }
protected void btnSave_Click(object sender, EventArgs e) { RockContext rockContext = new RockContext(); rockContext.WrapTransaction(() => { WidgityTypeService widgityTypeService = new WidgityTypeService(rockContext); AttributeService attributeService = new AttributeService(rockContext); WidgityType widgityType = GetWidgityType(widgityTypeService); if (widgityType.Id == 0) { widgityTypeService.Add(widgityType); } widgityType.Name = tbName.Text; widgityType.Description = tbDescription.Text; widgityType.EnabledLavaCommands = lcCommands.SelectedValue; widgityType.Icon = tbIcon.Text; widgityType.Markdown = ceMarkup.Text; widgityType.HasItems = cbHasItems.Checked; var _ = widgityType.EntityTypes.ToList(); //Attach the entity types to context widgityType.EntityTypes = new EntityTypeService(rockContext).GetByIds(lbEntityTypes.SelectedValuesAsInt).ToList(); widgityType.CategoryId = pCategory.SelectedValueAsId(); rockContext.SaveChanges(); //Widgity Attributes var widgityEntityTypeId = EntityTypeCache.Get(typeof(Widgity)).Id; var actualAttributes = attributeService.Queryable() .Where(a => a.EntityTypeId == widgityEntityTypeId && a.EntityTypeQualifierValue == widgityType.Id.ToString()) .OrderBy(a => a.Order) .ToList(); //Remove deleted attributes foreach (var attribute in actualAttributes) { if (!WidgityAttributes.Where(a => a.Guid == attribute.Guid).Any()) { attributeService.Delete(attribute); } } //Update db from viewstate foreach (var attribute in WidgityAttributes) { if (attribute.Id == 0) { attribute.Order = WidgityAttributes.IndexOf(attribute); attribute.EntityTypeQualifierValue = widgityType.Id.ToString(); attributeService.Add(attribute); } else { var trackedAttribute = actualAttributes.Where(a => a.Guid == attribute.Guid).FirstOrDefault(); trackedAttribute.CopyPropertiesFrom(attribute); foreach (var qualifier in trackedAttribute.AttributeQualifiers) { var value = attribute.AttributeQualifiers.Where(q => q.Key == qualifier.Key).FirstOrDefault(); if (value != null) { qualifier.Value = value.Value; } } trackedAttribute.Order = WidgityAttributes.IndexOf(attribute); } } rockContext.SaveChanges(); //Widgity Item Attributes var widgityItemEntityTypeId = EntityTypeCache.Get(typeof(WidgityItem)).Id; var actualItemAttributes = attributeService.Queryable() .Where(a => a.EntityTypeId == widgityItemEntityTypeId && a.EntityTypeQualifierValue == widgityType.Id.ToString()) .OrderBy(a => a.Order) .ToList(); //Remove deleted attributes foreach (var attribute in actualItemAttributes) { if (!WidgityItemAttributes.Where(a => a.Guid == attribute.Guid).Any()) { attributeService.Delete(attribute); } } //Update db from viewstate foreach (var attribute in WidgityItemAttributes) { if (attribute.Id == 0) { attribute.Order = WidgityItemAttributes.IndexOf(attribute); attribute.EntityTypeQualifierValue = widgityType.Id.ToString(); attributeService.Add(attribute); } else { var trackedAttribute = actualItemAttributes.Where(a => a.Guid == attribute.Guid).FirstOrDefault(); trackedAttribute.CopyPropertiesFrom(attribute); foreach (var qualifier in trackedAttribute.AttributeQualifiers) { var value = attribute.AttributeQualifiers.Where(q => q.Key == qualifier.Key).FirstOrDefault(); if (value != null) { qualifier.Value = value.Value; } } trackedAttribute.Order = WidgityItemAttributes.IndexOf(attribute); } } rockContext.SaveChanges(); }); WidgityTypeCache.Clear(); WidgityCache.Clear(); WidgityItemCache.Clear(); NavigateToParentPage(); }
public void Publish() { if (Page.IsValid) { var widgityRefs = Widgities; var widgityRefIds = widgityRefs.Select(w => w.Id).ToList(); RockContext rockContext = new RockContext(); rockContext.WrapTransaction(() => { WidgityService widgityService = new WidgityService(rockContext); var toRemove = widgityService.Queryable("WidgityType") .Where(w => w.EntityGuid == EntityGuid && w.EntityTypeId == EntityTypeId) .Where(w => !widgityRefIds.Contains(w.Id)) .ToList(); widgityService.DeleteRange(toRemove); List <Widgity> widgities = new List <Widgity>(); foreach (var widgityRef in widgityRefs) { Widgity widgity = widgityService.Get(widgityRef.Id); if (widgity == null) { widgity = widgityRef; widgity.EntityTypeId = EntityTypeId; widgity.EntityGuid = EntityGuid; widgityService.Add(widgity); rockContext.SaveChanges(); } else { widgity.LoadAttributes(); } widgities.Add(widgity); widgity.AttributeValues = widgityRef.AttributeValues; widgity.Order = widgities.IndexOf(widgity); } rockContext.SaveChanges(); foreach (var widgity in widgities) { widgity.SaveAttributeValues(); } //WidgityItems WidgityItemService widgityItemService = new WidgityItemService(rockContext); foreach (var widgity in widgities) { var storedWidgityItems = WidgityItems[widgity.Guid]; var storedWidgityItemGuids = storedWidgityItems.Select(wi => wi.Guid).ToList(); var databaseWidgityItems = widgityItemService.Queryable().Where(wi => wi.WidgityId == widgity.Id).ToList(); //Remove deleted items var itemsToRemove = databaseWidgityItems.Where(wi => !storedWidgityItemGuids.Contains(wi.Guid)).ToList(); widgityItemService.DeleteRange(itemsToRemove); foreach (var item in storedWidgityItems) { if (item.Id == 0) // new { item.Order = storedWidgityItems.IndexOf(item); item.WidgityId = widgity.Id; item.WidgityTypeId = widgity.WidgityTypeId; widgityItemService.Add(item); rockContext.SaveChanges(); item.SaveAttributeValues(); } else // update { var databaseItem = databaseWidgityItems.Where(wi => wi.Guid == item.Guid).FirstOrDefault(); databaseItem.Order = storedWidgityItems.IndexOf(item); databaseItem.LoadAttributes(); databaseItem.AttributeValues = item.AttributeValues; databaseItem.SaveAttributeValues(); rockContext.SaveChanges(); } } } }); } WidgityCache.Clear(); WidgityItemCache.Clear(); HideSettings(); EventHandler <WidgityPubArgs> handler = WidgityPublished; var args = new WidgityPubArgs(); args.WidgityIds = Widgities.Select(w => w.Id).ToList(); handler?.Invoke(this, args); }