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
    console.info("getDatasetInfo in service");
24

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

    
28

    
29
    return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
30
                    .map(res => <any> res.json())
31
                    .do(res => console.info(res['result']['metadata']['oaf:entity']))
32
                    .map(res => [res['result']['header']['dri:status'], res['result']['metadata']['oaf:entity']['oaf:result'],res])
33
                    .map(res => [res[1],
34
                                 res[1]['title'],
35
                                 res[1]['rels']['rel'],
36
                                 res[1]['children'],
37
                                 res[1]['pid'],
38
                                 res[1]['subject'],
39
                                 res[1]['bestaccessright'],
40
                                 res[1]['collectedfrom'],
41
                                 res[1]['context'],
42
                                 //res[1]['resulttype'],
43
                                 res[0],
44
                                 res[1]['creator'],
45
                                 res[1]['language'],
46
                                 res[1]['country'],
47
                                 res[2]
48
                    ]).map(res => this.parseDatasetInfo(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
  parseDatasetInfo (data: any):any {
59
    this.datasetInfo = new DatasetInfo();
60
    this.datasetInfo.record = data[13];
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
    if(data[1] != null) {
88
      if(Array.isArray(data[1])) {
89
        this.datasetInfo.title = data[1][0].content;
90
        if(data[1][1].classid == "subtitle") {
91
          this.datasetInfo.subtitle = data[1][1].content;
92
        }
93
      } else {
94
        this.datasetInfo.title = data[1].content;
95
      }
96
    }
97

    
98
    if(data[2] != null) {
99
      let relation;
100
      let length = data[2].length!=undefined ? data[2].length : 1;
101

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

    
115
            this.datasetInfo.relatedResearchResults = this.parsingFunctions.parseRelatedResearchResults(this.datasetInfo.relatedResearchResults, relation, provenanceAction);
116

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

    
124
    if(data[3] != null) {
125
      if(data[3].hasOwnProperty("instance")) {
126
        this.datasetInfo.hostedBy_collectedFrom = new Array<{ "downloadName": string, "downloadUrl": string[],
127
                                                              "collectedName": string, "collectedId": string,
128
                                                              "accessMode": string[], "bestAccessMode": string,
129
                                                              "type": string, "year":string}>();
130

    
131
        this.datasetInfo.types = new Array<string>();
132
        let types = new Set<string>();
133

    
134
        let counter = 0;
135
        let instance;
136

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

    
139
        for(let i=0; i<length; i++) {
140
          instance = data[3]['instance'].length!=undefined ? data[3]['instance'][i] : data[3]['instance'];
141

    
142
          this.parsingFunctions.parseTypes(this.datasetInfo.types, types, instance);
143

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

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

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

    
164
    if(data[5] != null) {
165
      let subjectResults: [string[], Map<string, string[]>, Map<string, string[]>] = this.parsingFunctions.parseAllSubjects(data[5]);
166
      this.datasetInfo.subjects = subjectResults[0];
167
      this.datasetInfo.otherSubjects = subjectResults[1];
168
      this.datasetInfo.classifiedSubjects = subjectResults[2];
169
    }
170

    
171
    this.datasetInfo.hostedBy_collectedFrom = this.parsingFunctions.addPublisherToHostedBy_collectedFrom(
172
                this.datasetInfo.hostedBy_collectedFrom, this.datasetInfo.publisher,
173
                null, this.datasetInfo.identifiers/*, this.datasetInfo.title*/);
174

    
175
    if(data[8] != null) {
176
      this.datasetInfo.contexts = this.parsingFunctions.parseContexts(data[8]);
177
    }
178

    
179
    // if(data[9] != null && this.datasetInfo.type == undefined) {
180
    //   if(data[9].hasOwnProperty('classname')) {
181
    //     this.datasetInfo.type = data[9].classname;
182
    //   }
183
    // }
184

    
185
    if(data[9] != null && data[9] == "under curation") {
186
      this.datasetInfo.underCurationMessage = true;
187
    } else {
188
      this.datasetInfo.underCurationMessage = false;
189
    }
190

    
191
    if(data[10] != null) {
192
      if(this.datasetInfo.authors == undefined) {
193
        this.datasetInfo.authors = new Array<string>();
194
      }
195

    
196
      let authors = data[10];
197
      let length = Array.isArray(authors) ? authors.length : 1;
198
      for(let i=0; i<length; i++) {
199
        let author = Array.isArray(authors) ? authors[i] : authors;
200
        this.datasetInfo.authors[author.rank] = author.content;
201

    
202
      }
203

    
204
      this.datasetInfo.authors = this.datasetInfo.authors.filter(function (item) {
205
        return (item != undefined);
206
      });
207
    }
208

    
209
    if(data[11] != null) {
210
      this.datasetInfo.languages = this.parsingFunctions.parseLanguages(data[11]);
211
    }
212
    if(data[12] != null) {
213
      this.datasetInfo.countries = this.parsingFunctions.parseCountries(data[12]);
214
    }
215

    
216
    if(this.datasetInfo.relatedResearchResults) {
217
      let self = this;
218
      this.datasetInfo.relatedResearchResults.forEach(function (value, key, map) {
219
        self.datasetInfo.relatedResearchResults.set(key, self.parsingFunctions.sortByPercentage(value));
220
      });
221
    }
222
    this.datasetInfo.similarResearchResults = this.parsingFunctions.sortByPercentage(this.datasetInfo.similarResearchResults);
223

    
224

    
225
    return this.datasetInfo;
226
  }
227
}
(5-5/5)