Project

General

Profile

1

    
2
import {Injectable} from '@angular/core';
3
import {Http, Response} from '@angular/http';
4
import {Observable}     from 'rxjs/Observable';
5
import {PublicationInfo} from '../../utils/entities/publicationInfo';
6
import {OpenaireProperties} from '../../utils/properties/openaireProperties';
7
import 'rxjs/add/observable/of';
8
import 'rxjs/add/operator/do';
9
import 'rxjs/add/operator/share';
10
import 'rxjs/add/operator/map';
11

    
12

    
13
import { ParsingFunctions } from '../landing-utils/parsingFunctions.class';
14

    
15
@Injectable()
16
export class PublicationService {
17

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

    
22
  public parsingFunctions: ParsingFunctions;
23
  publicationInfo: PublicationInfo;
24

    
25
  getPublicationInfo (id: string):any {
26
    console.info("getPublicationInfo in service");
27
    let url = OpenaireProperties. getSearchAPIURLLast() + 'publications/' +id+"?format=json";
28
    let key = url;
29

    
30
    return this.http.get((OpenaireProperties.isCacheEnabled())? (OpenaireProperties.getCacheUrl()+encodeURIComponent(url)): url)
31
                .map(res => <any> res.json())
32
                .map(res => [res['result']['header']['dri:status'], res['result']['metadata']['oaf:entity']])
33
                .map(res => [ res[1]['oaf:result'],
34
                              res[1]['oaf:result']['title'],
35
                              res[1]['oaf:result']['rels']['rel'],
36
                              res[1]['oaf:result']['children'],
37
                              res[1]['oaf:result']['pid'],
38
                              res[1]['oaf:result']['journal'],
39
                              res[1]['oaf:result']['language'],
40
                              res[1]['oaf:result']['subject'],
41
                              res[1]['oaf:result']['bestaccessright'],
42
                              res[1]['oaf:result']['collectedfrom'],
43
                              (res[1]['extraInfo']!= undefined && res[1]['extraInfo']['citations']!= undefined)? res[1]['extraInfo']['citations']['citation']:null,
44
                              res[1]['oaf:result']['context'],
45
                              res[0],
46
                              res[1]['oaf:result']['creator']
47
                ])
48
                .map(res => this.parsePublicationInfo(res));
49
  }
50

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

    
58
  parsePublicationInfo (data: any):any {
59
    this.publicationInfo = new PublicationInfo();
60

    
61
    if(data[0] != null) {
62
      var date:string = (data[0].dateofacceptance)+""; // transform to string in case it is an integer
63
      this.publicationInfo.date  = (date && (date).indexOf('-') !== -1)?date.split('-')[0]:date;
64
      this.publicationInfo.dateofacceptance = data[0].dateofacceptance;
65
      this.publicationInfo.publisher = data[0].publisher;
66
      if(!Array.isArray(data[0].description)) {
67
          this.publicationInfo.description = data[0].description;
68
      } else {
69
          this.publicationInfo.description = data[0].description[0];
70
      }
71
      this.publicationInfo.embargoEndDate = data[0].embargoenddate;
72
    }
73

    
74
    this.publicationInfo.title = {"name": "", "url": "", "accessMode": ""};
75
    if(data[0]['bestaccessright'].hasOwnProperty("classid")) {
76
      this.publicationInfo.title.accessMode = data[0]['bestaccessright'].classid;
77
    }
78

    
79
    if(data[1] != null) {
80
      if(Array.isArray(data[1])) {
81
        this.publicationInfo.title['name'] = data[1][0].content;
82
      } else {
83
        this.publicationInfo.title['name'] = data[1].content;
84
      }
85
    }
86

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

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

    
104
            this.publicationInfo.relatedResearchResults = this.parsingFunctions.parseRelatedResearchResults(this.publicationInfo.relatedResearchResults, relation, provenanceAction);
105
          } else if(relation['to'].class == "hasAmongTopNSimilarDocuments") {
106
            this.publicationInfo.similarResearchResults = this.parsingFunctions.parseSimilarResearchResults(this.publicationInfo.similarResearchResults, relation);
107
          } else if(relation['to'].class == "hasAuthorInstitution") {
108
            this.publicationInfo.organizations = this.parseRelatedOrganizations(this.publicationInfo.organizations, relation);
109
          }
110
        }
111
      }
112
    }
113

    
114
    if(data[3] != null) {
115
      if(data[3].hasOwnProperty("instance")) {
116
        //this.publicationInfo.collectedFrom = new Array<{"name": string, "id": string}>();
117
        //this.publicationInfo.downloadFrom = new Map<string, {"url": string[], "accessMode": string[], "bestAccessMode": string}>();
118
        //this.publicationInfo.publishedIn = new Map<string, {"url": string[], "accessMode": string[], "bestAccessMode": string}>();
119

    
120

    
121
        this.publicationInfo.hostedBy_collectedFrom = new Array<{"downloadName": string, "downloadUrl": string[], "collectedName": string, "collectedId": string, "accessMode": string[], "bestAccessMode": string, "type": string, "year":string}>();
122

    
123

    
124
        this.publicationInfo.types = new Array<string>();
125
        let types = new Set<string>();
126

    
127
        let counter = 0;
128
        let instance;
129

    
130
        let length = Array.isArray(data[3]['instance']) ? data[3]['instance'].length : 1;
131

    
132
        for(let i=0; i<length; i++) {
133
          instance = Array.isArray(data[3]['instance']) ? data[3]['instance'][i] : data[3]['instance'];
134

    
135
          this.parsingFunctions.parseTypes(this.publicationInfo.types, types, instance);
136

    
137
          if(instance.hasOwnProperty("webresource")) {
138
            let url;
139
            if(!Array.isArray(instance['webresource'])) {
140
              url = instance['webresource'].url;
141
            } else{
142
              url = instance['webresource'][0].url;
143
            }
144

    
145
            /**********************************************************/
146
            if(instance.hasOwnProperty("hostedby")) {
147
              counter = this.parsingFunctions.parseHostedBy_collectedFrom(this.publicationInfo.hostedBy_collectedFrom, instance, data[0], url, counter, this.publicationInfo.title);
148
            }
149
            /**********************************************************/
150
          }
151
        }
152
      }
153

    
154
      if(data[3].hasOwnProperty("externalreference")) {
155
        let externalResults: [Map<string, Map<string, string>>, { "name": string, "url": string}[]] = this.parseBioentitiesAndSoftware(data[3]);
156
        this.publicationInfo.bioentities = externalResults[0];
157
        this.publicationInfo.software = externalResults[1];
158
      }
159
    }
160

    
161
    if(data[4] != null) {
162
      this.publicationInfo.identifiers = this.parsingFunctions.parseIdentifiers(data[4]);
163
    }
164

    
165
    if(data[5] != null) {
166
      this.publicationInfo.journal = {"journal": "", "issn": "", "lissn": ""}
167

    
168
      this.publicationInfo.journal['journal'] = data[5].content;
169
      this.publicationInfo.journal['issn'] = data[5].issn;
170
      this.publicationInfo.journal['lissn'] = data[5].lissn;
171
    }
172

    
173
    if(data[6] != null) {
174
      this.publicationInfo.languages = new Array<string>();
175

    
176
      if(!Array.isArray(data[6])) {
177
        if(data[6].classname != "Undetermined" && data[6].classname) {
178
          this.publicationInfo.languages.push(data[6].classname);
179
        }
180
      } else {
181
        for(let i=0; i<data[6].length; i++) {
182
          if(data[6][i].classname != "Undetermined" && data[6][i].classname) {
183
            this.publicationInfo.languages.push(data[6][i].classname);
184
          }
185
        }
186
      }
187
    }
188

    
189
    if(data[7] != null) {
190
      let subjectResults: [string[], Map<string, string[]>, Map<string, string[]>] = this.parsingFunctions.parseAllSubjects(data[7]);
191
      this.publicationInfo.subjects = subjectResults[0];
192
      this.publicationInfo.otherSubjects = subjectResults[1];
193
      this.publicationInfo.classifiedSubjects = subjectResults[2];
194
    }
195

    
196
    // if(data[8] != null) {
197
    //     this.publicationInfo.bestaccessright = data[8].classid;
198
    // }
199

    
200
    // if(data[9] != null) {
201
    //   this.publicationInfo.collectedFrom = this.parsingFunctions.parseCollectedFrom(data[9]);
202
    // }
203

    
204
    this.publicationInfo.hostedBy_collectedFrom = this.parsingFunctions.addPublisherToHostedBy_collectedFrom(
205
                this.publicationInfo.hostedBy_collectedFrom, this.publicationInfo.publisher,
206
                this.publicationInfo.journal, this.publicationInfo.identifiers,
207
                this.publicationInfo.title);
208

    
209
    if(data[10] != null) {
210
      this.publicationInfo.references = this.parseReferences(data[10]);
211
    }
212

    
213
    if(data[11] != null) {
214
      this.publicationInfo.contexts = this.parsingFunctions.parseContexts(data[11]);
215
    }
216

    
217
    if(data[12] != null && data[12] == "under curation") {
218
      this.publicationInfo.underCurationMessage = true;
219
    } else {
220
      this.publicationInfo.underCurationMessage = false;
221
    }
222

    
223
    if(data[13] != null) {
224
      if(this.publicationInfo.authors == undefined) {
225
        this.publicationInfo.authors = new Array<string>();
226
      }
227

    
228
      let authors = data[13];
229
      let length = Array.isArray(authors) ? authors.length : 1;
230

    
231
      for(let i=0; i<length; i++) {
232
        let author = Array.isArray(authors) ? authors[i] : authors;
233
        this.publicationInfo.authors[author.rank-1] = author.content;
234
      }
235
      this.publicationInfo.authors = this.publicationInfo.authors.filter(function (item) {
236
        return (item != undefined);
237
      });
238
    }
239

    
240
    return this.publicationInfo;
241
  }
242

    
243
  parseRelatedOrganizations(organizations: {"name": string, "shortname":string, "id": string, "websiteUrl": string, "country": string, "trust": number}[], relation: any):
244
                  {"name": string, "shortname":string, "id": string, "websiteUrl": string, "country": string, "trust": number}[] {
245
    if(organizations == undefined) {
246
      organizations = new Array<{
247
                  "name": string, "shortname": string,
248
                  "id": string, "websiteUrl": string,
249
                  "country": string, "trust": number}>();
250
    }
251

    
252
    let organization: { "name": string, "shortname": string,
253
                        "id": string, "websiteUrl": string,
254
                        "country": string, "trust": number
255
                      } = {
256
                        "name": "", "shortname": "",
257
                        "id": "", "websiteUrl": "",
258
                        "country": "", "trust": null
259
                      };
260

    
261
    organization.id = relation['to'].content;
262
    organization.name = relation.legalname;
263
    organization.shortname = relation.legalshortname;
264
    organization.websiteUrl = relation.websiteurl;
265
    if(relation.country) {
266
      organization.country = relation.country.classname;
267
    }
268
    if(relation.trust) {
269
      organization.trust = Math.round(relation.trust*100);
270
    }
271

    
272
    organizations.push(organization);
273
    return organizations;
274
  }
275

    
276
  parseBioentitiesAndSoftware(children: any) : [Map<string, Map<string, string>>, { "name": string, "url": string}[]] {
277
    let bioentities: Map<string, Map<string, string>>;
278
    let software: {"name": string, "url": string}[];
279

    
280
    let length = Array.isArray(children['externalreference']) ? children['externalreference'].length : 1;
281

    
282
    let externalreference;
283
    for(let i=0; i<length; i++) {
284
      externalreference = Array.isArray(children['externalreference']) ? children['externalreference'][i] : children['externalreference'];
285

    
286
      if(externalreference.hasOwnProperty("qualifier")) {
287
        if(externalreference['qualifier'].classid == "accessionNumber") {
288

    
289
          if(bioentities == undefined) {
290
            bioentities = new Map<string, Map<string, string>>();
291
          }
292

    
293
          if(!bioentities.has(externalreference.sitename)) {
294
            bioentities.set(externalreference.sitename, new Map<string, string>());
295
          }
296
          bioentities.get(externalreference.sitename).set(externalreference.refidentifier, externalreference.url);
297

    
298
        } else if(externalreference['qualifier'].classid == "software") {
299

    
300
          if(software == undefined) {
301
            software = new Array<{"name": string, "url": string}>();
302
          }
303

    
304
          software.push({"name": externalreference.sitename, "url": externalreference.url});
305
        }
306
      }
307
    }
308

    
309
    return [bioentities, software];
310
  }
311

    
312
  parseReferences(citations: any): {"name": string, "url": string}[] {
313
    let references = new Array<{"name": string, "url": string}>();
314

    
315
    let citation;
316
    let length = Array.isArray(citations) ? citations.length : 1;
317
    for(let i=0; i<length; i++) {
318
      citation = Array.isArray(citations) ? citations[i] : citations;
319

    
320
      let url;
321
      if(citation.hasOwnProperty("id")) {
322
        let citationId;
323
        let length1 = Array.isArray(citation['id']) ? citation['id'].length : 1;
324
        for(let j=0; j<length1; j++) {
325
          citationId = Array.isArray(citation['id']) ? citation['id'][j] : citation['id'];
326

    
327
          if(citationId.type == "pmid") {
328
            url = OpenaireProperties.getPmidURL()+citationId.value;
329
          }
330
        }
331
      }
332

    
333
      references[citation.position-1] = { "name": "", "url": ""};
334
      references[citation.position-1]['name'] = citation.rawText;
335
      references[citation.position-1]['url'] = url;
336
    }
337
    return references;
338
  }
339
}
(5-5/5)