2024-05-24 01:18:13 +04:00
|
|
|
|
using ProjectBus.Drawnings;
|
2024-06-17 00:13:00 +04:00
|
|
|
|
using ProjectBus.Exceptions;
|
2024-05-24 01:18:13 +04:00
|
|
|
|
using System;
|
2024-05-20 14:22:37 +04:00
|
|
|
|
using System.Collections.Generic;
|
2024-06-17 00:13:00 +04:00
|
|
|
|
using System.Data;
|
2024-05-20 14:22:37 +04:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace ProjectBus.CollectionGenericObject;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Класс-хранилище коллекций
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
|
|
|
|
|
public class StorageCollection<T>
|
2024-05-24 01:18:13 +04:00
|
|
|
|
where T : DrawningSimpleBus
|
2024-05-20 14:22:37 +04:00
|
|
|
|
{/// <summary>
|
|
|
|
|
/// Словарь (хранилище) с коллекциями
|
|
|
|
|
/// </summary>
|
|
|
|
|
readonly Dictionary<string, ICollectionGenericObjects<T>> _storages;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Возвращение списка названий коллекций
|
|
|
|
|
/// </summary>
|
|
|
|
|
public List<string> Keys => _storages.Keys.ToList();
|
|
|
|
|
|
2024-05-24 01:18:13 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Ключевое слово, с которого должен начинаться файл
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly string _collectionKey = "CollectionsStorage";
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Разделитель для записи ключа и значения элемента словаря
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly string _separatorForKeyValue = "|";
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Разделитель для записей коллекции данных в файл
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly string _separatorItems = ";";
|
|
|
|
|
|
2024-05-20 14:22:37 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Конструктор
|
|
|
|
|
/// </summary>
|
|
|
|
|
public StorageCollection()
|
|
|
|
|
{
|
|
|
|
|
_storages = new Dictionary<string, ICollectionGenericObjects<T>>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Добавление коллекции в хранилище
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="name">Название коллекции</param>
|
|
|
|
|
/// <param name="collectionType">тип коллекции</param>
|
|
|
|
|
public void AddCollection(string name, CollectionType collectionType)
|
|
|
|
|
{
|
2024-06-17 00:13:00 +04:00
|
|
|
|
if (_storages.ContainsKey(name)) return;
|
|
|
|
|
if (collectionType == CollectionType.None) return;
|
|
|
|
|
else if (collectionType == CollectionType.Massive)
|
|
|
|
|
_storages[name] = new MassiveGenericObjects<T>();
|
|
|
|
|
else if (collectionType == CollectionType.List)
|
|
|
|
|
_storages[name] = new ListGenericObjects<T>();
|
2024-05-20 14:22:37 +04:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Удаление коллекции
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="name">Название коллекции</param>
|
|
|
|
|
public void DelCollection(string name)
|
|
|
|
|
{
|
2024-06-17 00:13:00 +04:00
|
|
|
|
if (_storages.ContainsKey(name))
|
|
|
|
|
_storages.Remove(name);
|
2024-05-20 14:22:37 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Доступ к коллекции
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="name">Название коллекции</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public ICollectionGenericObjects<T>? this[string name]
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2024-06-17 00:13:00 +04:00
|
|
|
|
if (_storages.ContainsKey(name))
|
|
|
|
|
return _storages[name];
|
|
|
|
|
return null;
|
2024-05-20 14:22:37 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-05-24 01:18:13 +04:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Сохранение информации по автомобилям в хранилище в файл
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="filename">Путь и имя файла</param>
|
|
|
|
|
/// <returns>true - сохранение прошло успешно, false - ошибка при сохранении данных</returns>
|
2024-06-17 00:13:00 +04:00
|
|
|
|
public void SaveData(string filename)
|
2024-05-24 01:18:13 +04:00
|
|
|
|
{
|
|
|
|
|
if (_storages.Count == 0)
|
|
|
|
|
{
|
2024-06-17 00:13:00 +04:00
|
|
|
|
throw new InvalidDataException("В хранилище отсутствуют коллекции для сохранения");
|
2024-05-24 01:18:13 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (File.Exists(filename))
|
|
|
|
|
{
|
|
|
|
|
File.Delete(filename);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using (StreamWriter writer = new StreamWriter(filename))
|
|
|
|
|
{
|
|
|
|
|
writer.Write(_collectionKey);
|
|
|
|
|
foreach (KeyValuePair<string, ICollectionGenericObjects<T>> value in _storages)
|
|
|
|
|
{
|
|
|
|
|
StringBuilder sb = new();
|
|
|
|
|
sb.Append(Environment.NewLine);
|
2024-06-17 00:13:00 +04:00
|
|
|
|
|
2024-05-24 01:18:13 +04:00
|
|
|
|
// не сохраняем пустые коллекции
|
|
|
|
|
if (value.Value.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sb.Append(value.Key);
|
|
|
|
|
sb.Append(_separatorForKeyValue);
|
|
|
|
|
sb.Append(value.Value.GetCollectionType);
|
|
|
|
|
sb.Append(_separatorForKeyValue);
|
|
|
|
|
sb.Append(value.Value.MaxCount);
|
|
|
|
|
sb.Append(_separatorForKeyValue);
|
|
|
|
|
foreach (T? item in value.Value.GetItems())
|
|
|
|
|
{
|
|
|
|
|
string data = item?.GetDataForSave() ?? string.Empty;
|
|
|
|
|
if (string.IsNullOrEmpty(data))
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
sb.Append(data);
|
|
|
|
|
sb.Append(_separatorItems);
|
|
|
|
|
}
|
|
|
|
|
writer.Write(sb);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Загрузка информации по автомобилям в хранилище из файла
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="filename">Путь и имя файла</param>
|
|
|
|
|
/// <returns>true - загрузка прошла успешно, false - ошибка при загрузке данных</returns>
|
2024-06-17 00:13:00 +04:00
|
|
|
|
public void LoadData(string filename)
|
2024-05-24 01:18:13 +04:00
|
|
|
|
{
|
|
|
|
|
if (!File.Exists(filename))
|
|
|
|
|
{
|
2024-06-17 00:13:00 +04:00
|
|
|
|
throw new FileNotFoundException($"{filename} не существует");
|
2024-05-24 01:18:13 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using (StreamReader fs = File.OpenText(filename))
|
|
|
|
|
{
|
|
|
|
|
string str = fs.ReadLine();
|
|
|
|
|
if (str == null || str.Length == 0)
|
|
|
|
|
{
|
2024-06-17 00:13:00 +04:00
|
|
|
|
throw new FileFormatException("Файл не подходит");
|
2024-05-24 01:18:13 +04:00
|
|
|
|
}
|
|
|
|
|
if (!str.StartsWith(_collectionKey))
|
|
|
|
|
{
|
2024-06-17 00:13:00 +04:00
|
|
|
|
throw new IOException("В файле неверные данные");
|
2024-05-24 01:18:13 +04:00
|
|
|
|
}
|
|
|
|
|
_storages.Clear();
|
|
|
|
|
string strs = "";
|
|
|
|
|
while ((strs = fs.ReadLine()) != null)
|
|
|
|
|
{
|
|
|
|
|
string[] record = strs.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
if (record.Length != 4)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]);
|
|
|
|
|
ICollectionGenericObjects<T>? collection = StorageCollection<T>.CreateCollection(collectionType);
|
|
|
|
|
if (collection == null)
|
|
|
|
|
{
|
2024-06-17 00:13:00 +04:00
|
|
|
|
throw new InvalidCastException("Не удалось определить тип коллекции:" + record[1]);
|
2024-05-24 01:18:13 +04:00
|
|
|
|
}
|
|
|
|
|
collection.MaxCount = Convert.ToInt32(record[2]);
|
|
|
|
|
string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
foreach (string elem in set)
|
|
|
|
|
{
|
2024-06-17 00:13:00 +04:00
|
|
|
|
if (elem?.CreateDrawningSimpleBus() is T simpleBus)
|
2024-05-24 01:18:13 +04:00
|
|
|
|
{
|
2024-06-17 00:13:00 +04:00
|
|
|
|
try
|
2024-05-24 01:18:13 +04:00
|
|
|
|
{
|
2024-06-17 00:13:00 +04:00
|
|
|
|
if (collection.Insert(simpleBus) == -1)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Объект не удалось добавить в коллекцию: " + record[3]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (CollectionOverflowException ex)
|
|
|
|
|
{
|
|
|
|
|
throw new DataException("Коллекция переполнена", ex);
|
2024-05-24 01:18:13 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_storages.Add(record[0], collection);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Создание коллекции по типу
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="collectionType"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
private static ICollectionGenericObjects<T>? CreateCollection(CollectionType collectionType)
|
|
|
|
|
{
|
|
|
|
|
return collectionType switch
|
|
|
|
|
{
|
|
|
|
|
CollectionType.Massive => new MassiveGenericObjects<T>(),
|
|
|
|
|
CollectionType.List => new ListGenericObjects<T>(),
|
|
|
|
|
_ => null,
|
|
|
|
|
};
|
|
|
|
|
}
|
2024-05-20 14:22:37 +04:00
|
|
|
|
}
|
2024-05-24 01:18:13 +04:00
|
|
|
|
|