More segment-only values

This commit is contained in:
Stephan
2017-11-23 16:30:04 +01:00
parent 23c0687ae0
commit 62712cef3d
5 changed files with 165 additions and 136 deletions

View File

@@ -275,57 +275,14 @@ namespace Umbraco.Core.Models
_publishedState = PublishedState.Publishing;
}
internal virtual void CopyValues(IContentBase other, bool published = false)
internal virtual void CopyValues(IContentBase other, int? languageId, string segment, bool published = false)
{
// clear all existing properties
ClearEditValues(null, null);
// copy other properties
var otherProperties = other.Properties;
foreach (var otherProperty in otherProperties)
{
var alias = otherProperty.PropertyType.Alias;
SetValue(alias, otherProperty.GetValue(published));
}
}
internal virtual void CopyValues(IContentBase other, int? nLanguageId, bool published = false)
{
if (!nLanguageId.HasValue)
{
CopyValues(other);
return;
}
var languageId = nLanguageId.Value;
// clear all existing properties
ClearEditValues(nLanguageId, null);
// copy other properties
var otherProperties = other.Properties;
foreach (var otherProperty in otherProperties)
{
var alias = otherProperty.PropertyType.Alias;
SetValue(alias, languageId, otherProperty.GetValue(languageId, published));
}
}
internal virtual void CopyValues(IContentBase other, int? nLanguageId, string segment, bool published = false)
{
if (segment == null)
{
CopyValues(other, nLanguageId, published);
return;
}
if (!nLanguageId.HasValue)
throw new ArgumentException("Cannot be null when segment is not null.", nameof(nLanguageId));
var languageId = nLanguageId.Value;
// clear all existing properties
ClearEditValues(nLanguageId, segment);
// note: use property.SetValue(), don't assign pvalue.EditValue, else change tracking fails
foreach (var property in Properties)
foreach (var pvalue in property.Values)
if (pvalue.LanguageId == languageId && pvalue.Segment == segment)
property.SetValue(pvalue.LanguageId, pvalue.Segment, null);
// copy other properties
var otherProperties = other.Properties;
@@ -336,29 +293,13 @@ namespace Umbraco.Core.Models
}
}
private void ClearEditValues()
{
// clear all existing properties
// note: use property.SetValue(), don't assign pvalue.EditValue, else change tracking fails
foreach (var property in Properties)
foreach (var pvalue in property.Values)
property.SetValue(pvalue.LanguageId, pvalue.Segment, null);
}
private void ClearEditValues(int? nLanguageId, string segment)
{
// clear all existing properties
// note: use property.SetValue(), don't assign pvalue.EditValue, else change tracking fails
foreach (var property in Properties)
foreach (var pvalue in property.Values)
if (pvalue.LanguageId == nLanguageId && pvalue.Segment == segment)
property.SetValue(pvalue.LanguageId, pvalue.Segment, null);
}
internal virtual void CopyAllValues(IContentBase other, bool published = false)
{
// clear all existing properties
ClearEditValues();
// note: use property.SetValue(), don't assign pvalue.EditValue, else change tracking fails
foreach (var property in Properties)
foreach (var pvalue in property.Values)
property.SetValue(pvalue.LanguageId, pvalue.Segment, null);
// copy other properties
var otherProperties = other.Properties;
@@ -367,14 +308,8 @@ namespace Umbraco.Core.Models
var alias = otherProperty.PropertyType.Alias;
foreach (var pvalue in otherProperty.Values)
{
// fixme can we update SetValue to accept null lang/segment and fallback?
var value = published ? pvalue.PublishedValue : pvalue.EditedValue;
if (!pvalue.LanguageId.HasValue)
SetValue(alias, value);
else if (pvalue.Segment == null)
SetValue(alias, pvalue.LanguageId.Value, value);
else
SetValue(alias, pvalue.LanguageId.Value, pvalue.Segment, value);
SetValue(alias, pvalue.LanguageId, pvalue.Segment, value);
}
}
}

View File

@@ -275,23 +275,39 @@ namespace Umbraco.Core.Models
/// </summary>
public virtual object GetValue(string propertyTypeAlias, bool published = false)
{
return Properties.Contains(propertyTypeAlias) ? Properties[propertyTypeAlias].GetValue(published) : null;
return Properties.TryGetValue(propertyTypeAlias, out var property)
? property.GetValue(published)
: null;
}
/// <summary>
/// Gets the culture value of a property.
/// </summary>
public virtual object GetValue(string propertyTypeAlias, int languageId, bool published = false)
public virtual object GetValue(string propertyTypeAlias, int? languageId, bool published = false)
{
return Properties.Contains(propertyTypeAlias) ? Properties[propertyTypeAlias].GetValue(languageId, published) : null;
return Properties.TryGetValue(propertyTypeAlias, out var property)
? property.GetValue(languageId, null, published)
: null;
}
/// <summary>
/// Gets the segment value of a property.
/// </summary>
public virtual object GetValue(string propertyTypeAlias, int languageId, string segment, bool published = false)
public virtual object GetValue(string propertyTypeAlias, string segment, bool published = false)
{
return Properties.Contains(propertyTypeAlias) ? Properties[propertyTypeAlias].GetValue(languageId, segment, published) : null;
return Properties.TryGetValue(propertyTypeAlias, out var property)
? property.GetValue(null, segment, published)
: null;
}
/// <summary>
/// Gets the culture+segment value of a property.
/// </summary>
public virtual object GetValue(string propertyTypeAlias, int? languageId, string segment, bool published = false)
{
return Properties.TryGetValue(propertyTypeAlias, out var property)
? property.GetValue(languageId, segment, published)
: null;
}
/// <summary>
@@ -299,34 +315,46 @@ namespace Umbraco.Core.Models
/// </summary>
public virtual TPropertyValue GetValue<TPropertyValue>(string propertyTypeAlias, bool published = false)
{
if (Properties.Contains(propertyTypeAlias) == false)
if (!Properties.TryGetValue(propertyTypeAlias, out var property))
return default;
var convertAttempt = Properties[propertyTypeAlias].GetValue(published).TryConvertTo<TPropertyValue>();
var convertAttempt = property.GetValue(published).TryConvertTo<TPropertyValue>();
return convertAttempt.Success ? convertAttempt.Result : default;
}
/// <summary>
/// Gets the typed culture value of a property.
/// </summary>
public virtual TPropertyValue GetValue<TPropertyValue>(string propertyTypeAlias, int languageId, bool published = false)
public virtual TPropertyValue GetValue<TPropertyValue>(string propertyTypeAlias, int? languageId, bool published = false)
{
if (Properties.Contains(propertyTypeAlias) == false)
if (!Properties.TryGetValue(propertyTypeAlias, out var property))
return default;
var convertAttempt = Properties[propertyTypeAlias].GetValue(languageId, published).TryConvertTo<TPropertyValue>();
var convertAttempt = property.GetValue(languageId, null, published).TryConvertTo<TPropertyValue>();
return convertAttempt.Success ? convertAttempt.Result : default;
}
/// <summary>
/// Gets the typed segment value of a property.
/// </summary>
public virtual TPropertyValue GetValue<TPropertyValue>(string propertyTypeAlias, int languageId, string segment, bool published = false)
public virtual TPropertyValue GetValue<TPropertyValue>(string propertyTypeAlias, string segment, bool published = false)
{
if (Properties.Contains(propertyTypeAlias) == false)
if (!Properties.TryGetValue(propertyTypeAlias, out var property))
return default;
var convertAttempt = Properties[propertyTypeAlias].GetValue(languageId, segment, published).TryConvertTo<TPropertyValue>();
var convertAttempt = property.GetValue(null, segment, published).TryConvertTo<TPropertyValue>();
return convertAttempt.Success ? convertAttempt.Result : default;
}
/// <summary>
/// Gets the typed culture+segment value of a property.
/// </summary>
public virtual TPropertyValue GetValue<TPropertyValue>(string propertyTypeAlias, int? languageId, string segment, bool published = false)
{
if (!Properties.TryGetValue(propertyTypeAlias, out var property))
return default;
var convertAttempt = property.GetValue(languageId, segment, published).TryConvertTo<TPropertyValue>();
return convertAttempt.Success ? convertAttempt.Result : default;
}
@@ -348,11 +376,11 @@ namespace Umbraco.Core.Models
/// <summary>
/// Sets the culture (draft) value of a property.
/// </summary>
public virtual void SetValue(string propertyTypeAlias, int languageId, object value)
public virtual void SetValue(string propertyTypeAlias, int? languageId, object value)
{
if (value == null)
{
SetValueOnProperty(propertyTypeAlias, languageId, null);
SetValueOnProperty(propertyTypeAlias, languageId, null, null);
return;
}
@@ -361,9 +389,24 @@ namespace Umbraco.Core.Models
}
/// <summary>
/// Sets the segment (draft) value of a property.
/// Sets the culture+segment (draft) value of a property.
/// </summary>
public virtual void SetValue(string propertyTypeAlias, int languageId, string segment, object value)
public virtual void SetValue(string propertyTypeAlias, string segment, object value)
{
if (value == null)
{
SetValueOnProperty(propertyTypeAlias, null, segment, null);
return;
}
// .NET magic to call one of the 'SetPropertyValue' handlers with matching signature
((dynamic) this).SetPropertyValue(propertyTypeAlias, null, segment, (dynamic) value);
}
/// <summary>
/// Sets the culture+segment (draft) value of a property.
/// </summary>
public virtual void SetValue(string propertyTypeAlias, int? languageId, string segment, object value)
{
if (value == null)
{
@@ -375,6 +418,16 @@ namespace Umbraco.Core.Models
((dynamic) this).SetPropertyValue(propertyTypeAlias, languageId, segment, (dynamic) value);
}
// FIXME fix everything below
/// <summary>
/// Sets the neutral (draft) string value of a Property
/// </summary>
@@ -595,30 +648,10 @@ namespace Umbraco.Core.Models
Properties.Add(property);
}
/// <summary>
/// Sets the culture (draft) value of a property.
/// </summary>
private void SetValueOnProperty(string propertyTypeAlias, int languageId, object value)
{
if (Properties.Contains(propertyTypeAlias))
{
Properties[propertyTypeAlias].SetValue(languageId, value);
return;
}
var propertyType = PropertyTypes.FirstOrDefault(x => x.Alias.InvariantEquals(propertyTypeAlias));
if (propertyType == null)
throw new InvalidOperationException($"No PropertyType exists with the supplied alias \"{propertyTypeAlias}\".");
var property = propertyType.CreateProperty();
property.SetValue(languageId, value);
Properties.Add(property);
}
/// <summary>
/// Sets the segment (draft) value of a property.
/// </summary>
private void SetValueOnProperty(string propertyTypeAlias, int languageId, string segment, object value)
private void SetValueOnProperty(string propertyTypeAlias, int? languageId, string segment, object value)
{
if (Properties.Contains(propertyTypeAlias))
{
@@ -649,11 +682,16 @@ namespace Umbraco.Core.Models
return _invalidProperties.Any() == false;
}
public virtual bool Validate(int? languageId, string segment, out IEnumerable<Property> invalidProperties)
{
// fixme etc
}
/// <summary>
/// Gets the properties marked as invalid during the last validation.
/// </summary>
[IgnoreDataMember]
internal IEnumerable<Property> InvalidProperties => _invalidProperties;
internal IEnumerable<Property> InvalidProperties => _invalidProperties; // fixme return it, don't keep it here!
#endregion

View File

@@ -48,7 +48,7 @@ namespace Umbraco.Core.Models
/// </summary>
/// <param name="propertyTypeAlias">Alias of the PropertyType</param>
/// <returns>True if Property with given alias exists, otherwise False</returns>
bool HasProperty(string propertyTypeAlias);
bool HasProperty(string propertyTypeAlias); // fixme - what does this mean????
/// <summary>
/// Gets the neutral value of a Property
@@ -58,12 +58,17 @@ namespace Umbraco.Core.Models
/// <summary>
/// Gets the culture value of a Property
/// </summary>
object GetValue(string propertyTypeAlias, int languageId, bool published = false);
object GetValue(string propertyTypeAlias, int? languageId, bool published = false);
/// <summary>
/// Gets the segment value of a Property
/// </summary>
object GetValue(string propertyTypeAlias, int languageId, string segment, bool published = false);
object GetValue(string propertyTypeAlias, string segment, bool published = false);
/// <summary>
/// Gets the culture+segment value of a Property
/// </summary>
object GetValue(string propertyTypeAlias, int? languageId, string segment, bool published = false);
/// <summary>
/// Gets the typed neutral value of a Property
@@ -71,34 +76,63 @@ namespace Umbraco.Core.Models
TPropertyValue GetValue<TPropertyValue>(string propertyTypeAlias, bool published = false);
/// <summary>
/// Gets the typed neutral value of a Property
/// Gets the typed culture value of a Property
/// </summary>
TPropertyValue GetValue<TPropertyValue>(string propertyTypeAlias, int languageId, bool published = false);
TPropertyValue GetValue<TPropertyValue>(string propertyTypeAlias, int? languageId, bool published = false);
/// <summary>
/// Gets the typed neutral value of a Property
/// Gets the typed segment value of a Property
/// </summary>
TPropertyValue GetValue<TPropertyValue>(string propertyTypeAlias, int languageId, string segment, bool published = false);
TPropertyValue GetValue<TPropertyValue>(string propertyTypeAlias, string segment, bool published = false);
/// <summary>
/// Sets the neutral (draft) value of a Property
/// Gets the typed culture+segment value of a Property
/// </summary>
TPropertyValue GetValue<TPropertyValue>(string propertyTypeAlias, int? languageId, string segment, bool published = false);
/// <summary>
/// Sets the neutral (edited) value of a Property
/// </summary>
void SetValue(string propertyTypeAlias, object value);
/// <summary>
/// Sets the culture (draft) value of a Property
/// Sets the culture (edited) value of a Property
/// </summary>
void SetValue(string propertyTypeAlias, int languageId, object value);
void SetValue(string propertyTypeAlias, int? languageId, object value);
/// <summary>
/// Sets the segment (draft) value of a Property
/// Sets the segment (edited) value of a Property
/// </summary>
void SetValue(string propertyTypeAlias, int languageId, string segment, object value);
void SetValue(string propertyTypeAlias, string segment, object value);
/// <summary>
/// Boolean indicating whether the content and its properties are valid
/// Sets the culture+segment (edited) value of a Property
/// </summary>
void SetValue(string propertyTypeAlias, int? languageId, string segment, object value);
/// <summary>
/// Gets a value indicating whether the content and its neutral properties values are valid.
/// </summary>
/// <returns>True if content is valid otherwise false</returns>
bool Validate();
/// <summary>
/// Gets a value indicating whether the content and its culture properties values are valid.
/// </summary>
bool Validate(int? languageId);
/// <summary>
/// Gets a value indicating whether the content and its segment properties values are valid.
/// </summary>
bool Validate(string segment);
/// <summary>
/// Gets a value indicating whether the content and its culture+segment properties values are valid.
/// </summary>
bool Validate(int? languageId, string segment);
/// <summary>
/// Gets a value indicating whether the content and all its properties values are valid.
/// </summary>
bool ValidateAll();
}
}

View File

@@ -91,9 +91,7 @@ namespace Umbraco.Core.Models
set
{
_values = value;
_pvalue = value.FirstOrDefault(x => !x.LanguageId.HasValue && x.Segment == null);
_vvalues = value.ToDictionary(x => (x.LanguageId, x.Segment), x => x);
}
}
@@ -349,7 +347,7 @@ namespace Umbraco.Core.Models
}
/// <summary>
/// Gets a value indicating whether the (edit) neutral value is valid.
/// Gets a value indicating whether the (edited) neutral value is valid.
/// </summary>
/// <remarks>An invalid value can be saved, but only valid values can be published.</remarks>
public bool IsValid()
@@ -358,23 +356,41 @@ namespace Umbraco.Core.Models
}
/// <summary>
/// Gets a value indicating whether the (edit) culture value is valid.
/// Gets a value indicating whether the (edited) culture value is valid.
/// </summary>
/// <remarks>An invalid value can be saved, but only valid values can be published.</remarks>
public bool IsValid(int languageId)
public bool IsValid(int? languageId)
{
return IsValid(GetValue(languageId));
}
/// <summary>
/// Gets a value indicating whether the (edit) segment value is valid.
/// Gets a value indicating whether the (edited) segment value is valid.
/// </summary>
/// <remarks>An invalid value can be saved, but only valid values can be published.</remarks>
public bool IsValue(int languageId, string segment)
public bool IsValue(string segment)
{
return IsValid(GetValue(segment));
}
/// <summary>
/// Gets a value indicating whether the (edited) culture+segment value is valid.
/// </summary>
/// <remarks>An invalid value can be saved, but only valid values can be published.</remarks>
public bool IsValue(int? languageId, string segment)
{
return IsValid(GetValue(languageId, segment));
}
/// <summary>
/// Gets a value indicating whether all (edited) values are valid.
/// </summary>
/// <remarks>An invalid value can be saved, but only valid values can be published.</remarks>
public bool IsAllValid()
{
return _values.All(x => IsValid(x.EditedValue));
}
/// <summary>
/// Boolean indicating whether the passed in value is valid
/// </summary>

View File

@@ -156,6 +156,12 @@ namespace Umbraco.Core.Models
}
}
public bool TryGetValue(string propertyTypeAlias, out Property property)
{
property = this[propertyTypeAlias];
return property != null;
}
/// <summary>
/// Occurs when the collection changes.
/// </summary>