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;
|
2015-08-11 14:12:39 +00:00
|
|
|
|
using System.IO;
|
2015-07-16 03:28:28 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
2015-08-11 14:12:39 +00:00
|
|
|
|
using System.Xml.Serialization;
|
|
|
|
|
using System.Reflection;
|
2015-07-31 15:19:49 +00:00
|
|
|
|
using zaaReloaded2.Controller.Elements;
|
2015-08-11 14:12:39 +00:00
|
|
|
|
using System.Runtime.Serialization.Formatters.Soap;
|
|
|
|
|
using System.Web;
|
|
|
|
|
using System.Runtime.Serialization;
|
2015-07-16 03:28:28 +00:00
|
|
|
|
|
|
|
|
|
namespace zaaReloaded2.Controller
|
|
|
|
|
{
|
2015-07-31 15:19:49 +00:00
|
|
|
|
/// <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]
|
2015-08-11 14:12:39 +00:00
|
|
|
|
public class SettingsRepository : ISerializable
|
2015-07-16 03:28:28 +00:00
|
|
|
|
{
|
2015-08-11 14:12:39 +00:00
|
|
|
|
#region Properties persistence
|
2015-07-16 03:28:28 +00:00
|
|
|
|
|
2015-08-11 17:51:23 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Loads the SettingsRepository instance that was persisted in
|
|
|
|
|
/// the assembly properties.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>SettingsRepository that was last stored in the
|
|
|
|
|
/// assembly properties, or a newly created SettingsRepository
|
|
|
|
|
/// if no previously stored exists.</returns>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// The SettingsRepository is serialized using a SoapFormatter, which
|
|
|
|
|
/// creates SOAP XML. Since the assembly properties are stored as
|
|
|
|
|
/// XML as well, the serialized SettingsRepository is converted to
|
|
|
|
|
/// a base-64 string, which does not mess with the properties XML.
|
|
|
|
|
/// </remarks>
|
2015-07-16 03:28:28 +00:00
|
|
|
|
public static SettingsRepository Load()
|
|
|
|
|
{
|
2015-08-11 14:12:39 +00:00
|
|
|
|
string s = Properties.Settings.Default.SettingsRepository;
|
|
|
|
|
if (String.IsNullOrEmpty(s))
|
|
|
|
|
{
|
2015-08-11 14:16:26 +00:00
|
|
|
|
return new SettingsRepository();
|
2015-08-11 14:12:39 +00:00
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2015-08-11 17:51:23 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Stores the SettingsRepository in the assembly properties.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// The SettingsRepository is serialized using a SoapFormatter, which
|
|
|
|
|
/// creates SOAP XML. Since the assembly properties are stored as
|
|
|
|
|
/// XML as well, the serialized SettingsRepository is converted to
|
|
|
|
|
/// a base-64 string, which does not mess with the properties XML.
|
|
|
|
|
/// </remarks>
|
2015-07-16 03:28:28 +00:00
|
|
|
|
public void Store()
|
|
|
|
|
{
|
2015-08-11 14:12:39 +00:00
|
|
|
|
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
|
|
|
|
|
|
2015-08-11 14:12:39 +00:00
|
|
|
|
public IList<Settings> SettingsList { get; protected set; }
|
2015-07-16 03:28:28 +00:00
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2015-08-01 19:23:56 +00:00
|
|
|
|
#region Constructors
|
2015-07-16 03:28:28 +00:00
|
|
|
|
|
2015-08-01 19:23:56 +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()
|
|
|
|
|
{
|
2015-07-31 15:19:49 +00:00
|
|
|
|
SettingsList = new List<Settings>();
|
2015-08-01 19:23:56 +00:00
|
|
|
|
CreateDefault();
|
2015-07-31 15:19:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2015-08-11 14:12:39 +00:00
|
|
|
|
#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);
|
|
|
|
|
}
|
2015-08-09 18:54:43 +00:00
|
|
|
|
|
2015-08-11 19:39:21 +00:00
|
|
|
|
public static bool IsDefaultSettings(Settings settings)
|
2015-08-11 14:12:39 +00:00
|
|
|
|
{
|
2015-08-11 19:39:21 +00:00
|
|
|
|
return (settings.Name == Properties.Settings.Default.SettingsNameClinic + BUILTIN_LABEL ||
|
|
|
|
|
settings.Name == Properties.Settings.Default.SettingsNameWard + BUILTIN_LABEL);
|
2015-08-11 14:12:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-08-02 05:19:12 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
2015-08-01 19:23:56 +00:00
|
|
|
|
#region Private methods
|
2015-07-31 15:19:49 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Resets the Settings contained in this SettingsRepository
|
|
|
|
|
/// to the default set of settings.
|
|
|
|
|
/// </summary>
|
2015-08-01 19:23:56 +00:00
|
|
|
|
private void CreateDefault()
|
2015-07-31 15:19:49 +00:00
|
|
|
|
{
|
|
|
|
|
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(
|
2015-08-11 19:39:21 +00:00
|
|
|
|
def.SettingsNameWard + BUILTIN_LABEL,
|
2015-07-31 15:19:49 +00:00
|
|
|
|
new List<ElementBase>()
|
|
|
|
|
{
|
2015-08-09 18:54:43 +00:00
|
|
|
|
new TwoColumns(),
|
2015-07-31 15:19:49 +00:00
|
|
|
|
new SelectFirstDay(defaultItems),
|
2015-08-09 18:54:43 +00:00
|
|
|
|
new NextColumn(),
|
2015-07-31 15:19:49 +00:00
|
|
|
|
new SelectLastDay(defaultItems)
|
2015-08-11 14:12:39 +00:00
|
|
|
|
},
|
|
|
|
|
Guid.Parse(DEFAULT_SETTINGS_1_UID)
|
2015-07-31 15:19:49 +00:00
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Configure the settings for the outpatient clinic
|
|
|
|
|
SettingsList.Add(
|
|
|
|
|
new Settings(
|
2015-08-11 19:39:21 +00:00
|
|
|
|
def.SettingsNameClinic + BUILTIN_LABEL,
|
2015-07-31 15:19:49 +00:00
|
|
|
|
new List<ElementBase>()
|
|
|
|
|
{
|
|
|
|
|
new SelectEachDay(defaultItems),
|
2015-08-11 14:12:39 +00:00
|
|
|
|
},
|
|
|
|
|
Guid.Parse(DEFAULT_SETTINGS_2_UID)
|
2015-07-31 15:19:49 +00:00
|
|
|
|
)
|
|
|
|
|
);
|
2015-07-16 03:28:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
2015-08-11 14:12:39 +00:00
|
|
|
|
|
2015-08-11 19:39:21 +00:00
|
|
|
|
#region Constants
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Label that is appended to the default Settings' names.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public const string BUILTIN_LABEL = " (eingebaut)";
|
2015-08-11 14:12:39 +00:00
|
|
|
|
|
|
|
|
|
/// <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
|
|
|
|
}
|
|
|
|
|
}
|