70 lines
2.3 KiB
C#
70 lines
2.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using log4net;
|
|
using Taloyhtio.GeneralApi.Common.Extensions;
|
|
|
|
namespace Taloyhtio.GeneralApi.IntegrationUtility.Services.Impl
|
|
{
|
|
public class FileSorter : IFileSorter
|
|
{
|
|
private ILog logger;
|
|
|
|
public FileSorter(ILog logger)
|
|
{
|
|
this.logger = logger;
|
|
}
|
|
|
|
public List<string> Sort(List<string> files)
|
|
{
|
|
try
|
|
{
|
|
if (files.IsNullOrEmpty())
|
|
{
|
|
this.logger.Warn("Files list is empty. There is nothing to sort");
|
|
return new List<string>();
|
|
}
|
|
|
|
var regex = new Regex("^(condo|boardmember)_(?<date>\\d{8,8}_\\d{9,9})\\.xml$", RegexOptions.Singleline | RegexOptions.IgnoreCase);
|
|
var list = new List<KeyValuePair<string, DateTime>>();
|
|
foreach (var file in files)
|
|
{
|
|
var match = regex.Match(Path.GetFileName(file));
|
|
if (match.Success)
|
|
{
|
|
var datePart = match.Result("${date}");
|
|
//var timePart = match.Result("{$time_part}");
|
|
DateTime dt;
|
|
if (DateTime.TryParseExact(datePart, "yyyyMMdd_HHmmssfff", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
|
|
{
|
|
list.Add(new KeyValuePair<string, DateTime>(file, dt));
|
|
}
|
|
else
|
|
{
|
|
this.logger.WarnFormat("Unsupported date part in file name '{0}'", file);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (list.IsNullOrEmpty())
|
|
{
|
|
this.logger.Warn("Sorter didn't find any matched files");
|
|
return new List<string>();
|
|
}
|
|
|
|
// select by date
|
|
return list.OrderBy(kv => kv.Value).Select(kv => kv.Key).ToList();
|
|
}
|
|
catch (Exception x)
|
|
{
|
|
this.logger.ErrorFormat("{0}\n{1}", x.Message, x.StackTrace);
|
|
return new List<string>();
|
|
}
|
|
}
|
|
}
|
|
}
|