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
  @Input() communityId:string= null;
31
  errors:ClaimsErrorMessage[] = [];
32
  warnings:Message[] = [];
33
  public commonAccessRights = "OPEN"; // for access rights- changes when user apply a change to every entity
34
  public commonEmbargoEndDate; // for access rights: embargoEndDate - changes when user apply a change to every entity
35
  public commonType; // for research entity type - changes when user apply a change to every entity
36
  public applyToResults: boolean; // true apply to results, false apply to sources
37
  public typeChanged: boolean = true; //
38
  accessTypes = ["OPEN", "CLOSED", "EMBARGO", "RESTRICTED"];
39
  embargoEndDates = [];
40

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

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

    
79
  }*/
80

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

    
93
  }
94

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

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

    
128
    this.updateLocalStorage();
129

    
130
  }
131

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

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

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

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

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

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

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

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

    
226
  }
227

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

    
281

    
282
  }
283

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

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

    
303
    }
304
  }
305

    
306

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

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

    
333
}
(6-6/14)