Skip to content

Commit

Permalink
Make Exportfilegenerator validateable
Browse files Browse the repository at this point in the history
  • Loading branch information
NeoCoderMatrix86 committed Apr 2, 2024
1 parent 26225e9 commit eb0ba76
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 45 deletions.
2 changes: 1 addition & 1 deletion AudioCuesheetEditor/Model/Entity/IValidateable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class ValidationResult
{
private List<ValidationMessage>? validationMessages;

public static ValidationResult Create(ValidationStatus validationStatus, IReadOnlyCollection<ValidationMessage>? validationMessages = null)
public static ValidationResult Create(ValidationStatus validationStatus, IEnumerable<ValidationMessage>? validationMessages = null)
{
return new ValidationResult() { Status = validationStatus, ValidationMessages = validationMessages?.ToList() };
}
Expand Down
142 changes: 98 additions & 44 deletions AudioCuesheetEditor/Model/IO/Export/ExportfileGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,62 +30,27 @@ public enum ExportType
Exportprofile
}

public class ExportfileGenerator
public class ExportfileGenerator : Validateable<ExportfileGenerator>
{
public Cuesheet Cuesheet { get; }
public Exportprofile? Exportprofile { get; set; }
public ApplicationOptions? ApplicationOptions { get; set; }
public IAudioConverterService? AudioConverterService { get; set; }
public ExportType ExportType { get; set; }

public ExportfileGenerator(Cuesheet cuesheet, Exportprofile? exportprofile = null, ApplicationOptions? applicationOptions = null, IAudioConverterService? audioConverterService = null)
public ExportfileGenerator(ExportType exportType, Cuesheet cuesheet, Exportprofile? exportprofile = null, ApplicationOptions? applicationOptions = null, IAudioConverterService? audioConverterService = null)
{
ExportType = exportType;
Cuesheet = cuesheet;
Exportprofile = exportprofile;
ApplicationOptions = applicationOptions;
AudioConverterService = audioConverterService;
}

/// <summary>
/// Is an export of the <see cref="Cuesheet"/> possible?
/// </summary>
/// <param name="exportType">Which type of export should be done?</param>
/// <returns>Boolean indicating if export is possible or not</returns>
public Boolean CanWrite(ExportType exportType)
{
Boolean canWrite = false;
switch (exportType)
{
case ExportType.Cuesheet:
canWrite = (Cuesheet.Validate().Status != ValidationStatus.Error)
&& (Cuesheet.Cataloguenumber.Validate().Status != ValidationStatus.Error)
&& Cuesheet.Tracks.All(x => x.Validate().Status != ValidationStatus.Error)
&& (ApplicationOptions?.Validate(x => x.CuesheetFilename).Status != ValidationStatus.Error);
break;
case ExportType.Exportprofile:
if (Exportprofile != null)
{
canWrite = (Cuesheet.Validate().Status != ValidationStatus.Error)
&& (Cuesheet.Cataloguenumber.Validate().Status != ValidationStatus.Error)
&& Cuesheet.Tracks.All(x => x.Validate().Status != ValidationStatus.Error)
&& (ApplicationOptions?.Validate(x => x.CuesheetFilename).Status != ValidationStatus.Error)
&& (Exportprofile.Validate().Status != ValidationStatus.Error);
}
break;
}
var hasSplitPoints = Cuesheet.SplitPoints.Any();
if (hasSplitPoints)
{
canWrite = canWrite
&& AudioConverterService != null
&& Cuesheet.Audiofile != null;
}
return canWrite;
}

public async Task<IReadOnlyCollection<Exportfile>> GenerateExportfilesAsync(ExportType exportType)
public async Task<IReadOnlyCollection<Exportfile>> GenerateExportfilesAsync()
{
List<Exportfile> exportfiles = new();
if (CanWrite(exportType))
if (Validate().Status != ValidationStatus.Error)
{
if (Cuesheet.SplitPoints.Count != 0)
{
Expand All @@ -99,7 +64,7 @@ public async Task<IReadOnlyCollection<Exportfile>> GenerateExportfilesAsync(Expo
audioFileName = String.Format("{0}({1}){2}", Path.GetFileNameWithoutExtension(Cuesheet.Audiofile?.Name), counter, Path.GetExtension(Cuesheet.Audiofile?.Name));
if (splitPoint.Validate().Status == ValidationStatus.Success)
{
switch (exportType)
switch (ExportType)
{
case ExportType.Cuesheet:
content = WriteCuesheet(audioFileName, previousSplitPointMoment, splitPoint);
Expand All @@ -124,7 +89,7 @@ public async Task<IReadOnlyCollection<Exportfile>> GenerateExportfilesAsync(Expo
}
//After a split point attach the last part
audioFileName = String.Format("{0}({1}){2}", Path.GetFileNameWithoutExtension(Cuesheet.Audiofile?.Name), counter, Path.GetExtension(Cuesheet.Audiofile?.Name));
switch (exportType)
switch (ExportType)
{
case ExportType.Cuesheet:
content = WriteCuesheet(audioFileName, previousSplitPointMoment);
Expand All @@ -148,7 +113,7 @@ public async Task<IReadOnlyCollection<Exportfile>> GenerateExportfilesAsync(Expo
{
String filename = String.Empty;
String? content = null;
switch (exportType)
switch (ExportType)
{
case ExportType.Cuesheet:
var cuesheetfilename = ApplicationOptions?.CuesheetFilename;
Expand Down Expand Up @@ -381,5 +346,94 @@ private String WriteExport(String audiofileName, TimeSpan? from = null, SplitPoi
}
return exportAudiofile;
}

protected override ValidationResult Validate(string property)
{
ValidationResult validationResult;
switch (property)
{
case nameof(Cuesheet):
var validationResults = new Dictionary<IValidateable, ValidationResult>
{
{ Cuesheet, Cuesheet.Validate() },
{ Cuesheet.Cataloguenumber, Cuesheet.Cataloguenumber.Validate() }
};
foreach (var track in Cuesheet.Tracks)
{
validationResults.Add(track, track.Validate());
}
if (validationResults.Any(x => x.Value.Status == ValidationStatus.Error))
{
//TODO: Für jedes Objekt eine Nachricht erstellen und den Objektnamen voranstellen!
var messages = validationResults.Values.Where(x => x.ValidationMessages != null).SelectMany(x => x.ValidationMessages!);
validationResult = ValidationResult.Create(ValidationStatus.Error, messages);
}
else
{
validationResult = ValidationResult.Create(ValidationStatus.Success);
}
break;
case nameof(ApplicationOptions):
if (ApplicationOptions == null)
{
var validationMessages = new List<ValidationMessage>()
{
new("{0} has no value!", nameof(ApplicationOptions))
};
validationResult = ValidationResult.Create(ValidationStatus.Error, validationMessages);
}
else
{
validationResult = ValidationResult.Create(ValidationStatus.Success);
}
break;
case nameof(Exportprofile):
if (ExportType == ExportType.Exportprofile)
{
if (Exportprofile != null)
{
validationResult = Exportprofile.Validate();
}
else
{
var validationMessages = new List<ValidationMessage>()
{
new("{0} has no value!", nameof(Exportprofile))
};
validationResult = ValidationResult.Create(ValidationStatus.Error, validationMessages);
}
}
else
{
validationResult = ValidationResult.Create(ValidationStatus.NoValidation);
}
break;
case nameof(AudioConverterService):
if (Cuesheet.SplitPoints.Any())
{
if (AudioConverterService == null)
{
var validationMessages = new List<ValidationMessage>()
{
new("{0} has no value!", nameof(Exportprofile))
};
validationResult = ValidationResult.Create(ValidationStatus.Error, validationMessages);
}
else
{
validationResult = ValidationResult.Create(ValidationStatus.Success);
}
}
else
{
validationResult = ValidationResult.Create(ValidationStatus.NoValidation);
}
break;
default:
validationResult = ValidationResult.Create(ValidationStatus.NoValidation);
break;
}
return validationResult;
}
}
}

0 comments on commit eb0ba76

Please sign in to comment.