Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 10 additions & 24 deletions Backend/Data/Configurations/ConfigurationBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,44 +13,30 @@
// You should have received a copy of the GNU General Public License
// along with AlarmWorkflow. If not, see <http://www.gnu.org/licenses/>.

using System;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration;
using System.Linq.Expressions;
using AlarmWorkflow.Backend.Data.Types;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace AlarmWorkflow.Backend.Data.Configurations
{
/// <summary>
/// Abstract base class for a entity type configuration.
/// </summary>
/// <typeparam name="TEntity"></typeparam>
abstract class ConfigurationBase<TEntity> : EntityTypeConfiguration<TEntity> where TEntity : EntityBase
abstract class ConfigurationBase<TEntity> : IEntityTypeConfiguration<TEntity> where TEntity : EntityBase
{
#region Constructors

/// <summary>
/// Initializes a new instance of the <see cref="ConfigurationBase{TEntity}"/> class.
/// </summary>
protected ConfigurationBase()
: base()
{
ToTable(GetTableName());

MapKeys();
}

#endregion

#region Methods

/// <summary>
/// Performs automatic mapping of the <see cref="EntityBase.Id"/> key.
/// </summary>
protected virtual void MapKeys()
public virtual void Configure(EntityTypeBuilder<TEntity> builder)
{
HasKey(e => e.Id);
Property(e => e.Id).HasColumnName("id").HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
builder.ToTable(GetTableName());
builder.HasKey(e => e.Id);
builder.Property(e => e.Id).HasColumnName("id").ValueGeneratedOnAdd();
}

/// <summary>
/// When overridden in a derived class, customizes the table name for the provided <typeparamref name="TEntity"/>.
/// </summary>
Expand Down
19 changes: 10 additions & 9 deletions Backend/Data/Configurations/DispositionedResourceConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,24 @@
// along with AlarmWorkflow. If not, see <http://www.gnu.org/licenses/>.

using AlarmWorkflow.Backend.Data.Types;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace AlarmWorkflow.Backend.Data.Configurations
{
// ReSharper disable once UnusedMember.Global
class DispositionedResourceConfiguration : ConfigurationBase<DispositionedResourceData>
{
#region Constructors
#region Methods

public DispositionedResourceConfiguration()
public override void Configure(EntityTypeBuilder<DispositionedResourceData> builder)
{
Property(dr => dr.OperationId).HasColumnName("operation_id").IsRequired();
Property(dr => dr.Timestamp).HasColumnName("timestamp").IsRequired();
Property(dr => dr.EmkResourceId).HasColumnName("emkresourceid").IsRequired();
}

#endregion
base.Configure(builder);

#region Methods
builder.Property(dr => dr.OperationId).HasColumnName("operation_id").IsRequired();
builder.Property(dr => dr.Timestamp).HasColumnName("timestamp").IsRequired();
builder.Property(dr => dr.EmkResourceId).HasColumnName("emkresourceid").IsRequired();
}

protected override string GetTableName()
{
Expand Down
90 changes: 43 additions & 47 deletions Backend/Data/Configurations/OperationConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,68 +14,64 @@
// along with AlarmWorkflow. If not, see <http://www.gnu.org/licenses/>.

using AlarmWorkflow.Backend.Data.Types;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace AlarmWorkflow.Backend.Data.Configurations
{
// ReSharper disable once UnusedMember.Global
class OperationConfiguration : ConfigurationBase<OperationData>
{
#region Constructors
#region Methods

public OperationConfiguration()
public override void Configure(EntityTypeBuilder<OperationData> builder)
{
Property(o => o.IsAcknowledged).HasColumnName("acknowledged").IsRequired();
Property(o => o.Guid).HasColumnName("operationguid").IsRequired();
Property(o => o.OperationNumber).HasColumnName("operationnumber");
Property(o => o.IncomeAt).HasColumnName("timestampincome");
Property(o => o.AlarmAt).HasColumnName("timestampalarm");
Property(o => o.Messenger).HasColumnName("messenger");
Property(o => o.Comment).HasColumnName("comment");
Property(o => o.Plan).HasColumnName("plan");
Property(o => o.Picture).HasColumnName("picture");
Property(o => o.Priority).HasColumnName("priority");
base.Configure(builder);

Property(o => o.Einsatzort.Street).HasColumnName("einsatzortstreet");
Property(o => o.Einsatzort.StreetNumber).HasColumnName("einsatzortstreetnumber");
Property(o => o.Einsatzort.City).HasColumnName("einsatzortcity");
Property(o => o.Einsatzort.ZipCode).HasColumnName("einsatzortzipcode");
Property(o => o.Einsatzort.Intersection).HasColumnName("einsatzortintersection");
Property(o => o.Einsatzort.Property).HasColumnName("einsatzortproperty");
Property(o => o.Einsatzort.Location).HasColumnName("einsatzortlocation");
Property(o => o.Einsatzort.GeoLatLng).HasColumnName("einsatzortlatlng");
builder.Property(o => o.IsAcknowledged).HasColumnName("acknowledged").IsRequired();
builder.Property(o => o.Guid).HasColumnName("operationguid").IsRequired();
builder.Property(o => o.OperationNumber).HasColumnName("operationnumber");
builder.Property(o => o.IncomeAt).HasColumnName("timestampincome");
builder.Property(o => o.AlarmAt).HasColumnName("timestampalarm");
builder.Property(o => o.Messenger).HasColumnName("messenger");
builder.Property(o => o.Comment).HasColumnName("comment");
builder.Property(o => o.Plan).HasColumnName("plan");
builder.Property(o => o.Picture).HasColumnName("picture");
builder.Property(o => o.Priority).HasColumnName("priority");

Property(o => o.Zielort.Street).HasColumnName("zielortstreet");
Property(o => o.Zielort.StreetNumber).HasColumnName("zielortstreetnumber");
Property(o => o.Zielort.City).HasColumnName("zielortcity");
Property(o => o.Zielort.ZipCode).HasColumnName("zielortzipcode");
Property(o => o.Zielort.Intersection).HasColumnName("zielortintersection");
Property(o => o.Zielort.Property).HasColumnName("zielortproperty");
Property(o => o.Zielort.Location).HasColumnName("zielortlocation");
Property(o => o.Zielort.GeoLatLng).HasColumnName("zielortlatlng");
builder.OwnsOne(o => o.Einsatzort).Property(o => o.Street).HasColumnName("einsatzortstreet");
builder.OwnsOne(o => o.Einsatzort).Property(o => o.StreetNumber).HasColumnName("einsatzortstreetnumber");
builder.OwnsOne(o => o.Einsatzort).Property(o => o.City).HasColumnName("einsatzortcity");
builder.OwnsOne(o => o.Einsatzort).Property(o => o.ZipCode).HasColumnName("einsatzortzipcode");
builder.OwnsOne(o => o.Einsatzort).Property(o => o.Intersection).HasColumnName("einsatzortintersection");
builder.OwnsOne(o => o.Einsatzort).Property(o => o.Property).HasColumnName("einsatzortproperty");
builder.OwnsOne(o => o.Einsatzort).Property(o => o.Location).HasColumnName("einsatzortlocation");
builder.OwnsOne(o => o.Einsatzort).Property(o => o.GeoLatLng).HasColumnName("einsatzortlatlng");

Property(o => o.Keywords.Keyword).HasColumnName("keyword");
Property(o => o.Keywords.EmergencyKeyword).HasColumnName("keywordmisc");
Property(o => o.Keywords.B).HasColumnName("keywordb");
Property(o => o.Keywords.R).HasColumnName("keywordr");
Property(o => o.Keywords.S).HasColumnName("keywords");
Property(o => o.Keywords.T).HasColumnName("keywordt");
builder.OwnsOne(o => o.Zielort).Property(o => o.Street).HasColumnName("zielortstreet");
builder.OwnsOne(o => o.Zielort).Property(o => o.StreetNumber).HasColumnName("zielortstreetnumber");
builder.OwnsOne(o => o.Zielort).Property(o => o.City).HasColumnName("zielortcity");
builder.OwnsOne(o => o.Zielort).Property(o => o.ZipCode).HasColumnName("zielortzipcode");
builder.OwnsOne(o => o.Zielort).Property(o => o.Intersection).HasColumnName("zielortintersection");
builder.OwnsOne(o => o.Zielort).Property(o => o.Property).HasColumnName("zielortproperty");
builder.OwnsOne(o => o.Zielort).Property(o => o.Location).HasColumnName("zielortlocation");
builder.OwnsOne(o => o.Zielort).Property(o => o.GeoLatLng).HasColumnName("zielortlatlng");

Property(o => o.Loops).HasColumnName("loopscsv");
Property(o => o.CustomData).HasColumnName("customdatajson");
builder.OwnsOne(o => o.Keywords).Property(o => o.Keyword).HasColumnName("keyword");
builder.OwnsOne(o => o.Keywords).Property(o => o.EmergencyKeyword).HasColumnName("keywordmisc");
builder.OwnsOne(o => o.Keywords).Property(o => o.B).HasColumnName("keywordb");
builder.OwnsOne(o => o.Keywords).Property(o => o.R).HasColumnName("keywordr");
builder.OwnsOne(o => o.Keywords).Property(o => o.S).HasColumnName("keywords");
builder.OwnsOne(o => o.Keywords).Property(o => o.T).HasColumnName("keywordt");

HasMany(o => o.Resources)
.WithRequired(r => r.Operation)
.HasForeignKey(r => r.OperationId)
.WillCascadeOnDelete();
builder.Property(o => o.Loops).HasColumnName("loopscsv");
builder.Property(o => o.CustomData).HasColumnName("customdatajson");

HasMany(o => o.DispositionedResources)
.WithRequired(dr => dr.Operation)
.HasForeignKey(dr => dr.OperationId)
.WillCascadeOnDelete();
}
builder.HasMany(o => o.Resources).WithOne(r => r.Operation).IsRequired().OnDelete(DeleteBehavior.Cascade);

#endregion
builder.HasMany(o => o.DispositionedResources).WithOne(r => r.Operation).IsRequired().OnDelete(DeleteBehavior.Cascade);
}

#region Methods

protected override string GetTableName()
{
Expand Down
21 changes: 11 additions & 10 deletions Backend/Data/Configurations/OperationResourceConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,25 @@
// along with AlarmWorkflow. If not, see <http://www.gnu.org/licenses/>.

using AlarmWorkflow.Backend.Data.Types;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace AlarmWorkflow.Backend.Data.Configurations
{
// ReSharper disable once UnusedMember.Global
class OperationResourceConfiguration : ConfigurationBase<OperationResourceData>
{
#region Constructors
#region Methods

public OperationResourceConfiguration()
public override void Configure(EntityTypeBuilder<OperationResourceData> builder)
{
Property(or => or.OperationId).HasColumnName("operation_id").IsRequired();
Property(or => or.Timestamp).HasColumnName("timestamp");
Property(or => or.FullName).HasColumnName("fullname");
Property(or => or.Equipment).HasColumnName("equipmentcsv");
}

#endregion
base.Configure(builder);

#region Methods
builder.Property(or => or.OperationId).HasColumnName("operation_id").IsRequired();
builder.Property(or => or.Timestamp).HasColumnName("timestamp");
builder.Property(or => or.FullName).HasColumnName("fullname");
builder.Property(or => or.Equipment).HasColumnName("equipmentcsv");
}

protected override string GetTableName()
{
Expand Down
20 changes: 10 additions & 10 deletions Backend/Data/Configurations/SettingConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@
// along with AlarmWorkflow. If not, see <http://www.gnu.org/licenses/>.

using AlarmWorkflow.Backend.Data.Types;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace AlarmWorkflow.Backend.Data.Configurations
{
// ReSharper disable once UnusedMember.Global
class SettingConfiguration : ConfigurationBase<SettingData>
{
#region Constructors
#region Methods

public SettingConfiguration()
: base()
public override void Configure(EntityTypeBuilder<SettingData> builder)
{
Property(s => s.Identifier).IsRequired().HasColumnName("identifier");
Property(s => s.Name).IsRequired().HasColumnName("name");
Property(s => s.Value).HasColumnName("value");
}

#endregion
base.Configure(builder);

#region Methods
builder.Property(s => s.Identifier).IsRequired().HasColumnName("identifier");
builder.Property(s => s.Name).IsRequired().HasColumnName("name");
builder.Property(s => s.Value).HasColumnName("value");
}

protected override string GetTableName()
{
Expand Down
73 changes: 55 additions & 18 deletions Backend/Data/Contexts/ContextCreationOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,27 @@
// along with AlarmWorkflow. If not, see <http://www.gnu.org/licenses/>.

using AlarmWorkflow.Backend.ServiceContracts.Communication;
using AlarmWorkflow.Shared.Core;
using AlarmWorkflow.Shared.Diagnostics;
using System;
using System.Linq;
using System.Threading;

namespace AlarmWorkflow.Backend.Data.Contexts
{
sealed class ContextCreationOptions
{
#region Enums

public enum DatabaseEngine { MySQL, SQLite }

#endregion

#region Constants

private const string ConnectionStringTemplate = "server={0};Port={1};User Id={2};Password={3};database={4};Persist Security Info=True";
private const string ConnectionStringTemplateMySQL = "server={0};Port={1};User Id={2};Password={3};database={4};Persist Security Info=True";

private const string ConnectionStringTemplateSQLite = "Data Source={0};";

#endregion

Expand All @@ -32,6 +45,10 @@ sealed class ContextCreationOptions
/// </summary>
public string HostName { get; set; }
/// <summary>
/// Gets/sets the host name of the server to connect to.
/// </summary>
public DatabaseEngine Engine { get; set; }
/// <summary>
/// Gets/sets the port of the server to connect to.
/// </summary>
public int Port { get; set; }
Expand All @@ -47,30 +64,31 @@ sealed class ContextCreationOptions
/// Gets/sets the name of the database to connect to.
/// </summary>
public string DatabaseName { get; set; }
/// <summary>
/// Gets/sets the name of the database path to connect to.
/// </summary>
public string SQLiteDatabase { get; set; }

#endregion

#region Constructors
#region Methods

/// <summary>
/// Initializes a new instance of the <see cref="ContextCreationOptions"/> class.
/// Returns the connection string from the values of this instancefor a MySQL database.
/// </summary>
public ContextCreationOptions()
/// <returns></returns>
public string GetMySqlConnectionString()
{

return string.Format(ConnectionStringTemplateMySQL, HostName, Port, UserId, Password, DatabaseName);
}

#endregion

#region Methods

/// <summary>
/// Returns the connection string from the values of this instance.
/// Returns the connection string from the values of this instance for a sqlite database.
/// </summary>
/// <returns></returns>
public string GetConnectionString()
public string GetSQLiteConnectionString()
{
return string.Format(ConnectionStringTemplate, HostName, Port, UserId, Password, DatabaseName);
return string.Format(ConnectionStringTemplateSQLite, SQLiteDatabase);
}

#endregion
Expand All @@ -83,15 +101,34 @@ public string GetConnectionString()
/// <returns>An instance of <see cref="ContextCreationOptions"/> which uses the values from the backend configuration.</returns>
public static ContextCreationOptions CreateFromSettings()
{
ContextCreationOptions options = new ContextCreationOptions();
options.HostName = ServiceFactory.BackendConfigurator.Get("Server.DB.HostName");
options.Port = int.Parse(ServiceFactory.BackendConfigurator.Get("Server.DB.Port"));
options.UserId = ServiceFactory.BackendConfigurator.Get("Server.DB.UserId");
options.Password = ServiceFactory.BackendConfigurator.Get("Server.DB.Password");
options.DatabaseName = ServiceFactory.BackendConfigurator.Get("Server.DB.DatabaseName");
var options = new ContextCreationOptions
{
Engine = GetEngine(),
HostName = ServiceFactory.BackendConfigurator.Get("Server.DB.HostName"),
Port = int.Parse(ServiceFactory.BackendConfigurator.Get("Server.DB.Port")),
UserId = ServiceFactory.BackendConfigurator.Get("Server.DB.UserId"),
Password = ServiceFactory.BackendConfigurator.Get("Server.DB.Password"),
DatabaseName = ServiceFactory.BackendConfigurator.Get("Server.DB.DatabaseName"),
SQLiteDatabase = Utilities.GetLocalAppDataFolderFileName(ServiceFactory.BackendConfigurator.Get("Server.DB.SQLite.Database"))
};
return options;
}

private static DatabaseEngine GetEngine()
{
string engineName = ServiceFactory.BackendConfigurator.Get("Server.DB.Engine");

try
{
return (DatabaseEngine)Enum.Parse(typeof(DatabaseEngine), engineName, true);
}
catch (ArgumentException)
{
Logger.Instance.LogFormat(LogType.Warning, typeof(ContextCreationOptions), Properties.Resources.EngineNotFound, engineName, "MySQL");
return DatabaseEngine.MySQL;
}
}

#endregion
}
}
Loading