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

    
10
import { ParsingFunctions } from '../landing-utils/parsingFunctions.class';
11

    
12
@Injectable()
13
export class SoftwareService {
14

    
15
  constructor(private http: Http ) {
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

    
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.parseSoftwareInfo(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
  parseSoftwareInfo (data: any):any {
56
    this.softwareInfo = new SoftwareInfo();
57

    
58
    if(data[0] != null) {
59
      var date:string = (data[0].dateofacceptance)+""; // transform to string in case it is an integer
60
      this.softwareInfo.date  = (date && (date).indexOf('-') !== -1)?date.split('-')[0]:date;
61
      this.softwareInfo.dateofacceptance = data[0].dateofacceptance;
62
      this.softwareInfo.publisher = data[0].publisher;
63
      if(!Array.isArray(data[0].description)) {
64
        this.softwareInfo.description = data[0].description;
65
      } else {
66
        this.softwareInfo.description = data[0].description[0];
67
      }
68
      this.softwareInfo.embargoEndDate = data[0].embargoenddate;
69
    }
70
    this.softwareInfo.title = {"name": "", "url": "", "accessMode": ""};
71
    if(data[0]['bestaccessright'] && data[0]['bestaccessright'].hasOwnProperty("classid")) {
72
      this.softwareInfo.title.accessMode = data[0]['bestaccessright'].classid;
73
    }
74
    if(data[1] != null) {
75
      if(Array.isArray(data[1])) {
76
        this.softwareInfo.title['name'] = data[1][0].content;
77
      } else {
78
        this.softwareInfo.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.softwareInfo.fundedByProjects = this.parsingFunctions.parseFundingByProjects(this.softwareInfo.fundedByProjects, relation, this.softwareInfo.projectsProvenanceVocabulary);
91
          } else if(relation['to'].class == "isRelatedTo") {
92
            let provenanceAction: string;
93
            if(relation.provenanceaction in this.softwareInfo.researchResultsProvenanceVocabulary) {
94
              provenanceAction = this.softwareInfo.researchResultsProvenanceVocabulary[relation.provenanceaction];
95
            } else {
96
              provenanceAction = "Other"
97
            }
98

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

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

    
108
    if(data[3] != null) {
109
      if(data[3].hasOwnProperty("instance")) {
110
        this.softwareInfo.hostedBy_collectedFrom = new Array<{  "downloadName": string, "downloadUrl": string[],
111
                                                                "collectedName": string, "collectedId": string,
112
                                                                "accessMode": string[], "bestAccessMode": string,
113
                                                                "type": string, "year":string}>();
114

    
115
        this.softwareInfo.types = new Array<string>();
116
        let types = new Set<string>();
117

    
118
        let counter = 0;
119
        let instance;
120

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

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

    
126
          this.parsingFunctions.parseTypes(this.softwareInfo.types, types, instance);
127

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

    
136
            if(instance.hasOwnProperty("hostedby")) {
137
              counter = this.parsingFunctions.parseHostedBy_collectedFrom(this.softwareInfo.hostedBy_collectedFrom, instance, data[0], url, counter, this.softwareInfo.title);
138
            }
139
          }
140
        }
141
      }
142
    }
143

    
144
    if(data[4] != null) {
145
      this.softwareInfo.identifiers = this.parsingFunctions.parseIdentifiers(data[4]);
146
    }
147

    
148
    if(data[5] != null) {
149
      let subjectResults: [string[], Map<string, string[]>, Map<string, string[]>] = this.parsingFunctions.parseAllSubjects(data[5]);
150
      this.softwareInfo.subjects = subjectResults[0];
151
      this.softwareInfo.otherSubjects = subjectResults[1];
152
      this.softwareInfo.classifiedSubjects = subjectResults[2];
153
    }
154

    
155
    // null argument is for journal
156
    this.softwareInfo.hostedBy_collectedFrom = this.parsingFunctions.addPublisherToHostedBy_collectedFrom(
157
                this.softwareInfo.hostedBy_collectedFrom, this.softwareInfo.publisher,
158
                null, this.softwareInfo.identifiers, this.softwareInfo.title);
159

    
160
    if(data[8] != null) {
161
      this.softwareInfo.contexts = this.parsingFunctions.parseContexts(data[8]);
162
    }
163

    
164
    // if(data[9] != null && this.softwareInfo.type == undefined) {
165
    //   if(data[9].hasOwnProperty('classname')) {
166
    //     this.softwareInfo.type = data[9].classname;
167
    //   }
168
    // }
169

    
170
    if(data[9] != null && data[9] == "under curation") {
171
      this.softwareInfo.underCurationMessage = true;
172
    } else {
173
      this.softwareInfo.underCurationMessage = false;
174
    }
175

    
176
    if(data[10] != null) {
177
      if(this.softwareInfo.authors == undefined) {
178
        this.softwareInfo.authors = new Array<string>();
179
      }
180

    
181
      let authors = data[10];
182
      let length = Array.isArray(authors) ? authors.length : 1;
183

    
184
      for(let i=0; i<length; i++) {
185
        let author = Array.isArray(authors) ? authors[i] : authors;
186
        this.softwareInfo.authors[author.rank-1] = author.content;
187
      }
188

    
189
      this.softwareInfo.authors = this.softwareInfo.authors.filter(function (item) {
190
        return (item != undefined);
191
      });
192
    }
193

    
194
    return this.softwareInfo;
195
  }
196
}
(5-5/5)