private static void MapHistoryCleanup(DocumentTypeSave source, IContentTypeWithHistoryCleanup target)
        {
            // If source history cleanup is null we don't have to map all properties
            if (source.HistoryCleanup is null)
            {
                target.HistoryCleanup = null;
                return;
            }

            // We need to reset the dirty properties, because it is otherwise true, just because the json serializer has set properties
            target.HistoryCleanup !.ResetDirtyProperties(false);
            if (target.HistoryCleanup.PreventCleanup != source.HistoryCleanup.PreventCleanup)
            {
                target.HistoryCleanup.PreventCleanup = source.HistoryCleanup.PreventCleanup;
            }

            if (target.HistoryCleanup.KeepAllVersionsNewerThanDays != source.HistoryCleanup.KeepAllVersionsNewerThanDays)
            {
                target.HistoryCleanup.KeepAllVersionsNewerThanDays = source.HistoryCleanup.KeepAllVersionsNewerThanDays;
            }

            if (target.HistoryCleanup.KeepLatestVersionPerDayForDays !=
                source.HistoryCleanup.KeepLatestVersionPerDayForDays)
            {
                target.HistoryCleanup.KeepLatestVersionPerDayForDays = source.HistoryCleanup.KeepLatestVersionPerDayForDays;
            }
        }
        public ActionResult <DocumentTypeDisplay> PostSave(DocumentTypeSave contentTypeSave)
        {
            //Before we send this model into this saving/mapping pipeline, we need to do some cleanup on variations.
            //If the doc type does not allow content variations, we need to update all of it's property types to not allow this either
            //else we may end up with ysods. I'm unsure if the service level handles this but we'll make sure it is updated here
            if (!contentTypeSave.AllowCultureVariant)
            {
                foreach (var prop in contentTypeSave.Groups.SelectMany(x => x.Properties))
                {
                    prop.AllowCultureVariant = false;
                }
            }

            var savedCt = PerformPostSave <DocumentTypeDisplay, DocumentTypeSave, PropertyTypeBasic>(
                contentTypeSave:    contentTypeSave,
                getContentType:     i => _contentTypeService.Get(i),
                saveContentType:    type => _contentTypeService.Save(type),
                beforeCreateNew:    ctSave =>
            {
                //create a default template if it doesn't exist -but only if default template is == to the content type
                if (ctSave.DefaultTemplate.IsNullOrWhiteSpace() == false && ctSave.DefaultTemplate == ctSave.Alias)
                {
                    var template = CreateTemplateForContentType(ctSave.Alias, ctSave.Name);

                    // If the alias has been manually updated before the first save,
                    // make sure to also update the first allowed template, as the
                    // name will come back as a SafeAlias of the document type name,
                    // not as the actual document type alias.
                    // For more info: http://issues.umbraco.org/issue/U4-11059
                    if (ctSave.DefaultTemplate != template.Alias)
                    {
                        var allowedTemplates = ctSave.AllowedTemplates.ToArray();
                        if (allowedTemplates.Any())
                        {
                            allowedTemplates[0] = template.Alias;
                        }
                        ctSave.AllowedTemplates = allowedTemplates;
                    }

                    //make sure the template alias is set on the default and allowed template so we can map it back
                    ctSave.DefaultTemplate = template.Alias;
                }
            });

            if (!(savedCt.Result is null))
            {
                return(savedCt.Result);
            }

            var display = _umbracoMapper.Map <DocumentTypeDisplay>(savedCt.Value);


            display.AddSuccessNotification(
                _localizedTextService.Localize("speechBubbles", "contentTypeSavedHeader"),
                string.Empty);

            return(display);
        }
Esempio n. 3
0
        // no MapAll - take care
        private void Map(DocumentTypeSave source, IContentType target, MapperContext context)
        {
            MapSaveToTypeBase <DocumentTypeSave, PropertyTypeBasic>(source, target, context);
            MapComposition(source, target, alias => _contentTypeService.Get(alias));

            target.AllowedTemplates = source.AllowedTemplates
                                      .Where(x => x != null)
                                      .Select(_fileService.GetTemplate)
                                      .Where(x => x != null)
                                      .ToArray();

            target.SetDefaultTemplate(source.DefaultTemplate == null ? null : _fileService.GetTemplate(source.DefaultTemplate));
        }
Esempio n. 4
0
        public void Can_Perform_Add_On_ContentTypeRepository_After_Model_Mapping()
        {
            // Arrange
            IScopeProvider provider = ScopeProvider;

            using (IScope scope = provider.CreateScope())
            {
                ContentTypeRepository repository = ContentTypeRepository;

                // Act
                var contentType = (IContentType)ContentTypeBuilder.CreateSimpleContentType2("test", "Test", propertyGroupAlias: "testGroup", propertyGroupName: "testGroup");

                Assert.AreEqual(4, contentType.PropertyTypes.Count());

                // remove all templates - since they are not saved, they would break the (!) mapping code
                contentType.AllowedTemplates = new ITemplate[0];

                // there is NO mapping from display to contentType, but only from save
                // to contentType, so if we want to test, let's to it properly!
                DocumentTypeDisplay display = Mapper.Map <DocumentTypeDisplay>(contentType);
                DocumentTypeSave    save    = MapToContentTypeSave(display);
                IContentType        mapped  = Mapper.Map <IContentType>(save);

                Assert.AreEqual(4, mapped.PropertyTypes.Count());

                repository.Save(mapped);

                Assert.AreEqual(4, mapped.PropertyTypes.Count());

                // re-get
                contentType = repository.Get(mapped.Id);

                Assert.AreEqual(4, contentType.PropertyTypes.Count());

                // Assert
                Assert.That(contentType.HasIdentity, Is.True);
                Assert.That(contentType.PropertyGroups.All(x => x.HasIdentity), Is.True);
                Assert.That(contentType.PropertyTypes.All(x => x.HasIdentity), Is.True);
                Assert.That(contentType.Path.Contains(","), Is.True);
                Assert.That(contentType.SortOrder, Is.GreaterThan(0));

                Assert.That(contentType.PropertyGroups.ElementAt(0).Name == "testGroup", Is.True);
                int groupId = contentType.PropertyGroups.ElementAt(0).Id;

                IPropertyType[] propertyTypes = contentType.PropertyTypes.ToArray();
                Assert.AreEqual("gen", propertyTypes[0].Alias); // just to be sure
                Assert.IsNull(propertyTypes[0].PropertyGroupId);
                Assert.IsTrue(propertyTypes.Skip(1).All(x => x.PropertyGroupId.Value == groupId));
                Assert.That(propertyTypes.Single(x => x.Alias == "title").LabelOnTop, Is.True);
            }
        }
        public DocumentTypeDisplay PostSave(DocumentTypeSave contentTypeSave)
        {
            var savedCt = PerformPostSave <IContentType, DocumentTypeDisplay, DocumentTypeSave, PropertyTypeBasic>(
                contentTypeSave:    contentTypeSave,
                getContentType:     i => Services.ContentTypeService.GetContentType(i),
                saveContentType:    type => Services.ContentTypeService.Save(type),
                beforeCreateNew:    ctSave =>
            {
                //create a default template if it doesnt exist -but only if default template is == to the content type
                if (ctSave.DefaultTemplate.IsNullOrWhiteSpace() == false && ctSave.DefaultTemplate == ctSave.Alias)
                {
                    var template = CreateTemplateForContentType(ctSave.Alias, ctSave.Name);

                    // If the alias has been manually updated before the first save,
                    // make sure to also update the first allowed template, as the
                    // name will come back as a SafeAlias of the document type name,
                    // not as the actual document type alias.
                    // For more info: http://issues.umbraco.org/issue/U4-11059
                    if (ctSave.DefaultTemplate != template.Alias)
                    {
                        var allowedTemplates = ctSave.AllowedTemplates.ToArray();
                        if (allowedTemplates.Any())
                        {
                            allowedTemplates[0] = template.Alias;
                        }
                        ctSave.AllowedTemplates = allowedTemplates;
                    }

                    //make sure the template alias is set on the default and allowed template so we can map it back
                    ctSave.DefaultTemplate = template.Alias;
                }
            });

            var display = Mapper.Map <DocumentTypeDisplay>(savedCt);

            display.AddSuccessNotification(
                Services.TextService.Localize("speechBubbles/contentTypeSavedHeader"),
                string.Empty);

            return(display);
        }
    // no MapAll - take care
    private void Map(DocumentTypeSave source, DocumentTypeDisplay target, MapperContext context)
    {
        MapTypeToDisplayBase <DocumentTypeSave, PropertyTypeBasic, DocumentTypeDisplay, PropertyTypeDisplay>(
            source,
            target,
            context);

        // sync templates
        IEnumerable <string?> destAllowedTemplateAliases = target.AllowedTemplates.Select(x => x.Alias);

        // if the dest is set and it's the same as the source, then don't change
        if (source.AllowedTemplates is not null &&
            destAllowedTemplateAliases.SequenceEqual(source.AllowedTemplates) == false)
        {
            IEnumerable <ITemplate>?templates = _fileService.GetTemplates(source.AllowedTemplates.ToArray());
            target.AllowedTemplates = source.AllowedTemplates
                                      .Select(x =>
            {
                ITemplate?template = templates?.SingleOrDefault(t => t.Alias == x);
                return(template != null
                        ? context.Map <EntityBasic>(template)
                        : null);
            })
                                      .WhereNotNull()
                                      .ToArray();
        }

        if (source.DefaultTemplate.IsNullOrWhiteSpace() == false)
        {
            // if the dest is set and it's the same as the source, then don't change
            if (target.DefaultTemplate == null || source.DefaultTemplate != target.DefaultTemplate.Alias)
            {
                ITemplate?template = _fileService.GetTemplate(source.DefaultTemplate);
                target.DefaultTemplate = template == null ? null : context.Map <EntityBasic>(template);
            }
        }
        else
        {
            target.DefaultTemplate = null;
        }
    }
        public DocumentTypeDisplay PostSave(DocumentTypeSave contentTypeSave)
        {
            var savedCt = PerformPostSave <IContentType, DocumentTypeDisplay, DocumentTypeSave, PropertyTypeBasic>(
                contentTypeSave:    contentTypeSave,
                getContentType:     i => Services.ContentTypeService.GetContentType(i),
                saveContentType:    type => Services.ContentTypeService.Save(type),
                beforeCreateNew:    ctSave =>
            {
                //create a default template if it doesnt exist -but only if default template is == to the content type
                if (ctSave.DefaultTemplate.IsNullOrWhiteSpace() == false && ctSave.DefaultTemplate == ctSave.Alias)
                {
                    var template = Services.FileService.GetTemplate(ctSave.Alias);
                    if (template == null)
                    {
                        var tryCreateTemplate = Services.FileService.CreateTemplateForContentType(ctSave.Alias, ctSave.Name);
                        if (tryCreateTemplate == false)
                        {
                            Logger.Warn <ContentTypeController>(
                                "Could not create a template for the Content Type: {0}, status: {1}",
                                () => ctSave.Alias,
                                () => tryCreateTemplate.Result.StatusType);
                        }
                        template = tryCreateTemplate.Result.Entity;
                    }

                    //make sure the template alias is set on the default and allowed template so we can map it back
                    ctSave.DefaultTemplate = template.Alias;
                }
            });

            var display = Mapper.Map <DocumentTypeDisplay>(savedCt);

            display.AddSuccessNotification(
                Services.TextService.Localize("speechBubbles/contentTypeSavedHeader"),
                string.Empty);

            return(display);
        }
Esempio n. 8
0
        public void Can_Perform_Update_On_ContentTypeRepository_After_Model_Mapping()
        {
            // Arrange
            IScopeProvider provider = ScopeProvider;

            using (IScope scope = provider.CreateScope())
            {
                ContentTypeRepository repository = ContentTypeRepository;

                // Act
                IContentType contentType = repository.Get(_textpageContentType.Id);

                // there is NO mapping from display to contentType, but only from save
                // to contentType, so if we want to test, let's to it properly!
                DocumentTypeDisplay display = Mapper.Map <DocumentTypeDisplay>(contentType);
                DocumentTypeSave    save    = MapToContentTypeSave(display);

                // modify...
                save.Thumbnail = "Doc2.png";
                PropertyGroupBasic <PropertyTypeBasic> contentGroup = save.Groups.Single(x => x.Name == "Content");
                contentGroup.Properties = contentGroup.Properties.Concat(new[]
                {
                    new PropertyTypeBasic
                    {
                        Alias       = "subtitle",
                        Label       = "Subtitle",
                        Description = "Optional Subtitle",
                        Validation  = new PropertyTypeValidation
                        {
                            Mandatory = false,
                            Pattern   = string.Empty
                        },
                        SortOrder  = 1,
                        DataTypeId = -88,
                        LabelOnTop = true
                    }
                });

                IContentType mapped = Mapper.Map(save, contentType);

                // just making sure
                Assert.AreEqual(mapped.Thumbnail, "Doc2.png");
                Assert.IsTrue(mapped.PropertyTypes.Any(x => x.Alias == "subtitle"));
                Assert.IsTrue(mapped.PropertyTypes.Single(x => x.Alias == "subtitle").LabelOnTop);

                repository.Save(mapped);

                bool dirty = mapped.IsDirty();

                // re-get
                contentType = repository.Get(_textpageContentType.Id);

                // Assert
                Assert.That(contentType.HasIdentity, Is.True);
                Assert.That(dirty, Is.False);
                Assert.That(contentType.Thumbnail, Is.EqualTo("Doc2.png"));
                Assert.That(contentType.PropertyTypes.Any(x => x.Alias == "subtitle"), Is.True);

                Assert.That(contentType.PropertyTypes.Single(x => x.Alias == "subtitle").LabelOnTop, Is.True);

                foreach (IPropertyType propertyType in contentType.PropertyTypes)
                {
                    Assert.IsTrue(propertyType.HasIdentity);
                    Assert.Greater(propertyType.Id, 0);
                }
            }
        }
    public void PropertyGroupBasic_To_PropertyGroup()
    {
        var dataType = new DataType(Services.GetRequiredService <LabelPropertyEditor>(), _serializer)
        {
            Name = "TODO"
        };

        _dataTypeService.Save(dataType);

        var basic = new PropertyGroupBasic <PropertyTypeBasic>
        {
            Id         = 222,
            Alias      = "group1",
            Name       = "Group 1",
            SortOrder  = 1,
            Properties = new[]
            {
                new PropertyTypeBasic
                {
                    Id          = 33,
                    SortOrder   = 1,
                    Alias       = "prop1",
                    Description = "property 1",
                    DataTypeId  = dataType.Id,
                    GroupId     = 222,
                    Label       = "Prop 1",
                    Validation  = new PropertyTypeValidation {
                        Mandatory = true, Pattern = null
                    }
                },
                new PropertyTypeBasic
                {
                    Id          = 34,
                    SortOrder   = 2,
                    Alias       = "prop2",
                    Description = "property 2",
                    DataTypeId  = dataType.Id,
                    GroupId     = 222,
                    Label       = "Prop 2",
                    Validation  = new PropertyTypeValidation {
                        Mandatory = false, Pattern = null
                    }
                }
            }
        };

        var contentType = new DocumentTypeSave
        {
            Id               = 0,
            ParentId         = -1,
            Alias            = "alias",
            AllowedTemplates = Enumerable.Empty <string>(),
            Groups           = new[] { basic }
        };

        // proper group properties mapping takes place when mapping the content type,
        // not when mapping the group - because of inherited properties and such
        // var result = Mapper.Map<PropertyGroup>(basic);
        var result = _sut.Map <IContentType>(contentType).PropertyGroups[0];

        Assert.AreEqual(basic.Name, result.Name);
        Assert.AreEqual(basic.Id, result.Id);
        Assert.AreEqual(basic.SortOrder, result.SortOrder);
        Assert.AreEqual(basic.Properties.Count(), result.PropertyTypes.Count());
    }