Project

General

Profile

1
/*
2
*  created by myrto on 1/22/2018
3
*/
4

    
5
import { Component, EventEmitter, OnInit, Output } from '@angular/core';
6
import { formErrorRequiredFields, formSuccessRegisteredDatasource, noServiceMessage } from '../../../domain/shared-messages';
7
import { RepositoryService } from "../../../services/repository.service";
8
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
9
import { Country, Repository } from '../../../domain/typeScriptClasses';
10
import { typologies } from '../../../domain/typologies';
11
import { timezones } from '../../../domain/timezones';
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
  aggregatorTypeDesc,
27
  adminEmailDesc
28
} from '../../../domain/oa-description';
29
import { AuthenticationService } from '../../../services/authentication.service';
30

    
31
@Component ({
32
  selector: 'aggregator-info-form',
33
  templateUrl: 'aggregator-info-form.component.html'
34
})
35

    
36
export class AggregatorInfoFormComponent implements OnInit {
37
  errorMessage: string;
38
  successMessage: string;
39
  loadingMessage: string;
40

    
41
  typologies = typologies;
42
  timezones = timezones;
43
  countries: Country[] = [];
44
  datasourceClasses: Map<string,string> = new Map<string,string>();
45
  classCodes: string[] = [];
46

    
47
  @Output() emittedInfo: EventEmitter<Repository> = new EventEmitter();
48

    
49
  group: FormGroup;
50
  readonly groupDefinition = {
51
    softwarePlatform : '',
52
    officialName : ['', Validators.required],
53
    repoDescription : ['', Validators.required],
54
    country : ['', Validators.required],
55
    longtitude : ['', [Validators.required, Validators.maxLength(9), Validators.min(-180), Validators.max(180)] ],
56
    latitude : ['', [Validators.required, Validators.maxLength(9), Validators.min(-90), Validators.max(90)] ],
57
    websiteUrl : ['', Validators.required],
58
    institutionName : ['', Validators.required],
59
    englishName: ['', Validators.required],
60
    logoUrl: '',
61
    timezone: ['', Validators.required],
62
    aggregatorType: ['', Validators.required],
63
    adminEmail: ['', [Validators.required, Validators.email] ]
64
  };
65

    
66
  softwarePlatformDesc : Description = softwarePlatformDesc;
67
  officialNameDesc : Description = officialNameDesc;
68
  repoDescriptionDesc : Description = repoDescriptionDesc;
69
  countryDesc : Description = countryDesc;
70
  longtitudeDesc : Description = longtitudeDesc;
71
  latitudeDesc : Description = latitudeDesc;
72
  websiteUrlDesc : Description = websiteUrlDesc;
73
  institutionNameDesc : Description = institutionNameDesc;
74
  englishNameDesc : Description = englishNameDesc;
75
  logoUrlDesc : Description = logoUrlDesc;
76
  timezoneDesc : Description = timezoneDesc;
77
  aggregatorTypeDesc : Description = aggregatorTypeDesc;
78
  adminEmailDesc : Description = adminEmailDesc;
79

    
80

    
81
  constructor(
82
    private fb: FormBuilder,
83
    private repoService: RepositoryService,
84
    private authService: AuthenticationService
85
  ) {}
86

    
87
  ngOnInit() {
88
    this.loadForm();
89
  }
90

    
91
  loadForm(){
92
    this.group = this.fb.group(this.groupDefinition);
93
    this.getCountries();
94
    this.getDatasourceClasses();
95
  }
96

    
97
  getCountries(){
98
    this.repoService.getCountries()
99
      .subscribe(
100
        countries => this.countries = countries.sort( function(a,b){
101
          if(a.name<b.name){
102
            return -1;
103
          } else if(a.name>b.name){
104
            return 1;
105
          } else {
106
            return 0;
107
          }
108
        } ),
109
        error => {
110
          this.errorMessage = noServiceMessage;
111
          console.log(error);
112
        });
113
  }
114

    
115
  getDatasourceClasses() {
116
    this.repoService.getDatasourceClasses('aggregator').subscribe(
117
      classes => this.datasourceClasses = classes,
118
      error => {
119
        this.errorMessage = noServiceMessage;
120
        console.log(error);
121
      },
122
      () => {
123
        for (let key in this.datasourceClasses){
124
          this.classCodes.push(key);
125
        }
126
      }
127
    );
128
  }
129

    
130
  registerDatasource(): boolean {
131
    if(this.group.valid){
132
      let newRepo = this.createNewRepository();
133
      this.repoService.addRepository('aggregator', newRepo).subscribe(
134
        response => console.log(`${JSON.stringify(response)}`),
135
        error => console.log(error)
136
      );
137
      this.successMessage = formSuccessRegisteredDatasource;
138
      this.errorMessage = '';
139
      return true;
140
    } else {
141
      this.errorMessage = formErrorRequiredFields;
142
      return false;
143
    }
144
  }
145

    
146
  createNewRepository(): Repository {
147
    let newRepo: Repository = new Repository();
148
    newRepo.dateOfCreation = new Date(Date.now());
149
    newRepo.officialName = this.group.get('officialName').value;
150
    newRepo.englishName = this.group.get('englishName').value;
151
    newRepo.websiteUrl = this.group.get('websiteUrl').value;
152
    newRepo.logoUrl = this.group.get('logoUrl').value;
153
    newRepo.contactEmail = this.group.get('adminEmail').value;
154
    newRepo.countryName = this.countries.filter(x => x.code == this.group.get('country').value)[0].name;
155
    newRepo.countryCode = this.group.get('country').value;
156
    newRepo.organization = this.group.get('institutionName').value;
157
    newRepo.latitude = this.group.get('latitude').value;
158
    newRepo.longitude = this.group.get('longtitude').value;
159
    newRepo.timezone = this.group.get('timezone').value;
160
    newRepo.datasourceClass = this.group.get('journalType').value;
161
    newRepo.typology = this.group.get('softwarePlatform').value;
162
    newRepo.description = this.group.get('repoDescription').value;
163
    newRepo.registeredBy = this.authService.userEmail;
164
    newRepo.datasourceType = 'aggregator';
165
    newRepo.registered = true;
166

    
167
    this.emittedInfo.emit(newRepo);
168

    
169
    return newRepo;
170
  }
171

    
172
}
(2-2/8)