Exemple #1
0
        /// <summary>
        /// Exports an <see cref="IMediaType"/> to xml as an <see cref="XElement"/>
        /// </summary>
        /// <param name="mediaType">MediaType to export</param>
        /// <returns><see cref="XElement"/> containing the xml representation of the MediaType item.</returns>
        internal XElement Export(IMediaType mediaType)
        {
            var info = new XElement("Info",
                                    new XElement("Name", mediaType.Name),
                                    new XElement("Alias", mediaType.Alias),
                                    new XElement("Icon", mediaType.Icon),
                                    new XElement("Thumbnail", mediaType.Thumbnail),
                                    new XElement("Description", mediaType.Description),
                                    new XElement("AllowAtRoot", mediaType.AllowedAsRoot.ToString()));

            var masterContentType = mediaType.CompositionAliases().FirstOrDefault();

            if (masterContentType != null)
            {
                info.Add(new XElement("Master", masterContentType));
            }

            var structure = new XElement("Structure");

            foreach (var allowedType in mediaType.AllowedContentTypes)
            {
                structure.Add(new XElement("MediaType", allowedType.Alias));
            }

            var genericProperties = new XElement("GenericProperties");

            foreach (var propertyType in mediaType.PropertyTypes)
            {
                var definition      = _dataTypeService.GetDataTypeDefinitionById(propertyType.DataTypeDefinitionId);
                var propertyGroup   = mediaType.PropertyGroups.FirstOrDefault(x => x.Id == propertyType.PropertyGroupId.Value);
                var genericProperty = new XElement("GenericProperty",
                                                   new XElement("Name", propertyType.Name),
                                                   new XElement("Alias", propertyType.Alias),
                                                   new XElement("Type", propertyType.DataTypeId.ToString()),
                                                   new XElement("Definition", definition.Key),
                                                   new XElement("Tab", propertyGroup == null ? "" : propertyGroup.Name),
                                                   new XElement("Mandatory", propertyType.Mandatory.ToString()),
                                                   new XElement("Validation", propertyType.ValidationRegExp),
                                                   new XElement("Description", new XCData(propertyType.Description)));
                genericProperties.Add(genericProperty);
            }

            var tabs = new XElement("Tabs");

            foreach (var propertyGroup in mediaType.PropertyGroups)
            {
                var tab = new XElement("Tab",
                                       new XElement("Id", propertyGroup.Id.ToString(CultureInfo.InvariantCulture)),
                                       new XElement("Caption", propertyGroup.Name));
                tabs.Add(tab);
            }

            var xml = new XElement("MediaType",
                                   info,
                                   structure,
                                   genericProperties,
                                   tabs);

            return(xml);
        }
        public void Can_Copy_MediaType_To_New_Parent_By_Performing_Clone()
        {
            // Arrange
            MediaType parentMediaType1 = MediaTypeBuilder.CreateSimpleMediaType("parent1", "Parent1");

            MediaTypeService.Save(parentMediaType1);
            MediaType parentMediaType2 = MediaTypeBuilder.CreateSimpleMediaType("parent2", "Parent2", null, true);

            MediaTypeService.Save(parentMediaType2);
            var mediaType = MediaTypeBuilder.CreateImageMediaType("Image2") as IMediaType;

            MediaTypeService.Save(mediaType);

            // Act
            IMediaType clone = mediaType.DeepCloneWithResetIdentities("newcategory");

            Assert.IsNotNull(clone);
            clone.RemoveContentType("parent1");
            clone.AddContentType(parentMediaType2);
            clone.ParentId = parentMediaType2.Id;
            MediaTypeService.Save(clone);

            // Assert
            Assert.That(clone.HasIdentity, Is.True);

            IMediaType clonedMediaType   = MediaTypeService.Get(clone.Id);
            IMediaType originalMediaType = MediaTypeService.Get(mediaType.Id);

            Assert.That(clonedMediaType.CompositionAliases().Any(x => x.Equals("parent2")), Is.True);
            Assert.That(clonedMediaType.CompositionAliases().Any(x => x.Equals("parent1")), Is.False);

            Assert.AreEqual(clonedMediaType.Path, "-1," + parentMediaType2.Id + "," + clonedMediaType.Id);
            Assert.AreEqual(clonedMediaType.PropertyTypes.Count(), originalMediaType.PropertyTypes.Count());

            Assert.AreNotEqual(clonedMediaType.ParentId, originalMediaType.ParentId);
            Assert.AreEqual(clonedMediaType.ParentId, parentMediaType2.Id);

            Assert.AreNotEqual(clonedMediaType.Id, originalMediaType.Id);
            Assert.AreNotEqual(clonedMediaType.Key, originalMediaType.Key);
            Assert.AreNotEqual(clonedMediaType.Path, originalMediaType.Path);

            Assert.AreNotEqual(clonedMediaType.PropertyTypes.First(x => x.Alias.StartsWith("umbracoFile")).Id, originalMediaType.PropertyTypes.First(x => x.Alias.StartsWith("umbracoFile")).Id);
            Assert.AreNotEqual(clonedMediaType.PropertyGroups.First(x => x.Name.StartsWith("Media")).Id, originalMediaType.PropertyGroups.First(x => x.Name.StartsWith("Media")).Id);
        }
Exemple #3
0
    public XElement Serialize(IMediaType mediaType)
    {
        var info = new XElement(
            "Info",
            new XElement("Name", mediaType.Name),
            new XElement("Alias", mediaType.Alias),
            new XElement("Key", mediaType.Key),
            new XElement("Icon", mediaType.Icon),
            new XElement("Thumbnail", mediaType.Thumbnail),
            new XElement("Description", mediaType.Description),
            new XElement("AllowAtRoot", mediaType.AllowedAsRoot.ToString()));

        var masterContentType = mediaType.CompositionAliases().FirstOrDefault();

        if (masterContentType != null)
        {
            info.Add(new XElement("Master", masterContentType));
        }

        var structure = new XElement("Structure");

        if (mediaType.AllowedContentTypes is not null)
        {
            foreach (ContentTypeSort allowedType in mediaType.AllowedContentTypes)
            {
                structure.Add(new XElement("MediaType", allowedType.Alias));
            }
        }

        var genericProperties = new XElement(
            "GenericProperties",
            SerializePropertyTypes(mediaType.PropertyTypes, mediaType.PropertyGroups)); // actually, all of them

        var tabs = new XElement(
            "Tabs",
            SerializePropertyGroups(mediaType.PropertyGroups)); // TODO Rename to PropertyGroups

        var xml = new XElement(
            "MediaType",
            info,
            structure,
            genericProperties,
            tabs);

        return(xml);
    }
        /// <summary>
        /// Exports an <see cref="IMediaType"/> to xml as an <see cref="XElement"/>
        /// </summary>
        /// <param name="mediaType">MediaType to export</param>
        /// <returns><see cref="XElement"/> containing the xml representation of the MediaType item.</returns>
        internal XElement Export(IMediaType mediaType)
        {
            var info = new XElement("Info",
                                    new XElement("Name", mediaType.Name),
                                    new XElement("Alias", mediaType.Alias),
                                    new XElement("Icon", mediaType.Icon),
                                    new XElement("Thumbnail", mediaType.Thumbnail),
                                    new XElement("Description", mediaType.Description),
                                    new XElement("AllowAtRoot", mediaType.AllowedAsRoot.ToString()));

            var masterContentType = mediaType.CompositionAliases().FirstOrDefault();
            if (masterContentType != null)
                info.Add(new XElement("Master", masterContentType));

            var structure = new XElement("Structure");
            foreach (var allowedType in mediaType.AllowedContentTypes)
            {
                structure.Add(new XElement("MediaType", allowedType.Alias));
            }

            var genericProperties = new XElement("GenericProperties");
            foreach (var propertyType in mediaType.PropertyTypes)
            {
                var definition = _dataTypeService.GetDataTypeDefinitionById(propertyType.DataTypeDefinitionId);
                var propertyGroup = mediaType.PropertyGroups.FirstOrDefault(x => x.Id == propertyType.PropertyGroupId.Value);
                var genericProperty = new XElement("GenericProperty",
                                                   new XElement("Name", propertyType.Name),
                                                   new XElement("Alias", propertyType.Alias),
                                                   new XElement("Type", propertyType.DataTypeId.ToString()),
                                                   new XElement("Definition", definition.Key),
                                                   new XElement("Tab", propertyGroup == null ? "" : propertyGroup.Name),
                                                   new XElement("Mandatory", propertyType.Mandatory.ToString()),
                                                   new XElement("Validation", propertyType.ValidationRegExp),
                                                   new XElement("Description", new XCData(propertyType.Description)));
                genericProperties.Add(genericProperty);
            }

            var tabs = new XElement("Tabs");
            foreach (var propertyGroup in mediaType.PropertyGroups)
            {
                var tab = new XElement("Tab",
                                       new XElement("Id", propertyGroup.Id.ToString(CultureInfo.InvariantCulture)),
                                       new XElement("Caption", propertyGroup.Name));
                tabs.Add(tab);
            }

            var xml = new XElement("MediaType",
                                   info,
                                   structure,
                                   genericProperties,
                                   tabs);
            return xml;
        }