Extracted some pages to their own assembly and finished the artifact display page code
This commit is contained in:
@@ -0,0 +1,358 @@
|
||||
@namespace OpenArchival.Blazor.AdminPages
|
||||
|
||||
@using Microsoft.AspNetCore.Components.Web
|
||||
@using System.ComponentModel.DataAnnotations
|
||||
@using MudBlazor
|
||||
@using global::OpenArchival.DataAccess
|
||||
@using OpenArchival.Blazor.CustomComponents
|
||||
|
||||
@inject IDialogService DialogService
|
||||
@inject NavigationManager NavigationManager;
|
||||
@inject IArchiveCategoryProvider CategoryProvider;
|
||||
@inject ArtifactEntrySharedHelpers Helpers;
|
||||
|
||||
<MudPaper Class="pa-4 ma-2 rounded" Elevation="3">
|
||||
<MudText Typo="Typo.h5" Color="Color.Primary">Add an Archive Item</MudText>
|
||||
|
||||
<MudDivider DividerType="DividerType.Middle"></MudDivider>
|
||||
|
||||
@foreach (var result in ValidationResults)
|
||||
{
|
||||
<MudAlert Severity="Severity.Error">@result.ErrorMessage</MudAlert>
|
||||
}
|
||||
|
||||
<MudGrid Justify="Justify.Center" Class="pt-4">
|
||||
<MudItem>
|
||||
<MudAutocomplete
|
||||
T="ArchiveCategory"
|
||||
ToStringFunc="@(val => val?.Name)"
|
||||
Label="Category"
|
||||
@bind-Value="Model.Category"
|
||||
@bind-Value:after=OnCategoryChanged
|
||||
SearchFunc="SearchCategory"
|
||||
CoerceValue=false
|
||||
CoerceText=false
|
||||
/>
|
||||
</MudItem>
|
||||
|
||||
<MudItem>
|
||||
<MudFab Color="Color.Primary" StartIcon="@Icons.Material.Filled.Add" OnClick="OnAddCategoryClicked"/>
|
||||
</MudItem>
|
||||
|
||||
</MudGrid>
|
||||
</MudPaper>
|
||||
|
||||
<div @ref="_formDiv" style="@_formDivStyle">
|
||||
|
||||
<MudPaper Class="pa-4 ma-2 rounded" Elevation="3">
|
||||
<MudText Typo="Typo.h6" Color="Color.Primary" Class="pt-4 pb-0">Archive Item Identifier</MudText>
|
||||
<MudDivider DividerType="DividerType.Middle"></MudDivider>
|
||||
<IdentifierTextBox @ref="_identifierTextBox" IdentifierFields="@Model.IdentifierFieldValues"></IdentifierTextBox>
|
||||
</MudPaper>
|
||||
|
||||
<MudPaper Class="pa-4 ma-2 rounded" Elevation="3">
|
||||
<MudText Typo="Typo.h6" Color="Color.Primary" Class="pt-4 pb-0">Grouping Title</MudText>
|
||||
<MudDivider DividerType="DividerType.Middle"></MudDivider>
|
||||
<MudTextField
|
||||
T="string"
|
||||
For="@(() => Model.Title)"
|
||||
Placeholder="Grouping Title"
|
||||
@bind-Value=Model.Title></MudTextField>
|
||||
|
||||
<MudText Typo="Typo.h6" Color="Color.Primary" Class="pt-4 pb-0">Grouping Description</MudText>
|
||||
<MudDivider DividerType="DividerType.Middle"></MudDivider>
|
||||
<MudTextField
|
||||
T="string"
|
||||
For="@(() => Model.Description)"
|
||||
Lines="5"
|
||||
Placeholder="Grouping Description"
|
||||
@bind-Value=Model.Description></MudTextField>
|
||||
|
||||
<MudText Typo="Typo.h6" Color="Color.Primary" Class="pt-4 pb-0">Grouping Type</MudText>
|
||||
<MudDivider DividerType="DividerType.Middle"></MudDivider>
|
||||
<MudAutocomplete
|
||||
For="@(() => Model.Type)"
|
||||
T="string"
|
||||
Label="Artifact Type"
|
||||
Class="pt-0 mt-0 pl-2 pr-2"
|
||||
@bind-Value=Model.Type
|
||||
SearchFunc="Helpers.SearchItemTypes"
|
||||
CoerceValue=true></MudAutocomplete>
|
||||
</MudPaper>
|
||||
|
||||
<MudPaper Class="pa-4 ma-2 rounded" Elevation="3">
|
||||
<UploadDropBox
|
||||
@ref="@_uploadComponent"
|
||||
FilesUploaded="OnFilesUploaded"
|
||||
ClearClicked="OnClearFilesClicked"
|
||||
ExistingFiles="ExistingFiles"></UploadDropBox>
|
||||
</MudPaper>
|
||||
@if (Model is not null)
|
||||
{
|
||||
@for (int index = 0; index < Model.ArtifactEntries.Count; ++index)
|
||||
{
|
||||
// Capture the current item in a local variable for the lambda
|
||||
var currentEntry = Model.ArtifactEntries[index];
|
||||
|
||||
<ArchiveEntryCreatorCard Model="currentEntry"
|
||||
ModelChanged="(updatedEntry) => HandleEntryUpdate(currentEntry, updatedEntry)"
|
||||
InputsChanged="OnChanged"
|
||||
@key="currentEntry"
|
||||
ArtifactEntryIndex="index"
|
||||
OnEntryDeletedClicked="OnDeleteEntryClicked"/>
|
||||
}
|
||||
}
|
||||
</div>
|
||||
|
||||
<MudGrid Justify="Justify.FlexEnd" Class="pt-6">
|
||||
<MudItem>
|
||||
<MudCheckBox Label="Publicly Visible" T="bool" @bind-Value=Model.IsPublicallyVisible></MudCheckBox>
|
||||
</MudItem>
|
||||
|
||||
@if (FormButtonsEnabled)
|
||||
{
|
||||
<MudItem Class="pr-0">
|
||||
<MudButton Color="Color.Primary" Variant="Variant.Filled" Class="ml-4" OnClick="CancelClicked">Cancel</MudButton>
|
||||
</MudItem>
|
||||
|
||||
<MudItem Class="pl-2">
|
||||
<MudButton Color="Color.Primary" Variant="Variant.Filled" Class="ml-4" OnClick="PublishClicked" Disabled="@(!IsValid)" >Publish</MudButton>
|
||||
</MudItem>
|
||||
}
|
||||
</MudGrid>
|
||||
|
||||
|
||||
@code {
|
||||
[Parameter]
|
||||
public bool ClearOnPublish { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// The URI to navigate to if cancel is pressed. null to navigate to no page
|
||||
/// </summary>
|
||||
[Parameter]
|
||||
public string? BackLink { get; set; } = null;
|
||||
|
||||
/// <summary>
|
||||
/// The URI to navigate to if publish is pressed, null to navigate to no page
|
||||
/// </summary>
|
||||
[Parameter]
|
||||
public string? ForwardLink { get; set; } = null;
|
||||
|
||||
/// <summary>
|
||||
/// Called when publish is clicked
|
||||
/// </summary>
|
||||
[Parameter]
|
||||
public EventCallback<ArtifactGroupingValidationModel> GroupingPublished { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The model to display on the form
|
||||
/// </summary>
|
||||
[Parameter]
|
||||
public ArtifactGroupingValidationModel Model { get; set; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the cancel and publish buttons should be show to the user or if the containing component will
|
||||
/// handle their functionality (ie if used in a dialog and you want to use the dialog buttons instead of this component's handlers)
|
||||
/// </summary>
|
||||
[Parameter]
|
||||
public bool FormButtonsEnabled { get; set; } = true;
|
||||
|
||||
private UploadDropBox _uploadComponent = default!;
|
||||
|
||||
private IdentifierTextBox _identifierTextBox = default!;
|
||||
|
||||
private ElementReference _formDiv = default!;
|
||||
|
||||
private bool _isFormDivVisible = false;
|
||||
|
||||
private string _formDivStyle => _isFormDivVisible ? "" : "display: none;";
|
||||
|
||||
public List<string> DatesData { get; set; } = [];
|
||||
|
||||
public List<ArchiveCategory> Categories { get; set; } = new();
|
||||
|
||||
private List<FilePathListing> _filePathListings = new();
|
||||
|
||||
private bool _categorySelected = false;
|
||||
|
||||
public bool IsValid { get; set; } = false;
|
||||
|
||||
public List<ValidationResult> ValidationResults { get; private set; } = [];
|
||||
|
||||
// Used to store the files that have already been uploaded if this component is being displayed
|
||||
// with a filled in model. Used to populate the upload drop box
|
||||
private List<FilePathListing> ExistingFiles { get; set; } = new();
|
||||
|
||||
protected override async Task OnParametersSetAsync()
|
||||
{
|
||||
// Ensure to reload the component if a model has been supplied so that the full
|
||||
// component will render
|
||||
if (Model?.Category is not null)
|
||||
{
|
||||
await OnCategoryChanged();
|
||||
}
|
||||
|
||||
if (Model is not null && Model.Category is not null)
|
||||
{
|
||||
// The data entry should only be shown if a category has been selected
|
||||
_isFormDivVisible = true;
|
||||
} else
|
||||
{
|
||||
_isFormDivVisible = false;
|
||||
}
|
||||
|
||||
if (Model is not null)
|
||||
{
|
||||
ExistingFiles = Model.ArtifactEntries
|
||||
.Where(e => e.Files.Any())
|
||||
.Select(e => e.Files[0])
|
||||
.ToList();
|
||||
}
|
||||
|
||||
StateHasChanged();
|
||||
}
|
||||
|
||||
private async Task PublishClicked(MouseEventArgs args)
|
||||
{
|
||||
var validationContext = new ValidationContext(Model);
|
||||
var validationResult = new List<ValidationResult>();
|
||||
|
||||
IsValid = Validator.TryValidateObject(Model, validationContext, validationResult);
|
||||
ArtifactGroupingValidationModel oldModel = Model;
|
||||
if (ForwardLink is not null)
|
||||
{
|
||||
if (IsValid)
|
||||
{
|
||||
NavigationManager.NavigateTo(ForwardLink);
|
||||
}
|
||||
}
|
||||
|
||||
if (IsValid && ClearOnPublish)
|
||||
{
|
||||
Model = new();
|
||||
await _uploadComponent.ClearClicked.InvokeAsync();
|
||||
StateHasChanged();
|
||||
}
|
||||
|
||||
await GroupingPublished.InvokeAsync(oldModel);
|
||||
}
|
||||
|
||||
private void CancelClicked(MouseEventArgs args)
|
||||
{
|
||||
if (BackLink is not null) {
|
||||
NavigationManager.NavigateTo(BackLink);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentNullException("No back link provided for the add archive item page.");
|
||||
}
|
||||
}
|
||||
|
||||
private async Task HandleEntryUpdate(ArtifactEntryValidationModel originalEntry, ArtifactEntryValidationModel updatedEntry)
|
||||
{
|
||||
// Find the index of the original object in our list
|
||||
var index = Model.ArtifactEntries.IndexOf(originalEntry);
|
||||
|
||||
// If found, replace it with the updated version
|
||||
if (index != -1)
|
||||
{
|
||||
Model.ArtifactEntries[index] = updatedEntry;
|
||||
}
|
||||
|
||||
// Now, run the validation logic
|
||||
await OnChanged();
|
||||
}
|
||||
|
||||
// You can now simplify your OnFilesUploaded method slightly
|
||||
private async Task OnFilesUploaded(List<FilePathListing> args)
|
||||
{
|
||||
_filePathListings = args;
|
||||
|
||||
// This part is tricky. Adding items while iterating can be problematic.
|
||||
// A better approach is to create the entries first, then tell Blazor to render.
|
||||
var newEntries = new List<ArtifactEntryValidationModel>();
|
||||
foreach (var file in args)
|
||||
{
|
||||
// Associate the file with the entry if needed
|
||||
newEntries.Add(new ArtifactEntryValidationModel { Files = [file]});
|
||||
}
|
||||
Model.ArtifactEntries.AddRange(newEntries);
|
||||
|
||||
// StateHasChanged() is implicitly called by OnChanged() if validation passes
|
||||
await OnChanged();
|
||||
}
|
||||
|
||||
private async Task OnClearFilesClicked()
|
||||
{
|
||||
_filePathListings = [];
|
||||
Model.ArtifactEntries = [];
|
||||
StateHasChanged();
|
||||
await OnChanged();
|
||||
}
|
||||
|
||||
async Task OnChanged()
|
||||
{
|
||||
var validationContext = new ValidationContext(Model);
|
||||
var validationResult = new List<ValidationResult>();
|
||||
|
||||
IsValid = Validator.TryValidateObject(Model, validationContext, validationResult);
|
||||
|
||||
if (IsValid)
|
||||
{
|
||||
StateHasChanged();
|
||||
}
|
||||
}
|
||||
|
||||
async Task OnCategoryChanged()
|
||||
{
|
||||
if (Model.Category is not null && _identifierTextBox is not null)
|
||||
{
|
||||
_identifierTextBox.VerifyFormatCategory = Model.Category;
|
||||
_isFormDivVisible = true;
|
||||
if (!_categorySelected)
|
||||
{
|
||||
_categorySelected = true;
|
||||
}
|
||||
StateHasChanged();
|
||||
}
|
||||
|
||||
await OnChanged();
|
||||
}
|
||||
|
||||
public async Task OnAddCategoryClicked()
|
||||
{
|
||||
var options = new DialogOptions { CloseOnEscapeKey = true, BackdropClick = false };
|
||||
var dialog = await DialogService.ShowAsync<CategoryCreatorDialog>("Create a Category", options);
|
||||
|
||||
var result = await dialog.Result;
|
||||
|
||||
if (result is not null && !result.Canceled)
|
||||
{
|
||||
await CategoryProvider.CreateCategoryAsync(CategoryValidationModel.ToArchiveCategory((CategoryValidationModel)result.Data));
|
||||
StateHasChanged();
|
||||
await OnChanged();
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<IEnumerable<ArchiveCategory>> SearchCategory(string value, CancellationToken cancellationToken)
|
||||
{
|
||||
List<ArchiveCategory> categories;
|
||||
if (string.IsNullOrEmpty(value))
|
||||
{
|
||||
categories = new(await CategoryProvider.Top(25) ?? []);
|
||||
}
|
||||
else
|
||||
{
|
||||
categories = new((await CategoryProvider.Search(value) ?? []));
|
||||
}
|
||||
|
||||
return categories;
|
||||
}
|
||||
|
||||
private async void OnDeleteEntryClicked((int index, string filename) data)
|
||||
{
|
||||
Model.ArtifactEntries.RemoveAt(data.index);
|
||||
_uploadComponent.RemoveFile(data.filename);
|
||||
StateHasChanged();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
@namespace OpenArchival.Blazor.AdminPages
|
||||
@using Microsoft.AspNetCore.Components.Web
|
||||
@using MudBlazor
|
||||
|
||||
<MudDialog>
|
||||
<TitleContent>
|
||||
<MudText Typo="Typo.h6">Edit a Category</MudText>
|
||||
</TitleContent>
|
||||
|
||||
<DialogContent>
|
||||
<AddArchiveGroupingComponent
|
||||
Model="Model"
|
||||
FormButtonsEnabled=false></AddArchiveGroupingComponent>
|
||||
</DialogContent>
|
||||
|
||||
<DialogActions>
|
||||
<MudButton OnClick="OnCancel">Cancel</MudButton>
|
||||
<MudButton Color="Color.Primary" OnClick="OnSubmit">OK</MudButton>
|
||||
</DialogActions>
|
||||
</MudDialog>
|
||||
|
||||
@code {
|
||||
[Parameter]
|
||||
public required ArtifactGroupingValidationModel Model { get; set; }
|
||||
|
||||
[CascadingParameter]
|
||||
IMudDialogInstance MudDialog { get; set; }
|
||||
|
||||
[Parameter]
|
||||
public bool IsUpdate { get; set; } = false;
|
||||
|
||||
|
||||
private void OnCancel(MouseEventArgs args)
|
||||
{
|
||||
MudDialog.Cancel();
|
||||
}
|
||||
private void OnSubmit(MouseEventArgs args)
|
||||
{
|
||||
MudDialog.Close(DialogResult.Ok(Model));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,326 @@
|
||||
@namespace OpenArchival.Blazor.AdminPages
|
||||
@using Microsoft.AspNetCore.Components.Web
|
||||
@using OpenArchival.DataAccess
|
||||
@using System.ComponentModel.DataAnnotations
|
||||
@using OpenArchival.Blazor.CustomComponents
|
||||
@using MudBlazor
|
||||
|
||||
@inject ArtifactEntrySharedHelpers Helpers;
|
||||
@inject IArtifactDefectProvider DefectsProvider;
|
||||
@inject IArtifactStorageLocationProvider StorageLocationProvider;
|
||||
@inject IArchiveEntryTagProvider TagsProvider;
|
||||
@inject IArtifactTypeProvider TypesProvider;
|
||||
@inject IListedNameProvider ListedNameProvider;
|
||||
|
||||
<MudPaper Class="pa-4 ma-2 rounded" Elevation="3">
|
||||
<MudGrid>
|
||||
<MudItem>
|
||||
@if (Model.Files.Count > 0) {
|
||||
<MudText Typo="Typo.h6">@(Model.Files[0].OriginalName)</MudText>
|
||||
}
|
||||
</MudItem>
|
||||
<MudItem>
|
||||
<MudButton
|
||||
Variant="Variant.Filled"
|
||||
StartIcon="@Icons.Material.Filled.Delete"
|
||||
Color="Color.Error"
|
||||
OnClick="OnDeleteEntryClicked"
|
||||
>Delete</MudButton>
|
||||
|
||||
</MudItem>
|
||||
</MudGrid>
|
||||
|
||||
@foreach (var error in ValidationResults)
|
||||
{
|
||||
<MudAlert Severity="Severity.Error">
|
||||
@error.ErrorMessage
|
||||
</MudAlert>
|
||||
}
|
||||
|
||||
<MudText Typo="Typo.h6" Color="Color.Primary" Class="pt-4 pb-0">Archive Item Title</MudText>
|
||||
<MudDivider DividerType="DividerType.Middle"></MudDivider>
|
||||
<MudTextField For="@(() => Model.Title)" Required=true Placeholder="Archive Item Title" T="string" Class="pl-4 pr-4" @bind-Value=Model.Title @bind-Value:after=OnInputsChanged></MudTextField>
|
||||
|
||||
<MudText Typo="Typo.h6" Color="Color.Primary" Class="pt-4 pb-0">Archive Item Numbering</MudText>
|
||||
<MudText Typo="Typo.caption" Color="Color.Secondary" Class="pb-2">Enter a unique ID for this entry</MudText>
|
||||
<MudDivider DividerType="DividerType.Middle"></MudDivider>
|
||||
<MudTextField For="@(() => Model.ArtifactNumber)" Placeholder="Numbering" T="string" @bind-Value=Model.ArtifactNumber @bind-Value:after=OnInputsChanged/>
|
||||
|
||||
<MudText Typo="Typo.h6" Color="Color.Primary" Class="pt-4 pb-0">Item Description</MudText>
|
||||
<MudDivider DividerType="DividerType.Middle"></MudDivider>
|
||||
<MudTextField For="@(() => Model.Description)" Lines=8 Placeholder="Description" T="string" Class="pl-4 pr-4" @bind-Value=Model.Description @bind-Value:after=OnInputsChanged></MudTextField>
|
||||
|
||||
<MudText Typo="Typo.h6" Color="Color.Primary" Class="pt-4 pb-0">Storage Location</MudText>
|
||||
<MudDivider DividerType="DividerType.Middle"></MudDivider>
|
||||
<MudAutocomplete For="@(() => Model.StorageLocation)" T="string" Label="Storage Location" Class="pt-0 mt-0 pl-2 pr-2" @bind-Value=Model.StorageLocation @bind-Value:after=OnInputsChanged SearchFunc="Helpers.SearchStorageLocation" CoerceValue=true></MudAutocomplete>
|
||||
|
||||
<MudText Typo="Typo.h6" Color="Color.Primary" Class="pt-4 pb-0">Artifact Type</MudText>
|
||||
<MudDivider DividerType="DividerType.Middle"></MudDivider>
|
||||
<MudAutocomplete For="@(() => Model.Type)" T="string" Label="Artifact Type" Class="pt-0 mt-0 pl-2 pr-2" @bind-Value=Model.Type @bind-Value:after=OnInputsChanged SearchFunc="Helpers.SearchItemTypes" CoerceValue=true></MudAutocomplete>
|
||||
|
||||
@* Tags entry *@
|
||||
<MudText Typo="Typo.h6" Color="Color.Primary" Class="pt-4 pb-0">Tags</MudText>
|
||||
<MudDivider DividerType="DividerType.Middle"></MudDivider>
|
||||
<ChipContainer T="string" @ref="@_tagsChipContainer" @bind-Items="Model.Tags">
|
||||
<InputContent>
|
||||
<MudAutocomplete T="string"
|
||||
OnInternalInputChanged="OnInputsChanged"
|
||||
SearchFunc="Helpers.SearchTags"
|
||||
Value="_tagsInputValue"
|
||||
ValueChanged="OnTagsInputTextChanged"
|
||||
OnKeyDown="@(ev => HandleChipContainerEnter<string>(ev, _tagsChipContainer, _tagsInputValue, () => _tagsInputValue = string.Empty))"
|
||||
CoerceValue=true
|
||||
Placeholder="Add Tags..."
|
||||
ShowProgressIndicator="true"
|
||||
>
|
||||
</MudAutocomplete>
|
||||
</InputContent>
|
||||
</ChipContainer>
|
||||
|
||||
<MudText Typo="Typo.h6" Color="Color.Primary" Class="pt-4 pb-0">Listed Names</MudText>
|
||||
<MudText Typo="Typo.caption" Color="Color.Secondary" Class="pb-2">Enter any names of the people associated with this entry.</MudText>
|
||||
<MudDivider DividerType="DividerType.Middle"></MudDivider>
|
||||
<ChipContainer T="string" @ref=_listedNamesChipContainer @bind-Items="Model.ListedNames">
|
||||
<InputContent>
|
||||
<MudAutocomplete T="string"
|
||||
SearchFunc="Helpers.SearchListedNames"
|
||||
OnInternalInputChanged="OnInputsChanged"
|
||||
Value="_listedNamesInputValue"
|
||||
ValueChanged="OnListedNamesTextChanged"
|
||||
OnKeyDown="@(ev=>HandleChipContainerEnter<string>(ev, _listedNamesChipContainer, _listedNamesInputValue, () => _listedNamesInputValue = string.Empty))"
|
||||
CoerceValue=true
|
||||
Placeholder="Add Listed Names..."
|
||||
ShowProgressIndicator=true>
|
||||
</MudAutocomplete>
|
||||
</InputContent>
|
||||
</ChipContainer>
|
||||
|
||||
<MudText Typo="Typo.h6" Color="Color.Primary" Class="pt-4 pb-0">Associated Dates</MudText>
|
||||
<MudDivider DividerType="DividerType.Middle"></MudDivider>
|
||||
<ChipContainer T="DateTime" @ref="_assocaitedDatesChipContainer" @bind-Items="Model.AssociatedDates" DisplayFunc="date => date.ToShortDateString()">
|
||||
<InputContent>
|
||||
<MudDatePicker @bind-Date=_associatedDateInputValue MinDate="new DateTime(1000, 1, 1)">
|
||||
</MudDatePicker>
|
||||
</InputContent>
|
||||
<SubmitButton>
|
||||
<MudButton Color="Color.Primary"
|
||||
OnClick="HandleAssociatedDateChipContainerAdd">+</MudButton>
|
||||
</SubmitButton>
|
||||
</ChipContainer>
|
||||
|
||||
<MudText Typo="Typo.h6" Color="Color.Primary" Class="pt-4 pb-0">Defects</MudText>
|
||||
<MudDivider DividerType="DividerType.Middle"></MudDivider>
|
||||
<ChipContainer T="string" @ref=_defectsChipContainer @bind-Items="Model.Defects">
|
||||
<InputContent>
|
||||
<MudAutocomplete T="string"
|
||||
SearchFunc="Helpers.SearchDefects"
|
||||
OnInternalInputChanged="OnInputsChanged"
|
||||
Value="_defectsInputValue"
|
||||
ValueChanged="OnDefectsValueChanged"
|
||||
OnKeyDown="@(ev=>HandleChipContainerEnter<string>(ev, _defectsChipContainer, _defectsInputValue, () => _defectsInputValue = string.Empty))"
|
||||
CoerceValue=true
|
||||
Placeholder="Add Defects..."
|
||||
ShowProgressIndicator=true>
|
||||
</MudAutocomplete>
|
||||
</InputContent>
|
||||
</ChipContainer>
|
||||
|
||||
<MudText Typo="Typo.h6" Color="Color.Primary" Class="pt-4 pb-0">Related Artifacts</MudText>
|
||||
<MudText Typo="Typo.caption" Color="Color.Secondary" Class="pb-2">Tag this entry with the identifier of any other entry to link them.</MudText>
|
||||
<MudDivider DividerType="DividerType.Middle"></MudDivider>
|
||||
<ChipContainer T="ArtifactEntry" @ref="_assocaitedArtifactsChipContainer" @bind-Items="Model.RelatedArtifacts" DisplayFunc="artifact => artifact.ArtifactIdentifier">
|
||||
<InputContent>
|
||||
<MudAutocomplete T="ArtifactEntry"
|
||||
OnInternalInputChanged="OnInputsChanged"
|
||||
Value="_associatedArtifactValue"
|
||||
ValueChanged="OnAssociatedArtifactChanged"
|
||||
OnKeyDown="@(EventArgs=>HandleChipContainerEnter<ArtifactEntry>(EventArgs, _assocaitedArtifactsChipContainer, _associatedArtifactValue, () => _associatedArtifactValue = null))"
|
||||
CoerceValue="false"
|
||||
Placeholder="Link artifact groupings..."
|
||||
ShowProgressIndicator=true>
|
||||
</MudAutocomplete>
|
||||
</InputContent>
|
||||
</ChipContainer>
|
||||
|
||||
<MudText Typo="Typo.h6" Color="Color.Primary" Class="pt-4 pb-0">Artifact Text Contents</MudText>
|
||||
<MudText Typo="Typo.caption" Color="Color.Secondary" Class="pb-2">Input the text transcription of the words on the artifact if applicable to aid the search engine.</MudText>
|
||||
<MudDivider DividerType="DividerType.Middle"></MudDivider>
|
||||
<MudTextField T="string"
|
||||
Value=Model.FileTextContent
|
||||
ValueChanged="OnArtifactTextContentChanged"
|
||||
Lines="5"
|
||||
For="@(() => Model.FileTextContent)"></MudTextField>
|
||||
|
||||
<MudText Typo=Typo.h6 Color="Color.Primary">Additional files</MudText>
|
||||
<UploadDropBox @ref=_uploadDropBox FilesUploaded="OnFilesUploaded"></UploadDropBox>
|
||||
</MudPaper>
|
||||
|
||||
|
||||
@code {
|
||||
[Parameter]
|
||||
public required FilePathListing MainFilePath { get; set; }
|
||||
|
||||
[Parameter]
|
||||
public EventCallback<ArtifactEntryValidationModel> ModelChanged { get; set; }
|
||||
|
||||
[Parameter]
|
||||
public EventCallback InputsChanged { get; set; }
|
||||
|
||||
[Parameter]
|
||||
public required ArtifactEntryValidationModel Model { get; set; } = new() { StorageLocation = "hello", Title = "Hello" };
|
||||
|
||||
[Parameter]
|
||||
public required int ArtifactEntryIndex {get; set;}
|
||||
|
||||
[Parameter]
|
||||
public EventCallback<(int index, string filename)> OnEntryDeletedClicked { get; set; }
|
||||
|
||||
private ChipContainer<string> _tagsChipContainer;
|
||||
|
||||
private string _tagsInputValue { get; set; } = "";
|
||||
|
||||
private ChipContainer<DateTime> _assocaitedDatesChipContainer;
|
||||
|
||||
private DateTime? _associatedDateInputValue { get; set; } = default;
|
||||
|
||||
private ChipContainer<string> _listedNamesChipContainer;
|
||||
|
||||
private string _listedNamesInputValue { get; set; } = "";
|
||||
|
||||
private ChipContainer<string> _defectsChipContainer;
|
||||
|
||||
private string _defectsInputValue = "";
|
||||
|
||||
private ChipContainer<ArtifactEntry> _assocaitedArtifactsChipContainer;
|
||||
|
||||
private ArtifactEntry? _associatedArtifactValue = null;
|
||||
|
||||
private string _artifactTextContent = "";
|
||||
|
||||
public bool IsValid { get; set; }
|
||||
|
||||
public List<ValidationResult> ValidationResults { get; private set; } = [];
|
||||
|
||||
public UploadDropBox _uploadDropBox = default!;
|
||||
|
||||
protected override Task OnParametersSetAsync()
|
||||
{
|
||||
if (_uploadDropBox is not null && Model is not null && Model.Files is not null)
|
||||
{
|
||||
_uploadDropBox.ExistingFiles = Model.Files.GetRange(1, Model.Files.Count - 1);
|
||||
}
|
||||
|
||||
if (Model.Files is not null && Model.Files.Any())
|
||||
{
|
||||
MainFilePath = Model.Files[0];
|
||||
}
|
||||
|
||||
return base.OnParametersSetAsync();
|
||||
}
|
||||
|
||||
public async Task OnInputsChanged()
|
||||
{
|
||||
// 1. Clear previous validation errors
|
||||
ValidationResults.Clear();
|
||||
|
||||
var validationContext = new ValidationContext(Model);
|
||||
|
||||
// 2. Run the validator
|
||||
IsValid = Validator.TryValidateObject(Model, validationContext, ValidationResults, validateAllProperties: true);
|
||||
// 3. REMOVE this line. Let the parent's update trigger the re-render.
|
||||
// StateHasChanged();
|
||||
|
||||
await InputsChanged.InvokeAsync();
|
||||
}
|
||||
|
||||
private async Task OnFilesUploaded(List<FilePathListing> filePathListings)
|
||||
{
|
||||
if (MainFilePath is not null)
|
||||
{
|
||||
var oldFiles = Model.Files.GetRange(1, Model.Files.Count - 1);
|
||||
Model.Files = [MainFilePath];
|
||||
Model.Files.AddRange(oldFiles);
|
||||
Model.Files.AddRange(filePathListings);
|
||||
} else
|
||||
{
|
||||
Model.Files = [];
|
||||
}
|
||||
Model.Files.AddRange(filePathListings);
|
||||
|
||||
StateHasChanged();
|
||||
}
|
||||
|
||||
private async Task OnFilesCleared()
|
||||
{
|
||||
if (MainFilePath is not null) {
|
||||
Model.Files = [MainFilePath];
|
||||
} else
|
||||
{
|
||||
Model.Files = [];
|
||||
}
|
||||
}
|
||||
|
||||
private Task OnDefectsValueChanged(string text)
|
||||
{
|
||||
_defectsInputValue = text;
|
||||
return ModelChanged.InvokeAsync(Model);
|
||||
}
|
||||
|
||||
private Task OnTagsInputTextChanged(string text)
|
||||
{
|
||||
_tagsInputValue = text;
|
||||
return ModelChanged.InvokeAsync(Model);
|
||||
}
|
||||
|
||||
private Task OnListedNamesTextChanged(string text)
|
||||
{
|
||||
_listedNamesInputValue = text;
|
||||
return ModelChanged.InvokeAsync(Model);
|
||||
}
|
||||
|
||||
private Task OnAssociatedArtifactChanged(ArtifactEntry grouping)
|
||||
{
|
||||
if (grouping is not null)
|
||||
{
|
||||
_associatedArtifactValue = grouping;
|
||||
return ModelChanged.InvokeAsync(Model);
|
||||
}
|
||||
|
||||
return ModelChanged.InvokeAsync(Model);
|
||||
}
|
||||
|
||||
private Task OnArtifactTextContentChanged(string value)
|
||||
{
|
||||
Model.FileTextContent = value;
|
||||
return ModelChanged.InvokeAsync(Model);
|
||||
}
|
||||
|
||||
public async Task HandleChipContainerEnter<Type>(KeyboardEventArgs args, ChipContainer<Type> container, Type value, Action resetInputAction)
|
||||
{
|
||||
if (args.Key == "Enter")
|
||||
{
|
||||
await container.AddItem(value);
|
||||
resetInputAction?.Invoke();
|
||||
StateHasChanged();
|
||||
await ModelChanged.InvokeAsync(Model);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task HandleAssociatedDateChipContainerAdd(MouseEventArgs args)
|
||||
{
|
||||
if (_associatedDateInputValue is not null)
|
||||
{
|
||||
DateTime unspecifiedDate = (DateTime)_associatedDateInputValue;
|
||||
|
||||
DateTime utcDate = DateTime.SpecifyKind(unspecifiedDate, DateTimeKind.Utc);
|
||||
|
||||
await _assocaitedDatesChipContainer.AddItem(utcDate);
|
||||
|
||||
_associatedDateInputValue = default;
|
||||
}
|
||||
}
|
||||
private async Task OnDeleteEntryClicked(MouseEventArgs args)
|
||||
{
|
||||
await OnEntryDeletedClicked.InvokeAsync((ArtifactEntryIndex, MainFilePath.OriginalName));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,176 @@
|
||||
@namespace OpenArchival.Blazor.AdminPages
|
||||
@using Microsoft.AspNetCore.Components.Web
|
||||
@using MudBlazor
|
||||
@using MudExtensions
|
||||
@using OpenArchival.DataAccess
|
||||
|
||||
<MudDataGrid
|
||||
T="ArtifactGroupingRowElement"
|
||||
MultiSelection=true
|
||||
Filterable=false
|
||||
SelectOnRowClick=true
|
||||
ServerData="new Func<GridState<ArtifactGroupingRowElement>, Task<GridData<ArtifactGroupingRowElement>>>(ServerReload)"
|
||||
@key=@ArtifactGroupingRows
|
||||
@ref=@DataGrid>
|
||||
|
||||
<ToolBarContent>
|
||||
<MudButton
|
||||
Variant="Variant.Filled"
|
||||
StartIcon="@Icons.Material.Filled.Delete"
|
||||
Color="Color.Error"
|
||||
OnClick="OnDeleteClicked">Delete</MudButton>
|
||||
</ToolBarContent>
|
||||
|
||||
<Columns>
|
||||
<SelectColumn T="ArtifactGroupingRowElement"/>
|
||||
|
||||
<PropertyColumn
|
||||
Title="Id"
|
||||
Property="x=>x.ArtifactGroupingIdentifier"
|
||||
Filterable="false"/>
|
||||
|
||||
<PropertyColumn
|
||||
Title="Category"
|
||||
Property="x=>x.CategoryName"
|
||||
Filterable="false"/>
|
||||
|
||||
<PropertyColumn
|
||||
Title="Title"
|
||||
Property="x=>x.Title"
|
||||
Filterable="false"/>
|
||||
|
||||
<PropertyColumn
|
||||
Title="Publically Visible"
|
||||
Property="x=>x.IsPublicallyVisible"
|
||||
Filterable="false"/>
|
||||
|
||||
<TemplateColumn Title="Edit">
|
||||
<CellTemplate>
|
||||
<MudIconButton
|
||||
Color="Color.Primary"
|
||||
Icon="@Icons.Material.Filled.Edit"
|
||||
Variant="Variant.Filled"
|
||||
OnClick="() => OnRowEditClick(context.Item)">Edit</MudIconButton>
|
||||
</CellTemplate>
|
||||
</TemplateColumn>
|
||||
|
||||
</Columns>
|
||||
|
||||
<PagerContent>
|
||||
<MudDataGridPager T="ArtifactGroupingRowElement"/>
|
||||
</PagerContent>
|
||||
</MudDataGrid>
|
||||
|
||||
|
||||
@inject IArtifactGroupingProvider GroupingProvider;
|
||||
@inject IDialogService DialogService;
|
||||
@inject IArtifactGroupingProvider GroupingProvider;
|
||||
@inject ArtifactEntrySharedHelpers Helpers;
|
||||
|
||||
@code
|
||||
{
|
||||
public List<ArtifactGroupingRowElement> ArtifactGroupingRows { get; set; } = new();
|
||||
|
||||
public MudDataGrid<ArtifactGroupingRowElement> DataGrid { get; set; } = default!;
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
// Load inital data
|
||||
List<ArtifactGrouping> groupings = await GroupingProvider.GetGroupingsPaged(1, 25);
|
||||
SetGroupingRows(groupings);
|
||||
|
||||
await base.OnInitializedAsync();
|
||||
StateHasChanged();
|
||||
}
|
||||
|
||||
private void SetGroupingRows(IEnumerable<ArtifactGrouping> groupings)
|
||||
{
|
||||
ArtifactGroupingRows.Clear();
|
||||
|
||||
foreach (var grouping in groupings)
|
||||
{
|
||||
ArtifactGroupingRows.Add(new ArtifactGroupingRowElement()
|
||||
{
|
||||
ArtifactGroupingIdentifier=grouping.ArtifactGroupingIdentifier ?? throw new ArgumentNullException(nameof(grouping), "Got a null grouping identifier"),
|
||||
CategoryName=grouping.Category.Name,
|
||||
Title=grouping.Title,
|
||||
IsPublicallyVisible=grouping.IsPublicallyVisible,
|
||||
Id=grouping.Id
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task OnRowEditClick(ArtifactGroupingRowElement row)
|
||||
{
|
||||
var parameters = new DialogParameters();
|
||||
|
||||
var model = await GroupingProvider.GetGroupingAsync(row.Id);
|
||||
|
||||
parameters.Add("Model", ArtifactGroupingValidationModel.ToValidationModel(model));
|
||||
|
||||
var options = new DialogOptions()
|
||||
{
|
||||
MaxWidth = MaxWidth.ExtraExtraLarge,
|
||||
FullWidth = true
|
||||
};
|
||||
|
||||
var dialog = await DialogService.ShowAsync<AddGroupingDialog>("Edit Grouping", parameters, options);
|
||||
|
||||
var result = await dialog.Result;
|
||||
|
||||
if (result is not null && !result.Canceled)
|
||||
{
|
||||
var validationModel = (ArtifactGroupingValidationModel)result.Data!;
|
||||
|
||||
await Helpers.OnGroupingPublished(validationModel);
|
||||
|
||||
await DataGrid.ReloadServerData();
|
||||
}
|
||||
}
|
||||
|
||||
private async Task OnDeleteClicked(MouseEventArgs args)
|
||||
{
|
||||
HashSet<ArtifactGroupingRowElement> selected = DataGrid.SelectedItems;
|
||||
|
||||
bool? confirmed = await DialogService.ShowMessageBox
|
||||
(
|
||||
new MessageBoxOptions(){
|
||||
Message=$"Are you sure you want to delete {selected.Count} groupings?",
|
||||
Title="Delete Groupings",
|
||||
CancelText="Cancel",
|
||||
YesText="Delete"
|
||||
});
|
||||
|
||||
if (confirmed is not null && (confirmed ?? throw new ArgumentNullException("confirmed was null")))
|
||||
{
|
||||
foreach (var grouping in selected)
|
||||
{
|
||||
await GroupingProvider.DeleteGroupingAsync(grouping.Id);
|
||||
StateHasChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<GridData<ArtifactGroupingRowElement>> ServerReload(GridState<ArtifactGroupingRowElement> state)
|
||||
{
|
||||
int totalItems = await GroupingProvider.GetTotalCount();
|
||||
|
||||
IEnumerable<ArtifactGrouping> groupings = await GroupingProvider.GetGroupingsPaged(state.Page + 1, state.PageSize);
|
||||
|
||||
var pagedItems = groupings.Select(grouping => new ArtifactGroupingRowElement()
|
||||
{
|
||||
Id = grouping.Id,
|
||||
Title = grouping.Title,
|
||||
ArtifactGroupingIdentifier = grouping.ArtifactGroupingIdentifier ?? throw new ArgumentNullException(nameof(grouping), "Got a null ArtifactGroupingIdentifier"),
|
||||
CategoryName = grouping.Category.Name,
|
||||
IsPublicallyVisible = grouping.IsPublicallyVisible,
|
||||
});
|
||||
|
||||
return new GridData<ArtifactGroupingRowElement>()
|
||||
{
|
||||
TotalItems = totalItems,
|
||||
Items = pagedItems
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,259 @@
|
||||
namespace OpenArchival.Blazor.AdminPages;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using OpenArchival.DataAccess;
|
||||
|
||||
public class ArtifactEntrySharedHelpers
|
||||
{
|
||||
IArtifactDefectProvider DefectsProvider { get; set; }
|
||||
|
||||
IArtifactStorageLocationProvider StorageLocationProvider { get; set; }
|
||||
|
||||
IArchiveEntryTagProvider TagsProvider { get; set; }
|
||||
|
||||
IArtifactTypeProvider TypesProvider { get; set; }
|
||||
|
||||
IListedNameProvider ListedNameProvider { get; set; }
|
||||
|
||||
IDbContextFactory<ApplicationDbContext> DbContextFactory { get; set; }
|
||||
|
||||
IArtifactGroupingProvider GroupingProvider { get; set; }
|
||||
|
||||
|
||||
public ArtifactEntrySharedHelpers(IArtifactDefectProvider defectsProvider, IArtifactStorageLocationProvider storageLocationProvider, IArchiveEntryTagProvider tagsProvider, IArtifactTypeProvider typesProvider, IListedNameProvider listedNamesProvider, IDbContextFactory<ApplicationDbContext> contextFactory, IArtifactGroupingProvider groupingProvider)
|
||||
{
|
||||
DefectsProvider = defectsProvider;
|
||||
StorageLocationProvider = storageLocationProvider;
|
||||
TagsProvider = tagsProvider;
|
||||
TypesProvider = typesProvider;
|
||||
ListedNameProvider = listedNamesProvider;
|
||||
DbContextFactory = contextFactory;
|
||||
GroupingProvider = groupingProvider;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<string>> SearchDefects(string value, CancellationToken cancellationToken)
|
||||
{
|
||||
List<string> defects;
|
||||
if (string.IsNullOrEmpty(value))
|
||||
{
|
||||
defects = new((await DefectsProvider.Top(25) ?? []).Select(prop => prop.Description));
|
||||
}
|
||||
else
|
||||
{
|
||||
defects = new((await DefectsProvider.Search(value) ?? []).Select(prop => prop.Description));
|
||||
}
|
||||
|
||||
return defects;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<string>> SearchStorageLocation(string value, CancellationToken cancellationToken)
|
||||
{
|
||||
List<string> storageLocations;
|
||||
if (string.IsNullOrEmpty(value))
|
||||
{
|
||||
storageLocations = new((await StorageLocationProvider.Top(25) ?? []).Select(prop => prop.Location));
|
||||
}
|
||||
else
|
||||
{
|
||||
storageLocations = new((await StorageLocationProvider.Search(value) ?? []).Select(prop => prop.Location));
|
||||
}
|
||||
|
||||
return storageLocations;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<string>> SearchTags(string value, CancellationToken cancellationToken)
|
||||
{
|
||||
List<string> tags;
|
||||
if (string.IsNullOrEmpty(value))
|
||||
{
|
||||
tags = new((await TagsProvider.Top(25) ?? []).Select(prop => prop.Name));
|
||||
}
|
||||
else
|
||||
{
|
||||
tags = new((await TagsProvider.Search(value) ?? []).Select(prop => prop.Name));
|
||||
}
|
||||
|
||||
return tags;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<string>> SearchItemTypes(string value, CancellationToken cancellationToken)
|
||||
{
|
||||
List<string> itemTypes;
|
||||
if (string.IsNullOrEmpty(value))
|
||||
{
|
||||
itemTypes = new((await TypesProvider.Top(25) ?? []).Select(prop => prop.Name));
|
||||
}
|
||||
else
|
||||
{
|
||||
itemTypes = new((await TypesProvider.Search(value) ?? []).Select(prop => prop.Name));
|
||||
}
|
||||
|
||||
return itemTypes;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<string>> SearchListedNames(string value, CancellationToken cancellationToken)
|
||||
{
|
||||
List<ListedName> names;
|
||||
if (string.IsNullOrEmpty(value))
|
||||
{
|
||||
names = new((await ListedNameProvider.Top(25) ?? []));
|
||||
}
|
||||
else
|
||||
{
|
||||
names = new((await ListedNameProvider.Search(value) ?? []));
|
||||
}
|
||||
|
||||
return names.Select(p => p.Value);
|
||||
}
|
||||
|
||||
/*
|
||||
public async Task OnGroupingPublished(ArtifactGroupingValidationModel model)
|
||||
{
|
||||
await using var context = await DbContextFactory.CreateDbContextAsync();
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
var grouping = model.ToArtifactGrouping();
|
||||
|
||||
// The old logic for attaching the category is still good.
|
||||
context.Attach(grouping.Category);
|
||||
|
||||
// 1. Handle ArtifactType (no change, this was fine)
|
||||
if (grouping.Type is not null)
|
||||
{
|
||||
var existingType = await context.ArtifactTypes
|
||||
.FirstOrDefaultAsync(t => t.Name == grouping.Type.Name);
|
||||
|
||||
if (existingType is not null)
|
||||
{
|
||||
grouping.Type = existingType;
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Process ChildArtifactEntries
|
||||
foreach (var entry in grouping.ChildArtifactEntries)
|
||||
{
|
||||
// Handle ArtifactStorageLocation (no change, this was fine)
|
||||
var existingLocation = await context.ArtifactStorageLocations
|
||||
.FirstOrDefaultAsync(l => l.Location == entry.StorageLocation.Location);
|
||||
|
||||
if (existingLocation is not null)
|
||||
{
|
||||
entry.StorageLocation = existingLocation;
|
||||
}
|
||||
|
||||
// Handle Defects
|
||||
if (entry.Defects is not null && entry.Defects.Any())
|
||||
{
|
||||
var defectDescriptions = entry.Defects.Select(d => d.Description).ToList();
|
||||
var existingDefects = await context.ArtifactDefects
|
||||
.Where(d => defectDescriptions.Contains(d.Description))
|
||||
.ToListAsync();
|
||||
|
||||
// Replace in-memory defects with existing ones
|
||||
for (int i = 0; i < entry.Defects.Count; i++)
|
||||
{
|
||||
var existingDefect = existingDefects
|
||||
.FirstOrDefault(ed => ed.Description == entry.Defects[i].Description);
|
||||
|
||||
if (existingDefect is not null)
|
||||
{
|
||||
entry.Defects[i] = existingDefect;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Handle ListedNames
|
||||
if (entry.ListedNames is not null && entry.ListedNames.Any())
|
||||
{
|
||||
var listedNamesValues = entry.ListedNames.Select(n => n.Value).ToList();
|
||||
var existingNames = await context.ArtifactAssociatedNames
|
||||
.Where(n => listedNamesValues.Contains(n.Value))
|
||||
.ToListAsync();
|
||||
|
||||
for (int i = 0; i < entry.ListedNames.Count; i++)
|
||||
{
|
||||
var existingName = existingNames
|
||||
.FirstOrDefault(en => en.Value == entry.ListedNames[i].Value);
|
||||
|
||||
if (existingName is not null)
|
||||
{
|
||||
entry.ListedNames[i] = existingName;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Handle Tags
|
||||
if (entry.Tags is not null && entry.Tags.Any())
|
||||
{
|
||||
var tagNames = entry.Tags.Select(t => t.Name).ToList();
|
||||
var existingTags = await context.ArtifactEntryTags
|
||||
.Where(t => tagNames.Contains(t.Name))
|
||||
.ToListAsync();
|
||||
|
||||
for (int i = 0; i < entry.Tags.Count; i++)
|
||||
{
|
||||
var existingTag = existingTags
|
||||
.FirstOrDefault(et => et.Name == entry.Tags[i].Name);
|
||||
|
||||
if (existingTag is not null)
|
||||
{
|
||||
entry.Tags[i] = existingTag;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 💡 NEW: Handle pre-existing FilePathListings
|
||||
// This is the key change to resolve the exception
|
||||
if (entry.Files is not null)
|
||||
{
|
||||
foreach (var filepath in entry.Files)
|
||||
{
|
||||
// The issue is trying to add a new entity that has an existing primary key.
|
||||
// Since you stated that all files are pre-added, you must attach them.
|
||||
// Attach() tells EF Core to track the entity, assuming it already exists.
|
||||
context.Attach(filepath);
|
||||
// Also ensure the parent-child relationship is set correctly, though it's likely set by ToArtifactGrouping
|
||||
filepath.ParentArtifactEntry = entry;
|
||||
}
|
||||
}
|
||||
// Tag each entry with the parent grouping so it is linked correctly in the database
|
||||
entry.ArtifactGrouping = grouping;
|
||||
}
|
||||
|
||||
// 3. Add the main grouping object and let EF Core handle the graph
|
||||
// The previous issues with the graph are resolved, so this line should now work.
|
||||
context.ArtifactGroupings.Add(grouping);
|
||||
|
||||
// 4. Save all changes in a single transaction
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
*/
|
||||
|
||||
public async Task OnGroupingPublished(ArtifactGroupingValidationModel model)
|
||||
{
|
||||
// The OnGroupingPublished method in this class should not contain DbContext logic.
|
||||
// It should orchestrate the data flow by calling the appropriate provider methods.
|
||||
var isNew = model.Id == 0 || model.Id is null;
|
||||
|
||||
// Convert the validation model to an entity
|
||||
var grouping = model.ToArtifactGrouping();
|
||||
|
||||
if (isNew)
|
||||
{
|
||||
// For a new grouping, use the CreateGroupingAsync method.
|
||||
// The provider method will handle the file path logic.
|
||||
await GroupingProvider.CreateGroupingAsync(grouping);
|
||||
}
|
||||
else
|
||||
{
|
||||
// For an existing grouping, use the UpdateGroupingAsync method.
|
||||
// The provider method will handle the change tracking.
|
||||
await GroupingProvider.UpdateGroupingAsync(grouping);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
@namespace OpenArchival.Blazor.AdminPages
|
||||
@using System.Text
|
||||
@using MudBlazor
|
||||
|
||||
<MudText Typo="Typo.body2" Color="Color.Primary">Item Identifier: @Value</MudText>
|
||||
@if (_identifierError)
|
||||
{
|
||||
<MudAlert Severity="Severity.Error" Class="mt-4">
|
||||
All identifier fields must be filled in.
|
||||
</MudAlert>
|
||||
}
|
||||
<MudDivider DividerType="DividerType.Middle"></MudDivider>
|
||||
<MudGrid Class="pt-0 pl-4 pr-4" Justify="Justify.FlexStart" AlignItems="AlignItems.Center">
|
||||
|
||||
@for (int index = 0; index < IdentifierFields.Count; index++)
|
||||
{
|
||||
// You must create a local variable inside the loop for binding to work correctly.
|
||||
var field = IdentifierFields[index];
|
||||
|
||||
<MudItem Class="pt-6">
|
||||
<MudTextField Label="@field.Name"
|
||||
@bind-Value="@field.Value"
|
||||
@bind-Value:after="OnInputChanged"
|
||||
DebounceInterval="100"
|
||||
Required=true/>
|
||||
</MudItem>
|
||||
|
||||
@if (index < IdentifierFields.Count - 1)
|
||||
{
|
||||
<MudItem Class="pt-6">
|
||||
<MudText>@FieldSeparator</MudText>
|
||||
</MudItem>
|
||||
}
|
||||
}
|
||||
</MudGrid>
|
||||
|
||||
@using OpenArchival.DataAccess;
|
||||
@inject IArchiveCategoryProvider CategoryProvider;
|
||||
|
||||
@code {
|
||||
[Parameter]
|
||||
public required string FieldSeparator { get; set; } = "-";
|
||||
|
||||
private List<IdentifierFieldValidationModel> _identifierFields = new();
|
||||
|
||||
[Parameter]
|
||||
public required List<IdentifierFieldValidationModel> IdentifierFields
|
||||
{
|
||||
get => _identifierFields;
|
||||
set => _identifierFields = value ?? new();
|
||||
}
|
||||
|
||||
[Parameter]
|
||||
public EventCallback<string> ValueChanged { get; set; }
|
||||
|
||||
private ArchiveCategory _verifyFormatCategory;
|
||||
public ArchiveCategory? VerifyFormatCategory
|
||||
{
|
||||
get
|
||||
{
|
||||
return _verifyFormatCategory;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (value is not null)
|
||||
{
|
||||
_identifierFields.Clear();
|
||||
_verifyFormatCategory = value;
|
||||
foreach (var field in value.FieldNames)
|
||||
{
|
||||
_identifierFields.Add(new IdentifierFieldValidationModel() {Name=field, Value=""});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsValid { get; set; } = false;
|
||||
|
||||
// Computed property that builds the final string
|
||||
public string Value => string.Join(FieldSeparator, IdentifierFields.Select(f => f.Value).Where(v => !string.IsNullOrEmpty(v)));
|
||||
|
||||
private bool _identifierError = false;
|
||||
|
||||
/// <summary>
|
||||
/// This runs when parameters are first set, ensuring the initial state is correct.
|
||||
/// </summary>
|
||||
protected override void OnParametersSet()
|
||||
{
|
||||
ValidateFields();
|
||||
StateHasChanged();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This runs after the user types into a field.
|
||||
/// </summary>
|
||||
private async Task OnInputChanged()
|
||||
{
|
||||
ValidateFields();
|
||||
await ValueChanged.InvokeAsync(this.Value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reusable method to check the validity of the identifier fields.
|
||||
/// </summary>
|
||||
private void ValidateFields()
|
||||
{
|
||||
// Set to true if ANY field is empty or null.
|
||||
_identifierError = IdentifierFields.Any(f => string.IsNullOrEmpty(f.Value));
|
||||
IsValid = !_identifierError;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
namespace OpenArchival.Blazor;
|
||||
|
||||
using OpenArchival.DataAccess;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
public class ArtifactEntryValidationModel
|
||||
{
|
||||
/// <summary>
|
||||
/// Used when translating between the validation model and the database model
|
||||
/// </summary>
|
||||
public int? Id { get; set; }
|
||||
|
||||
[Required(AllowEmptyStrings = false, ErrorMessage = "An artifact numbering must be supplied")]
|
||||
public string? ArtifactNumber { get; set; }
|
||||
|
||||
[Required(AllowEmptyStrings = false, ErrorMessage = "A title must be provided")]
|
||||
public string? Title { get; set; }
|
||||
|
||||
public string? Description { get; set; }
|
||||
|
||||
public string? Type { get; set; }
|
||||
|
||||
public string? StorageLocation { get; set; }
|
||||
|
||||
public List<string>? Tags { get; set; } = [];
|
||||
|
||||
public List<string>? ListedNames { get; set; } = [];
|
||||
|
||||
public List<DateTime>? AssociatedDates { get; set; } = [];
|
||||
|
||||
public List<string>? Defects { get; set; } = [];
|
||||
|
||||
public List<string>? Links { get; set; } = [];
|
||||
|
||||
public List<FilePathListing>? Files { get; set; } = [];
|
||||
|
||||
public string? FileTextContent { get; set; }
|
||||
|
||||
public List<ArtifactEntry> RelatedArtifacts { get; set; } = [];
|
||||
|
||||
public bool IsPublicallyVisible { get; set; } = true;
|
||||
|
||||
public ArtifactEntry ToArtifactEntry(ArtifactGrouping? parent = null)
|
||||
{
|
||||
List<ArtifactEntryTag> tags = new();
|
||||
if (Tags is not null)
|
||||
{
|
||||
foreach (var tag in Tags)
|
||||
{
|
||||
tags.Add(new ArtifactEntryTag() { Name = tag });
|
||||
}
|
||||
}
|
||||
|
||||
List<ArtifactDefect> defects = new();
|
||||
foreach (var defect in Defects)
|
||||
{
|
||||
defects.Add(new ArtifactDefect() { Description=defect});
|
||||
}
|
||||
|
||||
var entry = new ArtifactEntry()
|
||||
{
|
||||
Id = Id ?? 0,
|
||||
Files = Files,
|
||||
Type = new DataAccess.ArtifactType() { Name = Type },
|
||||
ArtifactNumber = ArtifactNumber,
|
||||
AssociatedDates = AssociatedDates,
|
||||
Defects = defects,
|
||||
Links = Links,
|
||||
StorageLocation = null,
|
||||
Description = Description,
|
||||
FileTextContent = FileTextContent,
|
||||
IsPubliclyVisible = IsPublicallyVisible,
|
||||
Tags = tags,
|
||||
Title = Title,
|
||||
ArtifactGrouping = parent,
|
||||
RelatedTo = RelatedArtifacts,
|
||||
};
|
||||
|
||||
List<ListedName> listedNames = new();
|
||||
foreach (var name in ListedNames)
|
||||
{
|
||||
listedNames.Add(new ListedName() { Value=name });
|
||||
}
|
||||
|
||||
entry.ListedNames = listedNames;
|
||||
|
||||
if (!string.IsNullOrEmpty(StorageLocation))
|
||||
{
|
||||
entry.StorageLocation = new ArtifactStorageLocation() { Location = StorageLocation };
|
||||
}
|
||||
|
||||
return entry;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
using OpenArchival.DataAccess;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace OpenArchival.Blazor;
|
||||
|
||||
public class ArtifactGroupingValidationModel : IValidatableObject
|
||||
{
|
||||
/// <summary>
|
||||
/// Used by update code to track the database record that corresponds to the data within this DTO
|
||||
/// </summary>
|
||||
public int? Id { get; set; }
|
||||
|
||||
[Required(ErrorMessage = "A grouping title is required.")]
|
||||
public string? Title { get; set; }
|
||||
|
||||
[Required(ErrorMessage = "A grouping description is required.")]
|
||||
public string? Description { get; set; }
|
||||
|
||||
[Required(ErrorMessage = "A type is required.")]
|
||||
public string? Type { get; set; }
|
||||
|
||||
public ArchiveCategory? Category { get; set; }
|
||||
|
||||
public List<IdentifierFieldValidationModel> IdentifierFieldValues { get; set; } = new();
|
||||
|
||||
public List<ArtifactEntryValidationModel> ArtifactEntries { get; set; } = new();
|
||||
|
||||
public bool IsPublicallyVisible { get; set; } = true;
|
||||
|
||||
public ArtifactGrouping ToArtifactGrouping()
|
||||
{
|
||||
IdentifierFields identifierFields = new();
|
||||
identifierFields.Values = IdentifierFieldValues.Select(p => p.Value).ToList();
|
||||
|
||||
List<ArtifactEntry> entries = [];
|
||||
foreach (var entry in ArtifactEntries)
|
||||
{
|
||||
entries.Add(entry.ToArtifactEntry());
|
||||
}
|
||||
|
||||
var grouping = new ArtifactGrouping()
|
||||
{
|
||||
Id = Id ?? default,
|
||||
Title = Title,
|
||||
Description = Description,
|
||||
Category = Category,
|
||||
IdentifierFields = identifierFields,
|
||||
IsPublicallyVisible = true,
|
||||
ChildArtifactEntries = entries,
|
||||
Type = new ArtifactType() { Name = Type }
|
||||
};
|
||||
|
||||
// Create the parent link
|
||||
foreach (var entry in grouping.ChildArtifactEntries)
|
||||
{
|
||||
entry.ArtifactGrouping = grouping;
|
||||
}
|
||||
|
||||
return grouping;
|
||||
}
|
||||
|
||||
public static ArtifactGroupingValidationModel ToValidationModel(ArtifactGrouping grouping)
|
||||
{
|
||||
var entries = new List<ArtifactEntryValidationModel>();
|
||||
|
||||
foreach (var entry in grouping.ChildArtifactEntries)
|
||||
{
|
||||
var defects = new List<string>();
|
||||
|
||||
if (entry.Defects is not null)
|
||||
{
|
||||
defects.AddRange(entry.Defects.Select(defect => defect.Description));
|
||||
}
|
||||
|
||||
var validationModel = new ArtifactEntryValidationModel()
|
||||
{
|
||||
Id = entry.Id,
|
||||
Title = entry.Title,
|
||||
StorageLocation = entry.StorageLocation.Location,
|
||||
ArtifactNumber = entry.ArtifactNumber,
|
||||
AssociatedDates = entry.AssociatedDates,
|
||||
Defects = entry?.Defects?.Select(defect => defect.Description).ToList(),
|
||||
Description = entry?.Description,
|
||||
Files = entry?.Files,
|
||||
FileTextContent = entry?.FileTextContent,
|
||||
IsPublicallyVisible = entry.IsPubliclyVisible,
|
||||
Links = entry.Links,
|
||||
ListedNames = entry?.ListedNames?.Select(name => name.Value).ToList(),
|
||||
RelatedArtifacts = entry.RelatedTo,
|
||||
Tags = entry?.Tags?.Select(tag => tag.Name).ToList(),
|
||||
Type = entry?.Type.Name,
|
||||
};
|
||||
|
||||
entries.Add(validationModel);
|
||||
}
|
||||
|
||||
var identifierFieldsStrings = grouping.IdentifierFields.Values;
|
||||
List<IdentifierFieldValidationModel> identifierFields = new();
|
||||
for (int index = 0; index < identifierFieldsStrings.Count; ++index)
|
||||
{
|
||||
identifierFields.Add(new IdentifierFieldValidationModel()
|
||||
{
|
||||
Value = identifierFieldsStrings[index],
|
||||
Name = grouping.Category.FieldNames[index]
|
||||
});
|
||||
}
|
||||
return new ArtifactGroupingValidationModel()
|
||||
{
|
||||
Id = grouping.Id,
|
||||
Title = grouping.Title,
|
||||
ArtifactEntries = entries,
|
||||
Category = grouping.Category,
|
||||
Description = grouping.Description,
|
||||
IdentifierFieldValues = identifierFields,
|
||||
IsPublicallyVisible = grouping.IsPublicallyVisible,
|
||||
Type = grouping.Type.Name
|
||||
};
|
||||
}
|
||||
|
||||
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
|
||||
{
|
||||
foreach (var entry in ArtifactEntries)
|
||||
{
|
||||
var context = new ValidationContext(entry);
|
||||
var validationResult = new List<ValidationResult>();
|
||||
|
||||
bool valid = Validator.TryValidateObject(entry, context, validationResult);
|
||||
foreach (var result in validationResult)
|
||||
{
|
||||
yield return result;
|
||||
}
|
||||
}
|
||||
|
||||
if (ArtifactEntries.Count > 0)
|
||||
{
|
||||
yield return new ValidationResult("Must upload one or more files");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace OpenArchival.Blazor;
|
||||
|
||||
public class IdentifierFieldValidationModel
|
||||
{
|
||||
public string Name { get; set; } = "";
|
||||
public string Value { get; set; } = "";
|
||||
}
|
||||
Reference in New Issue
Block a user