public string GetValue(Property property, ICollection <ArtifactDependency> dependencies)
        {
            var value = property.Value as string;

            if (string.IsNullOrWhiteSpace(value))
            {
                return(null);
            }

            if (value.DetectIsJson() == false)
            {
                return(null);
            }

            var relatedLinks = JsonConvert.DeserializeObject <JArray>(value);

            if (relatedLinks == null)
            {
                return(string.Empty);
            }

            foreach (var relatedLink in relatedLinks)
            {
                //Get the value from the JSON object
                var isInternal = Convert.ToBoolean(relatedLink["isInternal"]);

                //We are only concerned about internal links
                if (!isInternal)
                {
                    continue;
                }

                var linkIntId = Convert.ToInt32(relatedLink["link"]);

                //get the guid corresponding to the id
                //it *can* fail if eg the id points to a deleted content,
                //and then we use an empty guid
                var guidAttempt = _entityService.GetKeyForId(linkIntId, UmbracoObjectTypes.Document);
                if (guidAttempt.Success)
                {
                    // replace the picked content id by the corresponding Udi as a dependancy
                    var udi = new GuidUdi(Constants.UdiEntityType.Document, guidAttempt.Result);
                    dependencies.Add(new ArtifactDependency(udi, false, ArtifactDependencyMode.Exist));

                    //Set the current relatedlink 'internal' & 'link' properties to UDIs & not int's
                    relatedLink["link"]     = udi.ToString();
                    relatedLink["internal"] = udi.ToString();
                }
            }

            //Serialise the new updated object with UDIs in it to JSON to transfer across the wire
            return(JsonConvert.SerializeObject(relatedLinks));
        }
예제 #2
0
        public void RootUdiTest()
        {
            var stringUdi = new StringUdi(Constants.UdiEntityType.AnyString, string.Empty);

            Assert.IsTrue(stringUdi.IsRoot);
            Assert.AreEqual("umb://any-string/", stringUdi.ToString());

            var guidUdi = new GuidUdi(Constants.UdiEntityType.AnyGuid, Guid.Empty);

            Assert.IsTrue(guidUdi.IsRoot);
            Assert.AreEqual("umb://any-guid/00000000000000000000000000000000", guidUdi.ToString());

            var udi = Udi.Parse("umb://any-string/");

            Assert.IsTrue(udi.IsRoot);
            Assert.IsInstanceOf <StringUdi>(udi);

            udi = Udi.Parse("umb://any-guid/00000000000000000000000000000000");
            Assert.IsTrue(udi.IsRoot);
            Assert.IsInstanceOf <GuidUdi>(udi);

            udi = Udi.Parse("umb://any-guid/");
            Assert.IsTrue(udi.IsRoot);
            Assert.IsInstanceOf <GuidUdi>(udi);
        }
예제 #3
0
        protected uSyncDependency CreateDependency(GuidUdi udi, DependencyFlags flags)
        {
            var entity = GetElement(udi);

            return(new uSyncDependency()
            {
                Name = entity == null?udi.ToString() : entity.Name,
                           Udi = udi,
                           Flags = flags,
                           Order = DependencyOrders.OrderFromEntityType(udi.EntityType),
                           Level = entity == null ? 0 : entity.Level
            });
        }
예제 #4
0
        public static void Converting_String_Udi_To_A_Udi_Returns_Original_Udi_Value()
        {
            // Arrange
            Udi.ResetUdiTypes();
            Udi sample = new GuidUdi(Constants.UdiEntityType.AnyGuid, Guid.NewGuid());

            // Act
            bool success = UmbracoHelper.ConvertIdObjectToUdi(
                sample.ToString(),
                out Udi result
                );

            // Assert
            Assert.IsTrue(success, "Conversion of UDI failed.");
            Assert.That(result, Is.EqualTo(sample));
        }
예제 #5
0
        When_GetIncomingRelationsForItem_Called_We_Should_Fetch_All_Items_Where_ChildUdi_Matches_The_Item_Udi()
        {
            // arrange
            var udi       = new GuidUdi("foo", Guid.NewGuid());
            var relations = Mock.Of <List <NexuRelation> >();

            Sql actualSql = null;

            this.UmbracoDatabaseMock.Setup(x => x.Fetch <NexuRelation>(It.IsAny <Sql <ISqlContext> >())).Callback((Sql sql) =>
            {
                actualSql = sql;
            }).Returns(relations);


            // act
            var result = this.Repository.GetIncomingRelationsForItem(udi);

            // assert
            Assert.IsNotNull(result);
            Assert.That(actualSql.SQL == "WHERE ((Nexu_Relations.child_udi = @0))");
            Assert.That(actualSql.Arguments[0].ToString() == udi.ToString());

            this.UmbracoDatabaseMock.Verify(x => x.Fetch <NexuRelation>(It.IsAny <Sql <ISqlContext> >()), Times.Once);
        }
예제 #6
0
        When_GetGetUsedItemsFromList_Testsm_Called_We_Should_Fetch_All_Items_Where_ChildUdi_Matches_The_Udis_In_TheList()
        {
            // arrange
            var fooUdi   = new GuidUdi("foo", Guid.NewGuid());
            var barUdi   = new GuidUdi("bar", Guid.NewGuid());
            var listUdis = new List <Udi>
            {
                fooUdi,
                barUdi
            };
            var relations = Mock.Of <List <NexuRelation> >();

            Sql actualSql = null;

            this.UmbracoDatabaseMock.Setup(x => x.Fetch <NexuRelation>(It.IsAny <Sql <ISqlContext> >())).Callback((Sql sql) =>
            {
                actualSql = sql;
            }).Returns(relations);


            // act
            var result = this.Repository.GetUsedItemsFromList(listUdis);

            // assert
            Assert.Multiple(
                () =>
            {
                Assert.IsNotNull(result);
                Assert.That(actualSql.SQL == "WHERE (Nexu_Relations.child_udi IN (@0,@1))");
                Assert.That(actualSql.Arguments[0].ToString() == fooUdi.ToString());
                Assert.That(actualSql.Arguments[1].ToString() == barUdi.ToString());
            });


            this.UmbracoDatabaseMock.Verify(x => x.Fetch <NexuRelation>(It.IsAny <Sql <ISqlContext> >()), Times.Once);
        }
예제 #7
0
        public void GuidUdiCtorTest()
        {
            var guid = Guid.NewGuid();
            var udi  = new GuidUdi(Constants.UdiEntityType.AnyGuid, guid);

            Assert.AreEqual(Constants.UdiEntityType.AnyGuid, udi.EntityType);
            Assert.AreEqual(guid, udi.Guid);
            Assert.AreEqual("umb://" + Constants.UdiEntityType.AnyGuid + "/" + guid.ToString("N"), udi.ToString());
        }
        public override void Migrate()
        {
            var sqlDataTypes = Sql()
                               .Select <DataTypeDto>()
                               .From <DataTypeDto>()
                               .Where <DataTypeDto>(x => x.EditorAlias == Constants.PropertyEditors.Aliases.ContentPicker ||
                                                    x.EditorAlias == Constants.PropertyEditors.Aliases.MediaPicker ||
                                                    x.EditorAlias == Constants.PropertyEditors.Aliases.MultiNodeTreePicker);

            var dataTypes = Database.Fetch <DataTypeDto>(sqlDataTypes).ToList();

            foreach (var datatype in dataTypes.Where(x => !x.Configuration.IsNullOrWhiteSpace()))
            {
                switch (datatype.EditorAlias)
                {
                case Constants.PropertyEditors.Aliases.ContentPicker:
                case Constants.PropertyEditors.Aliases.MediaPicker:
                {
                    var config      = JsonConvert.DeserializeObject <JObject>(datatype.Configuration);
                    var startNodeId = config.Value <string>("startNodeId");
                    if (!startNodeId.IsNullOrWhiteSpace() && int.TryParse(startNodeId, out var intStartNode))
                    {
                        var guid = intStartNode <= 0
                                    ? null
                                    : Context.Database.ExecuteScalar <Guid?>(
                            Sql().Select <NodeDto>(x => x.UniqueId).From <NodeDto>().Where <NodeDto>(x => x.NodeId == intStartNode));
                        if (guid.HasValue)
                        {
                            var udi = new GuidUdi(datatype.EditorAlias == Constants.PropertyEditors.Aliases.MediaPicker
                                        ? Constants.UdiEntityType.Media
                                        : Constants.UdiEntityType.Document, guid.Value);
                            config["startNodeId"] = new JValue(udi.ToString());
                        }
                        else
                        {
                            config.Remove("startNodeId");
                        }

                        datatype.Configuration = JsonConvert.SerializeObject(config);
                        Database.Update(datatype);
                    }

                    break;
                }

                case Constants.PropertyEditors.Aliases.MultiNodeTreePicker:
                {
                    var config          = JsonConvert.DeserializeObject <JObject>(datatype.Configuration);
                    var startNodeConfig = config.Value <JObject>("startNode");
                    if (startNodeConfig != null)
                    {
                        var startNodeId = startNodeConfig.Value <string>("id");
                        var objectType  = startNodeConfig.Value <string>("type");
                        if (!objectType.IsNullOrWhiteSpace() &&
                            !startNodeId.IsNullOrWhiteSpace() &&
                            int.TryParse(startNodeId, out var intStartNode))
                        {
                            var guid = intStartNode <= 0
                                        ? null
                                        : Context.Database.ExecuteScalar <Guid?>(
                                Sql().Select <NodeDto>(x => x.UniqueId).From <NodeDto>().Where <NodeDto>(x => x.NodeId == intStartNode));

                            string entityType = null;
                            switch (objectType.ToLowerInvariant())
                            {
                            case "content":
                                entityType = Constants.UdiEntityType.Document;
                                break;

                            case "media":
                                entityType = Constants.UdiEntityType.Media;
                                break;

                            case "member":
                                entityType = Constants.UdiEntityType.Member;
                                break;
                            }

                            if (entityType != null && guid.HasValue)
                            {
                                var udi = new GuidUdi(entityType, guid.Value);
                                startNodeConfig["id"] = new JValue(udi.ToString());
                            }
                            else
                            {
                                startNodeConfig.Remove("id");
                            }

                            datatype.Configuration = JsonConvert.SerializeObject(config);
                            Database.Update(datatype);
                        }
                    }

                    break;
                }
                }
            }
        }
        public string ToArtifact(object value, PropertyType propertyType, ICollection <ArtifactDependency> dependencies)
        {
            var svalue = value as string;

            if (string.IsNullOrWhiteSpace(svalue))
            {
                return(null);
            }

            var valueAsJToken = JToken.Parse(svalue);

            if (valueAsJToken is JArray)
            {
                // Multiple links, parse as JArray
                var links = JsonConvert.DeserializeObject <JArray>(svalue);
                if (links == null)
                {
                    return(null);
                }

                foreach (var link in links)
                {
                    var     isMedia = link["isMedia"] != null;
                    int     intId;
                    string  url;
                    GuidUdi guidUdi;
                    // Only do processing if the Id is set on the element. OR if the url is set and its a media item
                    if (TryParseJTokenAttr(link, "id", out intId))
                    {
                        // Checks weather we are resolving a media item or a document
                        var objectTypeId = isMedia
                            ? UmbracoObjectTypes.Media
                            : UmbracoObjectTypes.Document;
                        var entityType = isMedia ? Constants.UdiEntityType.Media : Constants.UdiEntityType.Document;

                        var guidAttempt = _entityService.GetKey(intId, objectTypeId);
                        if (guidAttempt.Success == false)
                        {
                            continue;
                        }

                        var udi = new GuidUdi(entityType, guidAttempt.Result);
                        // Add the artifact dependency
                        dependencies.Add(new ArtifactDependency(udi, false, ArtifactDependencyMode.Exist));

                        // Set the Id attribute to the udi
                        link["id"] = udi.ToString();
                    }
                    else if (TryParseJTokenAttr(link, "udi", out guidUdi))
                    {
                        var entity = _entityService.Get(guidUdi.Guid, Constants.UdiEntityType.ToUmbracoObjectType(guidUdi.EntityType));
                        if (entity == null)
                        {
                            continue;
                        }

                        // Add the artifact dependency
                        dependencies.Add(new ArtifactDependency(guidUdi, false, ArtifactDependencyMode.Exist));
                    }
                    else if (isMedia && TryParseJTokenAttr(link, "url", out url))
                    {
                        // This state can happen due to an issue in RJP.MultiUrlPicker(or our linkPicker in RTE which it relies on),
                        // where you edit a media link, and just hit "Select".
                        // That will set the id to null, but the url will still be filled. We try to get the media item, and if so add it as
                        // a dependency to the package. If we can't find it, we abort(aka continue)
                        var entry = _mediaService.GetMediaByPath(url);
                        if (entry == null)
                        {
                            continue;
                        }

                        // Add the artifact dependency
                        var udi = entry.GetUdi();
                        dependencies.Add(new ArtifactDependency(udi, false, ArtifactDependencyMode.Exist));

                        // Update the url on the item to the udi aka umb://media/fileguid
                        link["url"] = udi.ToString();
                    }
                }
                return(JsonConvert.SerializeObject(links));
            }

            if (valueAsJToken is JObject)
            {
                // Single link, parse as JToken
                var link = JsonConvert.DeserializeObject <JToken>(svalue);
                if (link == null)
                {
                    return(string.Empty);
                }

                var     isMedia = link["isMedia"] != null;
                int     intId;
                string  url;
                GuidUdi guidUdi;
                // Only do processing if the Id is set on the element. OR if the url is set and its a media item
                if (TryParseJTokenAttr(link, "id", out intId))
                {
                    // Checks weather we are resolving a media item or a document
                    var objectTypeId = isMedia
                        ? UmbracoObjectTypes.Media
                        : UmbracoObjectTypes.Document;
                    var entityType = isMedia ? Constants.UdiEntityType.Media : Constants.UdiEntityType.Document;

                    var guidAttempt = _entityService.GetKey(intId, objectTypeId);
                    if (guidAttempt.Success)
                    {
                        var udi = new GuidUdi(entityType, guidAttempt.Result);
                        // Add the artifact dependency
                        dependencies.Add(new ArtifactDependency(udi, false, ArtifactDependencyMode.Exist));

                        // Set the Id attribute to the udi
                        link["id"] = udi.ToString();
                    }
                }
                else if (TryParseJTokenAttr(link, "udi", out guidUdi))
                {
                    var entity = _entityService.Get(guidUdi.Guid, Constants.UdiEntityType.ToUmbracoObjectType(guidUdi.EntityType));
                    if (entity != null)
                    {
                        // Add the artifact dependency
                        dependencies.Add(new ArtifactDependency(guidUdi, false, ArtifactDependencyMode.Exist));
                    }
                }
                else if (isMedia && TryParseJTokenAttr(link, "url", out url))
                {
                    // This state can happen due to an issue in RJP.MultiUrlPicker(or our linkPicker in RTE which it relies on),
                    // where you edit a media link, and just hits "Select".
                    // That will set the id to null, but the url will still be filled. We try to get the media item, and if so add it as
                    // a dependency to the package. If we can't find it, we abort(aka continue)
                    var entry = _mediaService.GetMediaByPath(url);
                    if (entry != null)
                    {
                        // Add the artifact dependency
                        var udi = entry.GetUdi();
                        dependencies.Add(new ArtifactDependency(udi, false, ArtifactDependencyMode.Exist));

                        // Update the url on the item to the udi aka umb://media/fileguid
                        link["url"] = udi.ToString();
                    }
                }
                return(JsonConvert.SerializeObject(link));
            }

            //if none of the above...
            return(string.Empty);
        }