File

src/app/streams/components/flo/metamodel.service.ts

Description

Metamodel Service for Flo based Stream Definition graph editor

Example

Index

Properties
Methods

Constructor

constructor(appsService: SharedAppsService)

Creates instance.

Parameters :
Name Type Optional Description
appsService SharedAppsService

Methods

Private addOtherGroup
addOtherGroup(metamodel: Map>)
Parameters :
Name Type Optional Description
metamodel Map<string | Map<stringFlo.ElementMetadata>>
Returns : void
clearCachedData
clearCachedData()
Returns : void
Private createEntry
createEntry(type: ApplicationType, name: string, version: string, metadata?: Flo.ExtraMetadata)
Parameters :
Name Type Optional Description
type ApplicationType
name string
version string
metadata Flo.ExtraMetadata true
Returns : AppMetadata
Private createMetadata
createMetadata(name: string, group: string, description: string, properties: Map, metadata?: Flo.ExtraMetadata)
Parameters :
Name Type Optional Description
name string
group string
description string
properties Map<string | Flo.PropertyMetadata>
metadata Flo.ExtraMetadata true
Returns : Flo.ElementMetadata
graphToText
graphToText(flo: Flo.EditorContext)
Parameters :
Name Type Optional Description
flo Flo.EditorContext
Returns : Promise<string>
groups
groups()
Returns : Array<string>
load
load()
refresh
refresh()
subscribe
subscribe(listener: Flo.MetamodelListener)
Parameters :
Name Type Optional Description
listener Flo.MetamodelListener
Returns : void
textToGraph
textToGraph(flo: Flo.EditorContext, dsl: string)
Parameters :
Name Type Optional Description
flo Flo.EditorContext
dsl string
Returns : Promise<any>
unsubscribe
unsubscribe(listener: Flo.MetamodelListener)
Parameters :
Name Type Optional Description
listener Flo.MetamodelListener
Returns : void

Properties

Private listeners
listeners: Array<Flo.MetamodelListener>
Type : Array<Flo.MetamodelListener>
Private request
request: Promise<Map<stringMap<stringFlo.ElementMetadata>>>
Type : Promise<Map<stringMap<stringFlo.ElementMetadata>>>
import { Flo } from 'spring-flo';
import { Injectable } from '@angular/core';
import { SharedAppsService } from '../../../shared/services/shared-apps.service';
import { ApplicationType } from '../../../shared/model/application-type';
import { convertGraphToText } from './graph-to-text';
import { convertTextToGraph } from './text-to-graph';
import { OTHER_GROUP_TYPE } from './support/shapes';
import { AppMetadata } from '../../../shared/flo/support/app-metadata';
import { LoggerService } from '../../../shared/services/logger.service';

/**
 * Metamodel Service for Flo based Stream Definition graph editor
 *
 * @author Alex Boyko
 * @author Andy Clement
 */
@Injectable()
export class MetamodelService implements Flo.Metamodel {

    private listeners: Array<Flo.MetamodelListener> = [];

    private request: Promise<Map<string, Map<string, Flo.ElementMetadata>>>;

    /**
     * Creates instance.
     * @param http handler for making calls to the data flow restful api
     * @param errorHandler used to generate the error messages.
     */
    constructor(
      private appsService: SharedAppsService
    ) {}

    textToGraph(flo: Flo.EditorContext, dsl: string): Promise<any> {
        LoggerService.log('> textToGraph ' + dsl);
        return new Promise(resolve => {
          this.load().then((metamodel) => resolve(convertTextToGraph(dsl, flo, metamodel)));
        });
    }

    graphToText(flo: Flo.EditorContext): Promise<string> {
        return Promise.resolve(convertGraphToText(flo.getGraph()));
    }

    subscribe(listener: Flo.MetamodelListener) {
        this.listeners.push(listener);
    }

    unsubscribe?(listener: Flo.MetamodelListener) {
        const index = this.listeners.indexOf(listener);
        if (index >= 0) {
            this.listeners.splice(index);
        }
    }

    groups(): Array<string> {
        return ['app', 'source', 'processor', 'sink', 'other'];
    }

    load(): Promise<Map<string, Map<string, Flo.ElementMetadata>>> {
        return this.request ? this.request : this.refresh();
    }

    refresh(): Promise<Map<string, Map<string, Flo.ElementMetadata>>> {
        const metamodel = new Map<string, Map<string, Flo.ElementMetadata>>();
        this.addOtherGroup(metamodel);
        this.request = new Promise(resolve => {
            this.appsService.getApps({page: 0, size: 1000}).subscribe(
                data => {
                    data.items.filter(item => {
                        return item.type.toString() === ApplicationType[ApplicationType.app]
                        || item.type.toString() === ApplicationType[ApplicationType.source]
                        || item.type.toString() === ApplicationType[ApplicationType.processor]
                        || item.type.toString() === ApplicationType[ApplicationType.sink];
                    }).forEach(item => {

                        if (!metamodel.has(item.type.toString())) {
                            metamodel.set(item.type.toString(), new Map<string, AppMetadata>());
                        }
                        const group: Map<string, Flo.ElementMetadata> = metamodel.get(item.type.toString());
                        if (group.has(item.name)) {
                            LoggerService.error(`Group '${item.type}' has duplicate element '${item.name}'`);
                        } else {
                            group.set(item.name, this.createEntry(item.type, item.name, item.version));
                        }
                    });
                    resolve(metamodel);
                },
                error => {
                    LoggerService.error(error);
                    resolve(metamodel);
                }
            );
        });
        return this.request;
    }

    private createEntry(type: ApplicationType, name: string, version: string, metadata?: Flo.ExtraMetadata): AppMetadata {
      return new AppMetadata(
        type.toString(),
        name,
        version,
        this.appsService.getAppInfo(type, name),
        metadata
      );
    }

    private addOtherGroup(metamodel: Map<string, Map<string, Flo.ElementMetadata>>): void {
        const elements = new Map<string, Flo.ElementMetadata>()
            .set('tap', this.createMetadata('tap', OTHER_GROUP_TYPE, 'Tap into an existing app',
                    new Map<string, Flo.PropertyMetadata>().set('name', {
                        name: 'Source Destination Name',
                        id: 'name',
                        defaultValue: '',
                        description: 'the identifier of the producer endpoint in a stream in the form <stream-name>.<app/app-name>',
                        pattern: '[\\w_]+[\\w_-]*\\.[\\w_]+[\\w_-]*'
                    }
                ), {
                    'hide-tooltip-options': true,
                })
            )
            .set('destination', this.createMetadata('destination',
                OTHER_GROUP_TYPE,
                'A destination channel that can be used as a source or a sink',
                new Map<string, Flo.PropertyMetadata>().set('name', {
                        name: 'name',
                        id: 'name',
                        defaultValue: '',
                        description: 'the input/output destination name',
                        pattern: '[\\w_]+[\\w_-]*'
                    }
                ), {
                    'fixed-name': true,
                })
            );
        metamodel.set(OTHER_GROUP_TYPE, elements);
    }

    private createMetadata(name: string, group: string, description: string,
                           properties: Map<string, Flo.PropertyMetadata>, metadata?: Flo.ExtraMetadata): Flo.ElementMetadata {
        return {
            name: name,
            group: group,
            metadata: metadata,
            description: () => Promise.resolve(description),
            get: (property: string) => Promise.resolve(properties.get(property)),
            properties: () => Promise.resolve(properties)
        };

    }

    clearCachedData() {
      this.request = undefined;
    }

}

results matching ""

    No results matching ""