Project

General

Profile

1
import {Component, Input, ViewChild} from '@angular/core';
2
import {AlertModal} from '../../../utils/modal/alert';
3
import {ClaimEntity, ClaimsErrorMessage, Message, ShowOptions} from '../../claim-utils/claimHelper.class';
4
import {EnvProperties} from "../../../utils/properties/env-properties";
5
import {Dates} from "../../../utils/string-utils.class";
6
import {HelperFunctions} from "../../../utils/HelperFunctions.class";
7

    
8
@Component({
9
  selector: 'metadata-preview',
10
  templateUrl: 'metadataPreview.component.html'
11

    
12
})
13
export class MetadataPreviewComponent {
14

    
15
  showSources = true;
16
  showLinksTo = true;
17
  showErrors = true;
18
  showWarnings = true;
19
  @Input() results: ClaimEntity[];
20
  @Input() sources: ClaimEntity[];
21
  @Input() title: string = "Research Results";
22
  @Input() bulkMode: boolean = false;
23
  @Input() inlineEntity: ClaimEntity = null;
24
  @Input() showOptions:ShowOptions;
25
  @Input() public properties: EnvProperties;
26
  @Input() pageContents;
27
  @Input() defaultColors:boolean=true;
28
  @ViewChild(AlertModal) alertApplyAll;
29
  @Input() localStoragePrefix: string = "";
30
  errors:ClaimsErrorMessage[] = [];
31
  warnings:Message[] = [];
32
  public commonAccessRights = "OPEN"; // for access rights- changes when user apply a change to every entity
33
  public commonEmbargoEndDate; // for access rights: embargoEndDate - changes when user apply a change to every entity
34
  public commonType; // for research entity type - changes when user apply a change to every entity
35
  public applyToResults: boolean; // true apply to results, false apply to sources
36
  public typeChanged: boolean = true; //
37
  accessTypes = ["OPEN", "CLOSED", "EMBARGO", "RESTRICTED"];
38
  embargoEndDates = [];
39

    
40
  ngOnInit() {
41
    this.setEmbargoEndDates();
42
    if (this.inlineEntity) {
43
      this.showSources = true;
44
      this.showLinksTo = true;
45
    }
46
    this.runValidation(true);
47
  }
48
  setEmbargoEndDates(){
49
    for(let entity of this.sources){
50
      if(entity.result){
51
        this.embargoEndDates[entity.id+"_source"] = MetadataPreviewComponent.getDate(entity.result.embargoEndDate);
52
        // entity.result.accessRights = "EMBARGO";
53
      }
54
    }
55
    for(let entity of this.results){
56
      if(entity.result){
57
        this.embargoEndDates[entity.id+"_result"] = MetadataPreviewComponent.getDate(entity.result.embargoEndDate);
58
      }
59
    }
60
  }
61
  private static getDate(date:string){
62
    let myDate = new Date();
63
    if(date){
64
       myDate = new Date(date);
65
    }
66
    return myDate;
67
  }
68

    
69
 /* removePublication(item: ClaimEntity) {
70
    var index: number = this.results.indexOf(item);
71
    if (index > -1) {
72
      this.results.splice(index, 1);
73
      if (this.results != null) {
74
        localStorage.setItem(this.localStoragePrefix, JSON.stringify(this.results));
75
      }
76
    }
77

    
78
  }*/
79

    
80
  onDateChanged(event: any, item: ClaimEntity, applyToResults) {
81
    item.result.embargoEndDate = Dates.getDateToString(event.value);
82
    this.embargoEndDates [item.id+"_"+(applyToResults?'result':'source')] = event.value;
83
    if ((applyToResults && this.results.length > 1) || (!applyToResults && this.sources.length > 1)) {
84
      this.commonAccessRights = "EMBARGO";
85
      this.commonEmbargoEndDate = item.result.embargoEndDate;
86
      this.applyToResults = applyToResults;
87
      this.confirmOpen(false);
88
    }
89
    this.updateLocalStorage();
90
    this.runValidation(true);
91

    
92
  }
93

    
94
  onTypeChanged(event: any, item: ClaimEntity, applyToResults) {
95
    item.type = (event);
96
    if ((applyToResults && this.results.length > 1) || (!applyToResults && this.sources.length > 1)) {
97
      this.commonType = item.type;
98
      this.applyToResults = applyToResults;
99
      this.confirmOpen(true );
100
    }
101
    this.updateLocalStorage();
102
  }
103

    
104
  //  resultsChanged($event) {
105
  //    this.results=$event.value;
106
  //    this.resultsChange.emit({
107
  //      value: this.results
108
  //    });
109
  //  }
110
  /* The following methods:
111
     *typeChanged
112
     *confirmOpen
113
     *confirmClose
114
   implement the functionality: change accessRights of a publication - apply to all if asked */
115
  accessRightsTypeChanged(type: any, item: any, applyToResults) {
116
    item.accessRights = type;
117
    if ((applyToResults && this.results.length > 1) || (!applyToResults && this.sources.length > 1)) {
118
      this.commonAccessRights = type;
119
      this.applyToResults = applyToResults;
120
      if (this.commonAccessRights != "EMBARGO") {
121
        this.commonEmbargoEndDate = item.embargoEndDate;
122
        this.confirmOpen(false );
123
      }
124
    }
125
    this.runValidation(true);
126

    
127
    this.updateLocalStorage();
128

    
129
  }
130

    
131
  confirmOpen(type: boolean) {
132
    this.typeChanged = type;
133
    this.alertApplyAll.cancelButton = true;
134
    this.alertApplyAll.okButton = true;
135
    this.alertApplyAll.alertTitle = "Change metadata";
136
    this.alertApplyAll.message = "Do you wish to apply the change to every entity?";
137
    this.alertApplyAll.okButtonText = "Yes";
138
    this.alertApplyAll.cancelButtonText = "No";
139
    this.alertApplyAll.open();
140
  }
141

    
142
  confirmClose() {
143
    if (this.typeChanged) {
144
      if (this.applyToResults) {
145
        for (let i = 0; i < this.results.length; i++) {
146
          if (this.results[i].result.source != 'openaire' && this.results[i].type != 'project' && this.results[i].type != 'community') {
147
            this.results[i].type = this.commonType;
148
          }
149
        }
150
      } else {
151
        for (let i = 0; i < this.sources.length; i++) {
152
          if (this.sources[i].result.source != 'openaire' && this.sources[i].type != 'project' && this.sources[i].type != 'community') {
153
            this.sources[i].type = this.commonType;
154
          }
155
        }
156
      }
157

    
158
    } else {
159
      if (this.applyToResults) {
160
        for (let i = 0; i < this.results.length; i++) {
161
          if (this.results[i].result.source != 'openaire' && this.results[i].type != 'project' && this.results[i].type != 'community') {
162
            this.results[i].result.accessRights = this.commonAccessRights;
163
            if (this.commonAccessRights == "EMBARGO") {
164
              this.results[i].result.embargoEndDate = this.commonEmbargoEndDate;
165
            }
166
          }
167
        }
168
      } else {
169
        for (let i = 0; i < this.sources.length; i++) {
170
          if (this.sources[i].result.source != 'openaire' && this.sources[i].type != 'project' && this.sources[i].type != 'community') {
171
            this.sources[i].result.accessRights = this.commonAccessRights;
172
            if (this.commonAccessRights == "EMBARGO") {
173
              this.sources[i].result.embargoEndDate = this.commonEmbargoEndDate;
174
            }
175
          }
176
        }
177
      }
178
      this.setEmbargoEndDates();
179
    }
180
    this.updateLocalStorage();
181
    this.runValidation(true)
182
  }
183

    
184
  remove(item: any, applyToResults: boolean) {
185
    if (applyToResults) {
186
      let index: number = this.results.indexOf(item);
187
      item.warningMessages.splice(0, item.warningMessages.length);
188
      if (index > -1) {
189
        this.results.splice(index, 1);
190
        localStorage.setItem(this.localStoragePrefix + 'results', JSON.stringify(this.results));
191
      }
192
    } else {
193
      let index: number = this.sources.indexOf(item);
194
      item.warningMessages.splice(0, item.warningMessages.length);
195
      if (index > -1) {
196
        this.sources.splice(index, 1);
197
        localStorage.setItem(this.localStoragePrefix + 'sources', JSON.stringify(this.sources));
198

    
199
      }
200
    }
201
    this.updateLocalStorage();
202
    this.runValidation(true);
203
  }
204

    
205
  addStringToNumber(str: string, num: number) {
206
    return (+str) + num;
207
  }
208

    
209
  runValidation(cleanErrors:boolean) {
210
    this.warnings = [];
211
    if(cleanErrors){
212
      this.errors = [];
213
    }
214
    if (this.inlineEntity) {
215
      this.inlineEntity.warningMessages = [];
216
      if(cleanErrors){
217
        this.inlineEntity.errorMessages = [];
218
      }
219

    
220
    }
221
    this.validateEmbargoDate(this.sources, cleanErrors);
222
    this.validateEmbargoDate(this.results, cleanErrors);
223
    this.validateProjectDuration();
224

    
225
  }
226

    
227
  validateProjectDuration() {
228
    for (let item of this.results) {
229
      if (item.type == 'project' && item.project) {
230
        for (let result of this.sources) {
231
          if (result.result && result.result.date) {
232
            if ((item.project.startDate && result.result.date < item.project.startDate) || (item.project.endDate && +result.result.date > (5 + +item.project.endDate))) {
233
              const message: Message = {
234
                type: 'projectDuration',
235
                resultId: result.id,
236
                resultTitle: result.title,
237
                projectId: item.id,
238
                projectInfo: {title: item.title, startDate: item.project.startDate, endDate: item.project.endDate}
239
              };
240
              result.warningMessages.push(message);
241
              this.warnings.push(message);
242
            }
243
          }
244
        }
245
        if (this.inlineEntity && this.inlineEntity.result) {
246
          if (this.inlineEntity.result.date) {
247
            if ((item.project.startDate && this.inlineEntity.result.date < item.project.startDate) || (item.project.endDate && +this.inlineEntity.result.date > (5 + +item.project.endDate))) {
248
              const message: Message = {
249
                type: 'projectDuration',
250
                resultId: this.inlineEntity.id,
251
                resultTitle: this.inlineEntity.title,
252
                projectId: item.id,
253
                projectInfo: {title: item.title, startDate: item.project.startDate, endDate: item.project.endDate}
254
              };
255
              this.inlineEntity.warningMessages.push(message);
256
              this.warnings.push(message);
257
            }
258
          }
259
        }
260
      }
261
    }
262
      if (this.inlineEntity && this.inlineEntity.project) {
263
        for (let result of this.results) {
264
          if (result.result && result.result.date) {
265
            if ((this.inlineEntity.project.startDate && result.result.date < this.inlineEntity.project.startDate) || (this.inlineEntity.project.endDate && +result.result.date > (5 + +this.inlineEntity.project.endDate))) {
266
              const message: Message = {
267
                type: 'projectDuration',
268
                resultId: result.id,
269
                resultTitle: result.title,
270
                projectId: this.inlineEntity.id,
271
                projectInfo: {title: this.inlineEntity.title, startDate: this.inlineEntity.project.startDate, endDate: this.inlineEntity.project.endDate}
272
              };
273
              result.warningMessages.push(message);
274
              this.warnings.push(message);
275
            }
276
          }
277
        }
278
      }
279

    
280

    
281
  }
282

    
283
  validateEmbargoDate(sources: ClaimEntity [],cleanErrors:boolean) {
284
    for (let entity of sources) {
285
      entity.warningMessages = [];
286
      if(cleanErrors){
287
        entity.errorMessages = [];
288
      }
289

    
290
      if(entity.result) {
291
        // console.log(entity.result.accessRights + " " + entity.result.date + " " + entity.result.embargoEndDate);
292
        if (entity.result.accessRights && entity.result.accessRights == 'EMBARGO' && entity.result.date && (entity.result.embargoEndDate) && entity.result.date > entity.result.embargoEndDate.substr(0, 4)) {
293
          let message: Message = new Message();
294
          message.type = "embargoEndDate";
295
          message.resultId = entity.id;
296
          message.resultTitle = entity.title;
297
          entity.warningMessages.push(message);
298
          this.warnings.push(message);
299
        }
300
      }
301

    
302
    }
303
  }
304

    
305

    
306
  getEmbargoEndDateMessage(result: ClaimEntity) {
307
    for (let message of result.warningMessages) {
308
      if (message.type == "embargoEndDate") {
309
        return "Embargo end date must be later than published date";
310
      }
311
    }
312
    return null;
313
  }
314
  updateLocalStorage(){
315
    if (this.sources != null) {
316
      localStorage.setItem(this.localStoragePrefix+"sources", JSON.stringify(this.sources));
317
    }
318
    if (this.results != null) {
319
      localStorage.setItem(this.localStoragePrefix+"results", JSON.stringify(this.results));
320
    }
321
  }
322

    
323
  goTo(show:string=null) {
324
    if(show && (['project','context', 'result']).indexOf(show)!=-1){
325
      this.showOptions.show = show;
326
      this.showOptions.basketShowSources=false;
327
      this.showOptions.basketShowLinksTo=true;
328
    }
329
    HelperFunctions.scroll();
330
  }
331

    
332
}
(6-6/14)