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 {
7
  formErrorInvalidFields, formErrorRequiredFields, formErrorWasntSaved, formSuccessRegisteredDatasource,
8
  noServiceMessage
9
} from '../../../domain/shared-messages';
10
import { RepositoryService } from "../../../services/repository.service";
11
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
12
import { Country, Repository } from '../../../domain/typeScriptClasses';
13
import { typologies } from '../../../domain/typologies';
14
import { timezones } from '../../../domain/timezones';
15
import {
16
  Description,
17
  softwarePlatformDesc,
18
  officialNameDesc,
19
  issnDesc,
20
  eissnDesc,
21
  lissnDesc,
22
  repoDescriptionDesc,
23
  countryDesc,
24
  longtitudeDesc,
25
  latitudeDesc,
26
  websiteUrlDesc,
27
  institutionNameDesc,
28
  englishNameDesc,
29
  logoUrlDesc,
30
  timezoneDesc,
31
  journalTypeDesc,
32
  adminEmailDesc
33
} from '../../../domain/oa-description';
34
import { ValidatorService } from '../../../services/validator.service';
35
import { AuthenticationService } from '../../../services/authentication.service';
36

    
37
@Component ({
38
  selector: 'journal-info-form',
39
  templateUrl: 'journal-info-form.component.html'
40
})
41

    
42
export class JournalInfoFormComponent implements OnInit {
43
  errorMessage: string;
44
  successMessage: string;
45
  loadingMessage: string;
46

    
47
  typologies = typologies;
48
  timezones = timezones;
49
  countries: Country[] = [];
50
  datasourceClasses: Map<string,string> = new Map<string,string>();
51
  classCodes: string[] = [];
52

    
53
  @Output() emittedInfo: EventEmitter<Repository> = new EventEmitter();
54

    
55
  group: FormGroup;
56
  readonly groupDefinition = {
57
    softwarePlatform : '',
58
    officialName : ['', Validators.required],
59
    issn : ['', [Validators.required, Validators.minLength(8), Validators.maxLength(8)]],
60
    eissn : ['', [Validators.minLength(8), Validators.maxLength(8)]],
61
    lissn : ['', [Validators.minLength(8), Validators.maxLength(8)]],
62
    repoDescription : ['', Validators.required],
63
    country : ['', Validators.required],
64
    longtitude : ['', [Validators.required, Validators.maxLength(9), Validators.min(-180), Validators.max(180)] ],
65
    latitude : ['', [Validators.required, Validators.maxLength(9), Validators.min(-90), Validators.max(90)] ],
66
    websiteUrl : ['', Validators.required],
67
    institutionName : ['', Validators.required],
68
    englishName: ['', Validators.required],
69
    logoUrl: '',
70
    timezone: ['', Validators.required],
71
    journalType: ['', Validators.required],
72
    adminEmail: ['', [Validators.required, Validators.email] ]
73
  };
74

    
75
  softwarePlatformDesc : Description = softwarePlatformDesc;
76
  officialNameDesc : Description = officialNameDesc;
77
  issnDesc : Description = issnDesc;
78
  eissnDesc : Description = eissnDesc;
79
  lissnDesc : Description = lissnDesc;
80
  repoDescriptionDesc : Description = repoDescriptionDesc;
81
  countryDesc : Description = countryDesc;
82
  longtitudeDesc : Description = longtitudeDesc;
83
  latitudeDesc : Description = latitudeDesc;
84
  websiteUrlDesc : Description = websiteUrlDesc;
85
  institutionNameDesc : Description = institutionNameDesc;
86
  englishNameDesc : Description = englishNameDesc;
87
  logoUrlDesc : Description = logoUrlDesc;
88
  timezoneDesc : Description = timezoneDesc;
89
  journalTypeDesc : Description = journalTypeDesc;
90
  adminEmailDesc : Description = adminEmailDesc;
91

    
92
  constructor(
93
    private fb: FormBuilder,
94
    private repoService: RepositoryService,
95
    private authService: AuthenticationService
96
  ) {}
97

    
98
  ngOnInit() {
99
    this.loadForm();
100
  }
101

    
102
  loadForm(){
103
    this.group = this.fb.group(this.groupDefinition);
104
    this.getCountries();
105
    this.getDatasourceClasses();
106
  }
107

    
108
  getCountries(){
109
    this.repoService.getCountries()
110
      .subscribe(
111
        countries => this.countries = countries.sort( function(a,b){
112
          if(a.name<b.name){
113
            return -1;
114
          } else if(a.name>b.name){
115
            return 1;
116
          } else {
117
            return 0;
118
          }
119
        } ),
120
        error => {
121
          this.errorMessage = noServiceMessage;
122
          console.log(error);
123
        });
124
  }
125

    
126
  getDatasourceClasses() {
127
    this.repoService.getDatasourceClasses('journal').subscribe(
128
      classes => this.datasourceClasses = classes,
129
      error => {
130
        this.errorMessage = noServiceMessage;
131
        console.log(error);
132
      },
133
      () => {
134
        for (let key in this.datasourceClasses){
135
          this.classCodes.push(key);
136
        }
137
      }
138
    );
139
  }
140

    
141
  registerDatasource(): boolean {
142
    if(this.group.valid){
143
      let newRepo = this.createNewRepository();
144
      this.repoService.addRepository('journal',newRepo).subscribe(
145
        response => console.log(`${JSON.stringify(response)}`),
146
        error => console.log(error)
147
      );
148
      this.successMessage = formSuccessRegisteredDatasource;
149
        this.errorMessage = '';
150
        return true;
151
    } else {
152
      this.errorMessage = formErrorRequiredFields;
153
      return false;
154
    }
155
  }
156

    
157
  updateEnglishName(id: string, name: string) {
158
    let status: boolean;
159
    this.repoService.updateEnglishName(id,name).subscribe(
160
      response => {
161
        console.log(`answered ${response}`);
162
        status = true;
163
      },
164
      error => {
165
        console.log(error);
166
        this.errorMessage = formErrorWasntSaved;
167
        status = false;
168
      }
169
    );
170
    console.log(status);
171
    return status;
172
  }
173

    
174
  createNewRepository(): Repository {
175
    let newRepo: Repository = new Repository();
176
    newRepo.dateOfCreation = new Date(Date.now());
177
    newRepo.officialName = this.group.get('officialName').value;
178
    newRepo.englishName = this.group.get('englishName').value;
179
    newRepo.websiteUrl = this.group.get('websiteUrl').value;
180
    newRepo.logoUrl = this.group.get('logoUrl').value;
181
    newRepo.contactEmail = this.group.get('adminEmail').value;
182
    newRepo.countryName = this.countries.filter(x => x.code == this.group.get('country').value)[0].name;
183
    newRepo.countryCode = this.group.get('country').value;
184
    newRepo.organization = this.group.get('institutionName').value;
185
    newRepo.latitude = this.group.get('latitude').value;
186
    newRepo.longitude = this.group.get('longtitude').value;
187
    newRepo.timezone = this.group.get('timezone').value;
188
    newRepo.datasourceClass = this.group.get('journalType').value;
189
    newRepo.typology = this.group.get('softwarePlatform').value;
190
    newRepo.description = this.group.get('repoDescription').value;
191
    newRepo.issn = this.group.get('issn').value;
192
    newRepo.eissn = this.group.get('eissn').value;
193
    newRepo.lissn = this.group.get('lissn').value;
194
    newRepo.registeredBy = this.authService.userEmail;
195
    newRepo.datasourceType = 'journal';
196
    newRepo.registered = true;
197

    
198
    this.emittedInfo.emit(newRepo);
199

    
200
    return newRepo;
201
  }
202

    
203

    
204
}
(8-8/8)