Exemplo n.º 1
0
        public Term CreateTag(string vocabularyName, string tagName, string culture)
        {
            if (string.IsNullOrWhiteSpace(vocabularyName) || string.IsNullOrWhiteSpace(tagName))
            {
                throw new ArgumentException("Please supply both a vocabulary name and tag name");
            }

            if (string.IsNullOrWhiteSpace(culture))
            {
                culture = StrixPlatform.DefaultCultureCode;
            }

            var term = this._dataSource.Query <Term>().FirstOrDefault(GetTermFunc(vocabularyName, tagName, culture));

            if (term != null)
            {
                return(term);
            }

            // Create the term if it does not exist yet.
            var vocabulary = this.GetVocabulary(vocabularyName, culture);
            var id         = Guid.NewGuid();

            term = new Term
            {
                Id           = id,
                Name         = tagName,
                Url          = UrlHelpers.CreateUniqueUrl(this._dataSource.Query <Term>(), tagName, id),
                VocabularyId = vocabulary.Id
            };

            this._dataSource.Save(term);
            vocabulary.Terms.Add(term);

            return(term);
        }
Exemplo n.º 2
0
        public Vocabulary CreateVocabulary(string vocabularyName, string culture)
        {
            if (string.IsNullOrWhiteSpace(vocabularyName))
            {
                throw new ArgumentNullException("vocabularyName");
            }

            if (string.IsNullOrWhiteSpace(culture))
            {
                culture = StrixPlatform.DefaultCultureCode;
            }

            var vocabulary = this._dataSource.Query <Vocabulary>().FirstOrDefault(GetVocabularyFunc(vocabularyName, culture));

            if (vocabulary != null)
            {
                return(vocabulary);
            }

            var isSystem = Enum.GetNames(typeof(CoreVocabulary)).ToLower().Contains(vocabularyName.ToLower());
            var id       = Guid.NewGuid();

            vocabulary = new Vocabulary
            {
                Id                 = id,
                GroupId            = StrixPlatform.User.GroupId,
                Culture            = culture,
                Name               = vocabularyName,
                Url                = UrlHelpers.CreateUniqueUrl(this._dataSource.Query <Vocabulary>(), vocabularyName, id),
                IsSystemVocabulary = isSystem,
                Terms              = new List <Term>(),
            };

            this._dataSource.Save(vocabulary);
            return(vocabulary);
        }
Exemplo n.º 3
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);
        }