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 {OpenaireProperties} from '../../utils/properties/openaireProperties';
6
import 'rxjs/add/observable/of';
7
import 'rxjs/add/operator/do';
8
import 'rxjs/add/operator/share';
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):any {
23
    console.info("getDatasetInfo in service");
24

    
25
    let url = OpenaireProperties. getSearchAPIURLLast()+'datasets/'+id+"?format=json";
26
    let key = url;
27

    
28

    
29
    return this.http.get((OpenaireProperties.isCacheEnabled())? (OpenaireProperties.getCacheUrl()+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']])
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
                    ]).map(res => this.parseDatasetInfo(res));
46
  }
47

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

    
55
  parseDatasetInfo (data: any):any {
56
    this.datasetInfo = new DatasetInfo();
57

    
58
    if(data[0] != null) {
59
      var date:string = (data[0].dateofacceptance)+""; // transform to string in case it is an integer
60
      this.datasetInfo.date  = (date && (date).indexOf('-') !== -1)?date.split('-')[0]:date;
61
      this.datasetInfo.dateofacceptance = data[0].dateofacceptance;
62
      this.datasetInfo.publisher = data[0].publisher;
63
      if(!Array.isArray(data[0].description)) {
64
        this.datasetInfo.description = data[0].description;
65
      } else {
66
        this.datasetInfo.description = data[0].description[0];
67
      }
68
      this.datasetInfo.embargoEndDate = data[0].embargoenddate;
69
    }
70
    this.datasetInfo.title = {"name": "", "url": "", "accessMode": ""};
71
    if(data[0]['bestaccessright'].hasOwnProperty("classid")) {
72
      this.datasetInfo.title.accessMode = data[0]['bestaccessright'].classid;
73
    }
74
    if(data[1] != null) {
75
      if(Array.isArray(data[1])) {
76
        this.datasetInfo.title['name'] = data[1][0].content;
77
      } else {
78
        this.datasetInfo.title['name'] = data[1].content;
79
      }
80
    }
81

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

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

    
99
            this.datasetInfo.relatedResearchResults = this.parsingFunctions.parseRelatedResearchResults(this.datasetInfo.relatedResearchResults, relation, provenanceAction);
100

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

    
108
    if(data[3] != null) {
109
      if(data[3].hasOwnProperty("instance")) {
110
        this.datasetInfo.downloadFrom = new Map<string, {"url": string[], "accessMode": string[], "bestAccessMode": string}>();
111
        this.datasetInfo.publishedIn = new Map<string, {"url": string[], "accessMode": string[], "bestAccessMode": string}>()
112

    
113
        this.datasetInfo.types = new Array<string>();
114
        let types = new Set<string>();
115

    
116
        let counter = 0;
117
        let instance;
118

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

    
121
        for(let i=0; i<length; i++) {
122
          instance = data[3]['instance'].length!=undefined ? data[3]['instance'][i] : data[3]['instance'];
123

    
124
          this.parsingFunctions.parseTypes(this.datasetInfo.types, types, instance);
125

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

    
134
            if(instance.hasOwnProperty("collectedfrom")) {
135
              this.datasetInfo.collectedFrom = this.parsingFunctions.parseCollectedFrom(instance['collectedfrom']);
136
            }
137

    
138
            if(instance.hasOwnProperty("hostedby")) {
139
              if(instance['hostedby'].name != "other resources" && instance['hostedby'].name != "Unknown Repository") {
140
                this.parsingFunctions.parseDownloadFrom(this.datasetInfo.downloadFrom, instance, url);
141
              } else {
142
                counter = this.parsingFunctions.parsePublishedIn(this.datasetInfo.publishedIn, instance, data[0], url, counter);
143
              }
144
              if(this.datasetInfo.title != undefined) {
145
                if(!this.datasetInfo.title['url']) {
146
                  this.datasetInfo.title['url'] = url;
147
                }
148

    
149
                if(this.parsingFunctions.changeBestAccessMode(this.datasetInfo.title['accessMode'], instance['licence'])) {
150
                  this.datasetInfo.title['accessMode'] = instance['licence'].classid;
151
                  this.datasetInfo.title['url'] = url;
152
                }
153
              }
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
    // if(data[6] != null) {
172
    //     this.datasetInfo.bestaccessright = data[6].classid;
173
    // }
174

    
175
    // if(data[7] != null) {
176
    //   this.datasetInfo.collectedFrom = this.parsingFunctions.parseCollectedFrom(data[7]);
177
    // }
178

    
179
    // null argument is for journal
180
    this.datasetInfo.downloadFrom = this.parsingFunctions.addPublisherToDownloadFrom(
181
                this.datasetInfo.downloadFrom, this.datasetInfo.publisher,
182
                null, this.datasetInfo.identifiers, this.datasetInfo.title);
183

    
184
    if(data[8] != null) {
185
      this.datasetInfo.contexts = this.parsingFunctions.parseContexts(data[8]);
186
    }
187

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

    
194
    if(data[9] != null && data[9] == "under curation") {
195
      this.datasetInfo.underCurationMessage = true;
196
    } else {
197
      this.datasetInfo.underCurationMessage = false;
198
    }
199

    
200
    if(data[10] != null) {
201
      if(this.datasetInfo.authors == undefined) {
202
        this.datasetInfo.authors = new Array<string>();
203
      }
204

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

    
211
      }
212

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

    
218
    return this.datasetInfo;
219
  }
220
}
(5-5/5)