restructured viewmodels, better serialization

This commit is contained in:
Simon Martens
2024-11-19 15:20:15 +01:00
parent 84fa6f7fa4
commit 7dc603df2c
26 changed files with 51479 additions and 198 deletions

4
providers/gnd/model.go Normal file
View File

@@ -0,0 +1,4 @@
package gnd
type Person struct {
}

View File

@@ -1,4 +1,4 @@
package providers
package xmlprovider
import (
"encoding/xml"

View File

@@ -1,4 +1,4 @@
package providers
package xmlprovider
import (
"encoding/xml"

View File

@@ -1,4 +1,4 @@
package providers
package xmlprovider
import (
"encoding/xml"
@@ -14,44 +14,19 @@ type Issues struct {
Issues []Issue `xml:"stueck"`
}
func (i *IssueProvider) GetYear(year string) YearViewModel {
res := YearViewModel{Year: year}
last := ""
for _, issue := range i.Items.Issues {
if len(issue.Datum.When) < 4 {
continue
}
date := issue.Datum.When[0:4]
if date != last {
res.PushAvailable(date)
last = date
}
if date == year {
res.PushIssue(issue)
}
}
res.SortAvailableYears()
return res
}
type Issue struct {
XMLName xml.Name `xml:"stueck"`
Number IssueNumber `xml:"nummer"`
Number Nummer `xml:"nummer"`
Datum KGPZDate `xml:"datum"`
Von string `xml:"von"`
Bis string `xml:"bis"`
Von int `xml:"von"`
Bis int `xml:"bis"`
Additionals []Additional `xml:"beilage"`
Identifier
AnnotationNote
}
type IssueNumber struct {
XMLName xml.Name `xml:"nummer"`
Value
type Nummer struct {
No int `xml:",chardata"`
Corrected string `xml:"korrigiert,attr"`
}
@@ -76,8 +51,8 @@ func (i Issues) String() string {
return fmt.Sprintf("Issues: %v", res)
}
func (i *Issue) String() string {
return fmt.Sprintf("ID: %s\nNumber: %v\nDatum: %v\nVon: %s\nBis: %s\nAdditionals: %v\n", i.ID, i.Number, i.Datum, i.Von, i.Bis, i.Additionals)
func (i Issue) String() string {
return fmt.Sprintf("Number: %v, Datum: %v, Von: %d, Bis: %d, Additionals: %v, Identifier: %v, AnnotationNote: %v\n", i.Number, i.Datum, i.Von, i.Bis, i.Additionals, i.Identifier, i.AnnotationNote)
}
func NewIssueProvider(paths []string) *IssueProvider {

View File

@@ -1,4 +1,4 @@
package providers
package xmlprovider
import (
"encoding/xml"
@@ -44,7 +44,7 @@ func (p Pieces) String() string {
return fmt.Sprintf("Pieces: %v", res)
}
func (p *Piece) String() string {
func (p Piece) String() string {
return fmt.Sprintf("ID: %s\nIssueRefs: %v\nPlaceRefs: %v\nCategoryRefs: %v\nAgentRefs: %v\nWorkRefs: %v\nPieceRefs: %v\nAdditionalRef: %v\nIncipit: %v\nTitle: %v\nAnnotations: %v\nNotes: %v\n", p.ID, p.IssueRefs, p.PlaceRefs, p.CategoryRefs, p.AgentRefs, p.WorkRefs, p.PieceRefs, p.AdditionalRef, p.Incipit, p.Title, p.Annotations, p.Notes)
}

View File

@@ -1,4 +1,4 @@
package providers
package xmlprovider
import (
"encoding/xml"

View File

@@ -1,4 +1,4 @@
package providers
package xmlprovider
import (
"encoding/xml"
@@ -15,14 +15,23 @@ type Works struct {
}
type Work struct {
XMLName xml.Name `xml:"werk"`
URLs []URL `xml:"url"`
Citation []string `xml:"zitation"`
Akteur []AgentRef `xml:"akteur"`
XMLName xml.Name `xml:"werk"`
URLs []URL `xml:"url"`
Citation Citation `xml:"zitation"`
PreferredTitle string `xml:"preferred"`
Akteur []AgentRef `xml:"akteur"`
Identifier
AnnotationNote
}
type Citation struct {
XMLName xml.Name `xml:"zitation"`
Title string `xml:"title"`
Year []string `xml:"year"`
Value
Inner
}
func (w Works) Append(data Works) Works {
w.Work = append(w.Work, data.Work...)
return w
@@ -38,7 +47,7 @@ func (w Works) String() string {
}
func (w *Work) String() string {
return fmt.Sprintf("ID: %s\nURLs: %v\nCitation: %v\nAnnotations: %v\nNotes: %v\n", w.ID, w.URLs, w.Citation, w.Annotations, w.Notes)
return fmt.Sprintf("URLs: %v, Citation: %v, PreferredTitle: %s, Akteur: %v, Identifier: %v, AnnotationNote: %v\n", w.URLs, w.Citation, w.PreferredTitle, w.Akteur, w.Identifier, w.AnnotationNote)
}
func NewWorkProvider(paths []string) *WorkProvider {

View File

@@ -0,0 +1,55 @@
package xmlprovider
import "encoding/xml"
type KGPZDate struct {
XMLName xml.Name `xml:"datum"`
When string `xml:"when,attr"`
NotBefore string `xml:"notBefore,attr"`
NotAfter string `xml:"notAfter,attr"`
From string `xml:"from,attr"`
To string `xml:"to,attr"`
Value
}
type URL struct {
XMLName xml.Name `xml:"url"`
Address string `xml:"address,attr"`
Value
}
type AnnotationNote struct {
Annotations []Annotation `xml:"anmerkung"`
Notes []Note `xml:"vermerk"`
}
type Annotation struct {
XMLName xml.Name `xml:"anmerkung"`
Value
Inner
}
type Note struct {
XMLName xml.Name `xml:"vermerk"`
Value
Inner
}
type Identifier struct {
ID string `xml:"id,attr"`
}
type Reference struct {
Ref string `xml:"ref,attr"`
Category string `xml:"kat,attr"`
Unsicher bool `xml:"unsicher,attr"`
Value
}
type Value struct {
Chardata string `xml:",chardata"`
}
type Inner struct {
InnerXML string `xml:",innerxml"`
}

View File

@@ -1,4 +1,4 @@
package providers
package xmlprovider
import (
"encoding/xml"
@@ -50,7 +50,6 @@ func (l *Library) Serialize() {
err := l.Agents.Serialize()
if err != nil {
l.Agents = nil
}
}()
@@ -59,7 +58,6 @@ func (l *Library) Serialize() {
err := l.Places.Serialize()
if err != nil {
l.Places = nil
}
}()
@@ -68,7 +66,6 @@ func (l *Library) Serialize() {
err := l.Works.Serialize()
if err != nil {
l.Works = nil
}
}()
@@ -77,7 +74,6 @@ func (l *Library) Serialize() {
err := l.Categories.Serialize()
if err != nil {
l.Categories = nil
}
}()
@@ -86,7 +82,6 @@ func (l *Library) Serialize() {
err := l.Issues.Serialize()
if err != nil {
l.Issues = nil
}
}()
@@ -95,7 +90,6 @@ func (l *Library) Serialize() {
err := l.Pieces.Serialize()
if err != nil {
l.Pieces = nil
}
}()
@@ -111,7 +105,6 @@ func (p *XMLProvider[T]) Serialize() error {
defer wg.Done()
var data T
if err := UnmarshalFile(path, &data); err != nil {
return
}
p.mu.Lock()
@@ -121,6 +114,7 @@ func (p *XMLProvider[T]) Serialize() error {
}
wg.Wait()
fmt.Println(p.Items)
return nil
}
@@ -133,10 +127,11 @@ func (a *XMLProvider[T]) String() string {
func UnmarshalFile[T any](filename string, data *T) error {
xmlFile, err := os.Open(filename)
if err != nil {
logging.Error(err, "Could not deserialize file: "+filename)
return err
}
defer xmlFile.Close()
logging.Info("Opened " + filename)
logging.Info("Deserialization: " + filename)
byteValue, _ := io.ReadAll(xmlFile)
xml.Unmarshal(byteValue, data)

View File

@@ -1,28 +1,12 @@
package providers
package xmlprovider
import "encoding/xml"
type KGPZDate struct {
XMLName xml.Name `xml:"datum"`
When string `xml:"when,attr"`
NotBefore string `xml:"notBefore,attr"`
NotAfter string `xml:"notAfter,attr"`
From string `xml:"from,attr"`
To string `xml:"to,attr"`
Value
}
type AgentRef struct {
XMLName xml.Name `xml:"akteur"`
Reference
}
type URL struct {
XMLName xml.Name `xml:"url"`
Address string `xml:"address,attr"`
Value
}
type AdditionalRef struct {
XMLName xml.Name `xml:"beilage"`
Reference
@@ -63,23 +47,3 @@ type PieceRef struct {
Page string `xml:"s,attr"`
Reference
}
type AnnotationNote struct {
Annotations []string `xml:"anmerkung"`
Notes []string `xml:"vermerk"`
}
type Identifier struct {
ID string `xml:"id,attr"`
}
type Reference struct {
Ref string `xml:"ref,attr"`
Category string `xml:"kat,attr"`
Unsicher bool `xml:"unsicher,attr"`
Value
}
type Value struct {
Chardata string `xml:",chardata"`
}

View File

@@ -1,84 +0,0 @@
package providers
import (
"slices"
"strconv"
"time"
)
const TLAYOUT = "2006-01-02"
var TRANSLM = map[string][]string{
"January": {"Januar", "Jan"},
"February": {"Februar", "Feb"},
"March": {"März", "Mär"},
"April": {"April", "Apr"},
"May": {"Mai", "Mai"},
"June": {"Juni", "Jun"},
"July": {"Juli", "Jul"},
"August": {"August", "Aug"},
"September": {"September", "Sep"},
"October": {"Oktober", "Okt"},
"November": {"November", "Nov"},
"December": {"Dezember", "Dez"},
}
var TRANSLD = map[string][]string{
"Monday": {"Montag", "Mo"},
"Tuesday": {"Dienstag", "Di"},
"Wednesday": {"Mittwoch", "Mi"},
"Thursday": {"Donnerstag", "Do"},
"Friday": {"Freitag", "Fr"},
"Saturday": {"Samstag", "Sa"},
"Sunday": {"Sonntag", "So"},
}
type IssueViewModel struct {
Issue
Weekday []string
Day int
Month []string
}
func NewIssueView(i Issue) (IssueViewModel, error) {
t, err := time.Parse(TLAYOUT, i.Datum.When)
if err != nil {
return IssueViewModel{}, err
}
return IssueViewModel{
Issue: i,
Weekday: append(TRANSLD[t.Weekday().String()], t.Weekday().String()),
Day: t.Day(),
Month: append(TRANSLM[t.Month().String()], i.Datum.When[5:7]),
}, nil
}
type YearViewModel struct {
Year string
AvailableYears []int
Issues []IssueViewModel
}
func (y *YearViewModel) PushIssue(i Issue) {
iv, err := NewIssueView(i)
if err != nil {
return
}
y.Issues = append(y.Issues, iv)
}
func (y *YearViewModel) PushAvailable(s string) {
i, err := strconv.Atoi(s)
if err != nil {
return
}
if !slices.Contains(y.AvailableYears, i) {
y.AvailableYears = append(y.AvailableYears, i)
}
}
func (y *YearViewModel) SortAvailableYears() {
slices.Sort(y.AvailableYears)
}