1 import {Component, ViewChild, Input, OnInit} from '@angular/core';
2 import {NetService} from '@eg/core/net.service';
3 import {OrgService} from '@eg/core/org.service';
4 import {AuthService} from '@eg/core/auth.service';
5 import {PcrudService} from '@eg/core/pcrud.service';
6 import {DialogComponent} from '@eg/share/dialog/dialog.component';
7 import {NgbModal} from '@ng-bootstrap/ng-bootstrap';
8 import {MarcField} from './marcrecord';
9 import {MarcEditContext} from './editor-context';
10 import {Pager} from '@eg/share/util/pager';
11 import {MarcEditorDialogComponent} from './editor-dialog.component';
14 * MARC Authority Linking Dialog
18 selector: 'eg-authority-linking-dialog',
19 templateUrl: './authority-linking-dialog.component.html'
22 export class AuthorityLinkingDialogComponent
23 extends DialogComponent implements OnInit {
25 @Input() bibField: MarcField;
26 @Input() thesauri: string = null;
27 @Input() controlSet: number = null;
28 @Input() pager: Pager;
29 @Input() context: MarcEditContext;
31 browseData: any[] = [];
33 // If false, show the raw MARC field data.
34 showAs: 'heading' | 'marc' = 'heading';
38 selectedSubfields: string[] = [];
40 cni: string; // Control Number Identifier
42 @ViewChild('marcEditDialog', {static: false})
43 marcEditDialog: MarcEditorDialogComponent;
46 private modal: NgbModal,
47 private auth: AuthService,
48 private org: OrgService,
49 private pcrud: PcrudService,
50 private net: NetService) {
56 this.pager = new Pager();
60 this.onOpen$.subscribe(_ => this.initData());
63 fieldHash(field?: MarcField): any {
64 if (!field) { field = this.bibField; }
70 subfields: field.subfields.map(sf => [sf[0], sf[1]])
76 this.pager.offset = 0;
78 this.org.settings(['cat.marc_control_number_identifier']).then(s => {
79 this.cni = s['cat.marc_control_number_identifier'] ||
80 'Set cat.marc_control_number_identifier in Library Settings';
83 this.pcrud.search('acsbf',
85 tag: this.bibField.tag,
86 // we're only interested in the authority fields
87 // that are linked to a heading field; i.e., we're not
88 // interested in subdivision authorities at this time
91 select: { acsaf: ['id'] },
94 heading_field: { '!=' : null }
99 {flesh: 1, flesh_fields: {acsbf: ['authority_field']}},
100 {atomic: true, anonymous: true}
102 ).subscribe(bibMetas => {
103 if (bibMetas.length === 0) { return; }
106 if (this.controlSet) {
107 bibMeta = bibMetas.filter(b =>
108 this.controlSet === +b.authority_field().control_set());
110 bibMeta = bibMetas[0];
114 this.authMeta = bibMeta.authority_field();
115 this.bibField.subfields.forEach(sf =>
116 this.selectedSubfields[sf[0]] =
117 this.isControlledBibSf(sf[0])
125 getPage(direction: number) {
126 this.browseData = [];
130 } else if (direction < 0) {
133 this.pager.offset = 0;
136 const hash = this.fieldHash();
138 // Only search the selected subfields
140 hash.subfields.filter(sf => this.selectedSubfields[sf[0]]);
142 if (hash.subfields.length === 0) { return; }
146 'open-ils.cat.authority.bib_field.linking_browse',
147 hash, this.pager.limit,
148 this.pager.offset, this.thesauri
149 ).subscribe(entry => this.browseData.push(entry));
152 applyHeading(authField: MarcField, authId?: number) {
155 'open-ils.cat.authority.bib_field.overlay_authority',
156 this.fieldHash(), this.fieldHash(authField), this.controlSet
157 ).subscribe(field => {
159 // If an authId is provided, it means we are using
160 // a main entry heading and we should set the bib
161 // field's subfield 0 to refer to the main entry record.
162 this.setSubfieldZero(authId, field);
168 isControlledBibSf(sf: string): boolean {
169 return this.authMeta ?
170 this.authMeta.sf_list().includes(sf) : false;
173 setSubfieldZero(authId: number, bibField?: MarcField) {
175 if (!bibField) { bibField = this.bibField; }
177 const sfZero = bibField.subfields.filter(sf => sf[0] === '0')[0];
179 this.context.deleteSubfield(bibField, sfZero);
181 this.context.insertSubfield(bibField,
182 ['0', `(${this.cni})${authId}`, bibField.subfields.length]);
184 // Reset the validation state.
185 bibField.authChecked = null;
186 bibField.authValid = null;
189 createNewAuthority(editFirst?: boolean) {
191 const method = editFirst ?
192 'open-ils.cat.authority.record.create_from_bib.readonly' :
193 'open-ils.cat.authority.record.create_from_bib';
196 'open-ils.cat', method,
197 this.fieldHash(), this.cni, this.auth.token()
198 ).subscribe(record => {
200 this.marcEditDialog.recordXml = record;
201 this.marcEditDialog.open({size: 'xl'})
202 // eslint-disable-next-line rxjs/no-nested-subscribe
203 .subscribe(saveEvent => {
204 if (saveEvent && saveEvent.recordId) {
205 this.setSubfieldZero(saveEvent.recordId);
210 this.setSubfieldZero(record.id());