public void Can_Serialize_From_Json_Model()
        {
            var converter = new ArchetypeValueConverter();
            var result = (Archetype.Models.ArchetypeModel)converter.ConvertDataToSource(null, _sampleJson, false);

            Assert.That(result != null);
            Assert.That(result.Fieldsets.Count() == 2);
        }
示例#2
0
        public void Can_Get_Fieldset_Property_Default_Value_By_Alias() 
		{
            var converter = new ArchetypeValueConverter();
            var result = (Archetype.Models.ArchetypeModel)converter.ConvertDataToSource(null, _sampleJson, false);

            var fieldset = result.Fieldsets.First();
            var propertyValue = fieldset.GetValue<string>("noSuchProperty", "noSuchProperty default value");

            Assert.That(propertyValue == "noSuchProperty default value");
        }
示例#3
0
        public void Can_Get_Fieldset_Property_By_Alias()
        {
            var converter = new ArchetypeValueConverter();
            var result = (Archetype.Models.ArchetypeModel)converter.ConvertDataToSource(null, _sampleJson, false);

            var fieldset = result.Fieldsets.First();
            var propertyValue = fieldset.GetValue("boxHeadline");

            Assert.That(propertyValue == "Box 1 Title");
        }
        public void Returns_Empty_Archetype_When_Data_Is_Null_Or_Empty(object data)
        {
            var converter = new ArchetypeValueConverter();
            var result = converter.ConvertDataToSource(null, data, false);

            Assert.AreEqual(result.GetType(), typeof (Archetype.Models.ArchetypeModel));

            var fieldsets = (Archetype.Models.ArchetypeModel) result;
            Assert.IsTrue(fieldsets.Count() == 0);
        }
示例#5
0
        public void Returns_String_When_No_Type_Specified()
        {
            var converter = new ArchetypeValueConverter();
            var result = (Archetype.Models.ArchetypeModel)converter.ConvertDataToSource(null, _sampleJson, false);

            var fieldset = result.Fieldsets.First();

            var property = fieldset.GetValue("link");
            Assert.That(property is string);
            Assert.That(property == "3175");
        }
示例#6
0
        public void Can_Convert_Property_Value_Types()
        {
            var converter = new ArchetypeValueConverter();
            var result = (Archetype.Models.ArchetypeModel)converter.ConvertDataToSource(null, _sampleJson, false);

            var fieldset = result.Fieldsets.First();

            Assert.That(fieldset.GetValue<int>("link") == 3175);
            Assert.That(fieldset.GetValue<bool>("show") == true);
            Assert.That(fieldset.GetValue<string>("blurb") == "A blurb here");
        }  
示例#7
0
		private void ReplacePropertyDataIds(Item item, ContentProperty propertyData, Direction direction)
		{
			if (propertyData != null && propertyData.Value != null)
			{
				var dataTypeId = ExecutionContext.DatabasePersistence.GetNodeId(propertyData.DataType, NodeObjectTypes.DataType);
				var fakePropertyType = this.CreateDummyPropertyType(dataTypeId, this.EditorAlias);

				var converter = new ArchetypeValueConverter();
				var archetype = (ArchetypeModel)converter.ConvertDataToSource(fakePropertyType, propertyData.Value, false);

				if (archetype != null)
				{
					// get the `PropertyItemProvider` from the collection.
					var propertyItemProvider = ItemProviderCollection.Instance.GetProvider(ProviderIDCollection.propertyDataItemProviderGuid, this.ExecutionContext);

					foreach (var property in archetype.Fieldsets.SelectMany(x => x.Properties))
					{
						if (property == null || string.IsNullOrWhiteSpace(property.PropertyEditorAlias))
							continue;

						// create a 'fake' item for Courier to process
						var fakeItem = new ContentPropertyData()
						{
							ItemId = item.ItemId,
							Name = string.Format("{0} [{1}: Nested {2} ({3})]", new[] { item.Name, this.EditorAlias, property.PropertyEditorAlias, property.Alias }),
							Data = new List<ContentProperty>
							{
								new ContentProperty
								{
									Alias = property.Alias,
									DataType = ExecutionContext.DatabasePersistence.GetUniqueId(property.DataTypeId, NodeObjectTypes.DataType),
									PropertyEditorAlias = property.PropertyEditorAlias,
									Value = property.Value
								}
							}
						};

						if (direction == Direction.Packaging)
						{
							try
							{
								// run the 'fake' item through Courier's data resolvers
								ResolutionManager.Instance.PackagingItem(fakeItem, propertyItemProvider);
							}
							catch (Exception ex)
							{
								LogHelper.Error<ArchetypeDataResolver>(string.Concat("Error packaging data value: ", fakeItem.Name), ex);
							}

							// pass up the dependencies and resources
							item.Dependencies.AddRange(fakeItem.Dependencies);
							item.Resources.AddRange(fakeItem.Resources);
						}
						else if (direction == Direction.Extracting)
						{
							try
							{
								// run the 'fake' item through Courier's data resolvers
								ResolutionManager.Instance.ExtractingItem(fakeItem, propertyItemProvider);
							}
							catch (Exception ex)
							{
								LogHelper.Error<ArchetypeDataResolver>(string.Concat("Error extracting data value: ", fakeItem.Name), ex);
							}
						}

						if (fakeItem.Data != null && fakeItem.Data.Any())
						{
							var firstDataType = fakeItem.Data.FirstOrDefault();
							if (firstDataType != null)
							{
								// set the resolved property data value
								property.Value = firstDataType.Value;

								// (if packaging) add a dependency for the property's data-type
								if (direction == Direction.Packaging)
									item.Dependencies.Add(firstDataType.DataType.ToString(), ProviderIDCollection.dataTypeItemProviderGuid);
							}
						}
					}

					// serialize the Archetype back to a string
					propertyData.Value = archetype.SerializeForPersistence();
				}
			}
		}
        private void ReplacePropertyDataIds(Item item, ContentProperty propertyData, Direction direction)
        {
            if (propertyData != null && propertyData.Value != null)
            {
                // just look at the amount of dancing around we have to do in order to fake a `PublishedPropertyType`?!
                var dataTypeId = PersistenceManager.Default.GetNodeId(propertyData.DataType, NodeObjectTypes.DataType);
                var fakePropertyType = this.CreateDummyPropertyType(dataTypeId, this.EditorAlias);

                var converter = new ArchetypeValueConverter();
                var archetype = (ArchetypeModel)converter.ConvertDataToSource(fakePropertyType, propertyData.Value, false);

                if (archetype != null)
                {
                    // create a 'fake' provider, as ultimately only the 'Packaging' enum will be referenced.
                    var fakeItemProvider = new PropertyItemProvider();

                    foreach (var property in archetype.Fieldsets.SelectMany(x => x.Properties))
                    {
                        if (property == null || string.IsNullOrWhiteSpace(property.PropertyEditorAlias))
                            continue;

                        // create a 'fake' item for Courier to process
                        var fakeItem = new ContentPropertyData()
                        {
                            ItemId = item.ItemId,
                            Name = string.Format("{0} [{1}: Nested {2} ({3})]", new[] { item.Name, this.EditorAlias, property.PropertyEditorAlias, property.Alias }),
                            Data = new List<ContentProperty>
                            {
                                new ContentProperty
                                {
                                    Alias = property.Alias,
                                    DataType = PersistenceManager.Default.GetUniqueId(property.DataTypeId, NodeObjectTypes.DataType),
                                    PropertyEditorAlias = property.PropertyEditorAlias,
                                    Value = property.Value
                                }
                            }
                        };

                        if (direction == Direction.Packaging)
                        {
                            try
                            {
                                // run the 'fake' item through Courier's data resolvers
                                ResolutionManager.Instance.PackagingItem(fakeItem, fakeItemProvider);
                            }
                            catch (Exception ex)
                            {
                                LogHelper.Error<ArchetypeDataResolver>(string.Concat("Error packaging data value: ", fakeItem.Name), ex);
                            }

                            // pass up the dependencies and resources
                            item.Dependencies.AddRange(fakeItem.Dependencies);
                            item.Resources.AddRange(fakeItem.Resources);
                        }
                        else if (direction == Direction.Extracting)
                        {
                            try
                            {
                                // run the 'fake' item through Courier's data resolvers
                                ResolutionManager.Instance.ExtractingItem(fakeItem, fakeItemProvider);
                            }
                            catch (Exception ex)
                            {
                                LogHelper.Error<ArchetypeDataResolver>(string.Concat("Error extracting data value: ", fakeItem.Name), ex);
                            }
                        }

                        if (fakeItem.Data != null && fakeItem.Data.Any())
                        {
                            var firstDataType = fakeItem.Data.FirstOrDefault();
                            if (firstDataType != null)
                            {
                                // set the resolved property data value
                                property.Value = firstDataType.Value;

                                // (if packaging) add a dependency for the property's data-type
                                if (direction == Direction.Packaging)
                                    item.Dependencies.Add(firstDataType.DataType.ToString(), ProviderIDCollection.dataTypeItemProviderGuid);
                            }
                        }
                    }

                    if (item.Name.Contains(string.Concat(this.EditorAlias, ": Nested")))
                    {
                        // if the Archetype is nested, then we only want to return the object itself - not a serialized string
                        propertyData.Value = archetype;
                    }
                    else
                    {
                        // if the Archetype is the root/container, then we can serialize it to a string
                        propertyData.Value = archetype.SerializeForPersistence();
                    }
                }
            }
        }