]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/acq/lineitem/brief-record.component.ts
LP1929741 ACQ Selection List & PO Angluar Port
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / acq / lineitem / brief-record.component.ts
1 import {Component, OnInit, Input, Output} from '@angular/core';
2 import {ActivatedRoute, Router, ParamMap} from '@angular/router';
3 import {IdlService, IdlObject} from '@eg/core/idl.service';
4 import {NetService} from '@eg/core/net.service';
5 import {PcrudService} from '@eg/core/pcrud.service';
6 import {AuthService} from '@eg/core/auth.service';
7
8 const MARC_NS = 'http://www.loc.gov/MARC21/slim';
9
10 const MARC_XML_BASE = `
11 <record xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
12     xmlns="http://www.loc.gov/MARC21/slim"
13     xmlns:marc="http://www.loc.gov/MARC21/slim"
14     xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/ standards/marcxml/schema/MARC21slim.xsd">
15     <leader>00000nam a22000007a 4500</leader>
16 </record>
17 `;
18
19 @Component({
20   templateUrl: 'brief-record.component.html',
21   selector: 'eg-lineitem-brief-record'
22 })
23 export class BriefRecordComponent implements OnInit {
24
25     targetPicklist: number;
26     targetPo: number;
27
28     attrs: IdlObject[] = [];
29     values: {[attr: string]: string} = {};
30
31     constructor(
32         private router: Router,
33         private route: ActivatedRoute,
34         private idl: IdlService,
35         private auth: AuthService,
36         private net: NetService,
37         private pcrud: PcrudService
38     ) { }
39
40     ngOnInit() {
41
42         this.route.parent.paramMap.subscribe((params: ParamMap) => {
43             this.targetPicklist = +params.get('picklistId');
44             this.targetPo = +params.get('poId');
45         });
46
47         this.pcrud.retrieveAll('acqlimad')
48         .subscribe(attr => this.attrs.push(attr));
49     }
50
51     compile(): string {
52
53         const doc = new DOMParser().parseFromString(MARC_XML_BASE, 'text/xml');
54
55         this.attrs.forEach(attr => {
56             const value = this.values[attr.id()];
57             if (value === undefined) { return; }
58
59             const expr = attr.xpath();
60
61             // Logic copied from openils/MarcXPathParser.js
62             // Any 3 numbers are a 'tag'.
63             // Any letters are a subfield.
64             // Always use the first.
65             const tags = expr.match(/\d{3}/g);
66             let subfields = expr.match(/['"]([a-z]+)['"]/);
67             if (subfields) { subfields = subfields[1].split(''); }
68
69             const dfNode = doc.createElementNS(MARC_NS, 'marc:datafield');
70             const sfNode = doc.createElementNS(MARC_NS, 'marc:subfield');
71
72             // Append fields to the document
73             dfNode.setAttribute('tag', '' + tags[0]);
74             dfNode.setAttribute('ind1', ' ');
75             dfNode.setAttribute('ind2', ' ');
76             sfNode.setAttribute('code', '' + subfields[0]);
77             const tNode = doc.createTextNode(value);
78
79             sfNode.appendChild(tNode);
80             dfNode.appendChild(sfNode);
81             doc.documentElement.appendChild(dfNode);
82         });
83
84         return new XMLSerializer().serializeToString(doc);
85     }
86
87     save() {
88         const xml = this.compile();
89
90         const li = this.idl.create('jub');
91         li.marc(xml);
92
93         if (this.targetPicklist) {
94             li.picklist(this.targetPicklist);
95         } else if (this.targetPo) {
96             li.purchase_order(this.targetPo);
97         }
98
99         li.selector(this.auth.user().id());
100         li.creator(this.auth.user().id());
101         li.editor(this.auth.user().id());
102
103         this.net.request('open-ils.acq',
104             'open-ils.acq.lineitem.create', this.auth.token(), li
105         ).toPromise().then(_ => {
106             this.router.navigate(['../'], {
107                 relativeTo: this.route,
108                 queryParamsHandling: 'merge'
109             });
110         });
111     }
112 }
113