Project

General

Profile

1
import {Injectable} from '@angular/core';
2
import {HttpClient} from "@angular/common/http";
3
import {ResultLandingInfo} from '../../../utils/entities/resultLandingInfo';
4
import {EnvProperties} from '../../../utils/properties/env-properties';
5
import {ParsingFunctions} from '../../landing-utils/parsingFunctions.class';
6
import {map} from "rxjs/operators";
7
import {HostedByCollectedFrom} from "../../../utils/result-preview/result-preview";
8

    
9
@Injectable()
10
export class DeletedByInferenceService {
11
  private sizeOfDescription: number = 270;
12

    
13
  constructor(private http: HttpClient ) {
14
    this.parsingFunctions = new ParsingFunctions();
15
  }
16

    
17
  public parsingFunctions: ParsingFunctions;
18

    
19
  getDeletedByInferenceResults (id: string, size: string, properties:EnvProperties):any {
20
    let url = properties.searchAPIURLLAst + 'deletedByInferenceResults/' +id+"?format=json&size="+size;
21
    let key = url;
22

    
23
    return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
24
                //.map(res => <any> res.json())
25
                .pipe(map(res => res['results']))
26
                .pipe(map(res => this.parseDeletedByInferenceResults(res)));
27
  }
28

    
29
  parseDeletedByInferenceResults (_results: any): ResultLandingInfo[] {
30
    /*title, authors, abstract, List of projects, PIDs,
31
      collectedfrom (link pointing to the download url), access rights*/
32

    
33
    let results: ResultLandingInfo[] = [];
34
    if(_results) {
35
      let result : ResultLandingInfo;
36

    
37
      let length = Array.isArray(_results) ? _results.length : 1;
38
      for(let i=0; i<length; i++) {
39
        var _result = Array.isArray(_results) ? _results[i]['result']['metadata']['oaf:entity'] : _results['result']['metadata']['oaf:entity'];
40

    
41
        result = new ResultLandingInfo();
42

    
43
        if(_result) {
44
          if(_result['oaf:result']) {
45
            let data = _result['oaf:result'];
46

    
47
            var date:string = (data.dateofacceptance)+""; // transform to string in case it is an integer
48
            result.date  = (date && (date).indexOf('-') !== -1)?date.split('-')[0]:date;
49
            result.dateofacceptance = data.dateofacceptance;
50
            result.embargoEndDate = data.embargoenddate;
51

    
52
            if(!Array.isArray(data.description)) {
53
              result.description = data.description;
54
            } else {
55
              result.description = data.description[0];
56
            }
57
            if(result.description && result.description.length > this.sizeOfDescription) {
58
              result.description = result.description.substring(0, this.sizeOfDescription) + "...";
59
            }
60

    
61
            if(data['bestaccessright'] && data['bestaccessright'].hasOwnProperty("classname")) {
62
              result.accessMode = data['bestaccessright'].classname;
63
            }
64
          }
65

    
66
          if(_result['oaf:result'] && _result['oaf:result']['title']) {
67
            let title = _result['oaf:result']['title'];
68

    
69
            if(Array.isArray(title)) {
70
              result.title = title[0].content;
71
            } else {
72
              result.title = title.content;
73
            }
74
          }
75

    
76
          if(_result['oaf:result'] && _result['oaf:result']['language']) {
77
            result.languages = this.parsingFunctions.parseLanguages(_result['oaf:result']['language']);
78
          }
79
          if(_result['oaf:result'] && _result['oaf:result']['country']) {
80
            result.countries = this.parsingFunctions.parseCountries(_result['oaf:result']['country']);
81
          }
82

    
83
          if(_result['oaf:result'] && _result['oaf:result']['children']) {
84
            let children = _result['oaf:result']['children'];
85

    
86
            if(children.hasOwnProperty("instance")) {
87
              result.types = new Array<string>();
88
              let types = new Set<string>();
89

    
90
              result.hostedBy_collectedFrom = new Array<HostedByCollectedFrom>();
91

    
92
              let counter = 0;
93
              let instance;
94

    
95
              let length = Array.isArray(children['instance']) ? children['instance'].length : 1;
96

    
97
              for(let i=0; i<length; i++) {
98
                instance = Array.isArray(children['instance']) ? children['instance'][i] : children['instance'];
99

    
100
                this.parsingFunctions.parseTypes(result.types, types, instance);
101

    
102
                if(instance.hasOwnProperty("webresource")) {
103
                  let url;
104
                  if(!Array.isArray(instance['webresource'])) {
105
                    url = instance['webresource'].url;
106
                  } else{
107
                    url = instance['webresource'][0].url;
108
                  }
109

    
110
                  if(instance.hasOwnProperty("hostedby")) {
111
                    counter = this.parsingFunctions.parseHostedBy_collectedFrom(result.hostedBy_collectedFrom, instance, _result['oaf:result'], url, counter, result.accessMode);
112
                  }
113
                }
114
              }
115
            }
116
          }
117

    
118
          if(_result['oaf:result'] && _result['oaf:result']['pid']) {
119
            result.identifiers = this.parsingFunctions.parseIdentifiers(_result['oaf:result']['pid']);
120
          }
121

    
122
          if(_result['oaf:result'] && _result['oaf:result']['creator']) {
123
            if(result.authors == undefined) {
124
              result.authors = new Array<{"fullName": string, "orcid": string}>();
125
            }
126

    
127
            let authors = _result['oaf:result']['creator'];
128
            let length = Array.isArray(authors) ? authors.length : 1;
129

    
130
            for(let i=0; i<length; i++) {
131
              let author = Array.isArray(authors) ? authors[i] : authors;
132
              if(author) {
133
                /*if (author.orcid && author.orcid.indexOf(properties.orcidURL) != -1) {
134
                  author.orcid = author.orcid.substr(properties.orcidURL.length);
135
                }*/
136
                result['authors'][author.rank] = {"fullName": author.content, "orcid": author.orcid};
137
              }
138
            }
139
            result.authors = result.authors.filter(function (item) {
140
              return (item != undefined && item.fullName != undefined);
141
            });
142
          }
143

    
144
        }
145
        results.push(result);
146
      }
147
    }
148

    
149
    return results;
150
  }
151
}
(3-3/3)