Skip to content

Commit

Permalink
2.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
maliming committed Apr 13, 2018
2 parents 7a6182e + 697ad54 commit fb46ed0
Show file tree
Hide file tree
Showing 16 changed files with 440 additions and 287 deletions.
67 changes: 43 additions & 24 deletions Abp.GeneralTree/GeneralTree/GeneralTreeCodeGenerate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,135 +2,154 @@
using System.Linq;
using Abp.Collections.Extensions;
using Abp.Extensions;
using Abp.GeneralTree.GeneralTree;

namespace Abp.GeneralTree
{
public static class GeneralTreeCodeGenerate
public class GeneralTreeCodeGenerate : IGeneralTreeCodeGenerate
{
public const int CodeUnitLength = 5;
private readonly IGeneralTreeCodeGenerateConfiguration _codeGenerateConfiguration;

public GeneralTreeCodeGenerate(IGeneralTreeCodeGenerateConfiguration codeGenerateConfiguration)
{
_codeGenerateConfiguration = codeGenerateConfiguration;
}

/// <summary>
/// Creates code for given numbers.
/// Example: if numbers are 1,2 then returns "00001.00002";
/// Creates code for given numbers.
/// Example: if numbers are 1,2 then returns "00001.00002";
/// </summary>
/// <param name="numbers">Numbers</param>
public static string CreateCode(params int[] numbers)
public string CreateCode(params int[] numbers)
{
if (numbers.IsNullOrEmpty())
{
return null;
}
return numbers.Select(number => number.ToString(new string('0', CodeUnitLength))).JoinAsString(".");

return numbers.Select(number => number.ToString(new string('0', _codeGenerateConfiguration.CodeLength)))
.JoinAsString(".");
}

/// <summary>
/// Merge a child code to a parent code.
/// Example: if parentCode = "00001", childCode = "00002" then returns "00001.00002".
/// Merge a child code to a parent code.
/// Example: if parentCode = "00001", childCode = "00002" then returns "00001.00002".
/// </summary>
/// <param name="parentCode">Parent code. Can be null or empty if parent is a root.</param>
/// <param name="childCode">Child code.</param>
public static string MergeCode(string parentCode, string childCode)
public string MergeCode(string parentCode, string childCode)
{
if (childCode.IsNullOrEmpty())
{
throw new ArgumentNullException(nameof(childCode), "childCode can not be null or empty.");
}

if (parentCode.IsNullOrEmpty())
{
return childCode;
}

return parentCode + "." + childCode;
}

/// <summary>
/// Merge a child FullName to a parent FullName.
/// Example: if parentFullName = "00001", childFullName = "00002" then returns "00001-00002".
/// Merge a child FullName to a parent FullName.
/// Example: if parentFullName = "00001", childFullName = "00002" then returns "00001-00002".
/// </summary>
/// <param name="parentFullName">Parent FullName. Can be null or empty if parent is a root.</param>
/// <param name="childFullName">Child FullName.</param>
public static string MergeFullName(string parentFullName, string childFullName)
public string MergeFullName(string parentFullName, string childFullName)
{
if (childFullName.IsNullOrEmpty())
{
throw new ArgumentNullException(nameof(childFullName), "childFullName can not be null or empty.");
}

if (parentFullName.IsNullOrEmpty())
{
return childFullName;
}

return parentFullName + "-" + childFullName;
}

/// <summary>
/// Remove the parent code
/// Example: if code = "00001.00002.00003" and parentCode = "00001" then returns "00002.00003".
/// Remove the parent code
/// Example: if code = "00001.00002.00003" and parentCode = "00001" then returns "00002.00003".
/// </summary>
/// <param name="code">The code.</param>
/// <param name="parentCode">The parent code.</param>
public static string RemoveParentCode(string code, string parentCode)
public string RemoveParentCode(string code, string parentCode)
{
if (code.IsNullOrEmpty())
{
throw new ArgumentNullException(nameof(code), "code can not be null or empty.");
}

if (parentCode.IsNullOrEmpty())
{
return code;
}

if (code.Length == parentCode.Length)
{
return null;
}

return code.Substring(parentCode.Length + 1);
}

/// <summary>
/// Get next code for given code.
/// Example: if code = "00001.00001" returns "00001.00002".
/// Get next code for given code.
/// Example: if code = "00001.00001" returns "00001.00002".
/// </summary>
/// <param name="code">The code.</param>
public static string GetNextCode(string code)
public string GetNextCode(string code)
{
if (code.IsNullOrEmpty())
{
throw new ArgumentNullException(nameof(code), "code can not be null or empty.");
}

var parentCode = GetParentCode(code);
var lastUnitCode = GetLastCode(code);
return MergeCode(parentCode, CreateCode(Convert.ToInt32(lastUnitCode) + 1));
}

/// <summary>
/// Gets the last code.
/// Example: if code = "00001.00002.00003" returns "00003".
/// Gets the last code.
/// Example: if code = "00001.00002.00003" returns "00003".
/// </summary>
/// <param name="code">The code.</param>
public static string GetLastCode(string code)
public string GetLastCode(string code)
{
if (code.IsNullOrEmpty())
{
throw new ArgumentNullException(nameof(code), "code can not be null or empty.");
}

var splittedCode = code.Split('.');
return splittedCode[splittedCode.Length - 1];
}

/// <summary>
/// Gets parent code.
/// Example: if code = "00001.00002.00003" returns "00001.00002".
/// Gets parent code.
/// Example: if code = "00001.00002.00003" returns "00001.00002".
/// </summary>
/// <param name="code">The code.</param>
public static string GetParentCode(string code)
public string GetParentCode(string code)
{
if (code.IsNullOrEmpty())
{
throw new ArgumentNullException(nameof(code), "code can not be null or empty.");
}

var splittedCode = code.Split('.');
if (splittedCode.Length == 1)
{
return null;
}

return splittedCode.Take(splittedCode.Length - 1).JoinAsString(".");
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Abp.GeneralTree.GeneralTree
{
public class GeneralTreeCodeGenerateConfiguration : IGeneralTreeCodeGenerateConfiguration
{
public int CodeLength { get; set; } = 5;
}
}
33 changes: 8 additions & 25 deletions Abp.GeneralTree/GeneralTree/GeneralTreeConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
using System;
using System.Linq.Expressions;

namespace Abp.GeneralTree
{
public class GeneralTreeConfiguration<TTree, TPrimaryKey> : IGeneralTreeConfiguration<TTree, TPrimaryKey>
where TPrimaryKey : struct
where TTree : class, IGeneralTree<TTree, TPrimaryKey>
{
public GeneralTreeConfiguration()
{
CheckSameNameExpression = null;
public Func<TTree, TTree, bool> CheckSameNameExpression { get; set; } = null;

ExceptionMessageFactory = tree =>
public Func<IGeneralTree<TTree, TPrimaryKey>, string> ExceptionMessageFactory { get; set; } =
tree =>
$"There is already an tree with name {tree.Name}. Two tree with same name can not be created in same level.";

Hyphen = "-";
}

public Func<TTree, TTree, bool> CheckSameNameExpression { get; set; }

public Func<IGeneralTree<TTree, TPrimaryKey>, string> ExceptionMessageFactory { get; set; }

public string Hyphen { get; set; }
public string Hyphen { get; set; } = "-";
}

public class
Expand All @@ -30,20 +21,12 @@ public class
where TPrimaryKey : class
where TTree : class, IGeneralTreeWithReferenceType<TTree, TPrimaryKey>
{
public GeneralTreeConfigurationWithReferenceType()
{
CheckSameNameExpression = null;
public Func<TTree, TTree, bool> CheckSameNameExpression { get; set; } = null;

ExceptionMessageFactory = tree =>
public Func<IGeneralTreeWithReferenceType<TTree, TPrimaryKey>, string> ExceptionMessageFactory { get; set; } =
tree =>
$"There is already an tree with name {tree.Name}. Two tree with same name can not be created in same level.";

Hyphen = "-";
}

public Func<TTree, TTree, bool> CheckSameNameExpression { get; set; }

public Func<IGeneralTreeWithReferenceType<TTree, TPrimaryKey>, string> ExceptionMessageFactory { get; set; }

public string Hyphen { get; set; }
public string Hyphen { get; set; } = "-";
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
using Abp.Configuration.Startup;
using Abp.GeneralTree.GeneralTree;

namespace Abp.GeneralTree
{
public static class GeneralTreeConfigurationExtensions
{
public static IGeneralTreeCodeGenerateConfiguration CodeGenerate(
this IModuleConfigurations moduleConfigurations)
{
return moduleConfigurations.AbpConfiguration
.Get<IGeneralTreeCodeGenerateConfiguration>();
}

public static IGeneralTreeConfiguration<TTree, TPrimaryKey> GeneralTree<TTree, TPrimaryKey>(
this IModuleConfigurations moduleConfigurations)
where TPrimaryKey : struct
Expand Down
8 changes: 8 additions & 0 deletions Abp.GeneralTree/GeneralTree/GeneralTreeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ public static IEnumerable<TTree> ToTree<TTree, TPrimaryKey>(this IEnumerable<TTr
{
return;
}
var parent = treeDic[x.ParentId.Value];
if (parent.Children == null)
{
parent.Children = new List<TTree>();
}
parent.Children.Add(x);
});

Expand All @@ -47,11 +49,13 @@ public static IEnumerable<TTree> ToTreeWithReferenceType<TTree, TPrimaryKey>(thi
{
return;
}
var parent = treeDic[x.ParentId];
if (parent.Children == null)
{
parent.Children = new List<TTree>();
}
parent.Children.Add(x);
});

Expand All @@ -77,11 +81,13 @@ public static IEnumerable<TTree> ToTreeDto<TTree, TPrimaryKey>(this IEnumerable<
{
return;
}
var parent = treeDic[x.ParentId.Value];
if (parent.Children == null)
{
parent.Children = new List<TTree>();
}
parent.Children.Add(x);
});

Expand All @@ -107,11 +113,13 @@ public static IEnumerable<TTree> ToTreeDtoWithReferenceType<TTree, TPrimaryKey>(
{
return;
}
var parent = treeDic[x.ParentId];
if (parent.Children == null)
{
parent.Children = new List<TTree>();
}
parent.Children.Add(x);
});

Expand Down
Loading

0 comments on commit fb46ed0

Please sign in to comment.