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
                    ]).map(res => this.parseDatasetInfo(res));
48
  }
49

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

    
57
  parseDatasetInfo (data: any):any {
58
    this.datasetInfo = new DatasetInfo();
59
    this.datasetInfo.record = data[13];
60
    // ['result']['metadata']['oaf:entity']['oaf:result']
61
    if(data[0] != null) {
62
      var date:string = (data[0].dateofacceptance)+""; // transform to string in case it is an integer
63
      this.datasetInfo.date  = (date && (date).indexOf('-') !== -1)?date.split('-')[0]:date;
64
      this.datasetInfo.dateofacceptance = data[0].dateofacceptance;
65
      this.datasetInfo.publisher = data[0].publisher;
66
      if(!Array.isArray(data[0].description)) {
67
        this.datasetInfo.description = data[0].description;
68
      } else {
69
        this.datasetInfo.description = data[0].description[0];
70
      }
71
      this.datasetInfo.embargoEndDate = data[0].embargoenddate;
72
    }
73
    /*this.datasetInfo.title = {"name": "", "url": "", "accessMode": ""};
74
    if(data[0]['bestaccessright'] && data[0]['bestaccessright'].hasOwnProperty("classid")) {
75
      this.datasetInfo.title.accessMode = data[0]['bestaccessright'].classid;
76
    }
77
    if(data[1] != null) {
78
      if(Array.isArray(data[1])) {
79
        this.datasetInfo.title['name'] = data[1][0].content;
80
      } else {
81
        this.datasetInfo.title['name'] = data[1].content;
82
      }
83
    }*/
84
    if(data[0]['bestaccessright'] && data[0]['bestaccessright'].hasOwnProperty("classid")) {
85
      this.datasetInfo.accessMode = data[0]['bestaccessright'].classid;
86
    }
87
    // ['result']['metadata']['oaf:entity']['oaf:result']['title']
88
    if(data[1] != null) {
89
      if(Array.isArray(data[1])) {
90
        this.datasetInfo.title = data[1][0].content;
91
        if(data[1][1].classid == "subtitle") {
92
          this.datasetInfo.subtitle = data[1][1].content;
93
        }
94
      } else {
95
        this.datasetInfo.title = data[1].content;
96
      }
97
    }
98

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

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

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

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

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

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

    
137
        let counter = 0;
138
        let instance;
139

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

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

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

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

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

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

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

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

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

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

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

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

    
204
      let authors = data[10];
205
      let length = Array.isArray(authors) ? authors.length : 1;
206
      for(let i=0; i<length; i++) {
207
        let author = Array.isArray(authors) ? authors[i] : authors;
208
        this.datasetInfo.authors[author.rank] = author.content;
209

    
210
      }
211

    
212
      this.datasetInfo.authors = this.datasetInfo.authors.filter(function (item) {
213
        return (item != undefined);
214
      });
215
    }
216

    
217
    // ['result']['metadata']['oaf:entity']['oaf:result']['language']
218
    if(data[11] != null) {
219
      this.datasetInfo.languages = this.parsingFunctions.parseLanguages(data[11]);
220
    }
221
    // ['result']['metadata']['oaf:entity']['oaf:result']['country']
222
    if(data[12] != null) {
223
      this.datasetInfo.countries = this.parsingFunctions.parseCountries(data[12]);
224
    }
225

    
226
    // ['result']['metadata']['oaf:entity']['oaf:result']['extraInfo']['citations']['citation']
227
    if(data[14] != null) {
228
      this.datasetInfo.references = this.parsingFunctions.parseReferences(data[14]);
229
    }
230

    
231
    if(this.datasetInfo.relatedResearchResults) {
232
      let self = this;
233
      this.datasetInfo.relatedResearchResults.forEach(function (value, key, map) {
234
        self.datasetInfo.relatedResearchResults.set(key, self.parsingFunctions.sortByPercentage(value));
235
      });
236
    }
237
    this.datasetInfo.similarResearchResults = this.parsingFunctions.sortByPercentage(this.datasetInfo.similarResearchResults);
238

    
239

    
240
    return this.datasetInfo;
241
  }
242
}
(5-5/5)