203 lines
8.8 KiB
C#
203 lines
8.8 KiB
C#
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using Kreta.BusinessLogic.Classes.ComboBox;
|
|
using Kreta.Core;
|
|
|
|
namespace Kreta.BusinessLogic.Utils
|
|
{
|
|
internal class LogicUtil
|
|
{
|
|
internal static IDictionary<string, string> CreateDropdownContent(DataSet ds, string key, string value, string baseItem = null, bool sort = true)
|
|
{
|
|
//Alapból mindenhol kell hogy legyen benne érték
|
|
//
|
|
IDictionary<string, string> result = new Dictionary<string, string>();
|
|
if (!string.IsNullOrWhiteSpace(baseItem))
|
|
result.Add("", baseItem);
|
|
//else
|
|
// result.Add("", StringResourcesUtil.GetString(364));//Kérem válasszon...
|
|
|
|
if (ds.Tables.Count < 1)
|
|
return result;
|
|
var dt = ds.Tables[0];
|
|
var items = dt.AsEnumerable().ToDictionary(row => row.Field<int>(key).ToString(), row => row.Field<string>(value));
|
|
if (sort)
|
|
{
|
|
foreach (var item in items.OrderBy(a => a.Value))
|
|
{
|
|
result.Add(item);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
foreach (var item in items)
|
|
{
|
|
result.Add(item);
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
internal static IDictionary<string, string> CreateGroupedDropdownContent(DataSet ds, string key, string value,
|
|
string group, IList<string> groupIds, string baseItem = null, bool groupIsEnum = false, bool noorder = false)
|
|
{
|
|
IDictionary<string, string> result = new Dictionary<string, string>();
|
|
if (!string.IsNullOrWhiteSpace(baseItem))
|
|
{ result.Add("", baseItem); }
|
|
//else { result.Add("", StringResourcesUtil.GetString(364)); } /*Kérem válasszon...*/
|
|
if (ds.Tables.Count < 1)
|
|
{ return result; }
|
|
var dt = ds.Tables[0];
|
|
|
|
if (groupIsEnum)
|
|
{
|
|
if (noorder == false)
|
|
{
|
|
foreach (var item in dt.AsEnumerable().OrderBy(a => a.Field<int>(group)))
|
|
{
|
|
var groupId = "SDAGID" + item.Field<int>(group);
|
|
if (groupIds.All(a => a != groupId))
|
|
{
|
|
groupIds.Add(groupId);
|
|
result.Add(groupId, item.Field<string>(group + "_DNAME"));
|
|
}
|
|
result.Add(item.Field<int>(key).ToString(), item.Field<string>(value));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
foreach (var item in dt.AsEnumerable())
|
|
{
|
|
var groupId = "SDAGID" + item.Field<int>(group);
|
|
if (groupIds.All(a => a != groupId))
|
|
{
|
|
groupIds.Add(groupId);
|
|
result.Add(groupId, item.Field<string>(group + "_DNAME"));
|
|
}
|
|
result.Add(item.Field<int>(key).ToString(), item.Field<string>(value));
|
|
}
|
|
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (noorder == false)
|
|
{
|
|
foreach (var item in dt.AsEnumerable().OrderBy(a => a.Field<string>(group)))
|
|
{
|
|
var groupId = "SDAGID" + item.Field<string>(group);
|
|
if (groupIds.All(a => a != groupId))
|
|
{
|
|
groupIds.Add(groupId);
|
|
result.Add(groupId, item.Field<string>(group));
|
|
}
|
|
result.Add(item.Field<int>(key).ToString(), item.Field<string>(value));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
foreach (var item in dt.AsEnumerable())
|
|
{
|
|
var groupId = "SDAGID" + item.Field<string>(group);
|
|
if (groupIds.All(a => a != groupId))
|
|
{
|
|
groupIds.Add(groupId);
|
|
result.Add(groupId, item.Field<string>(group));
|
|
}
|
|
result.Add(item.Field<int>(key).ToString(), item.Field<string>(value));
|
|
}
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// MVC-s csoportosítható select listát állít elő
|
|
/// </summary>
|
|
/// <param name="ds"></param>
|
|
/// <param name="keyColumn"></param>
|
|
/// <param name="valueColumn">ha null, akkor a <paramref name="valueFormat"/> paraméterből veszi a value formátumát</param>
|
|
/// <param name="groupNamesColumn"></param>
|
|
/// <param name="groupOrderColumn"></param>
|
|
/// <param name="valueFormat">az item-ek megjelnési formátumát definiálja:
|
|
/// pl.: "{Nev} ({OktatasiAzonosito})" ahol a Nev és OktatasiAzonosito a <paramref name="ds"/>-ben az oszlop neve</param>
|
|
/// <returns></returns>
|
|
internal static List<ComboBoxListItem> CreateMvcGroupedSelectListItem(DataSet ds, string keyColumn, string valueColumn, string groupNamesColumn, string groupOrderColumn = null, string valueFormat = null)
|
|
{
|
|
MatchCollection matchesInFormat = null;
|
|
if (!string.IsNullOrWhiteSpace(valueFormat))
|
|
{
|
|
matchesInFormat = Regex.Matches(valueFormat, Constants.RegularExpressions.OszlopNevHelyettesito);
|
|
}
|
|
DataTable dt = ds.Tables.Count > 0 ? ds.Tables[0] : null;
|
|
if (dt == null)
|
|
{
|
|
return new List<ComboBoxListItem>();
|
|
}
|
|
EnumerableRowCollection<DataRow> dataRowList = dt.AsEnumerable();
|
|
if (!string.IsNullOrWhiteSpace(groupOrderColumn))
|
|
{
|
|
dataRowList = dataRowList.OrderBy(x => x.Field<int>(groupOrderColumn));
|
|
}
|
|
var groups = dataRowList
|
|
.GroupBy(x => x.Field<string>(groupNamesColumn))
|
|
.Select(group => new KeyValuePair<string, ComboListGroup>(group.Key, new ComboListGroup { Name = group.Key }))
|
|
.ToDictionary(item => item.Key, item => item.Value);
|
|
|
|
var result = dt.Rows
|
|
.Cast<DataRow>()
|
|
.Select(item => new ComboBoxListItem
|
|
{
|
|
GroupName = groups[item.Field<string>(groupNamesColumn)].Name,
|
|
Text = valueColumn != null ? item.Field<string>(valueColumn) : GetFilledValueFormat(item, valueFormat, matchesInFormat),
|
|
Value = item.Field<int>(keyColumn).ToString()
|
|
})
|
|
.ToList();
|
|
return result;
|
|
}
|
|
|
|
internal static string GetFilledValueFormat(DataRow item, string valueFormat, MatchCollection matches)
|
|
{
|
|
var sb = new StringBuilder(valueFormat);
|
|
for (int i = 0; i < matches.Count; i++)
|
|
{
|
|
sb = sb.Replace(matches[i].Value, item[matches[i].Value.Trim("{}".ToCharArray())].ToString());
|
|
}
|
|
return sb.ToString();
|
|
}
|
|
|
|
internal static List<ExtraDataComboBoxListItem> CreateMvcGroupedExtendedComboBoxListItem(DataSet ds, string keyColumn, string valueColumn, string groupNamesColumn, string groupOrderColumn = null, IEnumerable<string> extraDataParameters = null)
|
|
{
|
|
DataTable dt = ds.Tables.Count > 0 ? ds.Tables[0] : null;
|
|
if (dt == null)
|
|
{
|
|
return new List<ExtraDataComboBoxListItem>();
|
|
}
|
|
EnumerableRowCollection<DataRow> dataRowList = dt.AsEnumerable();
|
|
if (!string.IsNullOrWhiteSpace(groupOrderColumn))
|
|
{
|
|
dataRowList = dataRowList.OrderBy(x => x.Field<int>(groupOrderColumn));
|
|
}
|
|
var groups = dataRowList
|
|
.GroupBy(x => x.Field<string>(groupNamesColumn))
|
|
.Select(group => new KeyValuePair<string, ComboListGroup>(group.Key, new ComboListGroup { Name = group.Key }))
|
|
.ToDictionary(item => item.Key, item => item.Value);
|
|
|
|
var result = dt.Rows
|
|
.Cast<DataRow>()
|
|
.Select(item => new ExtraDataComboBoxListItem
|
|
{
|
|
GroupName = groups[item.Field<string>(groupNamesColumn)].Name,
|
|
Text = item.Field<string>(valueColumn),
|
|
Value = item.Field<int>(keyColumn).ToString(),
|
|
Data = extraDataParameters.ToDictionary(extraParameterName => extraParameterName, extraParameterName => item[extraParameterName].ToString())
|
|
})
|
|
.ToList();
|
|
return result;
|
|
}
|
|
}
|
|
}
|