Exemple #1
0
        /// <summary>
        /// Import AttributeSets and Entities
        /// </summary>
        public void ImportIntoDb(IEnumerable <ContentType> newAttributeSets, IEnumerable <Entity> newEntities)
        {
            Storage.DoWithDelayedCacheInvalidation(() =>
            {
                #region initialize DB connection / transaction

                #endregion

                // run import, but rollback transaction if necessary
                Storage.DoInTransaction(() =>
                {
                    // get initial data state for further processing, content-typed definitions etc.
                    // important: must always create a new loader, because it will cache content-types which hurts the import
                    #region import AttributeSets if any were included

                    if (newAttributeSets != null)
                    {
                        Storage.DoWhileQueuingVersioning(() =>
                        {
                            _entireApp      = Storage.Loader.AppPackage(AppId); // load everything, as content-type metadata is normal entities
                            var newSetsList = newAttributeSets.ToList();
                            // first: import the attribute sets in the system scope, as they may be needed by others...
                            // ...and would need a cache-refresh before
                            var sysAttributeSets = newSetsList.Where(a => a.Scope == Constants.ScopeSystem).ToList();
                            if (sysAttributeSets.Any())
                            {
                                MergeAndSaveContentTypes(sysAttributeSets);
                            }

                            _entireApp = Storage.Loader.AppPackage(AppId); // load everything, as content-type metadata is normal entities

                            // now the remaining attributeSets
                            var nonSysAttribSets = newSetsList.Where(a => !sysAttributeSets.Contains(a)).ToList();
                            if (nonSysAttribSets.Any())
                            {
                                MergeAndSaveContentTypes(nonSysAttribSets);
                            }
                        });
                    }

                    #endregion

                    #region import Entities

                    if (newEntities != null)
                    {
                        _entireApp  = Storage.Loader.AppPackage(AppId); // load all entities
                        newEntities = newEntities
                                      .Select(entity => CreateMergedForSaving(entity, _entireApp, SaveOptions))
                                      .Where(e => e != null).ToList();
                        Storage.Save(newEntities.Cast <IEntity>().ToList(), SaveOptions);
                    }

                    #endregion
                });
            });
        }
Exemple #2
0
        /// <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);
        }