File

C:/GoDev/src/TCSTK-Angular/projects/tibco-tcstk/tc-core-lib/src/lib/services/tc-shared-state.service.ts

Index

Methods

Constructor

constructor(http: HttpClient, location: Location)
Parameters :
Name Type Optional
http HttpClient No
location Location No

Methods

Public createSharedState
createSharedState(name: string, type: string, description: string, sandboxId: number, attributes: StateAttribute[], roles: StateRole[], links: string[], content: SharedStateContent, scope?: string)
Parameters :
Name Type Optional
name string No
type string No
description string No
sandboxId number No
attributes StateAttribute[] No
roles StateRole[] No
links string[] No
content SharedStateContent No
scope string Yes
Returns : Observable<string>
Public createUiAppConfig
createUiAppConfig(sandboxId: number, uiAppConfig: UiAppConfig, uiAppId: string)
Parameters :
Name Type Optional
sandboxId number No
uiAppConfig UiAppConfig No
uiAppId string No
Returns : Observable<string>
Public deleteSharedState
deleteSharedState(id: number)
Parameters :
Name Type Optional
id number No
Returns : Observable<string>
Public deleteSharedStates
deleteSharedStates(name: string, scope?: string)
Parameters :
Name Type Optional
name string No
scope string Yes
Returns : Observable<string>
Public getSharedState
getSharedState(name: string, type: string, useCache: boolean, flushCache: boolean, scope?: string)
Parameters :
Name Type Optional
name string No
type string No
useCache boolean No
flushCache boolean No
scope string Yes
Public getUiAppConfig
getUiAppConfig(uiAppId: string, useCache: boolean, flushCache: boolean)
Parameters :
Name Type Optional
uiAppId string No
useCache boolean No
flushCache boolean No
Public updateSharedState
updateSharedState(sharedStateList)
Parameters :
Name Optional
sharedStateList No
Public updateUiAppConfig
updateUiAppConfig(sandboxId: number, uiAppConfig: UiAppConfig, uiAppId: string, id: string)
Parameters :
Name Type Optional
sandboxId number No
uiAppConfig UiAppConfig No
uiAppId string No
id string No
import { Injectable } from '@angular/core';
import {SharedStateContent, SharedStateEntry, SharedStateList, StateAttribute, StateRole} from '../models/tc-shared-state';
import {Observable, of} from 'rxjs';
import {HttpClient, HttpHeaders} from '@angular/common/http';
import {map, tap} from 'rxjs/operators';
import {Location} from '@angular/common';
import {UiAppConfig} from '../models/tc-app-config';
import {TcCoreCommonFunctions} from '../common/tc-core-common-functions';

@Injectable({
  providedIn: 'root'
})
export class TcSharedStateService {

  constructor(private http: HttpClient, private location: Location) {
  }

  public createSharedState(name: string,
                           type: string,
                           description: string,
                           sandboxId: number,
                           attributes: StateAttribute[],
                           roles: StateRole[],
                           links: string[],
                           content: SharedStateContent,
                           scope?: string): Observable<string> {
    const url = '/clientstate/v1/states';

    const body = {
      'name': name,
      'type': type,
      'description': description,
      'sandboxId': sandboxId,
      'attributes': attributes,
      'roles': roles,
      'links': links,
      'scope': scope,
      content: content
    };
    const bodyStr = JSON.stringify(body);
    const headers = new HttpHeaders()
      .set('Content-Type', 'application/json');
    return this.http.post(url, bodyStr, {headers })
      .pipe(
        tap(val => sessionStorage.setItem('tcsTimestamp', Date.now().toString())),
        map(result => {
          return result.toString();
        })
      );
  }

  public updateSharedState(sharedStateList): Observable<SharedStateList> {
    const url = '/clientstate/v1/states';

    const body = sharedStateList;
    const bodyStr = JSON.stringify(body);
    const headers = new HttpHeaders()
      .set('Content-Type', 'application/json');
    return this.http.put(url, bodyStr, {headers })
      .pipe(
        tap(val => sessionStorage.setItem('tcsTimestamp', Date.now().toString())),
        map(updatedSharedStateList => new SharedStateList().deserialize(updatedSharedStateList))
      );
  }

  public getSharedState(name: string, type: string, useCache: boolean, flushCache: boolean, scope?: string): Observable<SharedStateList> {
    let url = '/clientstate/v1/states?$filter=type=' + type
    // const url = '/clientstate/v1/states?$filter=type=' + type
      + ' and name=\'' + name + '\'';
    if (scope) {
      url = url + ' and scope eq \'' + scope + '\'';
    }
    let options = {}
    // set headers when caching required
    let headers: HttpHeaders = new HttpHeaders();
    if (useCache) {
      headers = headers.set('cacheResponse', 'true');
    }
    if (flushCache) {
      headers = headers.set('flushCache', 'true');
    }
    options = {headers: headers };

    return this.http.get(url, options)
      .pipe(
        tap(val => sessionStorage.setItem('tcsTimestamp', Date.now().toString())),
        map(sharedStateList => new SharedStateList().deserialize(sharedStateList)));
  }

  /* Ui App Config */

  public getUiAppConfig(uiAppId: string, useCache: boolean, flushCache: boolean): Observable<UiAppConfig> {
    // if useCache is false this will trigger the service to update the cached version with latest
    const ssName = uiAppId + '.config.tibcolabs.client.context.PUBLIC';

    return this.getSharedState(ssName, 'PUBLIC', useCache, flushCache)
      .pipe(
        map(value => {
            if (value.sharedStateEntries.length > 0) {
              const ssresult = new UiAppConfig().deserialize(JSON.parse(value.sharedStateEntries[0].content.json));
              ssresult.id = value.sharedStateEntries[0].id;
              return ssresult;
            } else {
              return undefined;
            }
          }
        )
      );
  }

  public createUiAppConfig(sandboxId: number, uiAppConfig: UiAppConfig, uiAppId: string): Observable<string> {
    const ssName = uiAppId + '.config.tibcolabs.client.context.PUBLIC';
    const content: SharedStateContent = new SharedStateContent();
    content.json = TcCoreCommonFunctions.escapeString(JSON.stringify(uiAppConfig));
    return this.createSharedState(ssName, 'PUBLIC', '', sandboxId, undefined, undefined, undefined, content)
      .pipe(
        map(value => value)
      );
  }

  public updateUiAppConfig(sandboxId: number, uiAppConfig: UiAppConfig, uiAppId: string, id: string): Observable<UiAppConfig> {
    const ssName = uiAppId + '.config.tibcolabs.client.context.PUBLIC';
    const content: SharedStateContent = new SharedStateContent();
    content.json = TcCoreCommonFunctions.escapeString(JSON.stringify(uiAppConfig));
    const entry: SharedStateEntry = new SharedStateEntry();
    entry.content = content;
    entry.sandboxId = sandboxId;
    entry.name = ssName;
    entry.type = 'PUBLIC';
    entry.id = id;
    const ssList: SharedStateList = new SharedStateList();
    ssList.sharedStateEntries = [];
    ssList.sharedStateEntries.push(entry);
    return this.updateSharedState(ssList.sharedStateEntries)
      .pipe(
        map(value => {
          return new UiAppConfig().deserialize((JSON.parse(value.sharedStateEntries[0].content.json)));
        })
      );
  }

  public deleteSharedState(id: number): Observable<string> {
    const url = '/clientstate/v1/states/' + id;

    return this.http.delete(url)
      .pipe(
        tap(val => sessionStorage.setItem('tcsTimestamp', Date.now().toString())),
        map(result => 'success')
      );
  }

  public deleteSharedStates(name: string, scope?: string): Observable<string> {
    let url = '/clientstate/v1/states?$filter=name eq \'' + name + '\'';
    if (scope) {
      url = url + ' and scope eq \'' + scope + '\'';
    }
    return this.http.delete(url)
      .pipe(
        tap(val => sessionStorage.setItem('tcsTimestamp', Date.now().toString())),
        map(result => 'success')
      );
  }

}

result-matching ""

    No results matching ""