Project

General

Profile

1
import {Component, Input, ViewChild} from '@angular/core';
2
import {AlertModal} from '../../../utils/modal/alert';
3
import {ClaimEntity, Message} from '../../claim-utils/claimEntities.class';
4
import {IMyOptions} from '../../../utils/my-date-picker/interfaces/index';
5

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

    
10
})
11
export class MetadataPreviewComponent {
12

    
13
  showSources = true;
14
  showLinksTo = false;
15
  showErrors = true;
16
  showWarnings = true;
17
  @Input() results: ClaimEntity[];
18
  @Input() sources: ClaimEntity[];
19
  @Input() title: string = "Research Results";
20
  @Input() bulkMode: boolean = false;
21
  @Input() inlineEntity: ClaimEntity = null;
22

    
23

    
24
  // nextDate = {};
25
  @ViewChild(AlertModal) alertApplyAll;
26
  @Input() localStoragePrefix: string = "";
27
  errors = [];
28
  warnings = [];
29
  public commonAccessRights = "OPEN"; // for access rights- changes when user apply a change to every entity
30
  public commonEmbargoEndDate; // for access rights: embargoEndDate - changes when user apply a change to every entity
31
  public commonType; // for research entity type - changes when user apply a change to every entity
32
  public applyToResults: boolean; // true apply to results, false apply to sources
33
  public typeChanged: boolean = true; //
34
  accessTypes = ["OPEN", "CLOSED", "EMBARGO", "RESTRICTED"];
35
  public myDatePickerOptions: IMyOptions = {
36
    // other options...
37
    dateFormat: 'yyyy-mm-dd',
38
    selectionTxtFontSize: '15px',
39
    height: '28px',
40
    width: '100%',
41
    editableDateField: false,
42
    showClearDateBtn: false
43
  };
44
  embargoEndDates = [];
45

    
46
  ngOnInit() {
47
    // var myDate = new Date();
48
    // this.nextDate = {date: {year: myDate.getFullYear() , month: (myDate.getMonth() + 1), day: myDate.getDate()}};
49
    //2015-05-01
50
    this.setEmbargoEndDates();
51
    if (this.inlineEntity) {
52
      this.showSources = true;
53
      this.showLinksTo = true;
54
    }
55
    this.runValidation();
56

    
57

    
58
  }
59
  setEmbargoEndDates(){
60
    for(let entity of this.sources){
61
      if(entity.result){
62
        this.embargoEndDates[entity.id+"_source"] = MetadataPreviewComponent.getDate(entity.result.embargoEndDate);
63
        // entity.result.accessRights = "EMBARGO";
64
      }
65
    }
66
    for(let entity of this.results){
67
      if(entity.result){
68
        this.embargoEndDates[entity.id+"_result"] = MetadataPreviewComponent.getDate(entity.result.embargoEndDate);
69
      }
70
    }
71
  }
72
  private static getDate(date:string){
73

    
74
    let myDate ;
75
    if(date){
76
       myDate = new Date(date);
77
    }else{
78
      myDate = new Date();
79
    }
80
    return {date: {year: myDate.getFullYear() , month: (myDate.getMonth() + 1), day: myDate.getDate()}};
81
  }
82

    
83
 /* removePublication(item: ClaimEntity) {
84
    var index: number = this.results.indexOf(item);
85
    if (index > -1) {
86
      this.results.splice(index, 1);
87
      if (this.results != null) {
88
        localStorage.setItem(this.localStoragePrefix, JSON.stringify(this.results));
89
      }
90
    }
91

    
92
  }*/
93

    
94

    
95
  onDateChanged(event: any, item: ClaimEntity, applyToResults) {
96
    item.result.embargoEndDate = event.formatted;
97
    this.embargoEndDates [item.id+"_"+(applyToResults?'result':'source')] = MetadataPreviewComponent.getDate(item.result.embargoEndDate);
98
    if ((applyToResults && this.results.length > 1) || (!applyToResults && this.sources.length > 1)) {
99
      this.commonAccessRights = "EMBARGO";
100
      this.commonEmbargoEndDate = item.result.embargoEndDate;
101
      this.applyToResults = applyToResults;
102
      this.confirmOpen(false);
103
    }
104
    this.updateLocalStorage();
105
    this.runValidation();
106

    
107
  }
108

    
109
  onTypeChanged(event: any, item: ClaimEntity, applyToResults) {
110
    item.type = (event);
111
    if ((applyToResults && this.results.length > 1) || (!applyToResults && this.sources.length > 1)) {
112
      this.commonType = item.type;
113
      this.applyToResults = applyToResults;
114
      this.confirmOpen(true );
115
    }
116
    this.updateLocalStorage();
117
  }
118

    
119
  //  resultsChanged($event) {
120
  //    this.results=$event.value;
121
  //    this.resultsChange.emit({
122
  //      value: this.results
123
  //    });
124
  //  }
125
  /* The following methods:
126
     *typeChanged
127
     *confirmOpen
128
     *confirmClose
129
   implement the functionality: change accessRights of a publication - apply to all if asked */
130
  accessRightsTypeChanged(type: any, item: any, applyToResults) {
131
    item.accessRights = type;
132
    if ((applyToResults && this.results.length > 1) || (!applyToResults && this.sources.length > 1)) {
133
      this.commonAccessRights = type;
134
      this.applyToResults = applyToResults;
135
      if (this.commonAccessRights != "EMBARGO") {
136
        this.commonEmbargoEndDate = item.embargoEndDate;
137
        this.confirmOpen(false );
138
      }
139
    }
140
    this.runValidation();
141

    
142
    this.updateLocalStorage();
143

    
144
  }
145

    
146
  confirmOpen(type: boolean) {
147

    
148
    this.typeChanged = type;
149
    this.alertApplyAll.cancelButton = true;
150
    this.alertApplyAll.okButton = true;
151
    this.alertApplyAll.alertTitle = "Change metadata";
152
    this.alertApplyAll.message = "Do you wish to apply the change to every entity?";
153
    this.alertApplyAll.okButtonText = "Yes";
154
    this.alertApplyAll.cancelButtonText = "No";
155
    this.alertApplyAll.open();
156
  }
157

    
158
  confirmClose() {
159
    if (this.typeChanged) {
160
      if (this.applyToResults) {
161
        for (let i = 0; i < this.results.length; i++) {
162
          if (this.results[i].result.source != 'openaire' && this.results[i].type != 'project' && this.results[i].type != 'community') {
163
            this.results[i].type = this.commonType;
164
          }
165
        }
166
      } else {
167
        for (let i = 0; i < this.sources.length; i++) {
168
          if (this.sources[i].result.source != 'openaire' && this.sources[i].type != 'project' && this.sources[i].type != 'community') {
169
            this.sources[i].type = this.commonType;
170
          }
171
        }
172
      }
173

    
174
    } else {
175
      if (this.applyToResults) {
176
        for (let i = 0; i < this.results.length; i++) {
177
          if (this.results[i].result.source != 'openaire' && this.results[i].type != 'project' && this.results[i].type != 'community') {
178
            this.results[i].result.accessRights = this.commonAccessRights;
179
            if (this.commonAccessRights == "EMBARGO") {
180
              this.results[i].result.embargoEndDate = this.commonEmbargoEndDate;
181
            }
182
          }
183
        }
184
      } else {
185
        for (let i = 0; i < this.sources.length; i++) {
186
          if (this.sources[i].result.source != 'openaire' && this.sources[i].type != 'project' && this.sources[i].type != 'community') {
187
            this.sources[i].result.accessRights = this.commonAccessRights;
188
            if (this.commonAccessRights == "EMBARGO") {
189
              this.sources[i].result.embargoEndDate = this.commonEmbargoEndDate;
190
            }
191
          }
192
        }
193
      }
194
      this.setEmbargoEndDates();
195
    }
196
    this.updateLocalStorage();
197
    this.runValidation()
198

    
199
  }
200

    
201

    
202
  remove(item: any, applyToResults: boolean) {
203
    if (applyToResults) {
204
      let index: number = this.results.indexOf(item);
205
      item.warningMessages.splice(0, item.warningMessages.length);
206
      if (index > -1) {
207
        this.results.splice(index, 1);
208
        localStorage.setItem(this.localStoragePrefix + 'results', JSON.stringify(this.results));
209
      }
210
    } else {
211
      let index: number = this.sources.indexOf(item);
212
      item.warningMessages.splice(0, item.warningMessages.length);
213
      if (index > -1) {
214
        this.sources.splice(index, 1);
215
        localStorage.setItem(this.localStoragePrefix + 'sources', JSON.stringify(this.sources));
216

    
217
      }
218
    }
219
    this.updateLocalStorage();
220
    this.runValidation();
221
  }
222

    
223
  addStringToNumber(str: string, num: number) {
224
    return (+str) + num;
225
  }
226

    
227
  runValidation() {
228
    this.warnings = [];
229
    this.errors = [];
230
    if (this.inlineEntity) {
231
      this.inlineEntity.warningMessages = [];
232
    }
233
    this.validateEmbargoDate(this.sources);
234
    this.validateEmbargoDate(this.results);
235
    this.validateProjectDuration();
236
  }
237

    
238
  validateProjectDuration() {
239
    for (let item of this.results) {
240
      if (item.type == 'project' && item.project) {
241
        for (let result of this.sources) {
242
          if (result.result && result.result.date) {
243
            if ((item.project.startDate && result.result.date < item.project.startDate) || (item.project.endDate && +result.result.date > (5 + +item.project.endDate))) {
244
              const message: Message = {
245
                type: 'projectDuration',
246
                resultId: result.id,
247
                resultTitle: result.title,
248
                projectId: item.id,
249
                projectInfo: {title: item.title, startDate: item.project.startDate, endDate: item.project.endDate}
250
              };
251
              result.warningMessages.push(message);
252
              this.warnings.push(message);
253
            }
254
          }
255
        }
256
        if (this.inlineEntity && this.inlineEntity.result) {
257
          if (this.inlineEntity.result.date) {
258
            if ((item.project.startDate && this.inlineEntity.result.date < item.project.startDate) || (item.project.endDate && +this.inlineEntity.result.date > (5 + +item.project.endDate))) {
259
              const message: Message = {
260
                type: 'projectDuration',
261
                resultId: this.inlineEntity.id,
262
                resultTitle: this.inlineEntity.title,
263
                projectId: item.id,
264
                projectInfo: {title: item.title, startDate: item.project.startDate, endDate: item.project.endDate}
265
              };
266
              this.inlineEntity.warningMessages.push(message);
267
              this.warnings.push(message);
268
            }
269
          }
270
        }
271
      }
272
    }
273
      if (this.inlineEntity && this.inlineEntity.project) {
274
        for (let result of this.results) {
275
          if (result.result && result.result.date) {
276
            if ((this.inlineEntity.project.startDate && result.result.date < this.inlineEntity.project.startDate) || (this.inlineEntity.project.endDate && +result.result.date > (5 + +this.inlineEntity.project.endDate))) {
277
              const message: Message = {
278
                type: 'projectDuration',
279
                resultId: result.id,
280
                resultTitle: result.title,
281
                projectId: this.inlineEntity.id,
282
                projectInfo: {title: this.inlineEntity.title, startDate: this.inlineEntity.project.startDate, endDate: this.inlineEntity.project.endDate}
283
              };
284
              result.warningMessages.push(message);
285
              this.warnings.push(message);
286
            }
287
          }
288
        }
289
      }
290

    
291

    
292
  }
293

    
294
  validateEmbargoDate(sources: ClaimEntity []) {
295
    for (let entity of sources) {
296
      entity.warningMessages = [];
297
      if(entity.result) {
298
        // console.log(entity.result.accessRights + " " + entity.result.date + " " + entity.result.embargoEndDate);
299
        if (entity.result.accessRights && entity.result.accessRights == 'EMBARGO' && entity.result.date && (entity.result.embargoEndDate) && entity.result.date > entity.result.embargoEndDate.substr(0, 4)) {
300
          let message: Message = new Message();
301
          message.type = "embargoEndDate";
302
          message.resultId = entity.id;
303
          message.resultTitle = entity.title;
304
          entity.warningMessages.push(message);
305
          this.warnings.push(message);
306
        }
307
      }
308

    
309
    }
310
  }
311

    
312
  getProjectDurationMessage(result: ClaimEntity) {
313
    for (let message of result.warningMessages) {
314
      if (message.type == "projectDuration") {
315
        return "Should be from " + message.projectInfo.startDate + ((message.projectInfo.endDate) ? (" to " + ((5 + +message.projectInfo.endDate))) : "");
316
      }
317
    }
318
    return null;
319
  }
320

    
321
  getEmbargoEndDateMessage(result: ClaimEntity) {
322
    for (var message of result.warningMessages) {
323
      if (message.type == "embargoEndDate") {
324
        return "Embargo end date must be later than published date";
325
      }
326
    }
327
    return null;
328
  }
329
  updateLocalStorage(){
330
    if (this.sources != null) {
331
      localStorage.setItem(this.localStoragePrefix+"sources", JSON.stringify(this.sources));
332
    }
333
    if (this.results != null) {
334
      localStorage.setItem(this.localStoragePrefix+"results", JSON.stringify(this.results));
335
    }
336
  }
337

    
338
}
(6-6/14)