Index: Application/Ringtoets/src/Application.Ringtoets.Storage/StorageSqLite.cs =================================================================== diff -u -r0155f2f233aefc7951782b302e1398e02c00f298 -r3a8bff057967bdb42389382472f6ce55789a0ced --- Application/Ringtoets/src/Application.Ringtoets.Storage/StorageSqLite.cs (.../StorageSqLite.cs) (revision 0155f2f233aefc7951782b302e1398e02c00f298) +++ Application/Ringtoets/src/Application.Ringtoets.Storage/StorageSqLite.cs (.../StorageSqLite.cs) (revision 3a8bff057967bdb42389382472f6ce55789a0ced) @@ -1,36 +1,119 @@ -using Application.Ringtoets.Storage.Converter; +using System; +using System.Data.Entity; +using System.Data.Entity.Core; +using System.IO; +using Application.Ringtoets.Storage.Converter; +using Application.Ringtoets.Storage.Properties; using Core.Common.Base.Data; namespace Application.Ringtoets.Storage { + /// + /// This class interacts with an SQLite database file. + /// public class StorageSqLite { + private readonly string connectionString; + /// + /// Creates a new instance of . + /// + /// Path to database file. + public StorageSqLite(string databaseFilePath) + { + if (!File.Exists(databaseFilePath)) + { + var message = Resources.Error_File_does_not_exist; + throw new FileNotFoundException(message); + } + connectionString = SqLiteStorageConnection.BuildConnectionString(databaseFilePath); + } + + /// + /// Tests if a connection can be made. + /// + /// Returns true if a valid connection can be made, false otherwise. + public bool TestConnection() + { + if (string.IsNullOrWhiteSpace(connectionString)) + { + return false; + } + + using (var dbContext = new RingtoetsEntities(connectionString)) + { + try + { + dbContext.Database.Initialize(true); + dbContext.Database.Connection.Open(); + dbContext.ProjectEntities.Load(); + return true; + } + catch (InvalidOperationException) + { + return false; + } + catch (EntityCommandExecutionException) + { + return false; + } + } + } + + /// /// Saves the at the default location. /// /// to save. /// Returns the number of changes, see . public int SaveProject(Project project) { - using (var dbContext = new RingtoetsEntities()) + using (var dbContext = new RingtoetsEntities(connectionString)) { - ProjectEntityConverter.UpdateProjectEntity(dbContext.ProjectEntities, project); - - return dbContext.SaveChanges(); + var changes = 0; + try + { + ProjectEntityConverter.UpdateProjectEntity(dbContext.ProjectEntities, project); + changes = dbContext.SaveChanges(); + } + catch (Exception e) + { + Console.WriteLine(e.InnerException); + } + return changes; } } /// /// Attempts to load the from the databaseconnection . /// - /// /// Returns a new instance of with the data from the database or null when not found. - public Project LoadProject(long projectId) + public Project LoadProject() { - using (var dbContext = new RingtoetsEntities()) + using (var dbContext = new RingtoetsEntities(connectionString)) { - return ProjectEntityConverter.GetProject(dbContext.ProjectEntities, projectId); + try + { + return ProjectEntityConverter.GetProject(dbContext.ProjectEntities); + } + catch (Exception e) + { + Console.WriteLine(e.InnerException); + } } + return null; } } + + /// + /// Partial implementation of that support a connection string and dos not read the connection string from the configuration. + /// + public partial class RingtoetsEntities + { + /// + /// A new instance of . + /// + /// A connection string. + public RingtoetsEntities(string connString) + : base(connString) {} + } } \ No newline at end of file