Project

General

Profile

1
import {Injectable} from '@angular/core';
2
import {Http, Response} from '@angular/http';
3
import {Observable}     from 'rxjs/Observable';
4
import {SoftwareInfo} from '../../utils/entities/softwareInfo';
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
import { CacheService  } from '../../shared/cache.service';
10
import { ParsingFunctions } from '../landing-utils/parsingFunctions.class';
11

    
12
@Injectable()
13
export class SoftwareService {
14

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

    
19
  public parsingFunctions: ParsingFunctions;
20
  softwareInfo: SoftwareInfo;
21

    
22
  getSoftwareInfo (id: string):any {
23
    console.info("getSoftwareInfo in service");
24

    
25
    let url = OpenaireProperties. getSearchAPIURLLast()+'software/'+id+"?format=json";
26
    let key = url;
27
    if (this._cache.has(key)) {
28
      return Observable.of(this._cache.get(key)).map(res => this.parseSoftwareInfo(res));
29
    }
30

    
31
    return this.http.get(url)
32
                    .map(res => <any> res.json())
33
                    .do(res => console.info(res['result']['metadata']['oaf:entity']))
34
                    .map(res => [res['result']['header']['dri:status'], res['result']['metadata']['oaf:entity']['oaf:result']])
35
                    .map(res => [res[1],
36
                                 res[1]['title'],
37
                                 res[1]['rels']['rel'],
38
                                 res[1]['children'],
39
                                 res[1]['pid'],
40
                                 res[1]['subject'],
41
                                 res[1]['bestlicense'],
42
                                 res[1]['collectedfrom'],
43
                                 res[1]['context'],
44
                                 //res[1]['resulttype'],
45
                                 res[0]
46
                    ]).do(res => {
47
                      this._cache.set(key, res);
48
                    })
49
                    .map(res => this.parseSoftwareInfo(res));
50
  }
51

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

    
59
  parseSoftwareInfo (data: any):any {
60
    this.softwareInfo = new SoftwareInfo();
61

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

    
86
    if(data[2] != null) {
87
      let relation;
88
      let length = data[2].length!=undefined ? data[2].length : 1;
89

    
90
      for(let i=0; i<length; i++) {
91
        relation = data[2].length!=undefined ? data[2][i] : data[2];
92
        if(relation.hasOwnProperty("to")) {
93
          if(relation['to'].class == "hasAuthor") {
94
            if(this.softwareInfo.authors == undefined) {
95
              this.softwareInfo.authors = new Array<{"name": string, "id": string}>();
96
            }
97

    
98
            this.softwareInfo.authors[relation.ranking-1] = {"name": "", "id": ""};
99
            this.softwareInfo.authors[relation.ranking-1]['name'] = relation.fullname;
100
          } else if(relation['to'].class == "isProducedBy") {
101
            this.softwareInfo.fundedByProjects = this.parsingFunctions.parseFundingByProjects(this.softwareInfo.fundedByProjects, relation, this.softwareInfo.projectsProvenanceVocabulary);
102
          } else if(relation['to'].class == "isRelatedTo") {
103
            let provenanceAction: string;
104
            if(relation.provenanceaction in this.softwareInfo.researchResultsProvenanceVocabulary) {
105
              provenanceAction = this.softwareInfo.researchResultsProvenanceVocabulary[relation.provenanceaction];
106
            } else {
107
              provenanceAction = "Other"
108
            }
109

    
110
            this.softwareInfo.relatedResearchResults = this.parsingFunctions.parseRelatedResearchResults(this.softwareInfo.relatedResearchResults, relation, provenanceAction);
111

    
112
          } else if(relation['to'].class == "hasAmongTopNSimilarDocuments") {
113
            this.softwareInfo.similarResearchResults = this.parsingFunctions.parseSimilarResearchResults(this.softwareInfo.similarResearchResults, relation);
114
          }
115
        }
116
      }
117

    
118
      if(this.softwareInfo.authors != undefined) {
119
        this.softwareInfo.authors = this.softwareInfo.authors.filter(function (item) {
120
          return (item != undefined);
121
        });
122
      }
123
    }
124

    
125
    if(data[3] != null) {
126
      if(data[3].hasOwnProperty("instance")) {
127
        this.softwareInfo.downloadFrom = new Map<string, {"url": string[], "accessMode": string[], "bestAccessMode": string}>();
128
        this.softwareInfo.publishedIn = new Map<string, {"url": string[], "accessMode": string[], "bestAccessMode": string}>()
129

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

    
133
        let counter = 0;
134
        let instance;
135

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

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

    
141
          this.parsingFunctions.parseTypes(this.softwareInfo.types, types, instance);
142

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

    
151
            if(instance.hasOwnProperty("hostedby")) {
152
              if(instance['hostedby'].name != "other resources" && instance['hostedby'].name != "Unknown Repository") {
153
                this.parsingFunctions.parseDownloadFrom(this.softwareInfo.downloadFrom, instance, url);
154
              } else {
155
                counter = this.parsingFunctions.parsePublishedIn(this.softwareInfo.publishedIn, instance, data[0], url, counter);
156
              }
157
              if(this.softwareInfo.title != undefined) {
158
                if(!this.softwareInfo.title['url']) {
159
                  this.softwareInfo.title['url'] = url;
160
                }
161

    
162
                if(this.parsingFunctions.changeBestAccessMode(this.softwareInfo.title['accessMode'], instance['licence'])) {
163
                  this.softwareInfo.title['accessMode'] = instance['licence'].classid;
164
                  this.softwareInfo.title['url'] = url;
165
                }
166
              }
167
            }
168
          }
169
        }
170
      }
171
    }
172

    
173
    if(data[4] != null) {
174
      this.softwareInfo.identifiers = this.parsingFunctions.parseIdentifiers(data[4]);
175
    }
176

    
177
    if(data[5] != null) {
178
      let subjectResults: [string[], Map<string, string[]>, Map<string, string[]>] = this.parsingFunctions.parseAllSubjects(data[5]);
179
      this.softwareInfo.subjects = subjectResults[0];
180
      this.softwareInfo.otherSubjects = subjectResults[1];
181
      this.softwareInfo.classifiedSubjects = subjectResults[2];
182
    }
183

    
184
    // if(data[6] != null) {
185
    //     this.softwareInfo.bestlicense = data[6].classid;
186
    // }
187

    
188
    if(data[7] != null) {
189
      this.softwareInfo.collectedFrom = this.parsingFunctions.parseCollectedFrom(data[7]);
190
    }
191

    
192
    // null argument is for journal
193
    this.softwareInfo.downloadFrom = this.parsingFunctions.addPublisherToDownloadFrom(
194
                this.softwareInfo.downloadFrom, this.softwareInfo.publisher,
195
                null, this.softwareInfo.identifiers, this.softwareInfo.title);
196

    
197
    if(data[8] != null) {
198
      this.softwareInfo.contexts = this.parsingFunctions.parseContexts(data[8]);
199
    }
200

    
201
    // if(data[9] != null && this.softwareInfo.type == undefined) {
202
    //   if(data[9].hasOwnProperty('classname')) {
203
    //     this.softwareInfo.type = data[9].classname;
204
    //   }
205
    // }
206

    
207
    if(data[9] != null && data[9] == "under curation") {
208
      this.softwareInfo.underCurationMessage = true;
209
    } else {
210
      this.softwareInfo.underCurationMessage = false;
211
    }
212

    
213
    return this.softwareInfo;
214
  }
215
}
(5-5/5)