Index: Ringtoets/Piping/src/Ringtoets.Piping.IO/PipingSoilProfileReader.cs =================================================================== diff -u -r15d2770669092ea9574682421c755fc9b6c2e16f -r41d06d91a2ef710ad07b3f474f190400751e09b8 --- Ringtoets/Piping/src/Ringtoets.Piping.IO/PipingSoilProfileReader.cs (.../PipingSoilProfileReader.cs) (revision 15d2770669092ea9574682421c755fc9b6c2e16f) +++ Ringtoets/Piping/src/Ringtoets.Piping.IO/PipingSoilProfileReader.cs (.../PipingSoilProfileReader.cs) (revision 41d06d91a2ef710ad07b3f474f190400751e09b8) @@ -82,7 +82,7 @@ return null; } - var dimensionValue = TryRead(dimensionColumn); + var dimensionValue = Read(dimensionColumn); return dimensionValue == 1 ? ReadPipingProfile1D() : ReadPipingProfile2D(); } @@ -106,10 +106,19 @@ private PipingSoilProfile ReadPipingProfile1D() { - var profileName = TryRead(profileNameColumn); - var layerCount = TryRead(layerCountColumn); - var bottom = TryRead(bottomColumn); + var profileName = Read(profileNameColumn); + var layerCount = Read(layerCountColumn); + double bottom; + try + { + bottom = Read(bottomColumn); + } catch (InvalidCastException e) { + SkipRecords((int)layerCount); + var message = string.Format(Resources.PipingSoilProfileReader_Profile_0_has_invalid_value_on_column_1_, profileName, intersectionXColumn); + throw new PipingSoilProfileReadException(message, e); + } + var soilProfileBuilder = new SoilProfileBuilder1D(profileName, bottom); for (var i = 1; i <= layerCount; i++) @@ -131,12 +140,22 @@ /// to be build. private PipingSoilProfile ReadPipingProfile2D() { - var profileName = TryRead(profileNameColumn); - var layerCount = TryRead(layerCountColumn); - var intersectionX = TryRead(intersectionXColumn); + var profileName = Read(profileNameColumn); + var layerCount = Read(layerCountColumn); try { + double intersectionX; + try + { + intersectionX = TryRead(intersectionXColumn); + } + catch (InvalidCastException e) + { + SkipRecords((int)layerCount); + var message = string.Format(Resources.PipingSoilProfileReader_Profile_0_has_invalid_value_on_column_1_, profileName, intersectionXColumn); + throw new PipingSoilProfileReadException(message, e); + } var soilProfileBuilder = new SoilProfileBuilder2D(profileName, intersectionX); for (int i = 1; i <= layerCount; i++) @@ -145,27 +164,49 @@ { soilProfileBuilder.Add(ReadPiping2DSoilLayer()); } - catch (SoilLayer2DConversionException e) + catch (XmlException e) { - HandleLayerParseException(layerCount, i, profileName, e); + SkipRecords((int)layerCount + 1 - i); + + var format = string.Format(Resources.PipingSoilProfileReader_CouldNotParseGeometryOfLayer_0_InProfile_1_, i, profileName); + throw new PipingSoilProfileReadException( + format, e); } - catch (XmlException e) + catch (SoilProfileBuilderException e) { - HandleLayerParseException(layerCount, i, profileName, e); + SkipRecords((int) layerCount + 1 - i); + + var format = string.Format(Resources.PipingSoilProfileReader_CouldNotParseGeometryOfLayer_0_InProfile_1_, i, profileName); + throw new PipingSoilProfileReadException( + format, e); } + catch (InvalidCastException e) + { + SkipRecords((int)layerCount + 1 - i); + + var message = string.Format(Resources.PipingSoilProfileReader_Profile_0_has_invalid_value_on_column_1_, profileName, intersectionXColumn); + throw new PipingSoilProfileReadException(message, e); + } MoveNext(); } - return soilProfileBuilder.Build(); + try + { + return soilProfileBuilder.Build(); + } + catch (SoilProfileBuilderException e) + { + SkipRecords((int) 0 + 1 - 1); + var exception = new PipingSoilProfileReadException( + string.Format(Resources.PipingSoilProfileReader_CouldNotParseGeometryOfLayer_0_InProfile_1_, 1, profileName), e); + + throw exception; + } } catch (ArgumentException e) { HandleCriticalBuildException(profileName, e); } - catch (SoilProfileBuilderException e) - { - HandleCriticalBuildException(profileName, e); - } return null; } @@ -175,15 +216,6 @@ throw new CriticalFileReadException(message, e); } - private void HandleLayerParseException(long layerCount, int i, string profileName, Exception e) - { - SkipRecords((int) layerCount + 1 - i); - var exception = new PipingSoilProfileReadException( - string.Format(Resources.PipingSoilProfileReader_CouldNotParseGeometryOfLayer_0_InProfile_1_, i, profileName), e); - - throw exception; - } - private void SkipRecords(int count) { for(int i = 0; i < count; i++) @@ -192,12 +224,6 @@ } } - private void SetReaderToFirstRecord() - { - InitializeDataReader(); - MoveNext(); - } - /// /// Reads a value at column from the database. /// @@ -215,39 +241,25 @@ return default(T); } - try - { - return (T)dbValue; - } - catch (InvalidCastException) - { - throw new CriticalFileReadException(Resources.PipingSoilProfileReader_Invalid_value_on_column); - } + return (T)dbValue; } - - private void OpenConnection(string dbFile) - { - var connectionStringBuilder = new SQLiteConnectionStringBuilder - { - FailIfMissing = true, - DataSource = dbFile, - ReadOnly = true, - ForeignKeys = true - }; - connection = new SQLiteConnection(connectionStringBuilder.ConnectionString); - Connect(); - } - - private void Connect() + /// + /// Reads a value at column from the database. + /// + /// The expected type of value in the column with name . + /// The name of the column to read from. + /// The read value from the column with name . + /// Thrown when the value in the column was not of type . + private T Read(string columnName) { try { - connection.Open(); + return (T)dataReader[columnName]; } - catch (SQLiteException) + catch (InvalidCastException) { - connection.Dispose(); + throw new CriticalFileReadException(string.Format(Resources.PipingSoilProfileReader_Critical_Unexpected_value_on_column_0_, columnName)); } } @@ -277,6 +289,7 @@ private SoilLayer2D ReadPiping2DSoilLayer() { var geometryValue = TryRead(layerGeometryColumn); + var isAquiferValue = TryRead(isAquiferColumn); var belowPhreaticLevelValue = TryRead(belowPhreaticLevelColumn); var abovePhreaticLevelValue = TryRead(abovePhreaticLevelColumn); @@ -291,6 +304,38 @@ return pipingSoilLayer; } + private void SetReaderToFirstRecord() + { + InitializeDataReader(); + MoveNext(); + } + + private void OpenConnection(string dbFile) + { + var connectionStringBuilder = new SQLiteConnectionStringBuilder + { + FailIfMissing = true, + DataSource = dbFile, + ReadOnly = true, + ForeignKeys = true + }; + + connection = new SQLiteConnection(connectionStringBuilder.ConnectionString); + Connect(); + } + + private void Connect() + { + try + { + connection.Open(); + } + catch (SQLiteException) + { + connection.Dispose(); + } + } + /// /// Prepares the two queries required for obtaining all the SoilProfile1D and SoilProfile2D with an x defined /// to take an intersection from. Since two separate queries are used, the will @@ -491,7 +536,7 @@ private void GetCount() { dataReader.Read(); - Count = (int)TryRead(profileCountColumn); + Count = (int)Read(profileCountColumn); dataReader.NextResult(); } }