1+ import { CloudAppRestService , HttpMethod } from "@exlibris/exl-cloudapp-angular-lib" ;
2+ import { Observable } from "rxjs" ;
3+ import { map } from "rxjs/operators" ;
4+
5+ export interface Bib {
6+ link : string ,
7+ mms_id : string ;
8+ title : string ;
9+ record_format : string ;
10+ anies : any ;
11+ }
12+
13+ export class BibUtils {
14+ private _restService : CloudAppRestService ;
15+
16+ constructor ( restService : CloudAppRestService ) {
17+ this . _restService = restService ;
18+ }
19+
20+ /** Retrieve a single BIB record */
21+ getBib ( mmsId : string ) : Observable < Bib > {
22+ return this . _restService . call ( `/bibs/${ mmsId } ` ) ;
23+ }
24+
25+ /** Update a BIB record with the specified MARCXML */
26+ updateBib ( bib : Bib ) : Observable < Bib > {
27+ return this . _restService . call ( {
28+ url : `/bibs/${ bib . mms_id } ` ,
29+ headers : {
30+ "Content-Type" : "application/xml" ,
31+ Accept : "application/json" } ,
32+ requestBody : `<bib>${ bib . anies } </bib>` ,
33+ method : HttpMethod . PUT
34+ } ) ;
35+ }
36+
37+ /** Adds a 500 note field to a MARC21 Bibliographic Record */
38+ addNoteToBib ( bib : Bib ) {
39+ const doc = new DOMParser ( ) . parseFromString ( bib . anies , "application/xml" ) ;
40+ const datafield = dom ( "datafield" , {
41+ parent : doc . documentElement ,
42+ attributes : [ [ "tag" , "500" ] , [ "ind1" , " " ] , [ "ind2" , " " ] ]
43+ } ) ;
44+ dom ( "subfield" , {
45+ parent : datafield ,
46+ text : `Record processed at ${ ( new Date ( ) ) . toLocaleString ( ) } ` ,
47+ attributes : [ [ "code" , "a" ] ]
48+ } ) ;
49+ bib . anies = new XMLSerializer ( ) . serializeToString ( doc . documentElement ) ;
50+ return bib ;
51+ }
52+ }
53+
54+ /** Adds Element to dom and returns it */
55+ const dom = ( name : string , options : { parent ?: Element | Node , text ?:
56+ string , className ?: string , id ?: string , attributes ?: string [ ] [ ] } = { }
57+ ) : Element => {
58+
59+ let ns = options . parent ? options . parent . namespaceURI : '' ;
60+ let element = document . createElementNS ( ns , name ) ;
61+
62+ if ( options . parent ) options . parent . appendChild ( element ) ;
63+ if ( options . text ) element . innerHTML = options . text ;
64+ if ( options . className ) element . className = options . className ;
65+ if ( options . id ) element . id = options . id ;
66+ if ( options . attributes ) options . attributes . forEach ( ( [ att , val ] ) => element . setAttribute ( att , val ) ) ;
67+
68+ return element ;
69+ }
0 commit comments