private static void LoadAllowedContentTypeChildrenIds(DocumentType nativeDocumentType, UmbracoDocumentType documentType)
        {
            documentType.AllowedContentTypeChildrenIds = new int[nativeDocumentType.AllowedChildContentTypeIDs.Count()];

            for (var idx = 0; idx < nativeDocumentType.AllowedChildContentTypeIDs.Count(); idx++)
            {
                documentType.AllowedContentTypeChildrenIds[idx] = nativeDocumentType.AllowedChildContentTypeIDs[idx];
            }
        }
        /// <summary>
        /// Gets a document type, based on its id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        public virtual UmbracoDocumentType GetById(int id)
        {
            var nativeDocumentType = new DocumentType(id);
            var documentType = new UmbracoDocumentType { Name = nativeDocumentType.Alias };

            LoadParentId(nativeDocumentType, documentType);
            LoadChildrenIds(nativeDocumentType, documentType);
            LoadAllowedContentTypeChildrenIds(nativeDocumentType, documentType);
            LoadPropertyTypes(nativeDocumentType, documentType);

            return documentType;
        }
        private static void LoadChildrenIds(DocumentType nativeDocumentType, UmbracoDocumentType documentType)
        {
            documentType.InherritanceParentId = -1;

            var h = UmbracoSQLHelper.Get();
            using (
                var reader = UmbracoConfiguration.IsVersion6() ?
                    h.ExecuteReader("SELECT childContentTypeId from [cmsContentType2ContentType] WHERE parentContentTypeId = @parentContentTypeId",
                    h.CreateParameter("parentContentTypeId", nativeDocumentType.Id)) :
                    h.ExecuteReader("SELECT nodeId FROM [cmsContentType] WHERE masterContentType = @nodeId",
                                    h.CreateParameter("nodeId", nativeDocumentType.Id))
                    )
            {
                while (reader.Read())
                {
                    var result = reader.GetInt(UmbracoConfiguration.IsVersion6() ? "childContentTypeId" : "nodeId");
                    if (result > 0)
                        documentType.InherritanceChildrenIds.Add(result);
                }

            }
        }
        public void GenerateNode()
        {
            var focusedNode = new UmbracoNode
                                  {
                                      Name = "FocusedNode",
                                      ChildrenIds = new int[] { 2 },
                                      DocumentTypeId = 15,
                                      ParentId = 3
                                  };
            focusedNode.Properties.Add(new UmbracoProperty() { Name = "title", Value = "Focused node" });
            focusedNode.Properties.Add(new UmbracoProperty() { Name = "content4testing", Value = "Test content" });

            var childNode = new UmbracoNode
            {
                Name = "ChildNode"
            };

            var parentNode = new UmbracoNode
                                 {
                                     Name = "ParentNode"
                                 };

            var mockNodeRepo = new Mock<IUmbracoNodeRepository>();
            mockNodeRepo.Setup(x => x.GetByNodeId(1)).Returns(focusedNode);
            mockNodeRepo.Setup(x => x.GetByNodeId(2)).Returns(childNode);
            mockNodeRepo.Setup(x => x.GetByNodeId(3)).Returns(parentNode);

            var titleDt = new UmbracoPropertyType { DatabaseType = "nvarchar", Name = "title" };
            var contentDt = new UmbracoPropertyType { DatabaseType = "nvarchar", Name = "content" };
            var contentTestNameDt = new UmbracoPropertyType { DatabaseType = "nvarchar", Name = "content4testing" };

            var mockPropTypeRepo = new Mock<IUmbracoPropertyTypeRepository>();
            mockPropTypeRepo.Setup(x => x.GetForNativeDocumentTypePropertyAlias(15, "title"))
                .Returns(titleDt);
            mockPropTypeRepo.Setup(x => x.GetForNativeDocumentTypePropertyAlias(15, "content"))
                .Returns(contentDt);
            mockPropTypeRepo.Setup(x => x.GetForNativeDocumentTypePropertyAlias(15, "content4testing"))
               .Returns(contentTestNameDt);

            var dt15 = new UmbracoDocumentType()
            {
                AllowedContentTypeChildrenIds = new int[] { },
                InherritanceParentId = -1,
                Name = "TestDocumentType",
                PropertyTypes = new List<UmbracoPropertyType>()
                                                {
                                                    new UmbracoPropertyType()
                                                        {DatabaseType = "nvarchar", Name = "title"},
                                                    new UmbracoPropertyType()
                                                        {DatabaseType = "nvarchar", Name = "content"}
                                                }

            };

            var mockDocTypeRepo = new Mock<IUmbracoDocumentTypeRepository>();
            mockDocTypeRepo.Setup(x => x.GetById(15))
                .Returns(dt15);

            var resourceFactory = new ResourceFactory(mockNodeRepo.Object, mockPropTypeRepo.Object, mockDocTypeRepo.Object);

            var node = resourceFactory.GenerateNode(1);

            Assert.AreEqual("http://localhost/rdf/resource/FocusedNode", node.Iri);
            Assert.AreEqual("http://localhost/rdf/resource/ParentNode", node.ParentIri);
            Assert.AreEqual("http://localhost/rdf/resource/ChildNode", node.ChildrenIris.First());

            Assert.IsTrue(node.Properties.Any(x => x.PropertyType.Iri == "http://localhost/rdf/ontology#hasTitle" &&
                x.PropertyType.DatabaseType == "nvarchar" && x.Value == "Focused node"));

            Assert.IsTrue(node.Properties.Any(x => x.PropertyType.Iri == "http://localhost/rdf/ontology#hasTestedContent" &&
                x.PropertyType.DatabaseType == "nvarchar" && x.Value == "Test content"));
        }
        private static void LoadParentId(DocumentType nativeDocumentType, UmbracoDocumentType documentType)
        {
            documentType.InherritanceParentId = -1;

            var h = UmbracoSQLHelper.Get();
            using (
                var reader = UmbracoConfiguration.IsVersion6() ?
                    h.ExecuteReader("SELECT parentContentTypeId from [cmsContentType2ContentType] WHERE childContentTypeId = @childContentTypeId",
                    h.CreateParameter("childContentTypeId", nativeDocumentType.Id)) :
                    h.ExecuteReader("SELECT masterContentType FROM [cmsContentType] WHERE nodeId = @nodeId",
                                    h.CreateParameter("nodeId", nativeDocumentType.Id)))
            {
                if (reader.Read())
                {
                    var result = reader.GetInt(UmbracoConfiguration.IsVersion6() ? "parentContentTypeId" : "masterContentType");
                    if (result > 0)
                        documentType.InherritanceParentId = result;
                }

            }
        }
        private static void LoadPropertyTypes(DocumentType nativeDocumentType, UmbracoDocumentType documentType)
        {
            documentType.PropertyTypes = new List<UmbracoPropertyType>();

            var propertyTypeRepository = new UmbracoPropertyTypeRepository();

            foreach (var propertyType in nativeDocumentType.PropertyTypes)
            {
                documentType.PropertyTypes.Add(propertyTypeRepository.GetForNativeDocumentTypePropertyAlias(nativeDocumentType.Id, propertyType.Alias));
            }
        }
        public void GenerateDocumentType()
        {
            var dt9 = new UmbracoDocumentType()
                        {
                            AllowedContentTypeChildrenIds = new int[] {10},

                            InherritanceParentId = 16,
                            InherritanceChildrenIds = new List<int>(){13},
                            Name = "TestDocumentType",
                            PropertyTypes = new List<UmbracoPropertyType>()
                                                {
                                                    new UmbracoPropertyType()
                                                        {DatabaseType = "nvarchar", Name = "title"},
                                                    new UmbracoPropertyType()
                                                        {DatabaseType = "nvarchar", Name = "content"}
                                                }

                        };

            var dt16 = new UmbracoDocumentType()
            {
                AllowedContentTypeChildrenIds = new int[] { 9 },

                Name = "TestParentDocumentType",
                PropertyTypes = new List<UmbracoPropertyType>()
                                                {
                                                    new UmbracoPropertyType()
                                                        {DatabaseType = "nvarchar", Name = "title"},
                                                    new UmbracoPropertyType()
                                                        {DatabaseType = "nvarchar", Name = "content"}
                                                }

            };

            var dt13 = new UmbracoDocumentType()
            {

                Name = "TestInherritanceChildType",
                PropertyTypes = new List<UmbracoPropertyType>()
                                                {
                                                    new UmbracoPropertyType()
                                                        {DatabaseType = "nvarchar", Name = "title"},
                                                    new UmbracoPropertyType()
                                                        {DatabaseType = "nvarchar", Name = "content"}
                                                }

            };

            var dt10 = new UmbracoDocumentType()
            {

                Name = "TestAllowedContentChild",
                PropertyTypes = new List<UmbracoPropertyType>()
                                                {
                                                    new UmbracoPropertyType()
                                                        {DatabaseType = "nvarchar", Name = "title"},
                                                    new UmbracoPropertyType()
                                                        {DatabaseType = "nvarchar", Name = "content"}
                                                }

            };
            var mockDocTypeRepo = new Mock<IUmbracoDocumentTypeRepository>();
            mockDocTypeRepo.Setup(x => x.GetById(9))
                .Returns(dt9);
            mockDocTypeRepo.Setup(x => x.GetById(16))
                .Returns(dt16);
            mockDocTypeRepo.Setup(x => x.GetById(13))
                .Returns(dt13);
            mockDocTypeRepo.Setup(x => x.GetById(10))
                .Returns(dt10);

            var titleDt = new UmbracoPropertyType {DatabaseType = "nvarchar", Name = "title"};
            var contentDt = new UmbracoPropertyType { DatabaseType = "nvarchar", Name = "content" };

            var mockPropTypeRepo = new Mock<IUmbracoPropertyTypeRepository>();
            mockPropTypeRepo.Setup(x => x.GetForNativeDocumentTypePropertyAlias(9, "title"))
                .Returns(titleDt);
            mockPropTypeRepo.Setup(x => x.GetForNativeDocumentTypePropertyAlias(9, "content"))
                .Returns(contentDt);

            var factory = new OntologyFactory(mockDocTypeRepo.Object, mockPropTypeRepo.Object);

            var docType = factory.GenerateDocumentType(9);

            Debug.WriteLine("Created document type with IRI: " + docType.Iri);

            Assert.AreEqual("http://localhost/rdf/ontology#TestDocumentType", docType.Iri);
            Assert.AreEqual("http://localhost/rdf/ontology#TestParentDocumentType", docType.ParentIri);
            Assert.AreEqual(2, docType.PropertyTypes.Count);
            Assert.IsTrue(docType.PropertyTypes.Any(x => x.Iri == "http://localhost/rdf/ontology#hasTitle" &&
                x.DatabaseType == "nvarchar"));
            Assert.IsTrue(docType.PropertyTypes.Single(x => x.Iri == "http://localhost/rdf/ontology#hasTitle").EquivalentIris.Count == 3);
            Assert.IsTrue(docType.PropertyTypes.Any(x => x.Iri == "http://localhost/rdf/ontology#hasContent" &&
                x.DatabaseType == "nvarchar"));
            Assert.AreEqual(1, docType.AllowedContentTypeChildrenIris.Count);
            Assert.AreEqual("http://localhost/rdf/ontology#TestAllowedContentChild", docType.AllowedContentTypeChildrenIris[0]);
            Assert.AreEqual(1, docType.ChildrenIris.Count);
            Assert.IsTrue(docType.ChildrenIris.Any(x => x == "http://localhost/rdf/ontology#TestInherritanceChildType"));
        }