zaaReloaded2/zaaReloaded2/Controller/SettingsRepository.cs

246 lines
8.1 KiB
C#
Raw Normal View History

2015-07-16 03:28:28 +00:00
/* SettingsRepository.cs
* part of zaaReloaded2
*
* Copyright 2015 Daniel Kraus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
2015-07-16 03:28:28 +00:00
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Reflection;
using zaaReloaded2.Controller.Elements;
using System.Runtime.Serialization.Formatters.Soap;
using System.Web;
using System.Runtime.Serialization;
2015-07-16 03:28:28 +00:00
namespace zaaReloaded2.Controller
{
/// <summary>
/// A repository for zaaReloaded2.Controller.Settings.
/// </summary>
/// <remarks>
/// This class is derived from ApplicationSettingsBase to be able
/// to simply store it in the assembly's properties. However, this
/// causes some confusion because .NET's ApplicationSettings are
/// different from zaaReloaded's Settings.
/// </remarks>
2015-07-16 03:28:28 +00:00
[Serializable]
public class SettingsRepository : ISerializable
2015-07-16 03:28:28 +00:00
{
#region Properties persistence
2015-07-16 03:28:28 +00:00
public static SettingsRepository Load()
{
string s = Properties.Settings.Default.SettingsRepository;
if (String.IsNullOrEmpty(s))
{
return null;
}
else
{
MemoryStream stream = new MemoryStream();
string encoded = Properties.Settings.Default.SettingsRepository;
byte[] bytes = Convert.FromBase64String(encoded);
stream.Write(bytes, 0, bytes.Length);
stream.Position = 0;
SoapFormatter serializer = new SoapFormatter();
return serializer.Deserialize(stream) as SettingsRepository;
}
2015-07-16 03:28:28 +00:00
}
public void Store()
{
MemoryStream stream = new MemoryStream();
SoapFormatter serializer = new SoapFormatter();
serializer.Serialize(stream, this);
stream.Position = 0;
string encoded = Convert.ToBase64String(stream.ToArray());
Properties.Settings.Default.SettingsRepository = encoded;
Properties.Settings.Default.Save();
2015-07-16 03:28:28 +00:00
}
#endregion
#region Properties
public IList<Settings> SettingsList { get; protected set; }
2015-07-16 03:28:28 +00:00
#endregion
#region Constructors
2015-07-16 03:28:28 +00:00
/// <summary>
/// Creates a new instance of a settings repository that will
/// contain a default set of settings.
/// </summary>
2015-07-16 03:28:28 +00:00
public SettingsRepository()
{
SettingsList = new List<Settings>();
CreateDefault();
}
#endregion
#region Serialization
protected SettingsRepository(SerializationInfo info, StreamingContext context)
{
int version = info.GetInt32("Version");
int settingsCount = info.GetInt32("SettingsCount");
SettingsList = new List<Settings>();
for (int i = 0; i < settingsCount; i++)
{
Type type = info.GetValue(SerializationSettingsName(i, "Type"), typeof(Type)) as Type;
Settings s = info.GetValue(SerializationSettingsName(i, "Object"), typeof(Settings)) as Settings;
SettingsList.Add(s);
}
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("Version", Properties.Settings.Default.SerializationVersion);
info.AddValue("SettingsCount", SettingsList.Count);
int i = 0;
foreach (Settings s in SettingsList)
{
info.AddValue(SerializationSettingsName(i, "Type"), s.GetType());
info.AddValue(SerializationSettingsName(i, "Object"), s);
i++;
}
}
string SerializationSettingsName(int index, string info)
{
return String.Format("Settings{0}{1}", index, info);
}
#endregion
2015-08-02 05:19:12 +00:00
#region Public methods
/// <summary>
/// Looks up a Settings object contained in this repository
/// by unique ID.
/// </summary>
/// <param name="uid">GUID to look for.</param>
/// <returns>Settings object with this GUID, or null if the
/// GUID was not found.</returns>
public Settings FindByGuid(Guid uid)
{
return SettingsList.FirstOrDefault(s => s.Uid == uid);
}
/// <summary>
/// Imports one Settings object by deserialization
/// from a stream.
/// </summary>
/// <param name="s">Stream to read from.</param>
/// <returns>True if the import was successful.</returns>
bool Import(Stream s)
{
XmlSerializer ser = new XmlSerializer(typeof(Settings));
Settings settings = ser.Deserialize(s) as Settings;
if (settings != null)
{
SettingsList.Add(settings);
return true;
}
else
{
return false;
}
}
2015-08-02 05:19:12 +00:00
#endregion
#region Private methods
/// <summary>
/// Resets the Settings contained in this SettingsRepository
/// to the default set of settings.
/// </summary>
private void CreateDefault()
{
SettingsList.Clear();
// Shortcut to assembly properties
zaaReloaded2.Properties.Settings def = zaaReloaded2.Properties.Settings.Default;
// TODO: May want to create deep copies of this list below
List<FormatElementBase> defaultItems = new List<FormatElementBase>()
{
new Items(def.DefaultItemsClinicalChem),
new Items(def.DefaultItemsInflammation),
new Items(def.DefaultItemsCardio),
new Items(def.DefaultItemsKidney),
new Items(def.DefaultItemsLiver),
new Items(def.DefaultItemsLipids),
new Items(def.DefaultItemsHematology),
new Items(def.DefaultItemsCoagulation),
new Items(def.DefaultItemsDrugs),
new Items(def.DefaultItemsCollectedUrine),
new Items(def.DefaultItemsSpotUrine),
new Items(def.DefaultItemsOther),
};
// Configure the settings for the ward
SettingsList.Add(
new Settings(
def.SettingsNameWard,
new List<ElementBase>()
{
new TwoColumns(),
new SelectFirstDay(defaultItems),
new NextColumn(),
new SelectLastDay(defaultItems)
},
Guid.Parse(DEFAULT_SETTINGS_1_UID)
)
);
// Configure the settings for the outpatient clinic
SettingsList.Add(
new Settings(
def.SettingsNameClinic,
new List<ElementBase>()
{
new SelectEachDay(defaultItems),
},
Guid.Parse(DEFAULT_SETTINGS_2_UID)
)
);
2015-07-16 03:28:28 +00:00
}
#endregion
#region Private constants
/// <summary>
/// Constant GUID for the first default Settings.
/// </summary>
const string DEFAULT_SETTINGS_1_UID = "EA79DE6C-E999-44F1-9122-929A8AA404CB";
/// <summary>
/// Constant GUID for the second default Settings.
/// </summary>
const string DEFAULT_SETTINGS_2_UID = "783C63B5-A964-4368-B2D0-D4595DCCB952";
#endregion
2015-07-16 03:28:28 +00:00
}
}