Project

General

Profile

1
import { Component, OnInit, QueryList, Type, ViewChild, ViewChildren } from '@angular/core';
2
import { FormBuilder, FormGroup } from '@angular/forms';
3
import { DatasourceInterfaceFormComponent } from './sources-forms/datasource-interface-form.component';
4
import { Repository, RepositoryInterface } from '../../domain/typeScriptClasses';
5
import { RepositoryService } from '../../services/repository.service';
6
import { ActivatedRoute, Router } from '@angular/router';
7
import { Description, interfaceFormDesc, } from '../../domain/oa-description';
8
import { formInfoLoading, loadingRepoError } from '../../domain/shared-messages';
9
import { DatasourceUpdateFormComponent } from './sources-forms/datasource-update-form.component';
10
import { ConfirmationDialogComponent } from '../../shared/reusablecomponents/confirmation-dialog.component';
11
import { AuthenticationService } from '../../services/authentication.service';
12
import { DatasourceNewInterfaceFormComponent } from './sources-forms/datasource-new-interface-form.component';
13

    
14
@Component ({
15
  selector: 'sources-update-repo',
16
  templateUrl: 'sources-update-repo.component.html'
17
})
18

    
19
export class SourcesUpdateRepoComponent implements OnInit {
20
  loadingMessage: string;
21
  errorMessage: string;
22

    
23
  repoId: string;
24
  logoURL: string;
25
  repo: Repository;
26
  repoInterfaces: RepositoryInterface[] = [];
27

    
28
  @ViewChild('datasourceUpdateForm') datasourceUpdateForm: DatasourceUpdateFormComponent;
29

    
30
  group: FormGroup;
31
  interfaceFormDesc: Description = interfaceFormDesc;
32
  updateDatasourceInterfaces: Type<any> = DatasourceInterfaceFormComponent;
33

    
34
  @ViewChildren('interfacesArray') interfacesArray: QueryList<DatasourceNewInterfaceFormComponent>;
35
  dataForInterfaceComp: any[] = [];
36

    
37
  isModalShown: boolean;
38
  @ViewChild('updateLogoUrlModal')
39
  public updateLogoUrlModal: ConfirmationDialogComponent;
40

    
41
  constructor ( private fb: FormBuilder,
42
                private repoService: RepositoryService,
43
                private authService: AuthenticationService,
44
                private route: ActivatedRoute,
45
                private router: Router) { }
46

    
47

    
48
  ngOnInit() {
49
    this.readRepoId();
50
  }
51

    
52
  readRepoId() {
53
    this.repoId = this.route.snapshot.paramMap.get('id');
54
    console.log(`repoId is ${this.repoId}`);
55
    this.getRepo();
56
  }
57

    
58
  getRepo() {
59
    if (this.repoId) {
60
      this.loadingMessage = formInfoLoading;
61
      this.repoService.getRepositoryById(this.repoId).subscribe(
62
        repo => {
63
          this.repo = repo;
64
        },
65
        error => {
66
          console.log(error);
67
          this.loadingMessage = '';
68
          this.errorMessage = loadingRepoError;
69
        },
70
        () => {
71
          if ( this.authService.activateFrontAuthorization && (this.authService.getUserEmail() !== this.repo.registeredBy.trim()) ) {
72
            this.router.navigateByUrl('/403-forbidden', { skipLocationChange: true });
73
          } else {
74
            this.logoURL = this.repo.logoUrl;
75
            this.getRepoInterfaces();
76
          }
77
        }
78
      );
79
    }
80
  }
81

    
82
  getRepoInterfaces() {
83
    this.group = this.fb.group({});
84
    this.repoService.getRepositoryInterface(this.repoId).subscribe(
85
      interfaces => {
86
        this.repoInterfaces = interfaces.sort( function(a, b) {
87
          if (a.id < b.id) {
88
            return -1;
89
          } else if (a.id > b.id) {
90
            return 1;
91
          } else {
92
            return 0;
93
          }
94
        });
95
        console.log(`the number of interfaces for ${this.repoId} is ${this.repoInterfaces.length}`);
96
      },
97
      error => {
98
        console.log(error);
99
        this.loadingMessage = '';
100
        this.errorMessage = loadingRepoError;
101
      },
102
      () => {
103
        this.loadingMessage = '';
104
        this.fillInterfacesForms();
105
      }
106
    );
107
  }
108

    
109

    
110
  fillInterfacesForms() {
111
    this.dataForInterfaceComp = [];
112
    if (this.repoInterfaces && (this.repoInterfaces.length > 0)) {
113
      for (let i = 0; i < this.repoInterfaces.length; i++) {
114
        this.dataForInterfaceComp.push([
115
          false, i,
116
          { id: this.repo.id,
117
            datasourceType: this.repo.datasourceType,
118
            datasourceClass: this.repo.datasourceType,
119
            registeredBy: this.repo.registeredBy
120
          },
121
          this.repoInterfaces[i]
122
        ]);
123
      }
124
    } else {
125
      this.dataForInterfaceComp.push([
126
        false, 0,
127
        { id: this.repo.id,
128
          datasourceType: this.repo.datasourceType,
129
          datasourceClass: this.repo.datasourceType,
130
          registeredBy: this.repo.registeredBy
131
        }
132
      ]);
133
    }
134
  }
135

    
136
  addInterfaceToList(intrf?: RepositoryInterface) {
137
    const curIndex = this.dataForInterfaceComp.length;
138
    const curRepoInfo = { id: this.repo.id, datasourceType: this.repo.datasourceType,
139
      datasourceClass: this.repo.datasourceType, registeredBy: this.repo.registeredBy };
140
    if (intrf) {
141
      this.dataForInterfaceComp.push([false, curIndex, curRepoInfo, intrf]);
142
    } else {
143
      this.dataForInterfaceComp.push([false, curIndex, curRepoInfo]);
144
    }
145
  }
146

    
147
  removeInterfaceFromList(i: number) {
148
    const tempArray = this.dataForInterfaceComp;
149
    this.dataForInterfaceComp = [];
150
    tempArray.splice(i, 1);
151
    this.dataForInterfaceComp = tempArray;
152
    console.log(JSON.stringify(this.dataForInterfaceComp));
153
  }
154

    
155
  getInterfaces() {
156
    this.repoInterfaces = [];
157
    for (const el of this.interfacesArray.toArray()) {
158
      const intrf = el.getInterface();
159
      if (intrf) {
160
        this.repoInterfaces.push(intrf);
161
        console.log(JSON.stringify(intrf));
162
      }
163
    }
164
    console.log('new interfaces is ', this.repoInterfaces);
165
  }
166

    
167
  updateLogoUrl(logoUrl: string) {
168
    this.updateLogoUrlModal.ids = [logoUrl];
169
    this.updateLogoUrlModal.showModal();
170
  }
171

    
172
  updatedLogoUrl(event: any) {
173
    this.repo.logoUrl = this.logoURL;
174
    this.datasourceUpdateForm.updateGroup.get('logoUrl').setValue(this.logoURL);
175
    this.datasourceUpdateForm.updateRepo();
176

    
177
  }
178

    
179
  getNewLogoUrl( event: any ) {
180
    this.logoURL = event.target.value;
181

    
182
  }
183

    
184
}
(4-4/10)