131 lines
4.1 KiB
C#
131 lines
4.1 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Xml.Serialization;
|
|
|
|
namespace CadParamPluging.Common
|
|
{
|
|
public static class DropdownOptionsStore
|
|
{
|
|
private const string FolderName = "CadParamPluging";
|
|
private const string FileName = "dropdown-options.xml";
|
|
|
|
public static string GetFilePath()
|
|
{
|
|
var dir = Path.Combine(
|
|
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
|
|
FolderName);
|
|
return Path.Combine(dir, FileName);
|
|
}
|
|
|
|
public static DropdownOptions Load()
|
|
{
|
|
try
|
|
{
|
|
var path = GetFilePath();
|
|
if (!File.Exists(path))
|
|
{
|
|
return DropdownOptions.CreateDefault();
|
|
}
|
|
|
|
using (var stream = File.OpenRead(path))
|
|
{
|
|
var serializer = new XmlSerializer(typeof(DropdownOptions));
|
|
var obj = serializer.Deserialize(stream) as DropdownOptions;
|
|
if (obj == null)
|
|
{
|
|
return DropdownOptions.CreateDefault();
|
|
}
|
|
|
|
obj.Normalize();
|
|
EnsureNonEmptyByFallbackToDefault(obj);
|
|
return obj;
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
return DropdownOptions.CreateDefault();
|
|
}
|
|
}
|
|
|
|
public static void Save(DropdownOptions options)
|
|
{
|
|
if (options == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(options));
|
|
}
|
|
|
|
options.Normalize();
|
|
ValidateNonEmpty(options);
|
|
|
|
var path = GetFilePath();
|
|
var dir = Path.GetDirectoryName(path);
|
|
if (!string.IsNullOrEmpty(dir))
|
|
{
|
|
Directory.CreateDirectory(dir);
|
|
}
|
|
|
|
var tempPath = path + ".tmp";
|
|
var serializer = new XmlSerializer(typeof(DropdownOptions));
|
|
using (var stream = File.Create(tempPath))
|
|
{
|
|
serializer.Serialize(stream, options);
|
|
}
|
|
|
|
if (File.Exists(path))
|
|
{
|
|
File.Delete(path);
|
|
}
|
|
|
|
File.Move(tempPath, path);
|
|
}
|
|
|
|
private static void ValidateNonEmpty(DropdownOptions options)
|
|
{
|
|
if (options.DeliveryStatuses == null || options.DeliveryStatuses.Count == 0)
|
|
{
|
|
throw new InvalidOperationException("交付状态不能为空。");
|
|
}
|
|
|
|
if (options.ProcessMethods == null || options.ProcessMethods.Count == 0)
|
|
{
|
|
throw new InvalidOperationException("工艺方法不能为空。");
|
|
}
|
|
|
|
if (options.StructuralFeatures == null || options.StructuralFeatures.Count == 0)
|
|
{
|
|
throw new InvalidOperationException("结构特征不能为空。");
|
|
}
|
|
|
|
if (options.SpecialConditions == null || options.SpecialConditions.Count == 0)
|
|
{
|
|
throw new InvalidOperationException("特殊条件不能为空。");
|
|
}
|
|
}
|
|
|
|
private static void EnsureNonEmptyByFallbackToDefault(DropdownOptions options)
|
|
{
|
|
var defaults = DropdownOptions.CreateDefault();
|
|
|
|
if (options.DeliveryStatuses == null || options.DeliveryStatuses.Count == 0)
|
|
{
|
|
options.DeliveryStatuses = defaults.DeliveryStatuses;
|
|
}
|
|
|
|
if (options.ProcessMethods == null || options.ProcessMethods.Count == 0)
|
|
{
|
|
options.ProcessMethods = defaults.ProcessMethods;
|
|
}
|
|
|
|
if (options.StructuralFeatures == null || options.StructuralFeatures.Count == 0)
|
|
{
|
|
options.StructuralFeatures = defaults.StructuralFeatures;
|
|
}
|
|
|
|
if (options.SpecialConditions == null || options.SpecialConditions.Count == 0)
|
|
{
|
|
options.SpecialConditions = defaults.SpecialConditions;
|
|
}
|
|
}
|
|
}
|
|
}
|