1 import {Injectable} from '@angular/core';
2 import {Observable, Observer} from 'rxjs';
3 import {IdlService, IdlObject} from './idl.service';
4 import {NetService, NetRequest} from './net.service';
5 import {AuthService} from './auth.service';
7 // Externally defined. Used here for debugging.
8 declare var js2JSON: (jsThing: any) => string;
9 declare var OpenSRF: any; // creating sessions
11 interface PcrudReqOps {
12 authoritative?: boolean;
16 // If true, link-type fields which link to a class that defines a
17 // selector will be fleshed with the linked value. This affects
18 // retrieve(), retrieveAll(), and search() calls.
19 fleshSelectors?: boolean;
22 // For for documentation purposes.
23 type PcrudResponse = any;
25 export class PcrudContext {
27 static verboseLogging = true; //
28 static identGenerator = 0; // for debug logging
30 private ident: number;
31 private authoritative: boolean;
32 private xactCloseMode: string;
33 private cudIdx: number;
34 private cudAction: string;
35 private cudLast: PcrudResponse;
36 private cudList: IdlObject[];
38 private idl: IdlService;
39 private net: NetService;
40 private auth: AuthService;
42 // Tracks nested CUD actions
43 cudObserver: Observer<PcrudResponse>;
45 session: any; // OpenSRF.ClientSession
47 constructor( // passed in by parent service -- not injected
55 this.xactCloseMode = 'rollback';
56 this.ident = PcrudContext.identGenerator++;
57 this.session = new OpenSRF.ClientSession('open-ils.pcrud');
61 return '[PCRUDContext ' + this.ident + ']';
64 log(msg: string): void {
65 if (PcrudContext.verboseLogging) {
66 console.debug(this + ': ' + msg);
70 err(msg: string): void {
71 console.error(this + ': ' + msg);
74 token(reqOps?: PcrudReqOps): string {
75 return (reqOps && reqOps.anonymous) ?
76 'ANONYMOUS' : this.auth.token();
79 connect(): Promise<PcrudContext> {
81 return new Promise( (resolve, reject) => {
82 this.session.connect({
83 onconnect : () => { resolve(this); }
89 this.log('disconnect');
90 this.session.disconnect();
93 // Adds "flesh" logic to retrieve linked values for all fields
94 // that link to a class which defines a selector field.
95 applySelectorFleshing(fmClass: string, pcrudOps: any) {
96 pcrudOps = pcrudOps || {};
98 if (!pcrudOps.flesh) {
102 if (!pcrudOps.flesh_fields) {
103 pcrudOps.flesh_fields = {};
106 this.idl.classes[fmClass].fields
107 .filter(f => f.datatype === 'link' && !f.virtual)
109 const selector = this.idl.getLinkSelector(fmClass, field.name);
110 if (!selector) { return; }
112 if (!pcrudOps.flesh_fields[fmClass]) {
113 pcrudOps.flesh_fields[fmClass] = [];
116 if (pcrudOps.flesh_fields[fmClass].indexOf(field.name) < 0) {
117 pcrudOps.flesh_fields[fmClass].push(field.name);
122 retrieve(fmClass: string, pkey: Number | string,
123 pcrudOps?: any, reqOps?: PcrudReqOps): Observable<PcrudResponse> {
124 reqOps = reqOps || {};
125 this.authoritative = reqOps.authoritative || false;
126 if (reqOps.fleshSelectors) {
127 this.applySelectorFleshing(fmClass, pcrudOps);
129 return this.dispatch(
130 `open-ils.pcrud.retrieve.${fmClass}`,
131 [this.token(reqOps), pkey, pcrudOps]);
134 retrieveAll(fmClass: string, pcrudOps?: any,
135 reqOps?: PcrudReqOps): Observable<PcrudResponse> {
137 search[this.idl.classes[fmClass].pkey] = {'!=' : null};
138 return this.search(fmClass, search, pcrudOps, reqOps);
141 search(fmClass: string, search: any,
142 pcrudOps?: any, reqOps?: PcrudReqOps): Observable<PcrudResponse> {
143 reqOps = reqOps || {};
144 this.authoritative = reqOps.authoritative || false;
146 const returnType = reqOps.idlist ? 'id_list' : 'search';
147 let method = `open-ils.pcrud.${returnType}.${fmClass}`;
149 if (reqOps.atomic) { method += '.atomic'; }
151 if (reqOps.fleshSelectors) {
152 this.applySelectorFleshing(fmClass, pcrudOps);
155 return this.dispatch(method, [this.token(reqOps), search, pcrudOps]);
158 create(list: IdlObject | IdlObject[]): Observable<PcrudResponse> {
159 return this.cud('create', list);
161 update(list: IdlObject | IdlObject[]): Observable<PcrudResponse> {
162 return this.cud('update', list);
164 remove(list: IdlObject | IdlObject[]): Observable<PcrudResponse> {
165 return this.cud('delete', list);
167 autoApply(list: IdlObject | IdlObject[]): Observable<PcrudResponse> { // RENAMED
168 return this.cud('auto', list);
171 xactClose(): Observable<PcrudResponse> {
172 return this.sendRequest(
173 'open-ils.pcrud.transaction.' + this.xactCloseMode,
178 xactBegin(): Observable<PcrudResponse> {
179 return this.sendRequest(
180 'open-ils.pcrud.transaction.begin', [this.token()]
184 private dispatch(method: string, params: any[]): Observable<PcrudResponse> {
185 if (this.authoritative) {
186 return this.wrapXact(() => {
187 return this.sendRequest(method, params);
190 return this.sendRequest(method, params);
198 // => xact_close(commit/rollback)
200 wrapXact(mainFunc: () => Observable<PcrudResponse>): Observable<PcrudResponse> {
201 return Observable.create(observer => {
206 // 2. start the transaction
207 .then(() => this.xactBegin().toPromise())
209 // 3. execute the main body
212 mainFunc().subscribe(
213 res => observer.next(res),
214 err => observer.error(err),
216 this.xactClose().toPromise().then(() => {
228 private sendRequest(method: string,
229 params: any[]): Observable<PcrudResponse> {
231 // this.log(`sendRequest(${method})`);
233 return this.net.requestCompiled(
235 'open-ils.pcrud', method, params, this.session)
239 private cud(action: string,
240 list: IdlObject | IdlObject[]): Observable<PcrudResponse> {
241 this.cudList = [].concat(list); // value or array
243 this.log(`CUD(): ${action}`);
246 this.cudAction = action;
247 this.xactCloseMode = 'commit';
249 return this.wrapXact(() => {
250 return Observable.create(observer => {
251 this.cudObserver = observer;
252 this.nextCudRequest();
258 * Loops through the list of objects to update and sends
259 * them one at a time to the server for processing. Once
260 * all are done, the cudObserver is resolved.
262 nextCudRequest(): void {
263 if (this.cudIdx >= this.cudList.length) {
264 this.cudObserver.complete();
268 let action = this.cudAction;
269 const fmObj = this.cudList[this.cudIdx++];
271 if (action === 'auto') {
272 if (fmObj.ischanged()) { action = 'update'; }
273 if (fmObj.isnew()) { action = 'create'; }
274 if (fmObj.isdeleted()) { action = 'delete'; }
276 if (action === 'auto') {
277 // object does not need updating; move along
278 this.nextCudRequest();
283 `open-ils.pcrud.${action}.${fmObj.classname}`,
284 [this.token(), fmObj]
286 res => this.cudObserver.next(res),
287 err => this.cudObserver.error(err),
288 () => this.nextCudRequest()
293 @Injectable({providedIn: 'root'})
294 export class PcrudService {
297 private idl: IdlService,
298 private net: NetService,
299 private auth: AuthService
302 // Pass-thru functions for one-off PCRUD calls
304 connect(): Promise<PcrudContext> {
305 return this.newContext().connect();
308 newContext(): PcrudContext {
309 return new PcrudContext(this.idl, this.net, this.auth);
312 retrieve(fmClass: string, pkey: Number | string,
313 pcrudOps?: any, reqOps?: PcrudReqOps): Observable<PcrudResponse> {
314 return this.newContext().retrieve(fmClass, pkey, pcrudOps, reqOps);
317 retrieveAll(fmClass: string, pcrudOps?: any,
318 reqOps?: PcrudReqOps): Observable<PcrudResponse> {
319 return this.newContext().retrieveAll(fmClass, pcrudOps, reqOps);
322 search(fmClass: string, search: any,
323 pcrudOps?: any, reqOps?: PcrudReqOps): Observable<PcrudResponse> {
324 return this.newContext().search(fmClass, search, pcrudOps, reqOps);
327 create(list: IdlObject | IdlObject[]): Observable<PcrudResponse> {
328 return this.newContext().create(list);
331 update(list: IdlObject | IdlObject[]): Observable<PcrudResponse> {
332 return this.newContext().update(list);
335 remove(list: IdlObject | IdlObject[]): Observable<PcrudResponse> {
336 return this.newContext().remove(list);
339 autoApply(list: IdlObject | IdlObject[]): Observable<PcrudResponse> {
340 return this.newContext().autoApply(list);