// Copyright (C) Stichting Deltares 2018. All rights reserved.
//
// This file is part of Ringtoets.
//
// Ringtoets is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see .
//
// All names, logos, and references to "Deltares" are registered trademarks of
// Stichting Deltares and remain full property of Stichting Deltares at all times.
// All rights reserved.
using System;
using System.ComponentModel;
using Core.Common.Controls.DataGrid;
using Ringtoets.ClosingStructures.Data;
using Ringtoets.Common.Data.AssessmentSection;
using Ringtoets.Common.Data.Exceptions;
using Ringtoets.Common.Forms.Helpers;
using Ringtoets.Common.Forms.TypeConverters;
using Ringtoets.Common.Forms.Views;
using Ringtoets.Common.Primitives;
using Riskeer.AssemblyTool.Data;
namespace Ringtoets.ClosingStructures.Forms.Views
{
///
/// This class represents a row of .
///
public class ClosingStructuresFailureMechanismSectionResultRow : FailureMechanismSectionResultRow
{
private readonly int simpleAssessmentResultIndex;
private readonly int detailedAssessmentResultIndex;
private readonly int detailedAssessmentProbabilityIndex;
private readonly int tailorMadeAssessmentResultIndex;
private readonly int tailorMadeAssessmentProbabilityIndex;
private readonly int simpleAssemblyCategoryGroupIndex;
private readonly int detailedAssemblyCategoryGroupIndex;
private readonly int tailorMadeAssemblyCategoryGroupIndex;
private readonly int combinedAssemblyCategoryGroupIndex;
private readonly int combinedAssemblyProbabilityIndex;
private readonly int manualAssemblyProbabilityIndex;
private readonly ClosingStructuresFailureMechanism failureMechanism;
private readonly IAssessmentSection assessmentSection;
private FailureMechanismSectionAssemblyCategoryGroup simpleAssemblyCategoryGroup;
private FailureMechanismSectionAssemblyCategoryGroup detailedAssemblyCategoryGroup;
private FailureMechanismSectionAssemblyCategoryGroup tailorMadeAssemblyCategoryGroup;
private FailureMechanismSectionAssemblyCategoryGroup combinedAssemblyCategoryGroup;
///
/// Creates a new instance of .
///
/// The to wrap
/// so that it can be displayed as a row.
/// The failure mechanism the result belongs to.
/// The assessment section the result belongs to.
/// The property values required to create an instance of
/// .
/// Thrown when any parameter is null.
/// Thrown when
/// is a valid value, but unsupported.
internal ClosingStructuresFailureMechanismSectionResultRow(ClosingStructuresFailureMechanismSectionResult sectionResult,
ClosingStructuresFailureMechanism failureMechanism,
IAssessmentSection assessmentSection,
ConstructionProperties constructionProperties)
: base(sectionResult)
{
if (failureMechanism == null)
{
throw new ArgumentNullException(nameof(failureMechanism));
}
if (assessmentSection == null)
{
throw new ArgumentNullException(nameof(assessmentSection));
}
if (constructionProperties == null)
{
throw new ArgumentNullException(nameof(constructionProperties));
}
this.failureMechanism = failureMechanism;
this.assessmentSection = assessmentSection;
simpleAssessmentResultIndex = constructionProperties.SimpleAssessmentResultIndex;
detailedAssessmentResultIndex = constructionProperties.DetailedAssessmentResultIndex;
detailedAssessmentProbabilityIndex = constructionProperties.DetailedAssessmentProbabilityIndex;
tailorMadeAssessmentResultIndex = constructionProperties.TailorMadeAssessmentResultIndex;
tailorMadeAssessmentProbabilityIndex = constructionProperties.TailorMadeAssessmentProbabilityIndex;
simpleAssemblyCategoryGroupIndex = constructionProperties.SimpleAssemblyCategoryGroupIndex;
detailedAssemblyCategoryGroupIndex = constructionProperties.DetailedAssemblyCategoryGroupIndex;
tailorMadeAssemblyCategoryGroupIndex = constructionProperties.TailorMadeAssemblyCategoryGroupIndex;
combinedAssemblyCategoryGroupIndex = constructionProperties.CombinedAssemblyCategoryGroupIndex;
combinedAssemblyProbabilityIndex = constructionProperties.CombinedAssemblyProbabilityIndex;
manualAssemblyProbabilityIndex = constructionProperties.ManualAssemblyProbabilityIndex;
CreateColumnStateDefinitions();
Update();
}
///
/// Gets or sets the value representing the simple assessment result.
///
/// Thrown when
/// is a valid value, but unsupported.
public SimpleAssessmentResultType SimpleAssessmentResult
{
get
{
return SectionResult.SimpleAssessmentResult;
}
set
{
SectionResult.SimpleAssessmentResult = value;
UpdateInternalData();
}
}
///
/// Gets or sets the value representing the detailed assessment result.
///
/// Thrown when
/// is a valid value, but unsupported.
public DetailedAssessmentProbabilityOnlyResultType DetailedAssessmentResult
{
get
{
return SectionResult.DetailedAssessmentResult;
}
set
{
SectionResult.DetailedAssessmentResult = value;
UpdateInternalData();
}
}
///
/// Gets the value representing the detailed assessment probability.
///
[TypeConverter(typeof(NoProbabilityValueDoubleConverter))]
public double DetailedAssessmentProbability
{
get
{
return SectionResult.GetDetailedAssessmentProbability(failureMechanism, assessmentSection);
}
}
///
/// Gets or sets the value representing the tailor made assessment result.
///
/// Thrown when
/// is a valid value, but unsupported.
public TailorMadeAssessmentProbabilityCalculationResultType TailorMadeAssessmentResult
{
get
{
return SectionResult.TailorMadeAssessmentResult;
}
set
{
SectionResult.TailorMadeAssessmentResult = value;
UpdateInternalData();
}
}
///
/// Gets or sets the value of the tailored assessment of safety.
///
/// Thrown when is
/// not in the range [0,1].
/// Thrown when
/// is a valid value, but unsupported.
[TypeConverter(typeof(NoProbabilityValueDoubleConverter))]
public double TailorMadeAssessmentProbability
{
get
{
return SectionResult.TailorMadeAssessmentProbability;
}
set
{
SectionResult.TailorMadeAssessmentProbability = value;
UpdateInternalData();
}
}
///
/// Gets the simple assembly category group.
///
public string SimpleAssemblyCategoryGroup
{
get
{
return FailureMechanismSectionAssemblyCategoryGroupHelper.GetCategoryGroupDisplayName(simpleAssemblyCategoryGroup);
}
}
///
/// Gets the detailed assembly category group.
///
public string DetailedAssemblyCategoryGroup
{
get
{
return FailureMechanismSectionAssemblyCategoryGroupHelper.GetCategoryGroupDisplayName(detailedAssemblyCategoryGroup);
}
}
///
/// Gets the tailor made assembly category group.
///
public string TailorMadeAssemblyCategoryGroup
{
get
{
return FailureMechanismSectionAssemblyCategoryGroupHelper.GetCategoryGroupDisplayName(tailorMadeAssemblyCategoryGroup);
}
}
///
/// Gets the combined assembly category group.
///
public string CombinedAssemblyCategoryGroup
{
get
{
return FailureMechanismSectionAssemblyCategoryGroupHelper.GetCategoryGroupDisplayName(combinedAssemblyCategoryGroup);
}
}
///
/// Gets the combined assembly probability.
///
[TypeConverter(typeof(NoProbabilityValueDoubleConverter))]
public double CombinedAssemblyProbability { get; private set; }
///
/// Gets or sets the indicator whether the combined assembly probability
/// should be overwritten by .
///
/// Thrown when
/// is a valid value, but unsupported.
public bool UseManualAssembly
{
get
{
return SectionResult.UseManualAssembly;
}
set
{
SectionResult.UseManualAssembly = value;
UpdateInternalData();
}
}
///
/// Gets or sets the manually entered assembly probability.
///
/// Thrown when is
/// not in the range [0,1].
/// Thrown when
/// is a valid value, but unsupported.
[TypeConverter(typeof(NoProbabilityValueDoubleConverter))]
public double ManualAssemblyProbability
{
get
{
return SectionResult.ManualAssemblyProbability;
}
set
{
SectionResult.ManualAssemblyProbability = value;
UpdateInternalData();
}
}
public override void Update()
{
UpdateDerivedData();
UpdateColumnStateDefinitions();
UpdateDetailedAssessmentProbabilityError();
}
private void UpdateDetailedAssessmentProbabilityError()
{
if (FailureMechanismSectionResultRowHelper.SimpleAssessmentIsSufficient(SimpleAssessmentResult)
|| !FailureMechanismSectionResultRowHelper.DetailedAssessmentResultIsProbability(DetailedAssessmentResult)
|| UseManualAssembly)
{
ColumnStateDefinitions[detailedAssessmentProbabilityIndex].ErrorText = string.Empty;
}
else
{
ColumnStateDefinitions[detailedAssessmentProbabilityIndex].ErrorText = FailureMechanismSectionResultRowHelper.GetDetailedAssessmentError(
DetailedAssessmentProbability,
SectionResult.Calculation);
}
}
private void CreateColumnStateDefinitions()
{
ColumnStateDefinitions.Add(simpleAssessmentResultIndex, new DataGridViewColumnStateDefinition());
ColumnStateDefinitions.Add(detailedAssessmentResultIndex, new DataGridViewColumnStateDefinition());
ColumnStateDefinitions.Add(detailedAssessmentProbabilityIndex, new DataGridViewColumnStateDefinition());
ColumnStateDefinitions.Add(tailorMadeAssessmentResultIndex, new DataGridViewColumnStateDefinition());
ColumnStateDefinitions.Add(tailorMadeAssessmentProbabilityIndex, new DataGridViewColumnStateDefinition());
ColumnStateDefinitions.Add(simpleAssemblyCategoryGroupIndex, DataGridViewColumnStateDefinitionFactory.CreateReadOnlyColumnStateDefinition());
ColumnStateDefinitions.Add(detailedAssemblyCategoryGroupIndex, DataGridViewColumnStateDefinitionFactory.CreateReadOnlyColumnStateDefinition());
ColumnStateDefinitions.Add(tailorMadeAssemblyCategoryGroupIndex, DataGridViewColumnStateDefinitionFactory.CreateReadOnlyColumnStateDefinition());
ColumnStateDefinitions.Add(combinedAssemblyCategoryGroupIndex, DataGridViewColumnStateDefinitionFactory.CreateReadOnlyColumnStateDefinition());
ColumnStateDefinitions.Add(combinedAssemblyProbabilityIndex, DataGridViewColumnStateDefinitionFactory.CreateReadOnlyColumnStateDefinition());
ColumnStateDefinitions.Add(manualAssemblyProbabilityIndex, new DataGridViewColumnStateDefinition());
}
private void UpdateDerivedData()
{
ResetErrorTexts();
TryGetSimpleAssemblyCategoryGroup();
TryGetDetailedAssemblyCategoryGroup();
TryGetTailorMadeAssemblyCategoryGroup();
TryGetCombinedAssemblyCategoryGroup();
}
private void ResetErrorTexts()
{
ColumnStateDefinitions[simpleAssemblyCategoryGroupIndex].ErrorText = string.Empty;
ColumnStateDefinitions[detailedAssemblyCategoryGroupIndex].ErrorText = string.Empty;
ColumnStateDefinitions[tailorMadeAssemblyCategoryGroupIndex].ErrorText = string.Empty;
ColumnStateDefinitions[combinedAssemblyCategoryGroupIndex].ErrorText = string.Empty;
ColumnStateDefinitions[combinedAssemblyProbabilityIndex].ErrorText = string.Empty;
}
private void TryGetSimpleAssemblyCategoryGroup()
{
try
{
simpleAssemblyCategoryGroup = ClosingStructuresFailureMechanismAssemblyFactory.AssembleSimpleAssessment(SectionResult).Group;
}
catch (AssemblyException e)
{
simpleAssemblyCategoryGroup = FailureMechanismSectionAssemblyCategoryGroup.None;
ColumnStateDefinitions[simpleAssemblyCategoryGroupIndex].ErrorText = e.Message;
}
}
private void TryGetDetailedAssemblyCategoryGroup()
{
try
{
detailedAssemblyCategoryGroup = ClosingStructuresFailureMechanismAssemblyFactory.AssembleDetailedAssessment(
SectionResult,
failureMechanism,
assessmentSection).Group;
}
catch (AssemblyException e)
{
detailedAssemblyCategoryGroup = FailureMechanismSectionAssemblyCategoryGroup.None;
ColumnStateDefinitions[detailedAssemblyCategoryGroupIndex].ErrorText = e.Message;
}
}
private void TryGetTailorMadeAssemblyCategoryGroup()
{
try
{
tailorMadeAssemblyCategoryGroup = ClosingStructuresFailureMechanismAssemblyFactory.AssembleTailorMadeAssessment(
SectionResult,
failureMechanism,
assessmentSection).Group;
}
catch (AssemblyException e)
{
tailorMadeAssemblyCategoryGroup = FailureMechanismSectionAssemblyCategoryGroup.None;
ColumnStateDefinitions[tailorMadeAssemblyCategoryGroupIndex].ErrorText = e.Message;
}
}
private void TryGetCombinedAssemblyCategoryGroup()
{
try
{
FailureMechanismSectionAssembly combinedAssembly =
ClosingStructuresFailureMechanismAssemblyFactory.AssembleCombinedAssessment(
SectionResult,
failureMechanism,
assessmentSection);
combinedAssemblyCategoryGroup = combinedAssembly.Group;
CombinedAssemblyProbability = combinedAssembly.Probability;
}
catch (AssemblyException e)
{
combinedAssemblyCategoryGroup = FailureMechanismSectionAssemblyCategoryGroup.None;
CombinedAssemblyProbability = double.NaN;
ColumnStateDefinitions[combinedAssemblyCategoryGroupIndex].ErrorText = e.Message;
ColumnStateDefinitions[combinedAssemblyProbabilityIndex].ErrorText = e.Message;
}
}
///
/// Updates the column state definitions.
///
/// Thrown when
/// is a valid value, but unsupported.
private void UpdateColumnStateDefinitions()
{
bool simpleAssessmentSufficient = FailureMechanismSectionResultRowHelper.SimpleAssessmentIsSufficient(SimpleAssessmentResult);
FailureMechanismSectionResultRowHelper.SetColumnState(ColumnStateDefinitions[simpleAssessmentResultIndex], UseManualAssembly);
FailureMechanismSectionResultRowHelper.SetColumnState(ColumnStateDefinitions[detailedAssessmentResultIndex], simpleAssessmentSufficient
|| UseManualAssembly);
if (simpleAssessmentSufficient
|| !FailureMechanismSectionResultRowHelper.DetailedAssessmentResultIsProbability(DetailedAssessmentResult)
|| UseManualAssembly)
{
FailureMechanismSectionResultRowHelper.DisableColumn(ColumnStateDefinitions[detailedAssessmentProbabilityIndex]);
}
else
{
FailureMechanismSectionResultRowHelper.EnableColumn(ColumnStateDefinitions[detailedAssessmentProbabilityIndex], true);
}
FailureMechanismSectionResultRowHelper.SetColumnState(ColumnStateDefinitions[tailorMadeAssessmentResultIndex],
simpleAssessmentSufficient
|| UseManualAssembly);
FailureMechanismSectionResultRowHelper.SetColumnState(ColumnStateDefinitions[tailorMadeAssessmentProbabilityIndex],
simpleAssessmentSufficient
|| !FailureMechanismSectionResultRowHelper.TailorMadeAssessmentResultIsProbability(TailorMadeAssessmentResult)
|| UseManualAssembly);
if (UseManualAssembly)
{
FailureMechanismSectionResultRowHelper.DisableColumn(ColumnStateDefinitions[simpleAssemblyCategoryGroupIndex]);
FailureMechanismSectionResultRowHelper.DisableColumn(ColumnStateDefinitions[detailedAssemblyCategoryGroupIndex]);
FailureMechanismSectionResultRowHelper.DisableColumn(ColumnStateDefinitions[tailorMadeAssemblyCategoryGroupIndex]);
FailureMechanismSectionResultRowHelper.DisableColumn(ColumnStateDefinitions[combinedAssemblyCategoryGroupIndex]);
FailureMechanismSectionResultRowHelper.DisableColumn(ColumnStateDefinitions[combinedAssemblyProbabilityIndex]);
}
else
{
FailureMechanismSectionResultRowHelper.SetAssemblyCategoryGroupStyle(ColumnStateDefinitions[simpleAssemblyCategoryGroupIndex],
simpleAssemblyCategoryGroup);
FailureMechanismSectionResultRowHelper.SetAssemblyCategoryGroupStyle(ColumnStateDefinitions[detailedAssemblyCategoryGroupIndex],
detailedAssemblyCategoryGroup);
FailureMechanismSectionResultRowHelper.SetAssemblyCategoryGroupStyle(ColumnStateDefinitions[tailorMadeAssemblyCategoryGroupIndex],
tailorMadeAssemblyCategoryGroup);
FailureMechanismSectionResultRowHelper.SetAssemblyCategoryGroupStyle(ColumnStateDefinitions[combinedAssemblyCategoryGroupIndex],
combinedAssemblyCategoryGroup);
FailureMechanismSectionResultRowHelper.EnableColumn(ColumnStateDefinitions[combinedAssemblyProbabilityIndex], true);
}
FailureMechanismSectionResultRowHelper.SetColumnState(ColumnStateDefinitions[manualAssemblyProbabilityIndex], !UseManualAssembly);
}
///
/// Class holding the various construction parameters for .
///
public class ConstructionProperties
{
///
/// Sets the simple assessment result index.
///
public int SimpleAssessmentResultIndex { internal get; set; }
///
/// Sets the detailed assessment result index.
///
public int DetailedAssessmentResultIndex { internal get; set; }
///
/// Sets the detailed assessment probability index.
///
public int DetailedAssessmentProbabilityIndex { internal get; set; }
///
/// Sets the tailor made assessment result index.
///
public int TailorMadeAssessmentResultIndex { internal get; set; }
///
/// Sets the tailor made assessment probability index.
///
public int TailorMadeAssessmentProbabilityIndex { internal get; set; }
///
/// Sets the simple assembly category group index.
///
public int SimpleAssemblyCategoryGroupIndex { internal get; set; }
///
/// Sets the detailed assembly category group index.
///
public int DetailedAssemblyCategoryGroupIndex { internal get; set; }
///
/// Sets the tailor made assembly category group index.
///
public int TailorMadeAssemblyCategoryGroupIndex { internal get; set; }
///
/// Sets the combined assembly category group index.
///
public int CombinedAssemblyCategoryGroupIndex { internal get; set; }
///
/// Sets the combined assembly probability index.
///
public int CombinedAssemblyProbabilityIndex { internal get; set; }
///
/// Sets the manual assembly category group index.
///
public int ManualAssemblyProbabilityIndex { internal get; set; }
}
}
}