253cfcb454df800a43645f692ef23ae646af0b71
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / cat / vandelay / export.component.ts
1 import {Component, AfterViewInit, ViewChild, Renderer2} from '@angular/core';
2 import {NgbPanelChangeEvent} from '@ng-bootstrap/ng-bootstrap';
3 import {HttpClient, HttpRequest, HttpEventType} from '@angular/common/http';
4 import {HttpResponse, HttpErrorResponse} from '@angular/common/http';
5 import {saveAs} from 'file-saver/FileSaver';
6 import {AuthService} from '@eg/core/auth.service';
7 import {ToastService} from '@eg/share/toast/toast.service';
8 import {ProgressInlineComponent} from '@eg/share/dialog/progress-inline.component';
9 import {VandelayService, VANDELAY_EXPORT_PATH} from './vandelay.service';
10
11
12 @Component({
13   templateUrl: 'export.component.html'
14 })
15 export class ExportComponent implements AfterViewInit {
16
17     recordSource: string;
18     fieldNumber: number;
19     selectedFile: File;
20     recordId: number;
21     bucketId: number;
22     recordType: string;
23     recordFormat: string;
24     recordEncoding: string;
25     includeHoldings: boolean;
26     isExporting: boolean;
27
28     @ViewChild('fileSelector') private fileSelector;
29     @ViewChild('exportProgress') 
30         private exportProgress: ProgressInlineComponent;
31
32     constructor(
33         private renderer: Renderer2,
34         private http: HttpClient,
35         private toast: ToastService,
36         private auth: AuthService
37     ) {
38         this.recordType = 'biblio';
39         this.recordFormat = 'USMARC';
40         this.recordEncoding = 'UTF-8';
41         this.includeHoldings = false;
42     }
43
44     ngAfterViewInit() {
45         this.renderer.selectRootElement('#csv-input').focus();
46     }
47
48     sourceChange($event: NgbPanelChangeEvent) {
49         this.recordSource = $event.panelId;
50
51         if ($event.nextState) { // panel opened
52
53             // give the panel a chance to render before focusing input
54             setTimeout(() => {
55                 this.renderer.selectRootElement(
56                     `#${this.recordSource}-input`).focus();
57             })
58         }
59     }
60
61     fileSelected($event) {
62        this.selectedFile = $event.target.files[0]; 
63     }
64
65     hasNeededData(): boolean {
66         return Boolean(
67             this.selectedFile || this.recordId || this.bucketId
68         );
69     }
70
71     exportRecords() {
72         this.isExporting = true;
73         this.exportProgress.update({value: 0});
74
75         const formData: FormData = new FormData();
76
77         formData.append('ses', this.auth.token());
78         formData.append('rectype', this.recordType);
79         formData.append('encoding', this.recordEncoding);
80         formData.append('format', this.recordFormat);
81
82         if (this.includeHoldings) {
83             formData.append('holdings', '1');
84         }
85
86         switch (this.recordSource) {
87
88             case 'csv':
89                 formData.append('idcolumn', ''+this.fieldNumber);
90                 formData.append('idfile', 
91                     this.selectedFile, this.selectedFile.name);
92                 break;
93
94             case 'record-id':
95                 formData.append('id', ''+this.recordId);
96                 break;
97
98             case 'bucket-id':
99                 formData.append('containerid', ''+this.bucketId);
100                 break;
101         }
102         
103         this.sendExportRequest(formData);
104     }
105
106     sendExportRequest(formData: FormData) {
107
108         const fileName = `export.${this.recordType}.` +
109             `${this.recordEncoding}.${this.recordFormat}`;
110
111         const req = new HttpRequest('POST', VANDELAY_EXPORT_PATH, 
112             formData, {reportProgress: true, responseType: 'text'});
113
114         this.http.request(req).subscribe(
115             evt => {
116                 console.log(evt);
117                 if (evt.type === HttpEventType.DownloadProgress) {
118                     // File size not reported by server in advance.
119                     this.exportProgress.update({value: evt.loaded});
120
121                 } else if (evt instanceof HttpResponse) {
122
123                     saveAs(new Blob([evt.body], 
124                         {type: 'application/octet-stream'}), fileName);
125
126                     this.isExporting = false;
127                 }
128             },
129
130             (err: HttpErrorResponse) => {
131                 console.error(err);
132                 this.toast.danger(err.error);
133                 this.isExporting = false;
134             }
135         );
136     }
137 }
138