-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsirsi.go
277 lines (248 loc) · 7.56 KB
/
sirsi.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package main
import (
"encoding/json"
"encoding/xml"
"fmt"
"log"
"net/http"
"regexp"
"strings"
"github.com/gin-gonic/gin"
)
type subField struct {
XMLName xml.Name `xml:"subfield"`
Code string `xml:"code,attr"`
Value string `xml:",chardata"`
}
type dataField struct {
XMLName xml.Name `xml:"datafield"`
Tag string `xml:"tag,attr"`
Subfields []subField `xml:"subfield"`
Value string `xml:",chardata"`
}
type controlField struct {
XMLName xml.Name `xml:"controlfield"`
Tag string `xml:"tag,attr"`
Value string `xml:",chardata"`
}
type marcRecord struct {
XMLName xml.Name `xml:"record"`
Leader string `xml:"leader"`
ControlFields []controlField `xml:"controlfield"`
DataFields []dataField `xml:"datafield"`
}
type marcMetadata struct {
XMLName xml.Name `xml:"collection"`
Record marcRecord
}
type sirsiResponse struct {
CatalogKey string `json:"catalogKey"`
Barcode string `json:"barcode"`
CallNumber string `json:"callNumber"`
Title string `json:"title"`
CreatorName string `json:"creatorName"`
CreatorType string `json:"creatorType"`
Year string `json:"year"`
PublicationPlace string `json:"publicationPlace"`
Location string `json:"location"`
CollectionID string `json:"collectionID"`
UseRightName string `json:"useRightName"`
UseRightURI string `json:"useRighURI"`
UseRightStatement string `json:"useRightStatement"`
}
func (svc *serviceContext) lookupSirsiMetadata(c *gin.Context) {
barcode := strings.TrimSpace(strings.ToUpper(c.Query("barcode")))
catKey := strings.TrimSpace(strings.ToLower(c.Query("ckey")))
if barcode == "" && catKey == "" {
log.Printf("ERROR: sirsi lookup requires barcode or catkey")
c.String(http.StatusBadRequest, "barcode or ckey required")
return
}
resp, err := svc.doSirsiLookup(catKey, barcode)
if err != nil {
return
}
out := struct {
*sirsiResponse
ExistingPID string `json:"existingPID"`
ExistingID int64 `json:"existingID"`
Exists bool `json:"exists"`
}{
sirsiResponse: resp,
}
var existMD metadata
err = svc.DB.Where("barcode=? and catalog_key=?", resp.Barcode, resp.CatalogKey).Limit(1).Find(&existMD).Error
if err != nil {
log.Printf("ERROR: failed check for existing metadata with barcode %s: %s", resp.Barcode, err.Error())
} else {
if existMD.ID > 0 {
log.Printf("INFO: metadata with barcode [%s] catkey [%s] already exists with id [%d]", barcode, catKey, existMD.ID)
out.Exists = true
out.ExistingID = existMD.ID
out.ExistingPID = existMD.PID
}
}
c.JSON(http.StatusOK, out)
}
type solrDocument struct {
FullRecord string `json:"fullrecord"`
}
type solrResponseHeader struct {
Status int `json:"status,omitempty"`
}
type solrResponseDocuments struct {
NumFound int `json:"numFound,omitempty"`
Start int `json:"start,omitempty"`
Docs []solrDocument `json:"docs,omitempty"`
}
type solrResponse struct {
Header solrResponseHeader `json:"responseHeader,omitempty"`
Response solrResponseDocuments `json:"response,omitempty"`
}
func (svc *serviceContext) doSirsiLookup(catKey, barcode string) (*sirsiResponse, error) {
// prefer catkey over barcode
url := fmt.Sprintf("%s/select?fl=fullrecord&q=barcode_a:%s", svc.ExternalSystems.Solr, barcode)
if catKey != "" {
url = fmt.Sprintf("%s/select?fl=fullrecord&q=id:%s", svc.ExternalSystems.Solr, catKey)
}
respStr, err := svc.getRequest(url)
if err != nil {
return nil, fmt.Errorf("getMarc from solr failed %d: %s", err.StatusCode, err.Message)
}
var solr solrResponse
jErr := json.Unmarshal(respStr, &solr)
if jErr != nil {
return nil, jErr
}
rawMarc := []byte(solr.Response.Docs[0].FullRecord)
var parsed marcMetadata
parseErr := xml.Unmarshal(rawMarc, &parsed)
if parseErr != nil {
return nil, parseErr
}
if len(parsed.Record.ControlFields) == 0 && len(parsed.Record.DataFields) == 0 {
return nil, fmt.Errorf("no matches found in sirsi")
}
log.Printf("INFO: extract fields from raw marc response")
resp := sirsiResponse{CatalogKey: catKey}
// catkey is in 001 of control fields. find it first
for _, cf := range parsed.Record.ControlFields {
if cf.Tag == "001" {
resp.CatalogKey = cf.Value
break
}
}
type bcHashData struct {
Barcode string
CallNumber string
Location string
}
// the remaining data ins in the datafields
titleRegex := regexp.MustCompile(`\s*\/$`) // strip trailing /
pubRegex := regexp.MustCompile(`(?:^\[|\]$|\.$|\]\.$|:$)`) // strip [] and trailing . or :
for _, df := range parsed.Record.DataFields {
if df.Tag == "100" {
for _, sf := range df.Subfields {
if sf.Code == "a" {
resp.CreatorName = strings.TrimSpace(sf.Value)
break
}
}
resp.CreatorType = "personal"
}
if (df.Tag == "110" || df.Tag == "111") && resp.CreatorType == "" {
for _, sf := range df.Subfields {
if sf.Code == "a" {
resp.CreatorName = strings.TrimSpace(sf.Value)
}
if sf.Code == "b" {
sub := strings.TrimSpace(sf.Value)
resp.CreatorName += fmt.Sprintf(" %s", sub)
}
}
resp.CreatorType = "corporate"
}
if df.Tag == "245" {
// Title; main is in 245a, subtitle in 245b
for _, sf := range df.Subfields {
if sf.Code == "a" {
trimmedTitle := strings.TrimSpace(sf.Value)
resp.Title = titleRegex.ReplaceAllString(trimmedTitle, "")
}
}
}
if df.Tag == "260" {
// publication info, a=place, c = year
for _, sf := range df.Subfields {
if sf.Code == "a" {
resp.PublicationPlace = strings.TrimSpace(sf.Value)
resp.PublicationPlace = strings.TrimSpace(pubRegex.ReplaceAllString(resp.PublicationPlace, ""))
}
if sf.Code == "c" && resp.Year == "" {
resp.Year = strings.TrimSpace(sf.Value)
resp.Year = strings.TrimSpace(pubRegex.ReplaceAllString(resp.Year, ""))
}
}
}
if df.Tag == "852" {
// 852c is collectionID
for _, sf := range df.Subfields {
if sf.Code == "c" {
resp.CollectionID = strings.TrimSpace(sf.Value)
}
}
}
if df.Tag == "856" {
// use rights are held in 856 r (uri), t (name/statement), u (item uri)
for _, sf := range df.Subfields {
if sf.Code == "t" {
if resp.UseRightName == "" {
resp.UseRightName = strings.TrimSpace(sf.Value)
} else {
resp.UseRightStatement = strings.TrimSpace(sf.Value)
}
}
if sf.Code == "r" {
resp.UseRightURI = strings.TrimSpace(sf.Value)
}
}
}
if df.Tag == "999" {
// 999 repeats, 1 per barcode. Match the queried barcode or just pick first
// subfields: i=barcode,l=location, a=call number
bcData := bcHashData{}
for _, sf := range df.Subfields {
if sf.Code == "a" {
bcData.CallNumber = strings.TrimSpace(sf.Value)
}
if sf.Code == "i" {
bcData.Barcode = strings.ToUpper(strings.TrimSpace(sf.Value))
}
if sf.Code == "l" {
bcData.Location = strings.TrimSpace(sf.Value)
}
}
if bcData.Barcode == barcode || barcode == "" && resp.Barcode == "" {
resp.Barcode = bcData.Barcode
resp.CallNumber = bcData.CallNumber
resp.Location = bcData.Location
}
}
}
if resp.CollectionID == "" {
resp.CollectionID = resp.CallNumber
}
if resp.UseRightName == "" {
log.Printf("INFO: no use right data found in sirsi response; default to CNE")
var cne useRight
dbErr := svc.DB.First(&cne, 1).Error
if dbErr != nil {
log.Printf("ERROR: unable to load CNE data: %s", dbErr.Error())
} else {
resp.UseRightName = cne.Name
resp.UseRightURI = cne.URI
resp.UseRightStatement = cne.Statement
}
}
return &resp, nil
}