]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/share/marc-edit/editor.component.ts
cea199052ae04d99141f174416d1ed5b6f2d6575
[working/Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / share / marc-edit / editor.component.ts
1 import {Component, Input, Output, OnInit, EventEmitter, ViewChild} from '@angular/core';
2 import {IdlService} from '@eg/core/idl.service';
3 import {EventService} from '@eg/core/event.service';
4 import {NetService} from '@eg/core/net.service';
5 import {AuthService} from '@eg/core/auth.service';
6 import {OrgService} from '@eg/core/org.service';
7 import {PcrudService} from '@eg/core/pcrud.service';
8 import {MarcRecord} from './marcrecord';
9 import {ComboboxEntry, ComboboxComponent
10   } from '@eg/share/combobox/combobox.component';
11 import {ConfirmDialogComponent} from '@eg/share/dialog/confirm.component';
12
13
14 /**
15  * MARC Record editor main interface.
16  */
17
18 @Component({
19   selector: 'eg-marc-editor',
20   templateUrl: './editor.component.html'
21 })
22
23 export class MarcEditorComponent implements OnInit {
24
25     record: MarcRecord;
26     editorTab: 'rich' | 'flat';
27     sources: ComboboxEntry[];
28
29     @Input() set recordId(id: number) {
30         if (!id) { return; }
31         if (this.record && this.record.id === id) { return; }
32         this.fromId(id);
33     }
34
35     @Input() set recordXml(xml: string) {
36         if (xml) { this.fromXml(xml); }
37     }
38
39     // If true, saving records to the database is assumed to
40     // happen externally.  IOW, the record editor is just an
41     // in-place MARC modification interface.
42     inPlaceMode: boolean;
43
44     // In inPlaceMode, this is emitted in lieu of saving the record
45     // in th database.  When inPlaceMode is false, this is emitted after
46     // the record is successfully saved.
47     @Output() recordSaved: EventEmitter<string>;
48
49     @ViewChild('sourceSelector') sourceSelector: ComboboxComponent;
50     @ViewChild('confirmDelete') confirmDelete: ConfirmDialogComponent;
51     @ViewChild('confirmUndelete') confirmUndelete: ConfirmDialogComponent;
52     @ViewChild('cannotDelete') cannotDelete: ConfirmDialogComponent;
53
54     constructor(
55         private evt: EventService,
56         private idl: IdlService,
57         private net: NetService,
58         private auth: AuthService,
59         private org: OrgService,
60         private pcrud: PcrudService
61     ) {
62         this.sources = [];
63         this.recordSaved = new EventEmitter<string>();
64     }
65
66     ngOnInit() {
67         // Default to flat for now since it's all that's supported.
68         this.editorTab = 'flat';
69
70         this.pcrud.retrieveAll('cbs').subscribe(
71             src => this.sources.push({id: +src.id(), label: src.source()}),
72             _ => {},
73             () => {
74                 this.sources = this.sources.sort((a, b) =>
75                     a.label.toLowerCase() < b.label.toLowerCase() ? -1 : 1
76                 );
77             }
78         );
79     }
80
81     saveRecord(): Promise<any> {
82         const xml = this.record.toXml();
83
84         if (this.inPlaceMode) {
85             // Let the caller have the modified XML and move on.
86             this.recordSaved.emit(xml);
87             return Promise.resolve();
88         }
89
90         const source = this.sourceSelector.selected ?
91             this.sourceSelector.selected.label : null; // 'label' not a typo
92
93         if (this.record.id) { // Editing an existing record
94
95             const method = 'open-ils.cat.biblio.record.marc.replace';
96
97             return this.net.request('open-ils.cat', method,
98                 this.auth.token(), this.record.id, xml, source
99             ).toPromise().then(response => {
100
101                 const evt = this.evt.parse(response);
102                 if (evt) {
103                     console.error(evt);
104                     // TODO: toast
105                 }
106
107                 // TODO: toast
108                 this.recordSaved.emit(xml);
109                 return response;
110             });
111
112         } else {
113             // TODO: create a new record
114         }
115     }
116
117     fromId(id: number): Promise<any> {
118         return this.pcrud.retrieve('bre', id)
119         .toPromise().then(bib => {
120             this.record = new MarcRecord(bib.marc());
121             this.record.id = id;
122             this.record.deleted = bib.deleted() === 't';
123             if (bib.source()) {
124                 this.sourceSelector.applyEntryId(+bib.source());
125             }
126         });
127     }
128
129     fromXml(xml: string) {
130         this.record = new MarcRecord(xml);
131         this.record.id = null;
132     }
133
134     deleteRecord(): Promise<any> {
135
136         return this.confirmDelete.open().toPromise()
137         .then(yes => {
138             if (!yes) { return; }
139
140             return this.net.request('open-ils.cat',
141                 'open-ils.cat.biblio.record_entry.delete',
142                 this.auth.token(), this.record.id).toPromise()
143
144             .then(resp => {
145
146                 const evt = this.evt.parse(resp);
147                 if (evt) {
148                     if (evt.textcode === 'RECORD_NOT_EMPTY') {
149                         return this.cannotDelete.open().toPromise();
150                     } else {
151                         console.error(evt);
152                         return alert(evt);
153                     }
154                 }
155                 return this.fromId(this.record.id)
156                 .then(_ => this.recordSaved.emit(this.record.toXml()));
157             });
158         });
159     }
160
161     undeleteRecord(): Promise<any> {
162
163         return this.confirmUndelete.open().toPromise()
164         .then(yes => {
165             if (!yes) { return; }
166
167             return this.net.request('open-ils.cat',
168                 'open-ils.cat.biblio.record_entry.undelete',
169                 this.auth.token(), this.record.id).toPromise()
170
171             .then(resp => {
172
173                 const evt = this.evt.parse(resp);
174                 if (evt) { console.error(evt); return alert(evt); }
175
176                 return this.fromId(this.record.id)
177                 .then(_ => this.recordSaved.emit(this.record.toXml()));
178             });
179         });
180     }
181 }
182