Fixes #U4-2414 and removes internal usage of action handlers Yay!

This commit is contained in:
Morten Christensen
2013-06-28 13:27:23 +02:00
parent ca1b05e30d
commit 2477500045
5 changed files with 95 additions and 13 deletions

View File

@@ -70,8 +70,9 @@ namespace Umbraco.Core.Persistence.Factories
{
if (property.Value is bool || property.PropertyType.DataTypeId == new Guid("38b352c1-e9f8-4fd8-9324-9a2eab06d97a"))
{
int val = Convert.ToInt32(property.Value);
dto.Integer = val;
dto.Integer = property.Value != null && string.IsNullOrEmpty(property.Value.ToString())
? 0
: Convert.ToInt32(property.Value);
}
else
{

View File

@@ -27,6 +27,8 @@ namespace Umbraco.Core.Persistence.Repositories
{
_contentTypeRepository = contentTypeRepository;
_templateRepository = templateRepository;
EnsureUniqueNaming = true;
}
public ContentRepository(IDatabaseUnitOfWork work, IRepositoryCacheProvider cache, IContentTypeRepository contentTypeRepository, ITemplateRepository templateRepository)
@@ -34,8 +36,12 @@ namespace Umbraco.Core.Persistence.Repositories
{
_contentTypeRepository = contentTypeRepository;
_templateRepository = templateRepository;
EnsureUniqueNaming = true;
}
public bool EnsureUniqueNaming { get; set; }
#region Overrides of RepositoryBase<IContent>
protected override IContent PerformGet(int id)
@@ -180,6 +186,9 @@ namespace Umbraco.Core.Persistence.Repositories
{
((Content)entity).AddingEntity();
//Ensure unique name on the same level
entity.Name = EnsureUniqueNodeName(entity.ParentId, entity.Name);
var factory = new ContentFactory(NodeObjectTypeId, entity.Id);
var dto = factory.BuildDto(entity);
@@ -260,6 +269,9 @@ namespace Umbraco.Core.Persistence.Repositories
entity.UpdateDate = DateTime.Now;
}
//Ensure unique name on the same level
entity.Name = EnsureUniqueNodeName(entity.ParentId, entity.Name, entity.Id);
//Look up parent to get and set the correct Path and update SortOrder if ParentId has changed
if (((ICanBeDirty)entity).IsPropertyDirty("ParentId"))
{
@@ -487,5 +499,77 @@ namespace Umbraco.Core.Persistence.Repositories
return new PropertyCollection(properties);
}
private string EnsureUniqueNodeName(int parentId, string nodeName, int id = 0)
{
if (EnsureUniqueNaming == false)
return nodeName;
var sql = new Sql();
sql.Select("*")
.From<NodeDto>()
.Where<NodeDto>(x => x.ParentId == parentId && x.Text.StartsWith(nodeName));
int uniqueNumber = 1;
var currentName = nodeName;
var dtos = Database.Fetch<NodeDto>(sql);
if (dtos.Any())
{
var results = dtos.OrderBy(x => x.Text, new SimilarNodeNameComparer());
foreach (var dto in results)
{
if(id != 0 && id == dto.NodeId) continue;
if (dto.Text.ToLowerInvariant().Equals(currentName.ToLowerInvariant()))
{
currentName = nodeName + string.Format(" ({0})", uniqueNumber);
uniqueNumber++;
}
}
}
return currentName;
}
/// <summary>
/// Comparer that takes into account the duplicate index of a node name
/// This is needed as a normal alphabetic sort would go Page (1), Page (10), Page (2) etc.
/// </summary>
private class SimilarNodeNameComparer : IComparer<string>
{
public int Compare(string x, string y)
{
if (x.LastIndexOf(')') == x.Length - 1 && y.LastIndexOf(')') == y.Length - 1)
{
if (x.ToLower().Substring(0, x.LastIndexOf('(')) == y.ToLower().Substring(0, y.LastIndexOf('(')))
{
int xDuplicateIndex = ExtractDuplicateIndex(x);
int yDuplicateIndex = ExtractDuplicateIndex(y);
if (xDuplicateIndex != 0 && yDuplicateIndex != 0)
{
return xDuplicateIndex.CompareTo(yDuplicateIndex);
}
}
}
return String.Compare(x.ToLower(), y.ToLower(), StringComparison.Ordinal);
}
private int ExtractDuplicateIndex(string text)
{
int index = 0;
if (text.LastIndexOf('(') != -1 && text.LastIndexOf('(') < text.Length - 2)
{
int startPos = text.LastIndexOf('(') + 1;
int length = text.Length - 1 - startPos;
int.TryParse(text.Substring(startPos, length), out index);
}
return index;
}
}
}
}

View File

@@ -12,7 +12,8 @@ namespace umbraco.ActionHandlers
/// It ensures that new content nodes gets a unique name, and thereby avoiding conflictiong URLs.
/// It can be disabled in the umbracoSettings.config file.
/// </summary>
public class umbEnsureUniqueName : umbraco.BusinessLogic.Actions.IActionHandler
[Obsolete("This handler is no longer used")]
public class umbEnsureUniqueName : IActionHandler
{
public umbEnsureUniqueName()
{
@@ -82,7 +83,7 @@ namespace umbraco.ActionHandlers
/// <returns></returns>
public interfaces.IAction[] ReturnActions()
{
interfaces.IAction[] _retVal = { ActionNew.Instance };
interfaces.IAction[] _retVal = { };
return _retVal;
}

View File

@@ -1,12 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Reflection;
using umbraco.BasePages;
using umbraco.BusinessLogic.Utils;
using umbraco.cms.businesslogic.web;
using umbraco.cms.businesslogic.workflow;
using umbraco.interfaces;
namespace umbraco.BusinessLogic.Actions
@@ -20,6 +13,7 @@ namespace umbraco.BusinessLogic.Actions
/// <example>
///
/// </example>
[Obsolete("Legacy! Use events instead")]
public interface IActionHandler
{
bool Execute(Document documentObject, IAction action);

View File

@@ -637,8 +637,10 @@ namespace umbraco.cms.businesslogic.member
{
if (property.Value is bool || property.PropertyType.DataTypeDefinition.DataType.Id == new Guid("38b352c1-e9f8-4fd8-9324-9a2eab06d97a"))
{
int val = Convert.ToInt32(property.Value);
poco.Integer = val;
poco.Integer = property.Value != null &&
string.IsNullOrEmpty(property.Value.ToString())
? 0
: Convert.ToInt32(property.Value);
}
else
{