//private void ExtendSaveContentTypes(IEnumerable<ContentType> contentTypes) // => Storage.DoWhileQueueingRelationships(() => contentTypes.ToList().ForEach(ExtendSaveContentTypes)); ///// <summary> ///// Import an AttributeSet with all Attributes and AttributeMetaData ///// </summary> //private void ExtendSaveContentTypes(ContentType contentType) //{ // // initialize destinationSet - create or test existing if ok // var foundSet = _dbDeepAccess.ContentType.GetOrCreateContentType(contentType); // if (foundSet == null) // something went wrong, skip this import // return; // var contentTypeId = foundSet.Value; // // append all Attributes // foreach (var newAtt in contentType.Attributes.Cast<AttributeDefinition>()) // { // var destAttribId = _dbDeepAccess.AttributesDefinition.GetOrCreateAttributeDefinition(contentTypeId, newAtt); // // save additional entities containing AttributeMetaData for this attribute // if (newAtt.InternalAttributeMetaData != null) // SaveAttributeMetadata(destAttribId, newAtt.InternalAttributeMetaData); // } // // optionally re-order the attributes if specified in import // if (contentType.OnSaveSortAttributes) // _dbDeepAccess.ContentType.SortAttributes(contentTypeId, contentType); //} ///// <summary> ///// Save additional entities describing the attribute ///// </summary> ///// <param name="attributeId"></param> ///// <param name="metadata"></param> //private void SaveAttributeMetadata(int attributeId, List<Entity> metadata) //{ // var entities = new List<IEntity>(); // foreach (var entity in metadata) // { // var md = (Metadata) entity.Metadata; // // Validate Entity // md.TargetType = Constants.MetadataForAttribute; // // Set KeyNumber // if (attributeId == 0 || attributeId < 0) // < 0 is ef-core temp id // throw new Exception($"trying to add metadata to attribute {attributeId} but attribute isn't saved yet");//_dbDeepAccess.SqlDb.SaveChanges(); // md.KeyNumber = attributeId; // //// Get guid of previously existing assignment - if it exists // //var existingMetadata = _dbDeepAccess.Entities // // .GetAssignedEntities(Constants.MetadataForAttribute, keyNumber: attributeId) // // .FirstOrDefault(e => e.AttributeSetId == attributeId); // //if (existingMetadata != null) // // entity.SetGuid(existingMetadata.EntityGuid); // //entities.Add(CreateMergedForSaving(entity, _entireApp, SaveOptions)); // entities.Add(entity); // } // Storage.Save(entities, SaveOptions.Build(ZoneId)); // don't use the standard save options, as this is attributes only //} private void MergeContentTypeUpdateWithExisting(IContentType contentType) { var existing = _entireApp.ContentTypes.Values.FirstOrDefault(ct => ct.StaticName == contentType.StaticName); if (existing == null) { return; } foreach (var newAttrib in contentType.Attributes) { var existAttrib = existing.Attributes.FirstOrDefault(a => a.Name == newAttrib.Name); if (existAttrib == null) { continue; } var impMeta = ((AttributeDefinition)newAttrib).Items; var newMetaList = new List <IEntity>(); foreach (var newMd in impMeta) { var existingMetadata = _entireApp.GetMetadata(Constants.MetadataForAttribute, existAttrib.AttributeId, newMd.Type.StaticName).FirstOrDefault(); if (existingMetadata == null) { newMetaList.Add(newMd); } else { newMetaList.Add(EntitySaver.CreateMergedForSaving(existingMetadata, newMd, SaveOptions) as Entity); } } ((AttributeDefinition)newAttrib).AddItems(newMetaList); } }
/// <summary> /// Update an entity /// </summary> /// <param name="id"></param> /// <param name="values"></param> public void UpdateParts(int id, Dictionary <string, object> values) { var saveOptions = SaveOptions.Build(_appManager.ZoneId); saveOptions.PreserveUntouchedAttributes = true; saveOptions.PreserveUnknownLanguages = true; var orig = _appManager.Cache.List[id]; var tempEnt = new Entity(_appManager.AppId, 0, "", values); var saveEnt = EntitySaver.CreateMergedForSaving(orig, tempEnt, saveOptions); Save(saveEnt, saveOptions); }
/// <summary> /// Import an Entity with all values /// </summary> private Entity CreateMergedForSaving(Entity update, AppDataPackage appDataPackage, SaveOptions saveOptions) { #region try to get AttributeSet or otherwise cancel & log error var dbAttrSet = appDataPackage.ContentTypes.Values .FirstOrDefault(ct => String.Equals(ct.StaticName, update.Type.StaticName, StringComparison.InvariantCultureIgnoreCase)); if (dbAttrSet == null) // AttributeSet not Found { Storage.Log.Add(new LogItem(EventLogEntryType.Error, "ContentType not found for " + update.Type.StaticName)); return(null); } #endregion // Find existing Enties - meaning both draft and non-draft List <IEntity> existingEntities = null; if (update.EntityGuid != Guid.Empty) { existingEntities = appDataPackage.List.Where(e => e.EntityGuid == update.EntityGuid).ToList(); } #region Simplest case - nothing existing to update: return entity if (existingEntities == null || !existingEntities.Any()) { return(update); } #endregion Storage.Log.Add(new LogItem(EventLogEntryType.Information, $"FYI: Entity {update.EntityId} already exists for guid {update.EntityGuid}")); // now update (main) entity id from existing - since it already exists var original = existingEntities.First(); update.ChangeIdForSaving(original.EntityId); return(EntitySaver.CreateMergedForSaving(original, update, saveOptions) as Entity); }