| /******************************************************************************** |
| * Copyright (c) 2015-2018 Contributors to the Eclipse Foundation |
| * |
| * See the NOTICE file(s) distributed with this work for additional |
| * information regarding copyright ownership. |
| * |
| * This program and the accompanying materials are made available under the |
| * terms of the Eclipse Public License v. 2.0 which is available at |
| * http://www.eclipse.org/legal/epl-2.0. |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| ********************************************************************************/ |
| |
| |
| import { Injectable } from '@angular/core'; |
| import { Http, Response, Headers, RequestOptions } from '@angular/http'; |
| |
| import { PropertyService } from '../core/property.service'; |
| |
| import { plainToClass } from 'class-transformer'; |
| import { HttpErrorHandler } from '../core/http-error-handler'; |
| import { throwError as observableThrowError, Observable } from 'rxjs'; |
| import { map, catchError } from 'rxjs/operators'; |
| import { Node } from '../navigator/node'; |
| |
| @Injectable() |
| export class ExtSystemService { |
| |
| private prefEndpoint: string; |
| |
| constructor(private http: Http, |
| private httpErrorHandler: HttpErrorHandler, |
| private _prop: PropertyService) { |
| this.prefEndpoint = _prop.getUrl('mdm/administration/'); |
| } |
| |
| getExtSystemForScope(scope: string, key?: string): Observable<Node[]> { |
| if (key == null) { |
| key = ''; |
| } |
| |
| return this.http.get(this.prefEndpoint + scope + '/externalsystems').pipe( |
| map(response => plainToClass(Node, response.json().data)), |
| catchError(this.handleError)); |
| } |
| |
| getExtSystemAttributesForScope(scope: string, id: string, key?: string): Observable<Node[]> { |
| if (key == null) { |
| key = ''; |
| } |
| |
| return this.http.get(this.prefEndpoint + scope + '/externalsystems/attributes/' + id).pipe( |
| map(response => plainToClass(Node, response.json().data)), |
| catchError(this.handleError)); |
| } |
| |
| getExtSystem(key?: string) { |
| if (key == null) { |
| key = ''; |
| } |
| return this.http.get(this.prefEndpoint + '?key=' + key).pipe( |
| map(response => plainToClass(Node, response.json().data)), |
| catchError(this.handleError)); |
| } |
| |
| getAttributeValueFromNode(node: Node, attribute: string) { |
| if (node.attributes !== undefined) { |
| for (let i in node.attributes) { |
| if (node.attributes[i].name === attribute) { |
| return node.attributes[i].value; |
| } |
| } |
| } |
| return ''; |
| } |
| |
| saveExtSystemAttr(scope: string, extSystemAttr: Node, extSystem: Node) { |
| let headers = new Headers({ 'Content-Type': 'application/json' }); |
| let options = new RequestOptions({ headers: headers }); |
| |
| let structure = {}; |
| structure['Name'] = extSystemAttr.name; |
| |
| if (parseInt(extSystemAttr.id, 10) > 0) { |
| structure['Description'] = this.getAttributeValueFromNode(extSystemAttr, 'Description'); |
| // update |
| return this.http.put(this.prefEndpoint + scope + '/externalsystems/attribute/' + extSystemAttr.id, |
| JSON.stringify(structure), options).pipe( |
| catchError(this.handleError) |
| ); |
| } else { |
| structure['ExtSystemAttribute'] = extSystemAttr; |
| // only provide the ID as the backend will evaluate the whole json string |
| structure['ExtSystem'] = extSystem.id; |
| // create |
| return this.http.post(this.prefEndpoint + scope + '/externalsystems/attribute', JSON.stringify(structure), options).pipe( |
| catchError(this.handleError)); |
| } |
| } |
| |
| saveExtSystemMDMAttr(scope: string, extSystemMDMAttr: Node, extSystemAttr: Node) { |
| let headers = new Headers({ 'Content-Type': 'application/json' }); |
| let options = new RequestOptions({ headers: headers }); |
| |
| let structure = {}; |
| structure['Name'] = extSystemMDMAttr.name; |
| |
| if (parseInt(extSystemMDMAttr.id, 10) > 0) { |
| structure['CompType'] = this.getAttributeValueFromNode(extSystemMDMAttr, 'CompType'); |
| structure['CompName'] = this.getAttributeValueFromNode(extSystemMDMAttr, 'CompName'); |
| structure['AttrName'] = this.getAttributeValueFromNode(extSystemMDMAttr, 'AttrName'); |
| // update |
| return this.http.put(this.prefEndpoint + scope + '/externalsystems/mdmattribute/' + extSystemMDMAttr.id, |
| JSON.stringify(structure), options).pipe( |
| catchError(this.handleError) |
| ); |
| } else { |
| structure['MDMAttribute'] = extSystemMDMAttr; |
| structure['ExtSystemAttribute'] = extSystemAttr.id; |
| // create |
| return this.http.post(this.prefEndpoint + scope + '/externalsystems/mdmattribute', JSON.stringify(structure), options).pipe( |
| catchError(this.handleError)); |
| } |
| } |
| |
| saveExtSystem(scope: string, extSystem: Node) { |
| let headers = new Headers({ 'Content-Type': 'application/json' }); |
| let options = new RequestOptions({ headers: headers }); |
| |
| let structure = {}; |
| structure['Name'] = extSystem.name; |
| |
| if (parseInt(extSystem.id, 10) > 0) { |
| // update |
| structure['Description'] = this.getAttributeValueFromNode(extSystem, 'Description'); |
| return this.http.put(this.prefEndpoint + scope + '/externalsystems/' + extSystem.id, JSON.stringify(structure), options).pipe( |
| catchError(this.handleError)); |
| } else { |
| structure['ExtSystem'] = extSystem; |
| // create |
| return this.http.post(this.prefEndpoint + scope + '/externalsystems', JSON.stringify(structure), options).pipe( |
| catchError(this.handleError)); |
| } |
| } |
| |
| deleteExtSystem(scope: string, id: string) { |
| return this.http.delete(this.prefEndpoint + scope + '/externalsystems/' + id).pipe( |
| catchError(this.handleError)); |
| } |
| deleteExtSystemAttr(scope: string, id: string) { |
| return this.http.delete(this.prefEndpoint + scope + '/externalsystems/attribute/' + id).pipe( |
| catchError(this.handleError)); |
| } |
| deleteExtSystemMDMAttr(scope: string, id: string) { |
| return this.http.delete(this.prefEndpoint + scope + '/externalsystems/mdmattribute/' + id).pipe( |
| catchError(this.handleError)); |
| } |
| |
| private handleError(e: Error | any) { |
| if (e instanceof Response) { |
| let response = <Response> e; |
| if (response.status !== 200) { |
| return observableThrowError('Could not request extSystems! ' |
| + 'Please check if application server is running and extSystem database is configured correctly.'); |
| } |
| } |
| return this.httpErrorHandler.handleError(e); |
| } |
| } |