Project

General

Profile

1
/*
2
*  updated by myrto on 19/12/2018
3
*/
4

    
5
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
6
import { RepositoryService } from '../../../services/repository.service';
7
import { loadingReposMessage, noRepositoriesFound, noRepositoryChosenMsg, noServiceMessage } from '../../../domain/shared-messages';
8
import { Country, RepositorySnippet } from '../../../domain/typeScriptClasses';
9

    
10
@Component({
11
  selector: 'app-select-existing-datasource',
12
  templateUrl: './register-datasource-select-existing.component.html'
13
})
14

    
15
export class RegisterDatasourceSelectExistingComponent implements OnInit {
16
  countries: Country[] = [];
17
  hasSelectedCountry: boolean;
18
  selectedCountry: Country;
19
  countryRepos: RepositorySnippet[] = [];
20
  hasSelectedRepo: boolean;
21

    
22
  noRepositories: string;
23
  alertMessage: string;
24
  loadingMessage: string = loadingReposMessage;
25

    
26
  repoId: string;
27

    
28
  sourceUrl: string;
29
  sourceTitle: string;
30
  latestUpdate: string;
31
  opendoarlatestUpdate: string;
32
  re3datalatestUpdate: string;
33
  fairsharinglatestUpdate: string;
34

    
35
  @Input() mode: string;
36

    
37
  @Output() emitRepoId: EventEmitter<string> = new EventEmitter();
38
  @Output() promptNext: EventEmitter<boolean> = new EventEmitter();
39

    
40
  searchBox = '';
41

    
42
  constructor(private repoService: RepositoryService) {}
43

    
44
  ngOnInit() {
45
    this.setUpSourceInfo();
46
    this.getCountries();
47
    this.hasSelectedCountry = false;
48
  }
49

    
50
  setUpSourceInfo() { // deprecated?
51
    if (this.mode === 'opendoar') {
52
      this.sourceUrl = 'http://v2.sherpa.ac.uk/opendoar/';
53
      this.sourceTitle = 'OpenDOAR';
54
    } else if (this.mode === 're3data') {
55
      this.sourceUrl = 'https://www.re3data.org/';
56
      this.sourceTitle = 'Re3data';
57
    } else if (this.mode === 'cris') {
58
      this.sourceUrl = 'https://eurocris.org';
59
      this.sourceTitle = 'euroCRIS';
60
    }
61
    this.getLatestUpdate();
62
  }
63

    
64
  getCountries() {
65
    this.repoService.getCountries().subscribe(
66
      countries => {
67
        // TODO: check again getCountries null return values
68
        /*/!* check for null values *!/
69
        let nullVals = countries.filter(el => el.name === null);
70
        /!* remove null values from array *!/
71
        for (let nullVal of nullVals) {
72
          let i = countries.findIndex(el => el === nullVal);
73
          /!* remove null value from array *!/
74
          if (i !== -1) { countries.splice(i, 1); }
75
        }*/
76

    
77
        /* sort countries array */
78
        this.countries = countries.sort( function(a, b) {
79
          if (a.name < b.name) {
80
            return -1;
81
          } else if (a.name > b.name) {
82
            return 1;
83
          } else {
84
            return 0;
85
          }
86
        } );
87
      },
88
      error => {
89
        this.alertMessage = noServiceMessage;
90
        console.log(error);
91
      });
92
  }
93

    
94
  getReposInCountry(i: number) {
95
    const country = this.countries[i];
96
    // console.log(`I got ${country} and ${this.mode}`);
97
    this.countryRepos = [];
98
    this.selectedCountry = country;
99
    this.hasSelectedCountry = false;
100
    this.loadingMessage = loadingReposMessage;
101
    this.noRepositories = '';
102
    this.repoService.getRepositoriesOfCountry(country.code, this.mode).subscribe (
103
      repos => {
104
        this.countryRepos = repos;
105
      },
106
      error => {
107
        console.log(error.statusText);
108
        this.loadingMessage = '';
109
        this.alertMessage = noServiceMessage;
110
        this.countryRepos = [];
111
      },
112
      () => {
113
        if (!this.countryRepos || !this.countryRepos.length) {
114
          this.noRepositories = noRepositoriesFound;
115
        } else {
116
          this.noRepositories = '';
117
          if (this.selectedCountry.code === country.code) {
118
            /* to make sure that the correct set of repositories is displayed - in case of consequent country selections */
119
            this.hasSelectedCountry = true;
120
          } else {
121
            this.countryRepos = [];
122
          }
123
        }
124
        this.loadingMessage = '';
125
        this.alertMessage = '';
126
        // console.log('this.selectedCountry became', JSON.stringify(this.selectedCountry));
127
      }
128
    );
129
  }
130

    
131
  getLatestUpdate() {
132
    return this.repoService.getListLatestUpdate(this.mode).subscribe(
133
      responseDate => {
134
        if (this.mode === 'cris') { this.latestUpdate = responseDate['lastCollectionDate']; }
135
        if (this.mode === 'repository') {
136
          this.opendoarlatestUpdate = responseDate['opendoar'];
137
          this.re3datalatestUpdate = responseDate['re3data'];
138
          this.fairsharinglatestUpdate = responseDate['fairsharing'];
139
        }
140
      },
141
      error => console.log(error)
142
    );
143
  }
144

    
145
  onChooseRepository(id: string) {
146
    this.hasSelectedRepo = true;
147
    this.repoId = id;
148
  }
149

    
150
  pushedNext() {
151
    this.promptNext.emit(true);
152
  }
153

    
154
  public goToNextStep(): boolean {
155
    if (!this.hasSelectedRepo || this.noRepositories) {
156
      this.alertMessage = noRepositoryChosenMsg;
157
      return false;
158
    } else {
159
      this.emitRepoId.emit(this.repoId);
160
      return true;
161
    }
162
  }
163

    
164
  searchTerm(event: any) {
165
    this.searchBox = event.target.value;
166
  }
167

    
168
}
(2-2/10)