You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
182 lines
6.2 KiB
182 lines
6.2 KiB
// Author: Gockner, Simon
|
|
// Created: 2020-01-27
|
|
// Copyright(c) 2020 SimonG. All Rights Reserved.
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Reflection;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using GBase.Api;
|
|
using GBase.Attributes;
|
|
using GBase.Factories;
|
|
using GBase.FileHandling.Factories;
|
|
using GBase.Interfaces;
|
|
using GBase.Interfaces.FileHandling;
|
|
|
|
namespace GBase
|
|
{
|
|
/// <summary>
|
|
/// A <see cref="IGBase"/> table
|
|
/// </summary>
|
|
public class GBaseTable : IGBaseTable
|
|
{
|
|
private readonly IFileHandler _fileHandler;
|
|
private readonly IGBaseColumnFactory _gBaseColumnFactory;
|
|
|
|
/// <summary>
|
|
/// A <see cref="IGBase"/> table
|
|
/// </summary>
|
|
public GBaseTable(IFileHandlerFactory fileHandlerFactory, IGBaseColumnFactory gBaseColumnFactory)
|
|
{
|
|
_fileHandler = fileHandlerFactory.Create();
|
|
_gBaseColumnFactory = gBaseColumnFactory;
|
|
|
|
Columns = new List<IGBaseColumn>();
|
|
Entries = new List<object>();
|
|
|
|
INotifyGBaseEntryChanged.GBaseEntryChanged += OnGBaseEntryChanged;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// The <see cref="System.Type"/> of the class that this <see cref="IGBaseTable"/> represents
|
|
/// </summary>
|
|
public Type Type { get; private set; }
|
|
|
|
/// <summary>
|
|
/// The name of this <see cref="IGBaseTable"/>
|
|
/// </summary>
|
|
public string Name { get; private set; }
|
|
|
|
/// <summary>
|
|
/// The <see cref="IGBaseColumn"/>s of this <see cref="IGBaseTable"/>
|
|
/// </summary>
|
|
public List<IGBaseColumn> Columns { get; }
|
|
|
|
/// <summary>
|
|
/// The entries of this <see cref="IGBaseTable"/>
|
|
/// </summary>
|
|
public List<object> Entries { get; }
|
|
|
|
|
|
/// <summary>
|
|
/// Initialize this <see cref="IGBase"/>
|
|
/// </summary>
|
|
/// <param name="type">The <see cref="System.Type"/> of the class that this <see cref="IGBaseTable"/> represents</param>
|
|
/// <param name="name">The name of this <see cref="IGBaseTable"/></param>
|
|
/// <param name="databasePath">The path to the database files</param>
|
|
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to cancel the asynchronous operation</param>
|
|
/// <returns>True if successful, false if not</returns>
|
|
public async Task<bool> Init(Type type, string name, string databasePath, CancellationToken cancellationToken)
|
|
{
|
|
Type = type;
|
|
Name = name;
|
|
|
|
string fileName = $"{name}.{GBase.GBASE_TABLE_FILE_EXTENSION}";
|
|
string path = Path.Combine(databasePath, fileName);
|
|
await _fileHandler.Init(path, cancellationToken);
|
|
|
|
//TODO: Init columns list depending on GBaseColumnAttributes set for this GBaseTable
|
|
foreach (var property in type.GetProperties())
|
|
{
|
|
GBaseColumnAttribute gBaseColumnAttribute = property.GetCustomAttribute<GBaseColumnAttribute>();
|
|
if (gBaseColumnAttribute == null)
|
|
continue;
|
|
|
|
IGBaseColumn gBaseColumn = _gBaseColumnFactory.Create();
|
|
AddColumn(gBaseColumn);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Add a given <see cref="IGBaseColumn"/> to this <see cref="IGBaseTable"/>
|
|
/// </summary>
|
|
/// <param name="column">The given <see cref="IGBaseColumn"/></param>
|
|
/// <returns>True if successful, false if not</returns>
|
|
public bool AddColumn(IGBaseColumn column)
|
|
{
|
|
if (Columns.Contains(column))
|
|
return false;
|
|
|
|
Columns.Add(column);
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Remove a given <see cref="IGBaseColumn"/> from this <see cref="IGBaseTable"/>
|
|
/// </summary>
|
|
/// <param name="column">The given <see cref="IGBaseColumn"/></param>
|
|
/// <returns>True if successful, false if not</returns>
|
|
public bool RemoveColumn(IGBaseColumn column)
|
|
{
|
|
if (!Columns.Contains(column))
|
|
return false;
|
|
|
|
return Columns.Remove(column);
|
|
}
|
|
|
|
private bool AddEntry(object entry) //TODO: Write to file
|
|
{
|
|
Entries.Add(entry);
|
|
return true;
|
|
}
|
|
|
|
private bool ModifyEntry(object entry, string propertyName, object value) //TODO: Write to file
|
|
{
|
|
PropertyInfo property = entry.GetType().GetProperty(propertyName);
|
|
if (property == null)
|
|
return false;
|
|
|
|
property.SetValue(entry, value);
|
|
return true;
|
|
}
|
|
|
|
private bool RemoveEntry(object entry) //TODO: remove from file
|
|
{
|
|
if (!Entries.Contains(entry))
|
|
return false;
|
|
|
|
return Entries.Remove(entry);
|
|
}
|
|
|
|
/// <summary>
|
|
/// A <see cref="IGBase"/> entry changed
|
|
/// </summary>
|
|
/// <param name="entry">The entry (sender)</param>
|
|
/// <param name="args">The <see cref="GBaseEntryChangedEventArgs"/></param>
|
|
private void OnGBaseEntryChanged(object entry, GBaseEntryChangedEventArgs args)
|
|
{
|
|
//TODO: Might change, depending on #23
|
|
bool success;
|
|
if (args == null)
|
|
success = RemoveEntry(entry);
|
|
else if (!Entries.Contains(entry))
|
|
success = AddEntry(entry);
|
|
else
|
|
success = ModifyEntry(entry, args.ColumnName, args.Value);
|
|
|
|
if (!success)
|
|
throw new Exception("Failed to handle EntryChanged"); //TODO: Decide what to do here
|
|
}
|
|
|
|
/// <summary>
|
|
/// The <see cref="IAsyncDisposable.DisposeAsync"/> method
|
|
/// </summary>
|
|
/// <returns>A <see cref="ValueTask"/> to await</returns>
|
|
public async ValueTask DisposeAsync()
|
|
{
|
|
await _fileHandler.DisposeAsync();
|
|
|
|
foreach (var column in Columns)
|
|
{
|
|
await column.DisposeAsync();
|
|
}
|
|
|
|
Columns.Clear();
|
|
}
|
|
}
|
|
} |