company-model-select-expert-knowledge.component.ts 3.97 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import { Component, OnDestroy, OnInit } from '@angular/core';
import { CompanyModel } from '../../../canvas-meta-model/company-model';
import { Subscription } from 'rxjs';
import { ActivatedRoute, Router } from '@angular/router';
import { CompanyModelService } from '../../../canvas-meta-model/company-model.service';
import { BmProcess } from '../../../development-process-registry/bm-process/bm-process';
import { BmProcessService } from '../../../development-process-registry/bm-process/bm-process.service';
import { MergeService } from '../../merge/merge.service';
import { FormBuilder, Validators } from '@angular/forms';
import { ExpertModel } from '../../../canvas-meta-model/expert-model';

@Component({
  selector: 'app-company-model-select-expert-knowledge',
  templateUrl: './company-model-select-expert-knowledge.component.html',
Alexander Philipp Nowosad's avatar
Alexander Philipp Nowosad committed
15
  styleUrls: ['./company-model-select-expert-knowledge.component.css'],
16
})
Alexander Philipp Nowosad's avatar
Alexander Philipp Nowosad committed
17
18
19
export class CompanyModelSelectExpertKnowledgeComponent
  implements OnInit, OnDestroy
{
20
21
22
  companyModel: CompanyModel;
  bmProcess: BmProcess;

Alexander Philipp Nowosad's avatar
Alexander Philipp Nowosad committed
23
24
25
  selectedExpertModelForm = this.fb.group({
    expertModelId: [null, Validators.required],
  });
26
27
28
29
30
31
32
33
34
35
36
37
  selectedExpertModelList: ExpertModel[];
  unselectedExpertModelList: ExpertModel[];

  private querySubscription: Subscription;
  private routeSubscription: Subscription;

  constructor(
    private bmProcessService: BmProcessService,
    private companyModelService: CompanyModelService,
    private fb: FormBuilder,
    private mergeService: MergeService,
    private route: ActivatedRoute,
Alexander Philipp Nowosad's avatar
Alexander Philipp Nowosad committed
38
39
    private router: Router
  ) {}
40

41
  ngOnInit(): void {
42
    this.querySubscription = this.route.queryParamMap.subscribe((paramMap) => {
43
      void this.loadBmProcess(paramMap.get('bmProcessId'));
44
45
    });
    this.routeSubscription = this.route.paramMap.subscribe((paramMap) => {
46
      void this.loadCompanyModel(paramMap.get('id'));
47
48
49
    });
  }

50
  ngOnDestroy(): void {
51
52
53
54
55
56
57
58
    if (this.querySubscription) {
      this.querySubscription.unsubscribe();
    }
    if (this.routeSubscription) {
      this.routeSubscription.unsubscribe();
    }
  }

59
  async loadCompanyModel(companyModelId: string): Promise<void> {
60
61
62
63
    this.companyModel = await this.companyModelService.get(companyModelId);
    await this.loadExpertModels(companyModelId);
  }

64
  async loadBmProcess(bmProcessId: string): Promise<void> {
65
    this.bmProcess = await this.bmProcessService.get(bmProcessId);
66
67
  }

68
69
70
71
72
73
  async mergeExpertModel(expertModelId: string): Promise<void> {
    await this.router.navigate([
      '/merge',
      this.companyModel._id,
      expertModelId,
    ]);
74
75
  }

76
  async selectExpertModel(): Promise<void> {
Alexander Philipp Nowosad's avatar
Alexander Philipp Nowosad committed
77
78
79
80
    await this.mergeService.selectExpertModel(
      this.companyModel._id,
      this.selectedExpertModelForm.value.expertModelId
    );
81
82
83
84
    this.selectedExpertModelForm.reset();
    await this.loadExpertModels(this.companyModel._id);
  }

85
  async unselectExpertModel(expertModelId: string): Promise<void> {
Alexander Philipp Nowosad's avatar
Alexander Philipp Nowosad committed
86
87
88
89
    await this.mergeService.unselectExpertModel(
      this.companyModel._id,
      expertModelId
    );
90
91
92
    await this.loadExpertModels(this.companyModel._id);
  }

93
  private async loadExpertModels(companyModelId: string): Promise<void> {
94
95
96
97
    await this.loadSelectedExpertModels(companyModelId);
    await this.loadUnselectedExpertModels(companyModelId);
  }

98
99
100
101
102
  private async loadSelectedExpertModels(
    companyModelId: string
  ): Promise<void> {
    this.selectedExpertModelList =
      await this.mergeService.getSelectedExpertModels(companyModelId);
103
104
  }

105
106
107
  private async loadUnselectedExpertModels(
    companyModelId: string
  ): Promise<void> {
Alexander Philipp Nowosad's avatar
Alexander Philipp Nowosad committed
108
109
110
    const result = await this.mergeService.getUnselectedExpertModels(
      companyModelId
    );
111
    if (this.bmProcess.domains.length > 0) {
112
      this.unselectedExpertModelList = result.filter((model) =>
Alexander Philipp Nowosad's avatar
Alexander Philipp Nowosad committed
113
114
        model.domains
          ? model.domains.some((domain) =>
115
              this.bmProcess.domains.some((d) => d._id === domain._id)
Alexander Philipp Nowosad's avatar
Alexander Philipp Nowosad committed
116
117
            )
          : false
118
119
      );
    } else {
120
      this.unselectedExpertModelList = result;
121
    }
122
123
  }
}