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, datasourceTypeDesc
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
  @Input() selectedRepo: Repository;
60

    
61
  formSubmitted: boolean = false;
62
  group: FormGroup;
63
  readonly groupDefinition = {
64
    softwarePlatform : ['', Validators.required],
65
    officialName : ['', Validators.required],
66
    issn : ['', [Validators.pattern('^\\d\\d\\d\\d[-]\\d\\d\\d\\d$')] ],
67
    eissn : ['', Validators.pattern('^\\d\\d\\d\\d[-]\\d\\d\\d\\d$') ],
68
    lissn : ['', Validators.pattern('^\\d\\d\\d\\d[-]\\d\\d\\d\\d$') ],
69
    repoDescription : ['', Validators.required],
70
    country : ['', Validators.required],
71
    longtitude : ['', [Validators.required, Validators.min(-180), Validators.max(180)] ],
72
    latitude : ['', [Validators.required, Validators.min(-90), Validators.max(90)] ],
73
    websiteUrl : ['', [Validators.required, Validators.pattern('^(http:\\/\\/www\\.|https:\\/\\/www\\.|http:\\/\\/|https:\\/\\/)?[a-z0-9]+([\\-\\.]{1}[a-z0-9]+)*\\.[a-z]{2,5}(:[0-9]{1,5})?(\\/.*)?$')] ],
74
    institutionName : ['', Validators.required],
75
    englishName: ['', Validators.required],
76
    logoUrl: ['', Validators.pattern('^(http:\\/\\/www\\.|https:\\/\\/www\\.|http:\\/\\/|https:\\/\\/)?[a-z0-9]+([\\-\\.]{1}[a-z0-9]+)*\\.[a-z]{2,5}(:[0-9]{1,5})?(\\/.*)?$') ],
77
    timezone: ['', Validators.required],
78
    datasourceType: ['', Validators.required],
79
    adminEmail: ['', [Validators.required, Validators.email] ]
80
  };
81

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

    
99
  constructor(
100
    private fb: FormBuilder,
101
    private route: ActivatedRoute,
102
    private repoService: RepositoryService,
103
    private authService: AuthenticationService
104
  ) {}
105

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

    
110
  loadForm(){
111
    this.mode = this.route.snapshot.url[0].path.toString();
112
    console.log(`my mode is ${this.mode}`);
113

    
114
    console.log(this.mode);
115
    this.group = this.fb.group(this.groupDefinition);
116
    if (this.mode == 'journal') {
117
      this.group.get('issn').setValidators([Validators.required, Validators.pattern('^\\d\\d\\d\\d[-]\\d\\d\\d\\d$')]);
118
    }
119
    this.getTypologies();
120
    this.getTimezones();
121
    this.getCountries();
122
    this.getDatasourceClasses();
123

    
124
    if (this.selectedRepo) {
125
      this.setupForm();
126
    }
127
  }
128

    
129
  setupForm() {
130
    if (this.selectedRepo) {
131
      console.log(`my datasource type is: ${this.selectedRepo.datasourceType}`);
132

    
133
      this.group.setValue({
134
        softwarePlatform: this.selectedRepo.typology,
135
        officialName: this.selectedRepo.officialName,
136
        issn: '',
137
        eissn: '',
138
        lissn: '',
139
        repoDescription: this.selectedRepo.description,
140
        country: this.selectedRepo.countryCode,
141
        longtitude: this.selectedRepo.longitude,
142
        latitude: this.selectedRepo.latitude,
143
        websiteUrl: this.selectedRepo.websiteUrl,
144
        institutionName: this.selectedRepo.organization,
145
        englishName: this.selectedRepo.englishName,
146
        logoUrl: this.selectedRepo.logoUrl,
147
        timezone: this.selectedRepo.timezone,
148
        datasourceType: this.selectedRepo.datasourceClass,
149
        adminEmail: this.selectedRepo.contactEmail
150
      });
151

    
152
      if (this.selectedRepo.datasourceType == 'journal') {
153

    
154
        let ssnToShow = this.selectedRepo.issn.slice(0, 4)+ '-' + this.selectedRepo.issn.toString().slice(4);
155
        this.group.get('issn').setValue(ssnToShow);
156

    
157
        if (this.selectedRepo.eissn.trim().length) {
158
          ssnToShow = this.selectedRepo.eissn.slice(0, 4)+ '-' + this.selectedRepo.eissn.toString().slice(4);
159
          this.group.get('eissn').setValue(ssnToShow);
160
        }
161

    
162
        if (this.selectedRepo.lissn.trim().length) {
163
          ssnToShow = this.selectedRepo.lissn.slice(0, 4)+ '-' + this.selectedRepo.lissn.toString().slice(4);
164
          this.group.get('lissn').setValue(ssnToShow);
165
        }
166

    
167
      }
168
    }
169
  }
170

    
171
  getCountries(){
172
    this.repoService.getCountries()
173
      .subscribe(
174
        countries => this.countries = countries.sort( function(a,b){
175
          if(a.name<b.name){
176
            return -1;
177
          } else if(a.name>b.name){
178
            return 1;
179
          } else {
180
            return 0;
181
          }
182
        } ),
183
        error => {
184
          this.errorMessage = noServiceMessage;
185
          console.log(error);
186
        });
187
  }
188

    
189
  getDatasourceClasses() {
190
    this.repoService.getDatasourceClasses(this.mode).subscribe(
191
      classes => this.datasourceClasses = classes,
192
      error => {
193
        this.errorMessage = noServiceMessage;
194
        console.log(error);
195
      },
196
      () => {
197
        for (let key in this.datasourceClasses){
198
          this.classCodes.push(key);
199
        }
200
      }
201
    );
202
  }
203

    
204
  getTypologies() {
205
    this.repoService.getTypologies().subscribe(
206
      types => this.typologies = types,
207
      error => console.log(error)
208
    );
209
  }
210

    
211
  getTimezones() {
212
    this.repoService.getTimezones().subscribe(
213
      zones => this.timezones = zones,
214
      error => console.log(error)
215
    );
216
  }
217

    
218
  registerDatasource() {
219
    this.formSubmitted = true;
220
    this.errorMessage = '';
221
    this.successMessage = '';
222

    
223
    if (this.group.valid) {
224
      if ( this.mode != 'journal' || this.group.get('issn').value ) {
225
        if (this.selectedRepo) {
226
          this.emittedInfo.emit(this.selectedRepo);
227
        } else {
228
          let newRepo = this.createNewRepository();
229
          this.emittedInfo.emit(newRepo);
230
        }
231
      } else {
232
        this.errorMessage = formErrorRequiredFields;
233
      }
234
    } else {
235
      this.errorMessage = formErrorRequiredFields;
236
    }
237
  }
238

    
239
  createNewRepository(): Repository {
240
    let newRepo: Repository = new Repository();
241
    newRepo.officialName = this.group.get('officialName').value;
242
    newRepo.englishName = this.group.get('englishName').value;
243
    newRepo.websiteUrl = this.group.get('websiteUrl').value;
244
    newRepo.logoUrl = this.group.get('logoUrl').value;
245
    newRepo.contactEmail = this.group.get('adminEmail').value;
246
    newRepo.countryName = this.countries.filter(x => x.code == this.group.get('country').value)[0].name;
247
    newRepo.countryCode = this.group.get('country').value;
248
    newRepo.organization = this.group.get('institutionName').value;
249
    newRepo.latitude = this.group.get('latitude').value;
250
    newRepo.longitude = this.group.get('longtitude').value;
251
    newRepo.timezone = this.group.get('timezone').value;
252
    newRepo.datasourceClass = this.group.get('datasourceType').value;
253
    newRepo.typology = this.group.get('softwarePlatform').value;
254
    newRepo.description = this.group.get('repoDescription').value;
255
    newRepo.issn = '';
256
    newRepo.eissn = '';
257
    newRepo.lissn = '';
258

    
259
    if ( this.group.get('issn').value ){
260
      let ssnParts = this.group.get('issn').value.split('-');
261
      let correctSSN = ssnParts[0]+ssnParts[1];
262
      newRepo.issn = correctSSN;
263
      if ( this.group.get('eissn').value ) {
264
        ssnParts = this.group.get('eissn').value.split('-');
265
        correctSSN = ssnParts[0]+ssnParts[1];
266
        newRepo.eissn = correctSSN;
267
      }
268
      if ( this.group.get('lissn').value ) {
269
        ssnParts = this.group.get('lissn').value.split('-');
270
        correctSSN = ssnParts[0]+ssnParts[1];
271
        newRepo.lissn = correctSSN;
272
      }
273
    }
274

    
275
    newRepo.registeredBy = this.authService.getUserEmail();
276

    
277
    /* THE BELOW FIELDS ARE NOT SET IN GWT CODE*/
278
    newRepo.datasourceType = this.mode;
279
    newRepo.dateOfCreation = new Date(Date.now()); // NOT NEEDED ??
280
    newRepo.registered = true;
281
    newRepo.registrationDate = new Date(Date.now()); // NOT NEEDED ??
282

    
283
    return newRepo;
284
  }
285

    
286

    
287
}
(2-2/6)