Fixed bug where deletes of artifact groupings would not cascade

This commit is contained in:
Vincent Allen
2025-11-12 19:10:35 -05:00
parent b34449808f
commit 9298829db6
325 changed files with 5233 additions and 20996 deletions

View File

@@ -0,0 +1,358 @@
@namespace OpenArchival.Blazor.AdminPages.Shared
@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();
}
}

View File

@@ -0,0 +1,41 @@
@namespace OpenArchival.Blazor.AdminPages.Shared
@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));
}
}

View File

@@ -0,0 +1,54 @@
@using Microsoft.AspNetCore.Components.Routing
@using static Microsoft.AspNetCore.Components.Web.RenderMode
@using MudBlazor
@inherits LayoutComponentBase
@rendermode InteractiveServer
<MudThemeProvider />
<MudPopoverProvider />
<MudDialogProvider />
<MudSnackbarProvider />
<MudLayout>
<MudAppBar Elevation="1">
<MudIconButton Icon="@Icons.Material.Filled.Menu" Color="Color.Inherit" Edge="Edge.Start" OnClick="@ToggleDrawer" />
<MudText Typo="Typo.h5" Class="ml-3">Admin Console</MudText>
<MudSpacer />
<MudIconButton Icon="@Icons.Material.Filled.MoreVert" Color="Color.Inherit" Edge="Edge.End" />
</MudAppBar>
<MudDrawer @bind-Open="_drawerOpen" ClipMode="DrawerClipMode.Always" Elevation="2">
<MudNavMenu>
<MudNavLink Href="/admin/archiveadmin" Match="NavLinkMatch.All" Icon="@Icons.Material.Filled.Dashboard">Archive Entry Management</MudNavLink>
<MudNavLink Href="/admin/searchpage" Match="NavLinkMatch.All" Icon="@Icons.Material.Filled.Search">Search Page Settings</MudNavLink>
<MudNavLink Href="/admin/homepageeditor" Match="NavLinkMatch.All" Icon="@Icons.Material.Filled.Home">Home Page Editor</MudNavLink>
<MudNavLink Href="/admin/aboutpageeditor" Match="NavLinkMatch.All" Icon="@Icons.Material.Filled.QuestionMark">About Page Editor</MudNavLink>
<MudNavGroup Title="Blog" Icon="@Icons.Material.Filled.Book">
<MudNavLink Href="/admin/blogedit" Match="NavLinkMatch.All" Icon="@Icons.Material.Filled.Edit">Create Blog Post</MudNavLink>
<MudNavLink Href="/admin/blogstable" Match="NavLinkMatch.All" Icon="@Icons.Material.Filled.Edit">Edit Blog Posts</MudNavLink>
</MudNavGroup>
</MudNavMenu>
</MudDrawer>
<MudMainContent>
<MudContainer MaxWidth="MaxWidth.ExtraLarge" Class="pa-8">
@Body
</MudContainer>
</MudMainContent>
</MudLayout>
<div id="blazor-error-ui" data-nosnippet>
An unhandled error has occurred.
<a href="." class="reload">Reload</a>
<span class="dismiss">🗙</span>
</div>
@code {
private bool _drawerOpen = true;
private void ToggleDrawer()
{
_drawerOpen = !_drawerOpen;
}
}

View File

@@ -0,0 +1,270 @@
@namespace OpenArchival.Blazor.AdminPages
@using Microsoft.AspNetCore.Components.Web
@using MudBlazor
@using MudExtensions
@using OpenArchival.Blazor.ArchiveSearch
@using OpenArchival.DataAccess
@using OpenArchival.Blazor.AdminPages.Shared;
@using OpenArchival.Blazor.AdminPages;
@using OpenArchival.Blazor;
<ArchiveSearchBar @ref=_searchBar SearchTermsChanged="OnSearchTermsChanged"></ArchiveSearchBar>
<MudDataGrid
T="ArtifactGroupingRowElement"
MultiSelection=true
Filterable=false
SelectOnRowClick=true
ServerData="new Func<GridState<ArtifactGroupingRowElement>, Task<GridData<ArtifactGroupingRowElement>>>(ServerReload)"
@ref=@DataGrid
@bind-SelectedItems="_selectedGroupings"
Comparer="_comparer">
<ToolBarContent>
<MudSpacer />
<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;
@inject ArtifactGroupingSearch SearchProvider;
@code
{
public class ArtifactGroupingRowElementComparer : IEqualityComparer<ArtifactGroupingRowElement>
{
public bool Equals(ArtifactGroupingRowElement? x, ArtifactGroupingRowElement? y)
{
if (ReferenceEquals(x, y)) return true;
if (x is null || y is null) return false;
return x.Id == y.Id;
}
public int GetHashCode(ArtifactGroupingRowElement obj)
{
return obj.Id.GetHashCode();
}
}
public List<ArtifactGroupingRowElement> ArtifactGroupingRows { get; set; } = new();
public MudDataGrid<ArtifactGroupingRowElement> DataGrid { get; set; } = default!;
private ArtifactGroupingRowElementComparer _comparer = new();
private string _searchString { get; set; } = "";
private ArchiveSearchBar _searchBar { get; set; } = default!;
private HashSet<ArtifactGroupingRowElement> _selectedGroupings { get; set; } = [];
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);
}
}
await DataGrid.ReloadServerData();
StateHasChanged();
}
private async Task<GridData<ArtifactGroupingRowElement>> ServerReload(GridState<ArtifactGroupingRowElement> state)
{
int totalItems = await GroupingProvider.GetTotalCount();
SearchProvider.PageSize = state.PageSize;
IEnumerable<ArtifactGrouping> groupings;
if (string.IsNullOrEmpty(_searchString))
{
groupings = await GroupingProvider.GetGroupingsPaged(state.Page + 1, state.PageSize);
totalItems = await GroupingProvider.GetTotalCount();
} else
{
await SearchProvider.Search(_searchString, _searchBar.SelectedFilter, state.Page + 1);
groupings = SearchProvider.SearchResults;
}
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,
});
StateHasChanged();
return new GridData<ArtifactGroupingRowElement>()
{
TotalItems = totalItems,
Items = pagedItems
};
}
private async Task OnSearchTermsChanged(string args)
{
_searchString = args;
await DataGrid.ReloadServerData();
}
public async Task<List<ArtifactGrouping>> SelectedItems()
{
List<ArtifactGrouping> selectedGroupings = [];
foreach (ArtifactGroupingRowElement row in DataGrid.SelectedItems)
{
selectedGroupings.Add(await GroupingProvider.GetGroupingAsync(row.Id));
}
return selectedGroupings;
}
public void ClearSelected()
{
// OLD: _selectedGroupings = new HashSet<ArtifactGroupingRowElement>();
// NEW: Initialize with the comparer
_selectedGroupings = new HashSet<ArtifactGroupingRowElement>(_comparer);
}
// 6. ADD THIS NEW PUBLIC METHOD
public Task SetSelectedItemsAsync(IEnumerable<ArtifactGrouping> groupingsToSelect)
{
// Convert from the full ArtifactGrouping entity to the RowElement.
// We only *really* need the Id for the comparer, but populating
// the other fields is good practice.
var rowElementsToSelect = groupingsToSelect.Select(grouping => new ArtifactGroupingRowElement
{
Id = grouping.Id,
Title = grouping.Title,
ArtifactGroupingIdentifier = grouping.ArtifactGroupingIdentifier ?? string.Empty,
CategoryName = grouping.Category?.Name ?? string.Empty,
IsPublicallyVisible = grouping.IsPublicallyVisible
});
// Create a new HashSet from the list, using the comparer.
// Assigning this to the bound variable will update the grid's UI.
_selectedGroupings = new HashSet<ArtifactGroupingRowElement>(rowElementsToSelect, _comparer);
StateHasChanged();
return Task.CompletedTask;
}
}

View File

@@ -0,0 +1,331 @@
@namespace OpenArchival.Blazor.AdminPages.Shared
@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">Item Quantity</MudText>
<MudDivider DividerType="DividerType.Middle"></MudDivider>
<MudNumericField Min="0" @bind-Value=Model.Quantity></MudNumericField>
<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));
}
}

View File

@@ -0,0 +1,111 @@
@namespace OpenArchival.Blazor.AdminPages.Shared
@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;
}
}

View File

@@ -0,0 +1,97 @@
namespace OpenArchival.Blazor.AdminPages.Shared;
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 int Quantity { get; set; }
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,
Quantity = Quantity
};
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;
}
}

View File

@@ -0,0 +1,140 @@
using OpenArchival.DataAccess;
using System.ComponentModel.DataAnnotations;
namespace OpenArchival.Blazor.AdminPages.Shared;
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,
Quantity = entry.Quantity
};
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");
}
}
}

View File

@@ -0,0 +1,7 @@
namespace OpenArchival.Blazor.AdminPages.Shared;
public class IdentifierFieldValidationModel
{
public string Name { get; set; } = "";
public string Value { get; set; } = "";
}

View File

@@ -0,0 +1,259 @@
namespace OpenArchival.Blazor.AdminPages.Shared;
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);
}
}
}

View File

@@ -0,0 +1,25 @@
namespace OpenArchival.Blazor.AdminPages.Shared;
public class ArtifactGroupingRowElement
{
public required int Id { get; set; }
public required string ArtifactGroupingIdentifier { get; set; }
public required string CategoryName { get; set; }
public required string Title { get; set; }
public bool IsPublicallyVisible { get; set; }
public bool Equals(ArtifactGroupingRowElement? other)
{
if (other is null) return false;
if (ReferenceEquals(this, other)) return true;
return Id == other.Id; // Compare based on the unique Id
}
public override bool Equals(object? obj) => Equals(obj as ArtifactGroupingRowElement);
public override int GetHashCode() => Id.GetHashCode();
}

View File

@@ -0,0 +1,86 @@
@namespace OpenArchival.Blazor.AdminPages.Shared
@using Microsoft.EntityFrameworkCore;
@using Microsoft.Extensions.Logging
@using MudBlazor.Interfaces
@using OpenArchival.DataAccess
@using MudBlazor
@using MudExtensions
@page "/categorieslist"
<MudPaper Class="pa-4 ma-2 rounded" Elevation="3">
<MudText Typo="Typo.h6">Categories</MudText>
<MudDivider Class="mb-2"></MudDivider>
<MudList T="string" Clickable="true">
@foreach (ArchiveCategory category in _categories)
{
<MudListItem OnClick="@(() => OnCategoryItemClicked(category))">
@category.Name
@if (ShowDeleteButton)
{
<MudListItemMeta ActionPosition="ActionPosition.End">
<MudIconButton
Icon="@Icons.Material.Filled.Delete"
Color="Color.Error"
Size="Size.Small"
OnClick="@((e) => HandleDeleteClick(category))"
/>
</MudListItemMeta>
}
</MudListItem>
}
</MudList>
@ChildContent
</MudPaper>
@inject IArchiveCategoryProvider CategoryProvider;
@inject ILogger<CategoriesListComponent> Logger;
@code {
[Parameter]
public RenderFragment ChildContent { get; set; } = default!;
[Parameter]
public bool ShowDeleteButton { get; set; } = false;
[Parameter]
public EventCallback<ArchiveCategory> ListItemClickedCallback { get; set; }
[Parameter]
public EventCallback<ArchiveCategory> OnDeleteClickedCallback { get; set; }
private List<ArchiveCategory> _categories = new();
protected override async Task OnInitializedAsync()
{
await LoadCategories();
}
private async Task LoadCategories()
{
var categories = await CategoryProvider.GetAllArchiveCategories();
if (categories is null)
{
Logger.LogError("There were no categories in the database when attempting to load the list of categories.");
_categories.Clear();
return;
}
_categories = categories.ToList();
}
public async Task RefreshData()
{
await LoadCategories();
StateHasChanged();
}
private async Task OnCategoryItemClicked(ArchiveCategory category)
{
await ListItemClickedCallback.InvokeAsync(category);
}
private async Task HandleDeleteClick(ArchiveCategory category)
{
await OnDeleteClickedCallback.InvokeAsync(category);
}
}

View File

@@ -0,0 +1,165 @@
@namespace OpenArchival.Blazor.AdminPages.Shared
@using System.ComponentModel.DataAnnotations;
@using OpenArchival.DataAccess;
@using MudBlazor
<MudDialog>
<TitleContent>
<MudText Typo="Typo.h6">Create a Category</MudText>
</TitleContent>
<DialogContent>
<MudForm @ref="_form">
<MudTextField @bind-Value="ValidationModel.Name"
For="@(() => ValidationModel.Name)"
Label="Category Name"
Variant="Variant.Filled" />
<MudDivider Class="pt-4" DividerType="DividerType.Middle"/>
<MudText Typo="Typo.h6">Item Tag Identifier</MudText>
<MudText Typo="Typo.subtitle2">This will be the format of the identifier used for each archive entry.</MudText>
<MudStack Row="true" AlignItems="AlignItems.Center" Spacing="2">
<MudText Typo="Typo.body2">Format Preview: </MudText>
<MudText Type="Typo.body2" Color="Color.Primary">@FormatPreview</MudText>
</MudStack>
<MudTextField @bind-Value="ValidationModel.FieldSeparator"
@bind-Value:after="UpdateFormatPreview"
For="@(() => ValidationModel.FieldSeparator)"
Label="Field Separator"
Variant="Variant.Filled"
MaxLength="1" />
<MudDivider Class="pt-4" />
<MudNumericField
Value="ValidationModel.NumFields"
ValueChanged="@((int newCount) => OnNumFieldsChanged(newCount))"
Label="Number of fields in the item identifiers"
Variant="Variant.Filled"
Min="1"></MudNumericField>
<MudDivider Class="pt-4" />
<MudGrid Class="pr-2 pt-2 pb-2 pl-8" Justify="Justify.FlexStart" Spacing="3">
@for (int index = 0; index < ValidationModel.FieldNames.Count; ++index)
{
var localIndex = index;
<MudItem xs="12" sm="6" md="6">
<CategoryFieldCardComponent Index="localIndex"
FieldName="@ValidationModel.FieldNames[localIndex]"
FieldDescription="@ValidationModel.FieldDescriptions[localIndex]"
OnNameUpdate="HandleNameUpdate"
OnDescriptionUpdate="HandleDescriptionUpdate"/>
</MudItem>
}
</MudGrid>
</MudForm>
</DialogContent>
<DialogActions>
<MudButton OnClick="Cancel">Cancel</MudButton>
<MudButton Color="Color.Primary" OnClick="Submit">OK</MudButton>
</DialogActions>
</MudDialog>
@inject IArchiveCategoryProvider CategoryProvider;
@code {
[CascadingParameter]
private IMudDialogInstance MudDialog { get; set; } = default!;
[Parameter]
public CategoryValidationModel ValidationModel { get; set; } = default!;
[Parameter]
public bool IsUpdate { get; set; }
[Parameter]
public string OriginalName { get; set; } = string.Empty;
private MudForm _form = default!;
private string FormatPreview { get; set; } = string.Empty;
protected override void OnParametersSet()
{
if (ValidationModel is null)
{
ValidationModel = new CategoryValidationModel { NumFields = 1 };
} else
{
ValidationModel.NumFields = ValidationModel.FieldNames.Count;
}
}
private void OnNumFieldsChanged(int newCount)
{
if (newCount < 1) return;
ValidationModel.NumFields = newCount;
UpdateStateFromModel();
}
private void UpdateStateFromModel()
{
ValidationModel.FieldNames ??= new List<string>();
ValidationModel.FieldDescriptions ??= new List<string>();
while (ValidationModel.FieldNames.Count < ValidationModel.NumFields)
{
ValidationModel.FieldNames.Add($"Field {ValidationModel.FieldNames.Count + 1}");
}
while (ValidationModel.FieldNames.Count > ValidationModel.NumFields)
{
ValidationModel.FieldNames.RemoveAt(ValidationModel.FieldNames.Count - 1);
}
while (ValidationModel.FieldDescriptions.Count < ValidationModel.NumFields)
{
ValidationModel.FieldDescriptions.Add("");
}
while (ValidationModel.FieldDescriptions.Count > ValidationModel.NumFields)
{
ValidationModel.FieldDescriptions.RemoveAt(ValidationModel.FieldDescriptions.Count - 1);
}
UpdateFormatPreview();
StateHasChanged();
}
private void UpdateFormatPreview()
{
var fieldNames = ValidationModel.FieldNames.Select(name => string.IsNullOrEmpty(name) ? "<...>" : $"<{name}>");
FormatPreview = string.Join(ValidationModel.FieldSeparator, fieldNames);
}
private async Task Submit()
{
await _form.Validate();
if (!_form.IsValid) return;
MudDialog.Close(DialogResult.Ok(ValidationModel));
}
private void Cancel() => MudDialog.Cancel();
// In your MudDialog component's @code block
private void HandleNameUpdate((int Index, string NewValue) data)
{
if (data.Index < ValidationModel.FieldNames.Count)
{
ValidationModel.FieldNames[data.Index] = data.NewValue;
UpdateFormatPreview(); // Update the preview in real-time
}
}
private void HandleDescriptionUpdate((int Index, string NewValue) data)
{
if (data.Index < ValidationModel.FieldDescriptions.Count)
{
ValidationModel.FieldDescriptions[data.Index] = data.NewValue;
}
}
}

View File

@@ -0,0 +1,42 @@
@namespace OpenArchival.Blazor.AdminPages.Shared
@using MudBlazor
<MudCard Outlined="true">
<MudCardContent>
<MudTextField @bind-Value="FieldName"
@bind-Value:after="OnNameChanged"
Label="Field Name"
Variant="Variant.Filled"
Immediate="true"
/>
<MudTextField @bind-Value="FieldDescription"
@bind-Value:after="OnDescriptionChanged"
Label="Field Description"
Variant="Variant.Filled"
Lines="2"
Class="mt-3"
Immediate="true"
/>
</MudCardContent>
</MudCard>
@code {
[Parameter] public int Index { get; set; }
[Parameter] public string FieldName { get; set; } = "";
[Parameter] public string FieldDescription { get; set; } = "";
[Parameter] public EventCallback<(int Index, string NewValue)> OnNameUpdate { get; set; }
[Parameter] public EventCallback<(int Index, string NewValue)> OnDescriptionUpdate { get; set; }
private async Task OnNameChanged()
{
await OnNameUpdate.InvokeAsync((Index, FieldName));
}
private async Task OnDescriptionChanged()
{
await OnDescriptionUpdate.InvokeAsync((Index, FieldDescription));
}
}

View File

@@ -0,0 +1,74 @@
namespace OpenArchival.Blazor.AdminPages.Shared;
using Microsoft.IdentityModel.Abstractions;
using Microsoft.IdentityModel.Tokens;
using OpenArchival.DataAccess;
using System.ComponentModel.DataAnnotations;
public class CategoryValidationModel
{
public int? DatabaseId { get; set; }
[Required(ErrorMessage = "Category name is required.")]
public string? Name { get; set; }
public string? Description { get; set; }
[Required(ErrorMessage = "Field separator is required.")]
[StringLength(1, ErrorMessage = "Separator must be a single character.")]
public string FieldSeparator { get; set; } = "-";
[Required(ErrorMessage = "At least one field is needed")]
[Range(1, int.MaxValue, ErrorMessage = "At least one field must be created.")]
public int NumFields { get; set; } = 1;
public List<string> FieldNames { get; set; } = [""];
public List<string> FieldDescriptions { get; set; } = [""];
public IEnumerable<ValidationResult> Validate(ValidationContext context)
{
if ((FieldNames is null || FieldNames.Count == 0) || (FieldDescriptions is null || FieldDescriptions.Count > 0))
{
yield return new ValidationResult(
"Either the FieldNames or FieldDescriptions were null or empty. At least one is required",
new[] { nameof(FieldNames), nameof(FieldDescriptions) }
);
}
}
public static CategoryValidationModel FromArchiveCategory(ArchiveCategory category)
{
return new CategoryValidationModel()
{
Name = category.Name,
Description = category.Description,
DatabaseId = category.Id,
FieldSeparator = category.FieldSeparator,
FieldNames = category.FieldNames,
FieldDescriptions = category.FieldDescriptions,
};
}
public static ArchiveCategory ToArchiveCategory(CategoryValidationModel model)
{
return new ArchiveCategory()
{
Name = model.Name,
FieldSeparator = model.FieldSeparator,
Description = model.Description,
FieldNames = model.FieldNames,
FieldDescriptions = model.FieldDescriptions
};
}
public static void UpdateArchiveValidationModel(CategoryValidationModel model, ArchiveCategory category)
{
category.Name = model.Name ?? throw new ArgumentNullException(nameof(model.Name), "The model name was null.");
category.Description = model.Description;
category.FieldSeparator = model.FieldSeparator;
category.FieldNames = model.FieldNames;
category.FieldDescriptions = model.FieldDescriptions;
}
}

View File

@@ -0,0 +1,86 @@
@page "/categories"
@namespace OpenArchival.Blazor.AdminPages.Shared
@using Microsoft.AspNetCore.Components.Web
@using Microsoft.EntityFrameworkCore
@using Microsoft.Extensions.Logging
@using MudBlazor
@using OpenArchival.DataAccess;
@inject IDialogService DialogService
@inject IArchiveCategoryProvider CategoryProvider;
@inject IDbContextFactory<ApplicationDbContext> DbContextFactory;
@inject ILogger<ViewAddCategoriesComponent> Logger;
<CategoriesListComponent
@ref=_categoriesListComponent
ListItemClickedCallback="ShowFilledDialog"
ShowDeleteButton=true
OnDeleteClickedCallback="DeleteCategory">
<MudButton Variant="Variant.Filled" Color="Color.Primary" OnClick="OnAddClick">Add Category</MudButton>
</CategoriesListComponent>
@code {
CategoriesListComponent _categoriesListComponent = default!;
private async Task DeleteCategory(ArchiveCategory category)
{
// 1. Show a confirmation dialog (recommended)
var confirmed = await DialogService.ShowMessageBox("Confirm", $"Delete {category.Name}?", yesText:"Delete", cancelText:"Cancel");
if (confirmed != true) return;
await CategoryProvider.DeleteCategoryAsync(category);
await _categoriesListComponent.RefreshData();
StateHasChanged();
}
private async Task ShowFilledDialog(ArchiveCategory category)
{
CategoryValidationModel validationModel = CategoryValidationModel.FromArchiveCategory(category);
var parameters = new DialogParameters { ["ValidationModel"] = validationModel, ["IsUpdate"] = true, ["OriginalName"] = category.Name};
var options = new DialogOptions { CloseOnEscapeKey = true, BackdropClick=false};
var dialog = await DialogService.ShowAsync<CategoryCreatorDialog>("Create a Category", parameters, options);
var result = await dialog.Result;
if (result is not null && !result.Canceled && _categoriesListComponent is not null)
{
if (result.Data is null)
{
Logger.LogError($"The new category received by the result had a null data result member.");
throw new NullReferenceException($"The new category received by the result had a null data result member.");
}
CategoryValidationModel model = (CategoryValidationModel)result.Data;
CategoryValidationModel.UpdateArchiveValidationModel(model, category);
await using var context = await DbContextFactory.CreateDbContextAsync();
await context.SaveChangesAsync();
StateHasChanged();
await _categoriesListComponent.RefreshData();
}
}
private async Task OnAddClick()
{
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 && _categoriesListComponent is not null && result.Data is not null)
{
await using var context = await DbContextFactory.CreateDbContextAsync();
CategoryValidationModel model = (CategoryValidationModel)result.Data;
context.ArchiveCategories.Add(CategoryValidationModel.ToArchiveCategory(model));
await context.SaveChangesAsync();
StateHasChanged();
await _categoriesListComponent.RefreshData();
}
}
}

View File

@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
</PropertyGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.IdentityModel.Abstractions" Version="8.14.0" />
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="8.14.0" />
<PackageReference Include="MudBlazor" Version="8.13.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\OpenArchival.Blazor.ArchiveSearch\OpenArchival.Blazor.ArchiveSearch.csproj" />
<ProjectReference Include="..\OpenArchival.Blazor.CustomComponents\OpenArchival.Blazor.CustomComponents.csproj" />
<ProjectReference Include="..\OpenArchival.DataAccess\OpenArchival.DataAccess.csproj" />
</ItemGroup>
</Project>