From 1b54e0aef17634f0b2e75504ebf0b14494bc7485 Mon Sep 17 00:00:00 2001 From: Simon Martens Date: Sun, 9 Feb 2025 14:05:27 +0100 Subject: [PATCH] Preferred title in entries & unused fields in contents --- dbmodels/deprecated.go | 2 + helpers/datatypes/map.go | 10 +++++ migrations/1738942133_partials.go | 14 ------- migrations/1739007272_insert_data.go | 2 +- migrations/seed/entries.go | 56 ++++++++++++++++++++++++---- 5 files changed, 62 insertions(+), 22 deletions(-) diff --git a/dbmodels/deprecated.go b/dbmodels/deprecated.go index 307faca..d2145d9 100644 --- a/dbmodels/deprecated.go +++ b/dbmodels/deprecated.go @@ -5,4 +5,6 @@ type Deprecated struct { Norm string `json:"norm"` BiblioID int `json:"biblio"` Status []string `json:"status"` + Gesichtet bool `json:"gesichtet"` + Erfasst bool `json:"erfasst"` } diff --git a/helpers/datatypes/map.go b/helpers/datatypes/map.go index b41fde9..5584642 100644 --- a/helpers/datatypes/map.go +++ b/helpers/datatypes/map.go @@ -1,5 +1,6 @@ package datatypes +// INFO: use this if your key is unique func MakeMap[T any, U comparable](data []T, f func(T) U) map[U]T { m := make(map[U]T) for _, v := range data { @@ -7,3 +8,12 @@ func MakeMap[T any, U comparable](data []T, f func(T) U) map[U]T { } return m } + +// INFO: use this if your key is not unique +func MakeMultiMap[T any, U comparable](data []T, f func(T) U) map[U][]T { + m := make(map[U][]T) + for _, v := range data { + m[f(v)] = append(m[f(v)], v) + } + return m +} diff --git a/migrations/1738942133_partials.go b/migrations/1738942133_partials.go index 51f0842..fa8c0bf 100644 --- a/migrations/1738942133_partials.go +++ b/migrations/1738942133_partials.go @@ -61,7 +61,6 @@ func partialsFields(app core.App) *core.FieldsList { // Other discerning Information &core.NumberField{Name: dbmodels.YEAR_FIELD, Required: false}, - &core.TextField{Name: dbmodels.EDITION_FIELD, Required: false}, // Media Information &core.SelectField{ @@ -80,18 +79,6 @@ func partialsFields(app core.App) *core.FieldsList { // Physical Description &core.TextField{Name: dbmodels.EXTENT_FIELD, Required: false}, &core.TextField{Name: dbmodels.DIMENSIONS_FIELD, Required: false}, - &core.SelectField{ - Name: dbmodels.MEDIA_TYPE_FIELD, - Required: false, - Values: dbmodels.MEDIA_TYPE_VALUES, - MaxSelect: len(dbmodels.MEDIA_TYPE_VALUES), - }, - &core.SelectField{ - Name: dbmodels.CARRIER_TYPE_FIELD, - Required: false, - Values: dbmodels.CARRIER_TYPE_VALUES, - MaxSelect: len(dbmodels.CARRIER_TYPE_VALUES), - }, // Musenalm specific data &core.SelectField{ @@ -146,5 +133,4 @@ func partialsIndexes(collection *core.Collection) { addIndex(collection, dbmodels.PLACE_STMT_FIELD, false) addIndex(collection, dbmodels.PUBLICATION_STMT_FIELD, false) addIndex(collection, dbmodels.YEAR_FIELD, false) - addIndex(collection, dbmodels.EDITION_FIELD, false) } diff --git a/migrations/1739007272_insert_data.go b/migrations/1739007272_insert_data.go index 7926fe8..1129b8b 100644 --- a/migrations/1739007272_insert_data.go +++ b/migrations/1739007272_insert_data.go @@ -56,7 +56,7 @@ func init() { wg.Wait() - if records, err := seed.RecordsFromBände(app, adb.Bände, adb.Orte); err == nil { + if records, err := seed.RecordsFromBände(app, *adb); err == nil { if err = seed.BatchSave(app, records); err != nil { panic(err) } diff --git a/migrations/seed/entries.go b/migrations/seed/entries.go index 368f62b..72c924f 100644 --- a/migrations/seed/entries.go +++ b/migrations/seed/entries.go @@ -2,6 +2,8 @@ package seed import ( "fmt" + "slices" + "strconv" "strings" "github.com/Theodor-Springmann-Stiftung/musenalm/dbmodels" @@ -12,32 +14,35 @@ import ( func RecordsFromBände( app core.App, - entries xmlmodels.Bände, - orte xmlmodels.Orte, + adb xmlmodels.AccessDB, ) ([]*core.Record, error) { collection, err := app.FindCollectionByNameOrId(dbmodels.ENTRIES_TABLE) - records := make([]*core.Record, 0, len(entries.Bände)) + records := make([]*core.Record, 0, len(adb.Bände.Bände)) if err != nil { fmt.Println(err) return records, err } - omap := datatypes.MakeMap(orte.Orte, func(o xmlmodels.Ort) string { return o.ID }) + omap := datatypes.MakeMap(adb.Orte.Orte, func(o xmlmodels.Ort) string { return o.ID }) + relmap := datatypes.MakeMultiMap( + adb.Relationen_Bände_Reihen.Relationen, + func(r xmlmodels.Relation_Band_Reihe) string { return r.Band }, + ) + rmap := datatypes.MakeMap(adb.Reihen.Reihen, func(r xmlmodels.Reihe) string { return r.ID }) ocoll, err := app.FindCollectionByNameOrId(dbmodels.PLACES_TABLE) if err != nil { app.Logger().Error("Error finding collection", "error", err, "collection", dbmodels.PLACES_TABLE) return records, err } - for i := 0; i < len(entries.Bände); i++ { - band := entries.Bände[i] + for i := 0; i < len(adb.Bände.Bände); i++ { + band := adb.Bände.Bände[i] record := core.NewRecord(collection) // TODO: Hier bevorzugter reihentitel + jahr, oder irgendein reihentitel, oder reihentitelALT if band.ReihentitelALT == "" { continue } - record.Set(dbmodels.PREFERRED_TITLE_FIELD, NormalizeString(band.ReihentitelALT)) record.Set(dbmodels.TITLE_STMT_FIELD, NormalizeString(band.Titelangabe)) record.Set(dbmodels.REFERENCES_FIELD, NormalizeString(band.Nachweis)) record.Set(dbmodels.ANNOTATION_FIELD, NormalizeString(band.Anmerkungen)) @@ -64,6 +69,7 @@ func RecordsFromBände( record.Set(dbmodels.EDITSTATE_FIELD, dbmodels.EDITORSTATE_VALUES[0]) } + handlePreferredTitleEntry(record, band, rmap, relmap) handleDeprecated(record, band) handleOrte(record, band, omap, app, ocoll) @@ -73,6 +79,40 @@ func RecordsFromBände( return records, nil } +func handlePreferredTitleEntry( + record *core.Record, + band xmlmodels.Band, + rmap map[string]xmlmodels.Reihe, + rrelmap map[string][]xmlmodels.Relation_Band_Reihe, +) { + rels := rrelmap[band.ID] + if len(rels) == 0 { + record.Set(dbmodels.PREFERRED_TITLE_FIELD, NormalizeString(band.ReihentitelALT)) + record.Set(dbmodels.EDITSTATE_FIELD, dbmodels.EDITORSTATE_VALUES[len(dbmodels.EDITORSTATE_VALUES)-2]) + return + } + + jahr := strconv.Itoa(band.Jahr) + if band.Jahr == 0 { + jahr = "[o. J.]" + } else { + jahr = " (" + jahr + ")" + } + + bevti := slices.IndexFunc(rels, func(r xmlmodels.Relation_Band_Reihe) bool { return r.Relation == "1" }) + if bevti != -1 { + bevt := rmap[rels[bevti].Reihe] + record.Set(dbmodels.PREFERRED_TITLE_FIELD, NormalizeString(bevt.Titel)+jahr) + return + } + + slices.SortFunc(rels, func(a, b xmlmodels.Relation_Band_Reihe) int { + return strings.Compare(a.Relation, b.Relation) + }) + + record.Set(dbmodels.PREFERRED_TITLE_FIELD, NormalizeString(rmap[rels[0].Reihe].Titel)+jahr) +} + func handleOrte( record *core.Record, band xmlmodels.Band, @@ -126,6 +166,8 @@ func handleDeprecated(record *core.Record, band xmlmodels.Band) { Norm: NormalizeString(band.Norm), BiblioID: band.BiblioID, Status: band.Status.Value, + Gesichtet: band.Gesichtet, + Erfasst: band.Erfasst, } record.Set(dbmodels.MUSENALM_DEPRECATED_FIELD, depr)