Project

General

Profile

1
import {Injectable} from '@angular/core';
2
import {Http, Response} from '@angular/http';
3
import {Observable}     from 'rxjs/Observable';
4
import {DatasetInfo} from '../../utils/entities/datasetInfo';
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 DatasetService {
14

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

    
19
  public parsingFunctions: ParsingFunctions;
20
  datasetInfo: DatasetInfo;
21

    
22
  getDatasetInfo (id: string, properties:EnvProperties):any {
23

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

    
27

    
28
    return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
29
                    .map(res => <any> res.json())
30
                    .map(res => [res['result']['header']['dri:status'], res['result']['metadata']['oaf:entity']['oaf:result'],res])
31
                    .map(res => [res[1],  //0
32
                                 res[1]['title'],   //1
33
                                 res[1]['rels']['rel'], //2
34
                                 res[1]['children'],  //3
35
                                 res[1]['pid'], //4
36
                                 res[1]['subject'], //5
37
                                 res[1]['bestaccessright'], //6
38
                                 res[1]['collectedfrom'], //7
39
                                 res[1]['context'], //8
40
                                 //res[1]['resulttype'],
41
                                 res[0],  //9
42
                                 res[1]['creator'], //10
43
                                 res[1]['language'],  //11
44
                                 res[1]['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]['journal'] //15
48
                    ]).map(res => this.parseDatasetInfo(res, properties));
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
  parseDatasetInfo (data: any, properties: EnvProperties):any {
59
    this.datasetInfo = new DatasetInfo();
60
    this.datasetInfo.record = data[13];
61
    // ['result']['metadata']['oaf:entity']['oaf:result']
62
    if(data[0] != null) {
63
      var date:string = (data[0].dateofacceptance)+""; // transform to string in case it is an integer
64
      this.datasetInfo.date  = (date && (date).indexOf('-') !== -1)?date.split('-')[0]:date;
65
      this.datasetInfo.dateofacceptance = data[0].dateofacceptance;
66
      this.datasetInfo.publisher = data[0].publisher;
67
      if(!Array.isArray(data[0].description)) {
68
        this.datasetInfo.description = String(data[0].description);
69
      } else {
70
        this.datasetInfo.description = String(data[0].description[0]);
71
      }
72
      this.datasetInfo.embargoEndDate = data[0].embargoenddate;
73
    }
74
    /*this.datasetInfo.title = {"name": "", "url": "", "accessMode": ""};
75
    if(data[0]['bestaccessright'] && data[0]['bestaccessright'].hasOwnProperty("classid")) {
76
      this.datasetInfo.title.accessMode = data[0]['bestaccessright'].classid;
77
    }
78
    if(data[1] != null) {
79
      if(Array.isArray(data[1])) {
80
        this.datasetInfo.title['name'] = data[1][0].content;
81
      } else {
82
        this.datasetInfo.title['name'] = data[1].content;
83
      }
84
    }*/
85
    if(data[0]['bestaccessright'] && data[0]['bestaccessright'].hasOwnProperty("classid")) {
86
      this.datasetInfo.accessMode = data[0]['bestaccessright'].classid;
87
    }
88
    // ['result']['metadata']['oaf:entity']['oaf:result']['title']
89
    if(data[1] != null) {
90
      if(Array.isArray(data[1])) {
91
        this.datasetInfo.title = data[1][0].content;
92
        if(data[1][1].classid == "subtitle") {
93
          this.datasetInfo.subtitle = data[1][1].content;
94
        }
95
      } else {
96
        this.datasetInfo.title = data[1].content;
97
      }
98
    }
99

    
100
    // ['result']['metadata']['oaf:entity']['oaf:result']['rels']['rel']
101
    if(data[2] != null) {
102
      let relation;
103
      let length = data[2].length!=undefined ? data[2].length : 1;
104

    
105
      for(let i=0; i<length; i++) {
106
        relation = data[2].length!=undefined ? data[2][i] : data[2];
107
        if(relation.hasOwnProperty("to")) {
108
          if(relation['to'].class == "isProducedBy") {
109
            this.datasetInfo.fundedByProjects = this.parsingFunctions.parseFundingByProjects(this.datasetInfo.fundedByProjects, relation, this.datasetInfo.projectsProvenanceVocabulary);
110
          } else if(relation['to'].class == "isRelatedTo") {
111
            let provenanceAction: string;
112
            if(relation.provenanceaction in this.datasetInfo.researchResultsProvenanceVocabulary) {
113
              provenanceAction = this.datasetInfo.researchResultsProvenanceVocabulary[relation.provenanceaction];
114
            } else {
115
              provenanceAction = "Other"
116
            }
117

    
118
            this.datasetInfo.relatedResearchResults = this.parsingFunctions.parseRelatedResearchResults(this.datasetInfo.relatedResearchResults, relation, provenanceAction);
119

    
120
          } else if(relation['to'].class == "hasAmongTopNSimilarDocuments") {
121
            this.datasetInfo.similarResearchResults = this.parsingFunctions.parseSimilarResearchResults(this.datasetInfo.similarResearchResults, relation);
122
          }
123
        }
124
      }
125
    }
126

    
127
    // ['result']['metadata']['oaf:entity']['oaf:result']['children']
128
    if(data[3] != null) {
129
      if(data[3].hasOwnProperty("instance")) {
130
        this.datasetInfo.hostedBy_collectedFrom = new Array<{ "downloadName": string, "downloadUrl": string[],
131
                                                              "collectedName": string, "collectedId": string,
132
                                                              "accessMode": string[], "bestAccessMode": string,
133
                                                              "type": string, "year":string}>();
134

    
135
        this.datasetInfo.types = new Array<string>();
136
        let types = new Set<string>();
137

    
138
        let counter = 0;
139
        let instance;
140

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

    
143
        for(let i=0; i<length; i++) {
144
          instance = data[3]['instance'].length!=undefined ? data[3]['instance'][i] : data[3]['instance'];
145

    
146
          this.parsingFunctions.parseTypes(this.datasetInfo.types, types, instance);
147

    
148
          if(instance.hasOwnProperty("webresource")) {
149
            let url;
150
            if(instance['webresource'].length == undefined) {
151
              url = instance['webresource'].url;
152
            } else{
153
              url = instance['webresource'][0].url;
154
            }
155

    
156
            if(instance.hasOwnProperty("hostedby")) {
157
              counter = this.parsingFunctions.parseHostedBy_collectedFrom(this.datasetInfo.hostedBy_collectedFrom, instance, data[0], url, counter/*, this.datasetInfo.title*/, this.datasetInfo.accessMode);
158
            }
159
          }
160
        }
161
      }
162
    }
163

    
164
    // ['result']['metadata']['oaf:entity']['oaf:result']['pid']
165
    if(data[4] != null) {
166
      this.datasetInfo.identifiers = this.parsingFunctions.parseIdentifiers(data[4]);
167
    }
168

    
169
    // ['result']['metadata']['oaf:entity']['oaf:result']['subject']
170
    if(data[5] != null) {
171
      let subjectResults: [string[], Map<string, string[]>, Map<string, string[]>] = this.parsingFunctions.parseAllSubjects(data[5]);
172
      this.datasetInfo.subjects = subjectResults[0];
173
      this.datasetInfo.otherSubjects = subjectResults[1];
174
      this.datasetInfo.classifiedSubjects = subjectResults[2];
175
    }
176

    
177
    this.datasetInfo.hostedBy_collectedFrom = this.parsingFunctions.addPublisherToHostedBy_collectedFrom(
178
                this.datasetInfo.hostedBy_collectedFrom, this.datasetInfo.publisher,
179
                null, this.datasetInfo.identifiers/*, this.datasetInfo.title*/);
180

    
181
    // ['result']['metadata']['oaf:entity']['oaf:result']['context']
182
    if(data[8] != null) {
183
      this.datasetInfo.contexts = this.parsingFunctions.parseContexts(data[8]);
184
    }
185

    
186
    // if(data[9] != null && this.datasetInfo.type == undefined) {
187
    //   if(data[9].hasOwnProperty('classname')) {
188
    //     this.datasetInfo.type = data[9].classname;
189
    //   }
190
    // }
191

    
192
    // ['result']['header']['dri:status']
193
    if(data[9] != null && data[9] == "under curation") {
194
      this.datasetInfo.underCurationMessage = true;
195
    } else {
196
      this.datasetInfo.underCurationMessage = false;
197
    }
198

    
199
    // ['result']['metadata']['oaf:entity']['oaf:result']['creator']
200
    if(data[10] != null) {
201
      if(this.datasetInfo.authors == undefined) {
202
        this.datasetInfo.authors = new Array<{"fullName": string, "orcid": string}>();
203
      }
204

    
205
      let authors = data[10];
206
      let length = Array.isArray(authors) ? authors.length : 1;
207

    
208
      for(let i=0; i<length; i++) {
209
        let author = Array.isArray(authors) ? authors[i] : authors;
210
        if(author) {
211
          /*if (author.ORCID && author.ORCID.indexOf(properties.orcidURL) != -1) {
212
            author.ORCID = author.ORCID.substr(properties.orcidURL.length);
213
          }*/
214
          this.datasetInfo['authors'][author.rank] = {"fullName": author.content, "orcid": author.ORCID};
215
        }
216
      }
217
      this.datasetInfo.authors = this.datasetInfo.authors.filter(function (item) {
218
        return (item != undefined && item.fullName != undefined);
219
      });
220
    }
221

    
222
    // ['result']['metadata']['oaf:entity']['oaf:result']['language']
223
    if(data[11] != null) {
224
      this.datasetInfo.languages = this.parsingFunctions.parseLanguages(data[11]);
225
    }
226
    // ['result']['metadata']['oaf:entity']['oaf:result']['country']
227
    if(data[12] != null) {
228
      this.datasetInfo.countries = this.parsingFunctions.parseCountries(data[12]);
229
    }
230

    
231
    // ['result']['metadata']['oaf:entity']['oaf:result']['extraInfo']['citations']['citation']
232
    if(data[14] != null) {
233
      this.datasetInfo.references = this.parsingFunctions.parseReferences(data[14]);
234
    }
235

    
236
    if(this.datasetInfo.relatedResearchResults) {
237
      let self = this;
238
      this.datasetInfo.relatedResearchResults.forEach(function (value, key, map) {
239
        self.datasetInfo.relatedResearchResults.set(key, self.parsingFunctions.sortByPercentage(value));
240
      });
241
    }
242
    this.datasetInfo.similarResearchResults = this.parsingFunctions.sortByPercentage(this.datasetInfo.similarResearchResults);
243
    if(data[15] != null) {
244
      this.datasetInfo.journal = {"journal": "", "issn": "", "lissn": "", "eissn": "", "issue": "", "volume": "", "start_page": "", "end_page": ""}
245

    
246
      this.datasetInfo.journal['journal'] = data[15].content;
247
      this.datasetInfo.journal['issn'] = data[15].issn;
248
      this.datasetInfo.journal['lissn'] = data[15].lissn;
249
      this.datasetInfo.journal['eissn'] = data[15].eissn;
250
      this.datasetInfo.journal['issue'] = data[15].iss;
251
      this.datasetInfo.journal['volume'] = data[15].vol;
252
      this.datasetInfo.journal['start_page'] = data[15].sp;
253
      this.datasetInfo.journal['end_page'] = data[15].ep;
254
    }
255

    
256
    return this.datasetInfo;
257
  }
258
}
(5-5/5)