Ejemplo n.º 1
0
        public virtual T Save <T>(T entity, string includes) where T : class, IContent
        {
            bool save       = false;
            var  entityType = entity.GetType();
            var  content    = entity as IContent;

            var      isNew         = content.EntityId == Guid.Empty || !this.DataSource.Query(entityType).Where("EntityId.Equals(@0)", content.EntityId).Any();
            var      currentUserId = StrixPlatform.User.Id;
            IContent theContent    = null;

            if (isNew)
            {
                var fixedUrl  = content.Entity.Url;
                var sortOrder = content.SortOrder;
                var newEntity = CreateEntity(entityType, currentUserId);
                theContent = this.CreateNewContent(entityType, content, newEntity, currentUserId);
                MapContent(entityType, content, theContent);

                if (sortOrder == 0)
                {
                    theContent.SortOrder = this.GetNextSortOrder(entityType);
                }

                theContent.Entity.Url = UrlHelpers.CreateUniqueUrl(this.DataSource.Query(entityType), !string.IsNullOrWhiteSpace(fixedUrl) ? fixedUrl : theContent.Name, theContent.EntityId, "Entity.Url", "EntityId");
                save = true;
            }
            else
            {
                if (includes == null || !includes.ToLower().Contains("entity"))
                {
                    if (string.IsNullOrWhiteSpace(includes))
                    {
                        includes = "Entity";
                    }
                    else
                    {
                        includes = includes + ", Entity";
                    }
                }

                // Check whether there is content for this culture already.
                theContent = this.Get(entityType, content.EntityId, content.Culture, 0, includes);

                // Check whether the current user is allowed to edit this item.
                CheckCanEditOrDelete(theContent);

                if (theContent == null)
                {
                    theContent = this.TranslateContent(entityType, content);
                    save       = true;
                }
                else
                {
                    var publishedDate = content.PublishedOn;

                    MapContent(entityType, content, theContent);

                    if (publishedDate.HasValue)
                    {
                        theContent.PublishedOn = publishedDate;
                    }

                    theContent.UpdatedByUserId = StrixPlatform.User.Id;
                    theContent.UpdatedOn       = DateTime.Now;

                    if (EntityHelper.IsServiceActive(entityType, EntityServiceActions.UpdatePaths))
                    {
                        theContent.Entity.Url = UrlHelpers.CreateUniqueUrl(this.DataSource.Query(entityType), theContent.Name, theContent.EntityId, "Entity.Url", "EntityId");
                    }

                    var modifiedProperties = this.DataSource.GetModifiedPropertyValues(theContent).Where(p => !PropertiesToIgnoreForVersioning.Contains(p.PropertyName)).ToArray();

                    if (!modifiedProperties.IsEmpty())
                    {
                        theContent = this.CreateNewVersion(entityType, theContent, modifiedProperties);
                        save       = true;
                    }
                    else
                    {
                        return(theContent as T);
                    }
                }
            }

            if (!theContent.IsValid)
            {
                throw new StrixValidationException();
            }

            // Todo: remove when implemented publishing.
            theContent.PublishedOn       = theContent.CreatedOn;
            theContent.PublishedByUserId = theContent.CreatedByUserId;

            if (save)
            {
                this.DataSource.Save(theContent);
            }

            return(theContent as T);
        }