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.pattern('^\\d\\d\\d\\d[-]\\d\\d\\d\\d$')] ],
64
    eissn : ['', Validators.pattern('^\\d\\d\\d\\d[-]\\d\\d\\d\\d$') ],
65
    lissn : ['', Validators.pattern('^\\d\\d\\d\\d[-]\\d\\d\\d\\d$') ],
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() {
170
    this.errorMessage = '';
171
    this.successMessage = '';
172

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

    
206
  createNewRepository(): Repository {
207
    let newRepo: Repository = new Repository();
208
    newRepo.officialName = this.group.get('officialName').value;
209
    newRepo.englishName = this.group.get('englishName').value;
210
    newRepo.websiteUrl = this.group.get('websiteUrl').value;
211
    newRepo.logoUrl = this.group.get('logoUrl').value;
212
    newRepo.contactEmail = this.group.get('adminEmail').value;
213
    newRepo.countryName = this.countries.filter(x => x.code == this.group.get('country').value)[0].name;
214
    newRepo.countryCode = this.group.get('country').value;
215
    newRepo.organization = this.group.get('institutionName').value;
216
    newRepo.latitude = this.group.get('latitude').value;
217
    newRepo.longitude = this.group.get('longtitude').value;
218
    newRepo.timezone = this.group.get('timezone').value;
219
    newRepo.datasourceClass = this.group.get('datasourceType').value;
220
    newRepo.typology = this.group.get('softwarePlatform').value;
221
    newRepo.description = this.group.get('repoDescription').value;
222

    
223
    if ( this.group.get('issn').value ){
224
      let ssnParts = this.group.get('issn').value.split('-');
225
      let correctSSN = ssnParts[0]+ssnParts[1];
226
      newRepo.issn = correctSSN;
227
      if ( this.group.get('eissn').value ) {
228
        ssnParts = this.group.get('eissn').value.split('-');
229
        correctSSN = ssnParts[0]+ssnParts[1];
230
        newRepo.eissn = correctSSN;
231
      }
232
      if ( this.group.get('lissn').value ) {
233
        ssnParts = this.group.get('lissn').value.split('-');
234
        correctSSN = ssnParts[0]+ssnParts[1];
235
        newRepo.lissn = correctSSN;
236
      }
237
    }
238

    
239
    newRepo.registeredBy = this.authService.getUserEmail();
240

    
241
    /* THE BELOW FIELDS ARE NOT SET IN GWT CODE*/
242
    newRepo.datasourceType = this.mode;
243
    /*newRepo.dateOfCreation = new Date(Date.now());*/ // NOT NEEDED
244
    newRepo.registered = true;
245
    /*newRepo.registrationDate = new Date(Date.now());*/ // NOT NEEDED
246

    
247
    return newRepo;
248
  }
249

    
250

    
251
}
(2-2/6)