mirror of
https://github.com/Theodor-Springmann-Stiftung/musenalm.git
synced 2025-10-29 09:15:33 +00:00
Relationen, fehlt -PreferredTitle für Entries -Überflüssige Felder in Contents
This commit is contained in:
144
migrations/seed/items.go
Normal file
144
migrations/seed/items.go
Normal file
@@ -0,0 +1,144 @@
|
||||
package seed
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/Theodor-Springmann-Stiftung/musenalm/dbmodels"
|
||||
"github.com/Theodor-Springmann-Stiftung/musenalm/xmlmodels"
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
)
|
||||
|
||||
func ItemsFromBändeAndBIBLIO(
|
||||
app core.App,
|
||||
entries xmlmodels.Bände,
|
||||
biblio map[int]xmlmodels.BIBLIOEintrag,
|
||||
) ([]*core.Record, error) {
|
||||
collection, err := app.FindCollectionByNameOrId(dbmodels.ITEMS_TABLE)
|
||||
records := make([]*core.Record, 0, len(entries.Bände))
|
||||
r := regexp.MustCompile("\\d{6}")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return records, err
|
||||
}
|
||||
|
||||
for i := 0; i < len(entries.Bände); i++ {
|
||||
band := entries.Bände[i]
|
||||
banddb, err := app.FindFirstRecordByData(dbmodels.ENTRIES_TABLE, dbmodels.MUSENALMID_FIELD, band.ID)
|
||||
if err != nil {
|
||||
app.Logger().Error("Error finding record", "error", err, "collection", dbmodels.ENTRIES_TABLE, "field", dbmodels.MUSENALMID_FIELD, "value", band.ID)
|
||||
continue
|
||||
}
|
||||
|
||||
nst := NormalizeString(band.Norm)
|
||||
matches := r.FindAllStringSubmatchIndex(nst, -1)
|
||||
t := map[string]string{}
|
||||
for i, m := range matches {
|
||||
nr := nst[m[0]:m[1]]
|
||||
end := len(nst)
|
||||
|
||||
if m[1] >= len(nst) {
|
||||
t[nr] = ""
|
||||
continue
|
||||
}
|
||||
|
||||
if len(matches)-1 > i {
|
||||
end = matches[i+1][0]
|
||||
}
|
||||
|
||||
rest := nst[m[1]:end]
|
||||
var last []rune
|
||||
|
||||
for y, c := range rest {
|
||||
if c == '\\' && y < len(rest)-1 && rest[y+1] == ')' {
|
||||
break
|
||||
}
|
||||
if c != '(' && c != ')' {
|
||||
last = append(last, c)
|
||||
}
|
||||
}
|
||||
|
||||
if last != nil && len(last) > 0 {
|
||||
t[nr] = string(last)
|
||||
}
|
||||
}
|
||||
|
||||
var exemlist []dbmodels.Exemplar
|
||||
|
||||
if band.BiblioID != 0 {
|
||||
exem := dbmodels.Exemplar{Identifier: strconv.Itoa(band.BiblioID)}
|
||||
if e, ok := biblio[band.BiblioID]; ok {
|
||||
exem.Location = strings.TrimSpace(e.Standort)
|
||||
exem.Condition = strings.TrimSpace(e.Zustand)
|
||||
message := ""
|
||||
message = appendMessage(e.NotizÄusseres, message)
|
||||
message = appendMessage(e.NotizInhalt, message)
|
||||
message = appendMessage(e.Anmerkungen, message)
|
||||
exem.Annotation = message
|
||||
}
|
||||
|
||||
exemlist = append(exemlist, exem)
|
||||
}
|
||||
|
||||
for nr, m := range t {
|
||||
exem := dbmodels.Exemplar{Identifier: nr}
|
||||
|
||||
no, err := strconv.Atoi(strings.TrimSpace(nr))
|
||||
message := strings.TrimSpace(m)
|
||||
if err != nil {
|
||||
if e, ok := biblio[no]; ok {
|
||||
exem.Location = strings.TrimSpace(e.Standort)
|
||||
exem.Condition = strings.TrimSpace(e.Zustand)
|
||||
message = appendMessage(e.NotizÄusseres, message)
|
||||
message = appendMessage(e.NotizInhalt, message)
|
||||
message = appendMessage(e.Anmerkungen, message)
|
||||
}
|
||||
}
|
||||
exem.Annotation = message
|
||||
|
||||
if exem.Identifier != "" {
|
||||
exemlist = append(exemlist, exem)
|
||||
}
|
||||
}
|
||||
|
||||
if len(exemlist) > 0 {
|
||||
for _, exem := range exemlist {
|
||||
record := core.NewRecord(collection)
|
||||
record.Set(dbmodels.ENTRIES_TABLE, banddb.Id)
|
||||
record.Set(dbmodels.ITEMS_IDENTIFIER_FIELD, exem.Identifier)
|
||||
record.Set(dbmodels.ITEMS_LOCATION_FIELD, exem.Location)
|
||||
record.Set(dbmodels.ITEMS_OWNER_FIELD, "Theodor Springmann Stiftung")
|
||||
record.Set(dbmodels.ITEMS_CONDITION_FIELD, exem.Condition)
|
||||
|
||||
if slices.Contains(band.Status.Value, "Original vorhanden") {
|
||||
record.Set(dbmodels.ITEMS_MEDIA_FIELD, dbmodels.ITEM_TYPE_VALUES[0])
|
||||
}
|
||||
|
||||
if slices.Contains(band.Status.Value, "Reprint vorhanden") {
|
||||
med := record.GetStringSlice(dbmodels.ITEMS_MEDIA_FIELD)
|
||||
record.Set(dbmodels.ITEMS_MEDIA_FIELD, append(med, dbmodels.ITEM_TYPE_VALUES[1]))
|
||||
}
|
||||
|
||||
record.Set(dbmodels.ANNOTATION_FIELD, exem.Annotation)
|
||||
|
||||
records = append(records, record)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return records, nil
|
||||
}
|
||||
|
||||
func appendMessage(message string, toAppend string) string {
|
||||
notiza := strings.TrimSpace(toAppend)
|
||||
if notiza != "" {
|
||||
if message != "" {
|
||||
message += "\n"
|
||||
}
|
||||
message += notiza
|
||||
}
|
||||
return message
|
||||
}
|
||||
Reference in New Issue
Block a user