Project

General

Profile

1
import {Injectable} from '@angular/core';
2
import {Http, Response} from '@angular/http';
3
import {HttpClient, HttpErrorResponse} from "@angular/common/http";
4
import {Observable, throwError} from 'rxjs';
5
import {OrpInfo} from '../../utils/entities/orpInfo';
6

    
7

    
8

    
9
import{EnvProperties} from '../../utils/properties/env-properties';
10

    
11
import { ParsingFunctions } from '../landing-utils/parsingFunctions.class';
12
import {map} from "rxjs/operators";
13

    
14
@Injectable()
15
export class OrpService {
16

    
17
  constructor(private http: HttpClient ) {
18
    this.parsingFunctions = new ParsingFunctions();
19
  }
20

    
21
  public parsingFunctions: ParsingFunctions;
22
  orpInfo: OrpInfo;
23

    
24
  getOrpInfo (id: string, properties:EnvProperties):any {
25

    
26
    let url = properties.searchAPIURLLAst+'other/'+id+"?format=json";
27
    let key = url;
28

    
29
    return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
30
                    //.map(res => <any> res.json())
31
                    .pipe(map(res => [res['result']['header']['dri:status'], res['result']['metadata']['oaf:entity'],res]))
32
                    .pipe(map(res => [res[1]['oaf:result'],  //0
33
                                 res[1]['oaf:result']['title'],   //1
34
                                 res[1]['oaf:result']['rels']['rel'], //2
35
                                 res[1]['oaf:result']['children'],  //3
36
                                 res[1]['oaf:result']['pid'], //4
37
                                 res[1]['oaf:result']['subject'], //5
38
                                 res[1]['oaf:result']['bestaccessright'], //6
39
                                 res[1]['oaf:result']['collectedfrom'], //7
40
                                 res[1]['oaf:result']['context'], //8
41
                                 res[0],  //9
42
                                 res[1]['oaf:result']['creator'], //10
43
                                 res[1]['oaf:result']['language'],  //11
44
                                 res[1]['oaf:result']['country'], //12
45
                                 res[2],  //13
46
                                 (res[1]['extraInfo']!= undefined && res[1]['extraInfo']['citations']!= undefined)? res[1]['extraInfo']['citations']['citation']:null, //14
47
                                  res[1]['oaf:result']['journal'] //15
48
                    ]))
49
                    .pipe(map(res => this.parseOrpInfo(res, properties)));
50
  }
51

    
52
  private handleError (error: HttpErrorResponse) {
53
  // in a real world app, we may send the error to some remote logging infrastructure
54
  // instead of just logging it to the console
55
    console.log(error);
56
    return throwError(error  || 'Server error');
57
  }
58

    
59
  parseOrpInfo (data: any, properties: EnvProperties):any {
60
    this.orpInfo = new OrpInfo();
61
    this.orpInfo.record = data[13];
62
    if(data[0] != null) {
63
      var date:string = (data[0].dateofacceptance)+""; // transform to string in case it is an integer
64
      this.orpInfo.date  = (date && (date).indexOf('-') !== -1)?date.split('-')[0]:date;
65
      this.orpInfo.dateofacceptance = data[0].dateofacceptance;
66
      this.orpInfo.publisher = data[0].publisher;
67
      if(!Array.isArray(data[0].description)) {
68
        this.orpInfo.description = String(data[0].description);
69
      } else {
70
        this.orpInfo.description = String(data[0].description[0]);
71
      }
72
      this.orpInfo.embargoEndDate = data[0].embargoenddate;
73
    }
74

    
75
    if(data[0]['bestaccessright'] && data[0]['bestaccessright'].hasOwnProperty("classid")) {
76
      this.orpInfo.accessMode = data[0]['bestaccessright'].classid;
77
    }
78
    if(data[1] != null) {
79
      if(Array.isArray(data[1])) {
80
        this.orpInfo.title = String(data[1][0].content);
81
      } else {
82
        this.orpInfo.title = String(data[1].content);
83
      }
84
    }
85

    
86
    if(data[2] != null) {
87
      let relation;
88
      let length = data[2].length!=undefined ? data[2].length : 1;
89

    
90
      for(let i=0; i<length; i++) {
91
        relation = data[2].length!=undefined ? data[2][i] : data[2];
92
        if(relation.hasOwnProperty("to")) {
93
          if(relation['to'].class == "isProducedBy") {
94
            this.orpInfo.fundedByProjects = this.parsingFunctions.parseFundingByProjects(this.orpInfo.fundedByProjects, relation, this.orpInfo.projectsProvenanceVocabulary);
95
          } else if(relation['to'].class == "isRelatedTo") {
96
            let provenanceAction: string;
97
            if(relation.provenanceaction in this.orpInfo.researchResultsProvenanceVocabulary) {
98
              provenanceAction = this.orpInfo.researchResultsProvenanceVocabulary[relation.provenanceaction];
99
            } else {
100
              provenanceAction = "Other"
101
            }
102

    
103
            this.orpInfo.relatedResearchResults = this.parsingFunctions.parseRelatedResearchResults(this.orpInfo.relatedResearchResults, relation, provenanceAction);
104

    
105
          } else if(relation['to'].class == "hasAmongTopNSimilarDocuments") {
106
            this.orpInfo.similarResearchResults = this.parsingFunctions.parseSimilarResearchResults(this.orpInfo.similarResearchResults, relation);
107
          }
108
        }
109
      }
110
    }
111

    
112
    if(data[3] != null) {
113
      if(data[3].hasOwnProperty("instance")) {
114
        this.orpInfo.hostedBy_collectedFrom = new Array<{ "downloadName": string, "downloadUrl": string[],
115
                                                              "collectedName": string, "collectedId": string,
116
                                                              "accessMode": string[], "bestAccessMode": string,
117
                                                              "type": string, "year":string}>();
118

    
119
        this.orpInfo.types = new Array<string>();
120
        let types = new Set<string>();
121

    
122
        let counter = 0;
123
        let instance;
124

    
125
        let length = data[3]['instance'].length!=undefined ? data[3]['instance'].length : 1;
126

    
127
        for(let i=0; i<length; i++) {
128
          instance = data[3]['instance'].length!=undefined ? data[3]['instance'][i] : data[3]['instance'];
129

    
130
          this.parsingFunctions.parseTypes(this.orpInfo.types, types, instance);
131

    
132
          if(instance.hasOwnProperty("webresource")) {
133
            let url;
134
            if(instance['webresource'].length == undefined) {
135
              url = instance['webresource'].url;
136
            } else{
137
              url = instance['webresource'][0].url;
138
            }
139

    
140
            if(instance.hasOwnProperty("hostedby")) {
141
              counter = this.parsingFunctions.parseHostedBy_collectedFrom(this.orpInfo.hostedBy_collectedFrom, instance, data[0], url, counter/*, this.orpInfo.title*/, this.orpInfo.accessMode);
142
            }
143
          }
144
        }
145
      }
146
    }
147

    
148
    if(data[4] != null) {
149
      this.orpInfo.identifiers = this.parsingFunctions.parseIdentifiers(data[4]);
150
    }
151

    
152
    if(data[5] != null) {
153
      let subjectResults: [string[], Map<string, string[]>, Map<string, string[]>] = this.parsingFunctions.parseAllSubjects(data[5]);
154
      this.orpInfo.subjects = subjectResults[0];
155
      this.orpInfo.otherSubjects = subjectResults[1];
156
      this.orpInfo.classifiedSubjects = subjectResults[2];
157
    }
158

    
159
    this.orpInfo.hostedBy_collectedFrom = this.parsingFunctions.addPublisherToHostedBy_collectedFrom(
160
                this.orpInfo.hostedBy_collectedFrom, this.orpInfo.publisher,
161
                null, this.orpInfo.identifiers/*, this.orpInfo.title*/);
162

    
163
    if(data[8] != null) {
164
      this.orpInfo.contexts = this.parsingFunctions.parseContexts(data[8]);
165
    }
166

    
167
    if(data[9] != null && data[9] == "under curation") {
168
      this.orpInfo.underCurationMessage = true;
169
    } else {
170
      this.orpInfo.underCurationMessage = false;
171
    }
172

    
173
    if(data[10] != null) {
174
      if(this.orpInfo.authors == undefined) {
175
        this.orpInfo.authors = new Array<{"fullName": string, "orcid": string}>();
176
      }
177

    
178
      let authors = data[10];
179
      let length = Array.isArray(authors) ? authors.length : 1;
180

    
181
      for(let i=0; i<length; i++) {
182
        let author = Array.isArray(authors) ? authors[i] : authors;
183
        if(author) {
184
          /*if (author.ORCID && author.ORCID.indexOf(properties.orcidURL) != -1) {
185
            author.ORCID = author.ORCID.substr(properties.orcidURL.length);
186
          }*/
187
          this.orpInfo['authors'][author.rank] = {"fullName": author.content, "orcid": author.ORCID};
188
        }
189
      }
190
      this.orpInfo.authors = this.orpInfo.authors.filter(function (item) {
191
        return (item != undefined && item.fullName != undefined);
192
      });
193
    }
194

    
195
    if(data[11] != null) {
196
      this.orpInfo.languages = this.parsingFunctions.parseLanguages(data[11]);
197
    }
198
    if(data[12] != null) {
199
      this.orpInfo.countries = this.parsingFunctions.parseCountries(data[12]);
200
    }
201

    
202
    if(data[14] != null) {
203
      this.orpInfo.references = this.parsingFunctions.parseReferences(data[14]);
204
    }
205

    
206
    if(this.orpInfo.relatedResearchResults) {
207
      let self = this;
208
      this.orpInfo.relatedResearchResults.forEach(function (value, key, map) {
209
        self.orpInfo.relatedResearchResults.set(key, self.parsingFunctions.sortByPercentage(value));
210
      });
211
    }
212
    this.orpInfo.similarResearchResults = this.parsingFunctions.sortByPercentage(this.orpInfo.similarResearchResults);
213
    if(data[15] != null) {
214
      this.orpInfo.journal = {"journal": "", "issn": "", "lissn": "", "eissn": "", "issue": "", "volume": "", "start_page": "", "end_page": ""}
215

    
216
      this.orpInfo.journal['journal'] = data[15].content;
217
      this.orpInfo.journal['issn'] = data[15].issn;
218
      this.orpInfo.journal['lissn'] = data[15].lissn;
219
      this.orpInfo.journal['eissn'] = data[15].eissn;
220
      this.orpInfo.journal['issue'] = data[15].iss;
221
      this.orpInfo.journal['volume'] = data[15].vol;
222
      this.orpInfo.journal['start_page'] = data[15].sp;
223
      this.orpInfo.journal['end_page'] = data[15].ep;
224
    }
225

    
226
    return this.orpInfo;
227
  }
228
}
(4-4/4)