Skip to content

Commit

Permalink
Port the configuration service
Browse files Browse the repository at this point in the history
  • Loading branch information
celeron533 committed Aug 23, 2023
1 parent e8a276f commit c964d4b
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DicomGrep/DicomGrep.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
</PropertyGroup>

<ItemGroup>
<Folder Include="Models\" />
<AvaloniaResource Include="Assets\**" />
</ItemGroup>

Expand All @@ -23,5 +22,6 @@
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.0.4" />
<PackageReference Include="Avalonia.ReactiveUI" Version="11.0.4" />
<PackageReference Include="NLog" Version="5.2.3" />
</ItemGroup>
</Project>
19 changes: 19 additions & 0 deletions DicomGrep/Models/Configuration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace DicomGrep.Models
{
public class Configuration
{
public List<string> SearchPathHistory { get; set; }
public List<string> FileTypesHistory { get; set; }
public List<string> SopClassUidHistory { get; set; }
public List<string> DicomTagHistory { get; set; }
public List<string> SearchTextHistory { get; set; }

// todo: other configuration item
public int HistoryCapacity { get; set; }
public SearchCriteria SearchCriteria { get; set; }
}
}
63 changes: 63 additions & 0 deletions DicomGrep/Models/SearchCriteria.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json.Serialization;

namespace DicomGrep.Models
{
public class SearchCriteria
{
public string SearchPath { get; set; }
public string SearchSopClassUid { get; set; }

private string _searchTag;

public string SearchTag
{
get => _searchTag;
set
{
_searchTag = value;
_searchTagFlattened = value?.Substring(0, value.IndexOf(':') > 0 ? value.IndexOf(':') : value.Length)
.Replace("(", "")
.Replace(")", "")
.Replace(",", "")
.Replace(".", "")
.Replace(" ", "")
.ToUpper();
}
}

private string _searchTagFlattened;
[JsonIgnore]
public string SearchTagFlattened => _searchTagFlattened;

public string FileTypes { get; set; }

public string SearchText { get; set; }

public bool CaseSensitive { get; set; }
public bool WholeWord { get; set; }
public bool IncludeSubfolders { get; set; }

[JsonIgnore]
public bool SearchInResults { get; set; }

public int SearchThreads { get; set; }

public override string ToString()
{
return
$"SearchPath = '{SearchPath}', " +
$"SearchSopClassUid = '{SearchSopClassUid}', " +
$"SearchTag = '{SearchTag}', " +
$"FileTypes = '{FileTypes}', " +
$"SearchText = '{SearchText}', " +
$"CaseSensitive = {CaseSensitive}, " +
$"WholeWord= {WholeWord}, " +
$"IncludeSubfolders = {IncludeSubfolders}, " +
$"SearchInResults = {SearchInResults}, " +
$"SearchThreads = {SearchThreads}";
}
}
}
97 changes: 97 additions & 0 deletions DicomGrep/Services/ConfigurationService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using DicomGrep.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Text.Json;

namespace DicomGrep.Services
{
public class ConfigurationService
{
private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
private static Configuration CurrentConfiguration { get; set; }
public const string CONFIG_FILE = "config.json";

public Configuration GetConfiguration() => CurrentConfiguration;

public void SetConfiguration(Configuration config)
{
CurrentConfiguration = config;
//todo: raise configuration changed event
}



public bool Save()
{
logger.Info("Saving the configuration to file");

JsonSerializerOptions option = new JsonSerializerOptions { WriteIndented = true };
try
{
File.WriteAllText(CONFIG_FILE, JsonSerializer.Serialize(CurrentConfiguration, option));
}
catch(Exception ex)
{
logger.Error(ex, "Failed to save the configuration to file.");
return false;
}
return true;
}

public bool Load()
{
logger.Info("Loading the configuration from file");
try
{
if (File.Exists(CONFIG_FILE))
{
CurrentConfiguration = JsonSerializer.Deserialize<Configuration>(File.ReadAllText(CONFIG_FILE));
return true;
}
}
catch (Exception ex)
{
logger.Error(ex, "Failed to load the configuration from file.");
}
CurrentConfiguration = DefaultConfiguration();
return false;
}






private static Configuration DefaultConfiguration()
{
logger.Info("Load default configuration");

SearchCriteria criteria = new SearchCriteria
{
SearchPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments),
FileTypes = "*.dcm",
SearchSopClassUid = "",
SearchTag = "",
SearchText = "TREATMENT",
IncludeSubfolders = true,
SearchInResults = false,
SearchThreads = 1
};

return new Configuration
{
SopClassUidHistory = new List<string> { criteria.SearchSopClassUid },
DicomTagHistory = new List<string> { criteria.SearchTag },
FileTypesHistory = new List<string> { criteria.FileTypes },
SearchPathHistory = new List<string> { criteria.SearchPath },
SearchTextHistory = new List<string> { criteria.SearchText },
HistoryCapacity = 10,
SearchCriteria = criteria
};
}

}
}

0 comments on commit c964d4b

Please sign in to comment.