Project

General

Profile

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

    
5
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
6
import {
7
  formErrorInvalidFields, formErrorRequiredFields, formErrorWasntSaved, formSubmitting, 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, Timezone, Typology } 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
  aggregatorTypeDesc,
33
  adminEmailDesc
34
} from '../../../domain/oa-description';
35
import { ValidatorService } from '../../../services/validator.service';
36
import { AuthenticationService } from '../../../services/authentication.service';
37
import { ActivatedRoute } from '@angular/router';
38

    
39
@Component ({
40
  selector: 'datasource-create-form',
41
  templateUrl: './datasource-create-form.component.html'
42
})
43

    
44
export class DatasourceCreateFormComponent implements OnInit {
45
  errorMessage: string;
46
  successMessage: string;
47
  loadingMessage: string;
48

    
49
  typologies: Typology[] = [];
50
  timezones: Timezone[] = [];
51
  countries: Country[] = [];
52
  datasourceClasses: Map<string,string> = new Map<string,string>();
53
  classCodes: string[] = [];
54

    
55
  mode: string;
56

    
57
  @Output() emittedInfo: EventEmitter<Repository> = new EventEmitter();
58

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

    
79
  softwarePlatformDesc : Description = softwarePlatformDesc;
80
  officialNameDesc : Description = officialNameDesc;
81
  issnDesc : Description = issnDesc;
82
  eissnDesc : Description = eissnDesc;
83
  lissnDesc : Description = lissnDesc;
84
  repoDescriptionDesc : Description = repoDescriptionDesc;
85
  countryDesc : Description = countryDesc;
86
  longtitudeDesc : Description = longtitudeDesc;
87
  latitudeDesc : Description = latitudeDesc;
88
  websiteUrlDesc : Description = websiteUrlDesc;
89
  institutionNameDesc : Description = institutionNameDesc;
90
  englishNameDesc : Description = englishNameDesc;
91
  logoUrlDesc : Description = logoUrlDesc;
92
  timezoneDesc : Description = timezoneDesc;
93
  datasourceTypeDesc : Description;
94
  adminEmailDesc : Description = adminEmailDesc;
95

    
96
  constructor(
97
    private fb: FormBuilder,
98
    private route: ActivatedRoute,
99
    private repoService: RepositoryService,
100
    private authService: AuthenticationService
101
  ) {}
102

    
103
  ngOnInit() {
104
    this.loadForm();
105
  }
106

    
107
  loadForm(){
108
    this.mode = this.route.snapshot.url[0].path.toString();
109
    if (this.mode == 'journal') {
110
      this.datasourceTypeDesc = journalTypeDesc;
111
    } else if (this.mode == 'aggregator') {
112
      this.datasourceTypeDesc = aggregatorTypeDesc;
113
    }
114
    console.log(this.mode);
115
    this.group = this.fb.group(this.groupDefinition);
116
    this.getTypologies();
117
    this.getTimezones();
118
    this.getCountries();
119
    this.getDatasourceClasses();
120
  }
121

    
122
  getCountries(){
123
    this.repoService.getCountries()
124
      .subscribe(
125
        countries => this.countries = countries.sort( function(a,b){
126
          if(a.name<b.name){
127
            return -1;
128
          } else if(a.name>b.name){
129
            return 1;
130
          } else {
131
            return 0;
132
          }
133
        } ),
134
        error => {
135
          this.errorMessage = noServiceMessage;
136
          console.log(error);
137
        });
138
  }
139

    
140
  getDatasourceClasses() {
141
    this.repoService.getDatasourceClasses(this.mode).subscribe(
142
      classes => this.datasourceClasses = classes,
143
      error => {
144
        this.errorMessage = noServiceMessage;
145
        console.log(error);
146
      },
147
      () => {
148
        for (let key in this.datasourceClasses){
149
          this.classCodes.push(key);
150
        }
151
      }
152
    );
153
  }
154

    
155
  getTypologies() {
156
    this.repoService.getTypologies().subscribe(
157
      types => this.typologies = types,
158
      error => console.log(error)
159
    );
160
  }
161

    
162
  getTimezones() {
163
    this.repoService.getTimezones().subscribe(
164
      zones => this.timezones = zones,
165
      error => console.log(error)
166
    );
167
  }
168

    
169
  registerDatasource(): boolean {
170
    let result: boolean;
171

    
172
    this.errorMessage = '';
173
    this.successMessage = '';
174

    
175
    if (this.group.valid) {
176
      if ( this.mode != 'journal' || this.group.get('issn').value ) {
177
        let newRepo = this.createNewRepository();
178
        this.loadingMessage = formSubmitting;
179
        this.errorMessage = '';
180
        this.repoService.addRepository(newRepo.datasourceType, newRepo).subscribe(
181
          response => {
182
            console.log(`addRepository responded:\n${JSON.stringify(response)}`);
183
            if (response) {
184
              result = true;
185
              this.emittedInfo.emit(response);
186
            }
187
          },
188
          error => {
189
            console.log(error);
190
            this.loadingMessage = '';
191
            this.errorMessage = formErrorWasntSaved;
192
            result = false;
193
          },
194
          () => {
195
            this.loadingMessage = '';
196
            if (result) {
197
              this.successMessage = formSuccessRegisteredDatasource;
198
            } else {
199
              this.errorMessage = formErrorWasntSaved;
200
            }
201
          }
202
        );
203
      } else {
204
        result = false;
205
      }
206
    } else {
207
      this.errorMessage = formErrorRequiredFields;
208
      result = false;
209
    }
210
    return result;
211
  }
212

    
213
  createNewRepository(): Repository {
214
    let newRepo: Repository = new Repository();
215
    newRepo.officialName = this.group.get('officialName').value;
216
    newRepo.englishName = this.group.get('englishName').value;
217
    newRepo.websiteUrl = this.group.get('websiteUrl').value;
218
    newRepo.logoUrl = this.group.get('logoUrl').value;
219
    newRepo.contactEmail = this.group.get('adminEmail').value;
220
    newRepo.countryName = this.countries.filter(x => x.code == this.group.get('country').value)[0].name;
221
    newRepo.countryCode = this.group.get('country').value;
222
    newRepo.organization = this.group.get('institutionName').value;
223
    newRepo.latitude = this.group.get('latitude').value;
224
    newRepo.longitude = this.group.get('longtitude').value;
225
    newRepo.timezone = this.group.get('timezone').value;
226
    newRepo.datasourceClass = this.group.get('datasourceType').value;
227
    newRepo.typology = this.group.get('softwarePlatform').value;
228
    newRepo.description = this.group.get('repoDescription').value;
229
    newRepo.issn = this.group.get('issn').value;
230
    newRepo.eissn = this.group.get('eissn').value;
231
    newRepo.lissn = this.group.get('lissn').value;
232
    newRepo.registeredBy = this.authService.getUserEmail();
233
    newRepo.datasourceType = this.mode;
234

    
235
    newRepo.dateOfCreation = new Date(Date.now()); //is this correct ?????
236
    newRepo.registered = true;
237

    
238
    return newRepo;
239
  }
240

    
241

    
242
}
(2-2/6)