Skip to content

Commit

Permalink
Added IsComponentTemplateMetadata to FieldAttribute
Browse files Browse the repository at this point in the history
This allows re-use of field attributes when access CT Metadata instead
of the Component fields
  • Loading branch information
Josh Einhorn committed Feb 13, 2015
1 parent 872d439 commit 7250907
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 75 deletions.
107 changes: 60 additions & 47 deletions DVM4T.Core/Attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public override object GetPropertyValue(IViewModel model, Type propertyType, IVi
object result = null;
if (model != null)
{
var fields = IsMetadata ? model.ModelData.Metadata : model.ModelData.Content;
var fields = IsComponentTemplateMetadata ? model.ModelData.ComponentTemplate.MetadataFields
: IsMetadata ? model.ModelData.Metadata : model.ModelData.Content;
if (fields != null && fields.ContainsKey(FieldName))
{
result = this.GetFieldValue(fields[FieldName], propertyType, model.ModelData.ComponentTemplate, builder);
Expand Down Expand Up @@ -113,6 +114,8 @@ public bool IsMetadata
get { return isMetadata; }
set { isMetadata = value; }
}

public bool IsComponentTemplateMetadata { get; set; }
}
/// <summary>
/// Base class for Property Attributes using Component Data
Expand Down Expand Up @@ -157,39 +160,49 @@ public override object GetPropertyValue(IViewModel model, Type propertyType, IVi
/// <summary>
/// Base class for Property Attributes using Component Template Metadata Fields Data
/// </summary>
public abstract class ComponentTemplateMetadataFieldAttributeBase : FieldAttributeBase
{
protected abstract IFieldAttribute BaseFieldAttribute { get; }
public ComponentTemplateMetadataFieldAttributeBase(string fieldName) : base(fieldName) { }
public override object GetPropertyValue(IViewModel model, Type propertyType, IViewModelBuilder builder = null)
{
object result = null;
if (model != null && model.ModelData != null && model.ModelData.ComponentTemplate != null)
{
var fields = model.ModelData.ComponentTemplate.MetadataFields;
if (fields != null && fields.ContainsKey(FieldName))
{
//public abstract class ComponentTemplateMetadataFieldAttributeBase : Attribute, IPropertyAttribute
//{
// protected abstract IFieldAttribute BaseFieldAttribute { get; }
// public object GetPropertyValue(IViewModel model, Type propertyType, IViewModelBuilder builder = null)
// {
// object result = null;
// if (model != null && model.ModelData != null && model.ModelData.ComponentTemplate != null)
// {
// var fields = model.ModelData.ComponentTemplate.MetadataFields;
// if (fields != null && fields.ContainsKey(BaseFieldAttribute.FieldName))
// {

result = this.GetFieldValue(fields[FieldName], propertyType, model.ModelData.ComponentTemplate, builder);
}
}
return result;
}
public override object GetFieldValue(IFieldData field, Type propertyType, IComponentTemplateData template, IViewModelBuilder builder = null)
{
new MappingHelper().MapFieldAttribute(this, BaseFieldAttribute);
return BaseFieldAttribute.GetFieldValue(field, propertyType, template, builder);
}
// result = this.GetFieldValue(fields[BaseFieldAttribute.FieldName], propertyType, model.ModelData.ComponentTemplate, builder);
// }
// }
// return result;
// }
// public object GetFieldValue(IFieldData field, Type propertyType, IComponentTemplateData template, IViewModelBuilder builder = null)
// {
// return BaseFieldAttribute.GetFieldValue(field, propertyType, template, builder);
// }

public override Type ExpectedReturnType
{
get
{
new MappingHelper().MapFieldAttribute(this, BaseFieldAttribute);
return BaseFieldAttribute.ExpectedReturnType;
}
}
}
// public Type ExpectedReturnType
// {
// get
// {
// return BaseFieldAttribute.ExpectedReturnType;
// }
// }
//}

//public class ComponentTemplateMetadataFieldAttribute : ComponentTemplateMetadataFieldAttributeBase
//{
// private IFieldAttribute fieldAttribute;
// public ComponentTemplateMetadataFieldAttribute(IFieldAttribute fieldAttribute)
// {
// this.fieldAttribute = fieldAttribute;
// }
// protected override IFieldAttribute BaseFieldAttribute
// {
// get { return fieldAttribute; }
// }
//}

/// <summary>
/// Attribute for a View Model. Required for DVM4T Framework to build a Model.
Expand Down Expand Up @@ -295,20 +308,20 @@ on i equals j

//Consider adding abstract classes for common Fields? Could I use Dependency Injection to add the concrete implementations?

internal class MappingHelper
{
//internal class MappingHelper
//{

internal MappingHelper MapFieldAttribute(IFieldAttribute mapFrom, IFieldAttribute mapTo)
{
mapTo.AllowMultipleValues = mapFrom.AllowMultipleValues;
mapTo.InlineEditable = mapFrom.InlineEditable;
mapTo.IsMetadata = mapFrom.IsMetadata;
mapTo.Mandatory = mapFrom.Mandatory;
if (mapFrom is ICanBeBoolean && mapTo is ICanBeBoolean)
{
(mapTo as ICanBeBoolean).IsBooleanValue = (mapFrom as ICanBeBoolean).IsBooleanValue;
}
return this;
}
}
// internal MappingHelper MapFieldAttribute(IFieldAttribute mapFrom, IFieldAttribute mapTo)
// {
// mapTo.AllowMultipleValues = mapFrom.AllowMultipleValues;
// mapTo.InlineEditable = mapFrom.InlineEditable;
// mapTo.IsMetadata = mapFrom.IsMetadata;
// mapTo.Mandatory = mapFrom.Mandatory;
// if (mapFrom is ICanBeBoolean && mapTo is ICanBeBoolean)
// {
// (mapTo as ICanBeBoolean).IsBooleanValue = (mapFrom as ICanBeBoolean).IsBooleanValue;
// }
// return this;
// }
//}
}
9 changes: 5 additions & 4 deletions DVM4T.Core/Contracts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public interface IComponentData : ITridionItemData
ISchemaData Schema { get; }
IMultimediaData MultimediaData { get; }
}

public interface IComponentTemplateData : ITridionItemData
{
IFieldsData MetadataFields { get; }
Expand Down Expand Up @@ -125,7 +125,7 @@ public interface IViewModelData //Should this extend IHaveData? I don't think so
/// The underlying data object that the View Model represents
/// </summary>
IViewModelBuilder Builder { get; }
IFieldsData Content { get; }
IFieldsData Content { get; }
IFieldsData Metadata { get; }
IComponentTemplateData ComponentTemplate { get; } //we required Component Template here in order to generate Site Edit markup for any linked components in the embedded fields
/// <summary>
Expand All @@ -149,7 +149,7 @@ public interface IComponentViewModel : IViewModel
//TODO: Consider removing this interface, holding on to template is not actually necessary after building is done
public interface IEmbeddedSchemaViewModel : IViewModel
{

}

public interface IViewModelBuilder
Expand Down Expand Up @@ -332,7 +332,7 @@ public interface ICanBeBoolean
{
bool IsBooleanValue { get; set; }
}

public interface IReflectionHelper
{
List<ModelAttributeProperty> GetModelProperties(Type type);
Expand Down Expand Up @@ -368,6 +368,7 @@ public interface IFieldAttribute : IPropertyAttribute
bool InlineEditable { get; set; }
bool Mandatory { get; set; }
bool IsMetadata { get; set; }
bool IsComponentTemplateMetadata { get; set; }
}

//TODO: Anyway to merge all three interfaces into one? They're so similar
Expand Down
25 changes: 1 addition & 24 deletions DVM4T.DD4T/Attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -536,28 +536,5 @@ public override Type ExpectedReturnType
{
get { return typeof(Dynamic.IMultimedia); }
}
}

//A test class for re-using existing TextField in combo with CT Metadata
public class TemplateMetaTextFieldAttribute : ComponentTemplateMetadataFieldAttributeBase, ICanBeBoolean
{
private IFieldAttribute textField;
public TemplateMetaTextFieldAttribute(string fieldName) : base(fieldName)
{
this.textField = new TextFieldAttribute(fieldName);
}
public bool IsBooleanValue
{
get;
set;
}

protected override IFieldAttribute BaseFieldAttribute
{
get { return textField; }
}

}


}
}
24 changes: 24 additions & 0 deletions DVM4T.Tests/Models.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using DVM4T.Base;
using DVM4T.DD4T.Attributes;
using DVM4T.Core;
using DVM4T.Contracts;
using DVM4T.Attributes;

namespace DVM4T.Testing.Models
{
Expand All @@ -13,6 +15,9 @@ public class TitleViewModel : ComponentPresentationViewModelBase
{
[TextField("title")]
public string Title { get; set; }

[ComponentTitle]
public string CompTitle { get; set; }
}

[ViewModel("GeneralContent", true, ViewModelKeys = new string[] { "BasicGeneralContent", "Test" })]
Expand All @@ -34,6 +39,18 @@ public class GeneralContentViewModel : ComponentPresentationViewModelBase
public double NumberFieldExample { get; set; }
}

[ViewModel("Image", true)]
public class Image : ComponentPresentationViewModelBase
{
[MultimediaUrl]
public string Url { get; set; }

[Multimedia]
public IMultimediaData Multimedia { get; set; }

[TextField("alt", IsMetadata = true)]
public string AltText { get; set; }
}
[ViewModel("ContentContainer", true, ViewModelKeys = new string[] { "Test" })]
public class ContentContainerViewModel : ComponentPresentationViewModelBase
{
Expand All @@ -48,6 +65,12 @@ public class ContentContainerViewModel : ComponentPresentationViewModelBase

[EmbeddedSchemaField("links", typeof(EmbeddedLinkViewModel), AllowMultipleValues = true)]
public ViewModelList<EmbeddedLinkViewModel> Links { get; set; }

[LinkedComponentField("image", LinkedComponentTypes = new Type[] { typeof(Image) })]
public Image Image { get; set; }

[TextField("view", IsComponentTemplateMetadata = true)]
public string ViewName { get; set; }
}

[ViewModel("EmbeddedLink", true)]
Expand Down Expand Up @@ -83,5 +106,6 @@ public class BrokenViewModel : ComponentPresentationViewModelBase

[NumberField("someNumber")]
public double NumberFieldExample { get; set; }

}
}
12 changes: 12 additions & 0 deletions DVM4T.Tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,17 @@ private Dynamic.ComponentPresentation GetManuallyBuiltCp()

},
ComponentTemplate = new Dynamic.ComponentTemplate()
{
MetadataFields = new Dynamic.FieldSet
{
{ "view", new Dynamic.Field
{
Values = new List<string> { "Component View Name"},
FieldType = Dynamic.FieldType.Text
}
}
}
}
};
return cp;
}
Expand Down Expand Up @@ -262,6 +273,7 @@ public void TestViewModelKey()
new Dynamic.Field { Values = new List<string> { viewModelKey }}
}
};
((Dynamic.Component)cp.Component).Title = "test title";

//exercise
var model = ViewModelDefaults.Builder.BuildCPViewModel(new ComponentPresentation(cp));
Expand Down

0 comments on commit 7250907

Please sign in to comment.