Templates
This commit is contained in:
10
SzCore/DataObjects/ISzTemplate.cs
Normal file
10
SzCore/DataObjects/ISzTemplate.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace SzCore.DataObjects;
|
||||
|
||||
public interface ISzTemplate
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string Id { get; set; }
|
||||
public Guid Uuid { get; set; }
|
||||
public string Description { get; set; }
|
||||
public Dictionary<string, SzTemplateField> TemplateFields { get; set; }
|
||||
}
|
||||
@@ -1,10 +1,12 @@
|
||||
namespace SzCore.DataObjects;
|
||||
|
||||
public class SzDataObjectTemplate
|
||||
public class SzDataObjectTemplate : ISzTemplate
|
||||
{
|
||||
public required string Name {get; set;}
|
||||
public required string Id {get; set;}
|
||||
public Guid? Uuid {get; set;}
|
||||
public required string DataObjectType {get; set;}
|
||||
public Guid Uuid {get; set;}
|
||||
public string Description { get; set; } = "";
|
||||
public Dictionary<string, SzTemplateField> TemplateFields {get; set;} = [];
|
||||
|
||||
public required string DataObjectType {get; set;}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
using System.ComponentModel.Design;
|
||||
using SzCore.DataObjects;
|
||||
|
||||
namespace SzCore;
|
||||
@@ -5,6 +6,7 @@ namespace SzCore;
|
||||
public class SzDataHandler
|
||||
{
|
||||
private Dictionary<string, SzDataset> _loadedDatasets = [];
|
||||
private Dictionary<string, ISzTemplate> _loadedTemplates = [];
|
||||
|
||||
public SzDataHandler(){}
|
||||
|
||||
@@ -26,7 +28,7 @@ public class SzDataHandler
|
||||
{
|
||||
if (_loadedDatasets.TryGetValue(datasetId, out SzDataset? value))
|
||||
{
|
||||
if (value is not null) return value;
|
||||
return value;
|
||||
}
|
||||
|
||||
var datasetPath = Path.Combine(SZ.LocalFileManager.DataPath, "datasets", datasetId, "dataset.json");
|
||||
@@ -49,19 +51,62 @@ public class SzDataHandler
|
||||
return parsedDataset;
|
||||
}
|
||||
|
||||
// The client should periodically call this to free memory, probably
|
||||
private void ClearLoadedDatasets()
|
||||
public bool SaveTemplate(ISzTemplate template)
|
||||
{
|
||||
_loadedDatasets.Clear();
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
// Only use this for test purposes, we probably don't want to load all datasets to memory in production
|
||||
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;
|
||||
}
|
||||
|
||||
// The client should periodically call this to free memory, probably
|
||||
private void ClearCache()
|
||||
{
|
||||
_loadedDatasets.Clear();
|
||||
_loadedTemplates.Clear();
|
||||
}
|
||||
|
||||
// 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()
|
||||
{
|
||||
ClearLoadedDatasets();
|
||||
_loadedDatasets.Clear();
|
||||
if (!Directory.Exists(SZ.LocalFileManager.DatasetsPath))
|
||||
{
|
||||
throw new Exception("Could not load datasets, the datase directory does not exist.");
|
||||
throw new Exception("Could not load datasets, the dataset directory does not exist.");
|
||||
}
|
||||
|
||||
foreach (var dir in Directory.GetDirectories(SZ.LocalFileManager.DatasetsPath))
|
||||
@@ -82,4 +127,31 @@ public class SzDataHandler
|
||||
_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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,7 +17,7 @@ public class SzParser
|
||||
}
|
||||
catch (JsonException e)
|
||||
{
|
||||
throw new Exception("Parse Error: " + e.Message);
|
||||
throw new Exception("Dataset Serialization Error: " + e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,5 +34,33 @@ public class SzParser
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public string SerializeTemplateToJson(ISzTemplate template)
|
||||
{
|
||||
try
|
||||
{
|
||||
var templateType = template.GetType();
|
||||
var jsonString = JsonSerializer.Serialize(template, templateType, _jsonOptions);
|
||||
return jsonString;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new Exception("Template Serialization Error: " + e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public T? DeserializeTemplate<T>(string jsonString)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = JsonSerializer.Deserialize<T>(jsonString, _jsonOptions);
|
||||
return result;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
SZ.Logger.LogError($"Could not deserialize JSON to type {typeof(T)}: " + e.Message);
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user