Project

General

Profile

1
import {Injectable} from '@angular/core';
2
import {Http, Response} from '@angular/http';
3
import {Observable}     from 'rxjs/Observable';
4
import {OrpInfo} from '../../utils/entities/orpInfo';
5
import 'rxjs/add/observable/of';
6
import 'rxjs/add/operator/do';
7
import 'rxjs/add/operator/share';
8
import{EnvProperties} from '../../utils/properties/env-properties';
9

    
10
import { ParsingFunctions } from '../landing-utils/parsingFunctions.class';
11

    
12
@Injectable()
13
export class OrpService {
14

    
15
  constructor(private http: Http ) {
16
    this.parsingFunctions = new ParsingFunctions();
17
  }
18

    
19
  public parsingFunctions: ParsingFunctions;
20
  orpInfo: OrpInfo;
21

    
22
  getOrpInfo (id: string, properties:EnvProperties):any {
23
    console.info("getOrpInfo in service");
24

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

    
28
    return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
29
                    .map(res => <any> res.json())
30
                    .do(res => console.info(res['result']['metadata']['oaf:entity']))
31
                    .map(res => [res['result']['header']['dri:status'], res['result']['metadata']['oaf:entity']['oaf:result'],res])
32
                    .map(res => [res[1],
33
                                 res[1]['title'],
34
                                 res[1]['rels']['rel'],
35
                                 res[1]['children'],
36
                                 res[1]['pid'],
37
                                 res[1]['subject'],
38
                                 res[1]['bestaccessright'],
39
                                 res[1]['collectedfrom'],
40
                                 res[1]['context'],
41
                                 res[0],
42
                                 res[1]['creator'],
43
                                 res[1]['language'],
44
                                 res[1]['country'],
45
                                 res[2]
46
                    ]).map(res => this.parseOrpInfo(res));
47
  }
48

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

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

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

    
83
    if(data[2] != null) {
84
      let relation;
85
      let length = data[2].length!=undefined ? data[2].length : 1;
86

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

    
100
            this.orpInfo.relatedResearchResults = this.parsingFunctions.parseRelatedResearchResults(this.orpInfo.relatedResearchResults, relation, provenanceAction);
101

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

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

    
116
        this.orpInfo.types = new Array<string>();
117
        let types = new Set<string>();
118

    
119
        let counter = 0;
120
        let instance;
121

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

    
124
        for(let i=0; i<length; i++) {
125
          instance = data[3]['instance'].length!=undefined ? data[3]['instance'][i] : data[3]['instance'];
126

    
127
          this.parsingFunctions.parseTypes(this.orpInfo.types, types, instance);
128

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

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

    
145
    if(data[4] != null) {
146
      this.orpInfo.identifiers = this.parsingFunctions.parseIdentifiers(data[4]);
147
    }
148

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

    
156
    this.orpInfo.hostedBy_collectedFrom = this.parsingFunctions.addPublisherToHostedBy_collectedFrom(
157
                this.orpInfo.hostedBy_collectedFrom, this.orpInfo.publisher,
158
                null, this.orpInfo.identifiers/*, this.orpInfo.title*/);
159

    
160
    if(data[8] != null) {
161
      this.orpInfo.contexts = this.parsingFunctions.parseContexts(data[8]);
162
    }
163

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

    
170
    if(data[10] != null) {
171
      if(this.orpInfo.authors == undefined) {
172
        this.orpInfo.authors = new Array<string>();
173
      }
174

    
175
      let authors = data[10];
176
      let length = Array.isArray(authors) ? authors.length : 1;
177
      for(let i=0; i<length; i++) {
178
        let author = Array.isArray(authors) ? authors[i] : authors;
179
        this.orpInfo.authors[author.rank] = author.content;
180

    
181
      }
182

    
183
      this.orpInfo.authors = this.orpInfo.authors.filter(function (item) {
184
        return (item != undefined);
185
      });
186
    }
187

    
188
    if(data[11] != null) {
189
      this.orpInfo.languages = this.parsingFunctions.parseLanguages(data[11]);
190
    }
191
    if(data[12] != null) {
192
      this.orpInfo.countries = this.parsingFunctions.parseCountries(data[12]);
193
    }
194

    
195
    if(this.orpInfo.relatedResearchResults) {
196
      let self = this;
197
      this.orpInfo.relatedResearchResults.forEach(function (value, key, map) {
198
        self.orpInfo.relatedResearchResults.set(key, self.parsingFunctions.sortByPercentage(value));
199
      });
200
    }
201
    this.orpInfo.similarResearchResults = this.parsingFunctions.sortByPercentage(this.orpInfo.similarResearchResults);
202

    
203

    
204
    return this.orpInfo;
205
  }
206
}
(4-4/4)