running-process-select-output-artifact.component.ts 3.64 KB
Newer Older
1
2
3
4
5
6
import { Component, Input, OnDestroy, OnInit } from '@angular/core';
import { AbstractControl, ControlContainer, FormGroup } from '@angular/forms';
import { Artifact } from '../../development-process-registry/method-elements/artifact/artifact';
import { RunningArtifact } from '../../development-process-registry/running-process/running-artifact';
import { OutputArtifactMappingFormValue } from '../shared/output-artifact-mapping-form.service';
import { Subscription } from 'rxjs';
7
8
9
import { StepArtifact } from '../../development-process-registry/running-process/step-artifact';
import { MetaModelService } from '../../development-process-registry/meta-model.service';
import { ArtifactDataType } from '../../development-process-registry/running-process/artifact-data';
10
11
12
13
14
15
16
17
18
19

@Component({
  selector: 'app-running-process-select-output-artifact',
  templateUrl: './running-process-select-output-artifact.component.html',
  styleUrls: ['./running-process-select-output-artifact.component.css'],
})
export class RunningProcessSelectOutputArtifactComponent
  implements OnInit, OnDestroy
{
  @Input() artifact!: Artifact;
20
  @Input() internalArtifact?: StepArtifact;
21
22
  @Input() processArtifacts!: RunningArtifact[];

23
  private artifactIsDefinitionChangeSubscription?: Subscription;
24
25
  private artifactSelectionChangeSubscription?: Subscription;

26
27
28
29
  constructor(
    private controlContainer: ControlContainer,
    private metaModelService: MetaModelService
  ) {}
30
31

  ngOnInit(): void {
32
33
34
35
36
37
    this.artifactIsDefinitionChangeSubscription =
      this.definitionControl.valueChanges.subscribe(async (value) => {
        if (value && !this.artifactNameControl.value) {
          await this.setDefaultName();
        }
      });
38
    this.artifactSelectionChangeSubscription =
39
40
41
      this.artifactControl.valueChanges.subscribe((value: number) =>
        this.loadData(value)
      );
42
43
44
  }

  ngOnDestroy(): void {
45
46
47
    if (this.artifactIsDefinitionChangeSubscription != null) {
      this.artifactIsDefinitionChangeSubscription.unsubscribe();
    }
48
49
50
51
52
    if (this.artifactSelectionChangeSubscription != null) {
      this.artifactSelectionChangeSubscription.unsubscribe();
    }
  }

53
54
55
56
57
58
59
60
61
62
63
64
  /**
   * Load the data of an artifact into the data control.
   *
   * @param artifact the number of the artifact to load
   */
  loadData(artifact: number): void {
    const runningArtifact = this.processArtifacts[artifact];
    this.dataControl.setValue(
      runningArtifact.versions[runningArtifact.versions.length - 1].data.data
    );
  }

65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
  async setDefaultName(): Promise<void> {
    if (
      this.internalArtifact != null &&
      this.internalArtifact.data.type === ArtifactDataType.REFERENCE
    ) {
      const api = this.metaModelService.getMetaModelApi(
        this.internalArtifact.metaModelType
      );
      if (api != null) {
        const name = await api.getName(this.internalArtifact.data.data);
        if (name != null) {
          this.artifactNameControl.setValue(name);
        }
      }
    }
  }

82
83
84
85
86
87
88
89
  get formGroup(): FormGroup {
    return this.controlContainer.control as FormGroup;
  }

  get definitionControl(): AbstractControl {
    return this.formGroup.get('isDefinition');
  }

90
91
92
93
  get artifactNameControl(): AbstractControl {
    return this.formGroup.get('artifactName');
  }

94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
  get artifactControl(): AbstractControl {
    return this.formGroup.get('artifact');
  }

  get dataControl(): AbstractControl {
    return this.formGroup.get('data');
  }

  get showNotes(): boolean {
    if (this.artifact.internalArtifact) {
      return false;
    }
    const formValue: OutputArtifactMappingFormValue = this.formGroup.value;
    if (formValue.isDefinition) {
      return true;
    }
    return formValue.artifact != null;
  }
}