]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/share/marc-edit/authority-linking-dialog.component.ts
LP1850473: manual and automated eslint fixes
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / share / marc-edit / authority-linking-dialog.component.ts
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';
12
13 /**
14  * MARC Authority Linking Dialog
15  */
16
17 @Component({
18     selector: 'eg-authority-linking-dialog',
19     templateUrl: './authority-linking-dialog.component.html'
20 })
21
22 export class AuthorityLinkingDialogComponent
23     extends DialogComponent implements OnInit {
24
25     @Input() bibField: MarcField;
26     @Input() thesauri: string = null;
27     @Input() controlSet: number = null;
28     @Input() pager: Pager;
29     @Input() context: MarcEditContext;
30
31     browseData: any[] = [];
32
33     // If false, show the raw MARC field data.
34     showAs: 'heading' | 'marc' = 'heading';
35
36     authMeta: any;
37
38     selectedSubfields: string[] = [];
39
40     cni: string; // Control Number Identifier
41
42     @ViewChild('marcEditDialog', {static: false})
43         marcEditDialog: MarcEditorDialogComponent;
44
45     constructor(
46         private modal: NgbModal,
47         private auth: AuthService,
48         private org: OrgService,
49         private pcrud: PcrudService,
50         private net: NetService) {
51         super(modal);
52     }
53
54     ngOnInit() {
55         if (!this.pager) {
56             this.pager = new Pager();
57             this.pager.limit = 5;
58         }
59
60         this.onOpen$.subscribe(_ => this.initData());
61     }
62
63     fieldHash(field?: MarcField): any {
64         if (!field) { field = this.bibField; }
65
66         return {
67             tag: field.tag,
68             ind1: field.ind1,
69             ind2: field.ind2,
70             subfields: field.subfields.map(sf => [sf[0], sf[1]])
71         };
72     }
73
74     initData() {
75
76         this.pager.offset = 0;
77
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';
81         });
82
83         this.pcrud.search('acsbf',
84             {
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
89                 authority_field: {
90                     in: {
91                         select: { acsaf: ['id'] },
92                         from: 'acsaf',
93                         where: {
94                             heading_field: { '!=' : null }
95                         }
96                     }
97                 }
98             },
99             {flesh: 1, flesh_fields: {acsbf: ['authority_field']}},
100             {atomic:  true, anonymous: true}
101
102         ).subscribe(bibMetas => {
103             if (bibMetas.length === 0) { return; }
104
105             let bibMeta;
106             if (this.controlSet) {
107                 bibMeta = bibMetas.filter(b =>
108                     this.controlSet === +b.authority_field().control_set());
109             } else {
110                 bibMeta = bibMetas[0];
111             }
112
113             if (bibMeta) {
114                 this.authMeta = bibMeta.authority_field();
115                 this.bibField.subfields.forEach(sf =>
116                     this.selectedSubfields[sf[0]] =
117                         this.isControlledBibSf(sf[0])
118                 );
119             }
120
121             this.getPage(0);
122         });
123     }
124
125     getPage(direction: number) {
126         this.browseData = [];
127
128         if (direction > 0) {
129             this.pager.offset++;
130         } else if (direction < 0) {
131             this.pager.offset--;
132         } else {
133             this.pager.offset = 0;
134         }
135
136         const hash = this.fieldHash();
137
138         // Only search the selected subfields
139         hash.subfields =
140             hash.subfields.filter(sf => this.selectedSubfields[sf[0]]);
141
142         if (hash.subfields.length === 0) { return; }
143
144         this.net.request(
145             'open-ils.cat',
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));
150     }
151
152     applyHeading(authField: MarcField, authId?: number) {
153         this.net.request(
154             'open-ils.cat',
155             'open-ils.cat.authority.bib_field.overlay_authority',
156             this.fieldHash(), this.fieldHash(authField), this.controlSet
157         ).subscribe(field => {
158             if (authId) {
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);
163             }
164             this.close(field);
165         });
166     }
167
168     isControlledBibSf(sf: string): boolean {
169         return this.authMeta ?
170             this.authMeta.sf_list().includes(sf) : false;
171     }
172
173     setSubfieldZero(authId: number, bibField?: MarcField) {
174
175         if (!bibField) { bibField = this.bibField; }
176
177         const sfZero = bibField.subfields.filter(sf => sf[0] === '0')[0];
178         if (sfZero) {
179             this.context.deleteSubfield(bibField, sfZero);
180         }
181         this.context.insertSubfield(bibField,
182             ['0', `(${this.cni})${authId}`, bibField.subfields.length]);
183
184         // Reset the validation state.
185         bibField.authChecked = null;
186         bibField.authValid = null;
187     }
188
189     createNewAuthority(editFirst?: boolean) {
190
191         const method = editFirst ?
192             'open-ils.cat.authority.record.create_from_bib.readonly' :
193             'open-ils.cat.authority.record.create_from_bib';
194
195         this.net.request(
196             'open-ils.cat', method,
197             this.fieldHash(), this.cni, this.auth.token()
198         ).subscribe(record => {
199             if (editFirst) {
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);
206                         }
207                         this.close();
208                     });
209             } else {
210                 this.setSubfieldZero(record.id());
211                 this.close();
212             }
213         });
214     }
215 }
216
217