253 lines
7.5 KiB
C#
253 lines
7.5 KiB
C#
using System.ComponentModel.Design;
|
|
using SzCore.DataObjects;
|
|
|
|
namespace SzCore;
|
|
|
|
public class SzDataHandler
|
|
{
|
|
private Dictionary<string, SzDataset> _loadedDatasets = [];
|
|
private Dictionary<string, ISzTemplate> _loadedTemplates = [];
|
|
|
|
public SzDataHandler(){}
|
|
|
|
// The client should periodically call this to free memory, probably
|
|
private void ClearCache()
|
|
{
|
|
_loadedDatasets.Clear();
|
|
_loadedTemplates.Clear();
|
|
}
|
|
|
|
#region Datasets
|
|
public bool SaveDataset(SzDataset dataset)
|
|
{
|
|
var datasetPath = Path.Combine(SZ.LocalFileManager.DatasetsPath, dataset.Id, "dataset.json");
|
|
try
|
|
{
|
|
return SZ.LocalFileManager.SaveFile(datasetPath, SZ.SzParser.SerializeDatasetToJson(dataset));
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
SZ.Logger.LogError("Error saving dataset: " + e.Message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public bool DeleteDataset(string datasetId)
|
|
{
|
|
try
|
|
{
|
|
Directory.Delete(Path.Combine(SZ.LocalFileManager.DatasetsPath, datasetId), true);
|
|
return true;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
SZ.Logger.LogError($"Could not delete Dataset with id '{datasetId}': {e.Message}");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public SzDataset? LoadDataset(string datasetId)
|
|
{
|
|
if (_loadedDatasets.TryGetValue(datasetId, out SzDataset? value))
|
|
{
|
|
return value;
|
|
}
|
|
|
|
var datasetPath = Path.Combine(SZ.LocalFileManager.DataPath, "datasets", datasetId, "dataset.json");
|
|
|
|
var json = SZ.LocalFileManager.LoadFile(datasetPath);
|
|
if (json is null)
|
|
{
|
|
SZ.Logger.LogWarning($"Could not load dataset with ID '{datasetId}'");
|
|
return null;
|
|
}
|
|
|
|
var parsedDataset = SZ.SzParser.DeserializeDataset(json);
|
|
if (parsedDataset is null)
|
|
{
|
|
SZ.Logger.LogWarning($"Could not load dataset with ID '{datasetId}'");
|
|
return null;
|
|
}
|
|
|
|
_loadedDatasets.Add(parsedDataset.Id, parsedDataset);
|
|
return parsedDataset;
|
|
}
|
|
|
|
public SzDataset CreateDataset()
|
|
{
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Templates
|
|
public bool SaveTemplate(ISzTemplate template)
|
|
{
|
|
var templatePath = Path.Combine(SZ.LocalFileManager.TemplatesPath, template.Id, "template.json");
|
|
try
|
|
{
|
|
return SZ.LocalFileManager.SaveFile(templatePath, SZ.SzParser.SerializeTemplateToJson(template));
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
SZ.Logger.LogError("Error saving template: " + e.Message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public bool DeleteTemplate(string templateId)
|
|
{
|
|
try
|
|
{
|
|
Directory.Delete(Path.Combine(SZ.LocalFileManager.TemplatesPath, templateId), true);
|
|
return true;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
SZ.Logger.LogError($"Could not delete Template with id '{templateId}': {e.Message}");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public T? LoadTemplate<T>(string templateId) where T : ISzTemplate
|
|
{
|
|
if (_loadedTemplates.TryGetValue(templateId, out ISzTemplate? existing))
|
|
{
|
|
return (T)existing;
|
|
}
|
|
|
|
var templatePah = Path.Combine(SZ.LocalFileManager.TemplatesPath, templateId, "template.json");
|
|
|
|
var json = SZ.LocalFileManager.LoadFile(templatePah);
|
|
if (json is null)
|
|
{
|
|
SZ.Logger.LogWarning($"Could not load tetmplate with ID '{templateId}'");
|
|
return default;
|
|
}
|
|
|
|
var parsedTemplate = SZ.SzParser.DeserializeTemplate<T>(json);
|
|
if (parsedTemplate is null)
|
|
{
|
|
SZ.Logger.LogWarning($"Could not load template with ID '{templateId}'");
|
|
return default;
|
|
}
|
|
|
|
_loadedTemplates.TryAdd(parsedTemplate.Id, parsedTemplate);
|
|
|
|
return parsedTemplate;
|
|
}
|
|
|
|
public SzDataObjectTemplate? CreateDataObjectTemplate(string name, string dataObjectType, string? id = null, string? description = null, List<SzTemplateField>? fields = null)
|
|
{
|
|
if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(dataObjectType)) return null;
|
|
|
|
string newId;
|
|
if (!string.IsNullOrEmpty(id))
|
|
{
|
|
newId = id;
|
|
}
|
|
else
|
|
{
|
|
newId = name.ToLower().Replace(" ", "-").Trim();
|
|
}
|
|
|
|
var newTemplate = new SzDataObjectTemplate()
|
|
{
|
|
Name = name,
|
|
Id = newId,
|
|
DataObjectType = dataObjectType,
|
|
Uuid = Guid.NewGuid(),
|
|
Description = description ?? ""
|
|
};
|
|
|
|
if (fields is not null)
|
|
{
|
|
foreach (var field in fields)
|
|
{
|
|
newTemplate.TemplateFields.Add(field.Id, field);
|
|
}
|
|
}
|
|
|
|
return newTemplate;
|
|
|
|
}
|
|
#endregion
|
|
|
|
#region Other
|
|
|
|
public SzTemplateField? CreateTemplateField(string id, SzFieldType fieldType, bool isList = false, string? defaultValue = null, bool isSpecialType = false, string? specialTypeValue = null)
|
|
{
|
|
if (string.IsNullOrEmpty(id)) return null;
|
|
|
|
var newField = new SzTemplateField()
|
|
{
|
|
Id = id,
|
|
FieldType = fieldType,
|
|
IsList = isList,
|
|
IsSpecialType = isSpecialType,
|
|
DefaultValue = defaultValue ?? "",
|
|
SpecialTypeValue = specialTypeValue ?? ""
|
|
};
|
|
|
|
return newField;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Tests
|
|
// Only use these two methods for test purposes, we probably don't want to load all datasets and templates to memory in production
|
|
private void LoadAllDatasets()
|
|
{
|
|
_loadedDatasets.Clear();
|
|
if (!Directory.Exists(SZ.LocalFileManager.DatasetsPath))
|
|
{
|
|
throw new Exception("Could not load datasets, the dataset directory does not exist.");
|
|
}
|
|
|
|
foreach (var dir in Directory.GetDirectories(SZ.LocalFileManager.DatasetsPath))
|
|
{
|
|
var datasetFilePath = Path.Combine(dir, "dataset.json");
|
|
if (!File.Exists(datasetFilePath)) continue;
|
|
|
|
var datasetJson = SZ.LocalFileManager.LoadFile(datasetFilePath);
|
|
if (datasetJson == null) continue;
|
|
|
|
var parsedDataset = SZ.SzParser.DeserializeDataset(datasetJson);
|
|
if (parsedDataset == null)
|
|
{
|
|
SZ.Logger.LogWarning($"Parse error: Could not parse file {datasetFilePath} as an SzDataset");
|
|
continue;
|
|
}
|
|
|
|
_loadedDatasets.Add(parsedDataset.Id, parsedDataset);
|
|
}
|
|
}
|
|
|
|
private void LoadAllTemplates()
|
|
{
|
|
_loadedTemplates.Clear();
|
|
if (!Directory.Exists(SZ.LocalFileManager.TemplatesPath))
|
|
{
|
|
throw new Exception("Could not load templates, the template directory does not exist.");
|
|
}
|
|
|
|
foreach (var dir in Directory.GetDirectories(SZ.LocalFileManager.TemplatesPath))
|
|
{
|
|
var templateFilePath = Path.Combine(dir, "template.json");
|
|
if (!File.Exists(templateFilePath)) continue;
|
|
|
|
var templateJson = SZ.LocalFileManager.LoadFile(templateFilePath);
|
|
if (templateJson == null) continue;
|
|
|
|
var parsedTemplate = SZ.SzParser.DeserializeTemplate<ISzTemplate>(templateJson);
|
|
if (parsedTemplate == null)
|
|
{
|
|
SZ.Logger.LogWarning($"Parse error: Could not parse file {templateFilePath} as an ISzTemplate");
|
|
continue;
|
|
}
|
|
|
|
_loadedTemplates.Add(parsedTemplate.Id, parsedTemplate);
|
|
}
|
|
}
|
|
#endregion
|
|
} |