Project

General

Profile

1
import { Component, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core';
2
import {
3
  formErrorRequiredFields,
4
  formErrorWasntSaved,
5
  formSubmitting,
6
  formSuccessUpdatedRepo,
7
  noServiceMessage
8
} from '../../../domain/shared-messages';
9
import { RepositoryService } from "../../../services/repository.service";
10
import { AbstractControl, FormBuilder, FormGroup, Validators } from '@angular/forms';
11
import { Country, Repository, Timezone, Typology } from '../../../domain/typeScriptClasses';
12
import {
13
  Description,
14
  softwarePlatformDesc,
15
  platformNameDesc,
16
  officialNameDesc,
17
  repoDescriptionDesc,
18
  countryDesc,
19
  longtitudeDesc,
20
  latitudeDesc,
21
  websiteUrlDesc,
22
  institutionNameDesc,
23
  englishNameDesc,
24
  logoUrlDesc,
25
  timezoneDesc,
26
  datasourceTypeDesc,
27
  journalTypeDesc,
28
  aggregatorTypeDesc,
29
  adminEmailDesc, lissnDesc, eissnDesc, issnDesc
30
} from '../../../domain/oa-description';
31
import { ConfirmationDialogComponent } from '../../../shared/reusablecomponents/confirmation-dialog.component';
32

    
33
@Component ({
34
  selector: 'datasource-update-form',
35
  templateUrl: './datasource-update-form.component.html'
36
})
37

    
38
export class DatasourceUpdateFormComponent implements OnInit {
39

    
40
  errorMessage: string;
41
  successMessage: string;
42
  loadingMessage: string;
43

    
44
  typologies: Typology[] = [];
45
  timezones: Timezone[] = [];
46
  countries: Country[] = [];
47
  datasourceClasses: Map<string,string> = new Map<string,string>();
48
  classCodes: string[] = [];
49

    
50
  @ViewChild('updateLogoUrlModal')
51
  public updateLogoUrlModal: ConfirmationDialogComponent;
52

    
53
  /* in sources/update emits the new logUrl */
54
  @Output() emittedUrl: EventEmitter<string> = new EventEmitter();
55

    
56
  /*  in sources/register (of literature or data repository) emits the updated repository */
57
  @Output() emittedInfo: EventEmitter<Repository> = new EventEmitter();
58

    
59
  @Input() selectedRepo: Repository;
60

    
61
  @Input() showButton: boolean;
62

    
63
  updateGroup: FormGroup;
64
  readonly updateGroupDefinition = {
65
    softwarePlatform : '',
66
    platformName : '',
67
    officialName : ['', Validators.required],
68
    issn : ['', [Validators.minLength(8), Validators.maxLength(8)]],
69
    eissn : ['', [Validators.minLength(8), Validators.maxLength(8)]],
70
    lissn : ['', [Validators.minLength(8), Validators.maxLength(8)]],
71
    repoDescription : ['', Validators.required],
72
    country : ['', Validators.required],
73
    longtitude : ['', [Validators.required, Validators.min(-180), Validators.max(180)] ],
74
    latitude : ['', [Validators.required, Validators.min(-90), Validators.max(90)] ],
75
    websiteUrl : ['', Validators.required],
76
    institutionName : ['', Validators.required],
77
    englishName: ['', Validators.required],
78
    logoUrl: '',
79
    timezone: ['', Validators.required],
80
    datasourceType: ['', Validators.required],
81
    adminEmail: ['', [Validators.required, Validators.email]]
82
  };
83

    
84
  softwarePlatformDesc : Description = softwarePlatformDesc;
85
  platformNameDesc : Description = platformNameDesc;
86
  officialNameDesc : Description = officialNameDesc;
87
  issnDesc : Description = issnDesc;
88
  eissnDesc : Description = eissnDesc;
89
  lissnDesc : Description = lissnDesc;
90
  repoDescriptionDesc : Description = repoDescriptionDesc;
91
  countryDesc : Description = countryDesc;
92
  longtitudeDesc : Description = longtitudeDesc;
93
  latitudeDesc : Description = latitudeDesc;
94
  websiteUrlDesc : Description = websiteUrlDesc;
95
  institutionNameDesc : Description = institutionNameDesc;
96
  englishNameDesc : Description = englishNameDesc;
97
  logoUrlDesc : Description = logoUrlDesc;
98
  timezoneDesc : Description = timezoneDesc;
99
  datasourceTypeDesc : Description;
100
  adminEmailDesc : Description = adminEmailDesc;
101

    
102
  constructor(
103
    private fb: FormBuilder,
104
    private repoService: RepositoryService
105
  ) {}
106

    
107
  ngOnInit(){
108
    this.loadForm();
109
  }
110

    
111
  loadForm() {
112
    this.updateGroup = this.fb.group(this.updateGroupDefinition, {validator: checkPlatform});
113
    this.setupUpdateForm();
114
    this.getDatasourceClasses();
115
    this.getCountries();
116
    this.getTypologies();
117
    this.getTimezones();
118
  }
119

    
120
  setupUpdateForm(){
121
    if (this.selectedRepo) {
122
      if (this.selectedRepo.datasourceType == 'journal') {
123
        this.datasourceTypeDesc = journalTypeDesc;
124
      } else if (this.selectedRepo.datasourceType == 'aggregator') {
125
        this.datasourceTypeDesc = aggregatorTypeDesc;
126
      } else {
127
        this.datasourceTypeDesc = datasourceTypeDesc;
128
      }
129
      this.updateGroup.setValue({
130
        softwarePlatform: this.selectedRepo.typology,
131
        platformName: '',
132
        officialName: this.selectedRepo.officialName,
133
        issn: '',
134
        eissn: '',
135
        lissn: '',
136
        repoDescription: this.selectedRepo.description,
137
        country: this.selectedRepo.countryCode,
138
        longtitude: this.selectedRepo.longitude,
139
        latitude: this.selectedRepo.latitude,
140
        websiteUrl: this.selectedRepo.websiteUrl,
141
        institutionName: this.selectedRepo.organization,
142
        englishName: this.selectedRepo.englishName,
143
        logoUrl: this.selectedRepo.logoUrl,
144
        timezone: this.selectedRepo.timezone,
145
        datasourceType: this.selectedRepo.datasourceClass,
146
        adminEmail: this.selectedRepo.contactEmail
147
      });
148
      if ( this.typologies.filter(x => x.value == this.updateGroup.get('softwarePlatform').value) == [] ) {
149
        this.updateGroup.get('softwarePlatform').setValue('');
150
        this.updateGroup.get('platformName').setValue(this.selectedRepo.typology);
151
      }
152
      if (this.selectedRepo.datasourceType == 'journal') {
153
        this.updateGroup.get('issn').setValue(this.selectedRepo.issn);
154
          this.updateGroup.get('eissn').setValue(this.selectedRepo.eissn);
155
        this.updateGroup.get('lissn').setValue(this.selectedRepo.lissn);
156
      }
157
    }
158
  }
159

    
160
  getCountries(){
161
    this.repoService.getCountries()
162
      .subscribe(
163
        countries => this.countries = countries.sort( function(a,b){
164
          if(a.name<b.name){
165
            return -1;
166
          } else if(a.name>b.name){
167
            return 1;
168
          } else {
169
            return 0;
170
          }
171
        } ),
172
        error => {
173
          this.errorMessage = noServiceMessage;
174
          console.log(error);
175
        });
176
  }
177

    
178
  getDatasourceClasses() {
179
    this.repoService.getDatasourceClasses(this.selectedRepo.datasourceType).subscribe(
180
      classes => this.datasourceClasses = classes,
181
      error => {
182
        this.errorMessage = noServiceMessage;
183
        console.log(error);
184
      },
185
      () => {
186
        for (let key in this.datasourceClasses){
187
          this.classCodes.push(key);
188
        }
189
      }
190
    );
191
  }
192

    
193
  getTypologies() {
194
    this.repoService.getTypologies().subscribe(
195
      types => this.typologies = types,
196
      error => console.log(error)
197
    );
198
  }
199

    
200
  getTimezones() {
201
    this.repoService.getTimezones().subscribe(
202
      zones => this.timezones = zones,
203
      error => console.log(error)
204
    );
205
  }
206

    
207
  updateRepo(): boolean {
208
    let result: boolean;
209

    
210
    this.errorMessage = '';
211
    this.successMessage = '';
212

    
213
    if (this.updateGroup.valid) {
214
      if ( this.selectedRepo.datasourceType != 'journal' || this.updateGroup.get('issn').value ) {
215
        this.refreshSelectedRepo();
216
/*        this.loadingMessage = formSubmitting;
217
        this.errorMessage = '';
218
        this.repoService.updateRepository(this.selectedRepo).subscribe(
219
          response => {
220
            console.log(`updateRepository responded: ${response}`);
221
            result = (response == '200');
222
          },
223
          error => {
224
            console.log(error);
225
            this.loadingMessage = '';
226
            this.errorMessage = formErrorWasntSaved;
227
            result = false;
228
          },
229
          () => {
230
            this.loadingMessage = '';
231
            if (result) {
232
              this.emittedInfo.emit(this.selectedRepo);
233
              this.successMessage = formSuccessUpdatedRepo;
234
            } else {
235
              this.errorMessage = formErrorWasntSaved;
236
            }
237
          }
238
        );*/
239

    
240
        this.emittedInfo.emit(this.selectedRepo); /* DELETE ME LATER !!! */
241
        result = true;
242
      } else {
243
        this.errorMessage = formErrorRequiredFields;
244
        result = false;
245
      }
246
    } else {
247
      this.errorMessage = formErrorRequiredFields;
248
      result = false;
249
    }
250
    return result;
251
  }
252

    
253
  refreshSelectedRepo() {
254
    if (this.updateGroup.get('platformName').value.trim() ) {
255
      this.selectedRepo.typology = this.updateGroup.get('platformName').value;
256
    } else if (this.updateGroup.get('softwarePlatform').value){
257
      this.selectedRepo.typology = this.updateGroup.get('softwarePlatform').value;
258
    }
259
    this.selectedRepo.officialName = this.updateGroup.get('officialName').value;
260
    this.selectedRepo.description = this.updateGroup.get('repoDescription').value;
261
    this.selectedRepo.countryCode = this.updateGroup.get('country').value;
262
    this.selectedRepo.countryName = this.countries.filter(x => x.code == this.updateGroup.get('country').value)[0].name;
263
    this.selectedRepo.longitude = this.updateGroup.get('longtitude').value;
264
    this.selectedRepo.latitude = this.updateGroup.get('latitude').value;
265
    this.selectedRepo.websiteUrl = this.updateGroup.get('websiteUrl').value;
266
    this.selectedRepo.organization = this.updateGroup.get('institutionName').value;
267
    this.selectedRepo.englishName = this.updateGroup.get('englishName').value;
268
    this.selectedRepo.logoUrl = this.updateGroup.get('logoUrl').value;
269
    this.selectedRepo.timezone = this.updateGroup.get('timezone').value;
270
    this.selectedRepo.datasourceClass = this.updateGroup.get('datasourceType').value;
271
    this.selectedRepo.contactEmail = this.updateGroup.get('adminEmail').value;
272
    if (this.selectedRepo.datasourceType == 'journal') {
273
        this.selectedRepo.issn = this.updateGroup.get('issn').value;
274
        this.selectedRepo.eissn = this.updateGroup.get('eissn').value;
275
        this.selectedRepo.lissn = this.updateGroup.get('lissn').value;
276
    }
277
  }
278

    
279
  changeLogoUrl(logoUrl: string) {
280
    this.updateGroup.get('logoUrl').setValue(logoUrl);
281
  }
282

    
283
  updateLogoUrl(logoUrl: string){
284
    this.updateLogoUrlModal.ids = [logoUrl];
285
    this.updateLogoUrlModal.showModal();
286
  }
287

    
288
  updatedLogoUrl() {
289
    this.emittedUrl.emit(this.updateGroup.get('logoUrl').value);
290
  }
291

    
292
}
293

    
294
export function checkPlatform(c: AbstractControl) {
295
  if ( c.get('softwarePlatform').value || c.get('platformName').value )
296
    return null;
297
  return 'invalid';
298
}
(6-6/6)