summaryrefslogtreecommitdiffstats
path: root/sdc-workflow-designer-ui/src/app/services/workflow.service.ts
blob: 127629c9cfdb1cd9b5c93326cb672321b9041faa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/**
 * Copyright (c) 2017 ZTE Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and the Apache License 2.0 which both accompany this distribution,
 * and are available at http://www.eclipse.org/legal/epl-v10.html
 * and http://www.apache.org/licenses/LICENSE-2.0
 *
 * Contributors:
 *     ZTE - initial API and implementation and/or initial documentation
 */

import { Injectable } from '@angular/core';
import { DataService } from "./data/data.service";
import { Observable } from "rxjs/Observable";
import { PlanModel } from "../model/plan-model";
import { BroadcastService } from "./broadcast.service";

/**
 * ModelService
 * provides all operations about plan model.
 */
@Injectable()
export class WorkflowService {

    public workflows = new Map<string, any>();
    public planModel: PlanModel;
    private planName : string;

    constructor(private broadcastService: BroadcastService, private dataAccessService: DataService) {
        this.broadcastService.workflows$.subscribe(workflows => this.workflows = workflows);
        this.broadcastService.planModel$.subscribe(workflow => this.planModel = workflow);
    }

    public save() {
        console.log(this.planModel);
        console.log(JSON.stringify(this.planModel));
        this.broadcastService.broadcast(this.broadcastService.saveEvent, {"name": this.planName, "planModel": this.planModel});
    }

    public getPlanName(planId: string): string {
        const planInfo = this.workflows.get(planId);
        return planInfo ? planInfo.planName: null;
    }

    public getPlanModel(planId: string): PlanModel {
        const planInfo = this.workflows.get(planId);
        return planInfo ? planInfo.plan: null;
    }

    public getWorkflows(): Map<string, any> {

        return this.workflows;
    }

    public addWorkflow() {
        this.workflows.set(this.getPlanId(), {"planName": "newPlan", "plan": new PlanModel()});
        this.broadcastWorkflows();
    }

    public deleteWorkflow(planId: string): PlanModel {
        this.workflows.delete(planId);
        this.broadcastWorkflows();

        return undefined;
    }

    public broadcastWorkflows() {
        this.broadcastService.broadcast(this.broadcastService.workflows, this.workflows);
    }

    private getPlanId(): string {
        for(let index=0; index <= this.workflows.size; index++) {
            if(!this.workflows.has(index + "")) {
                return index + "";
            }
        }
    }
}