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 '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 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, properties:EnvProperties):any {
23

    
24
    let url = properties.searchAPIURLLAst+'software/'+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],
32
                                 res[1]['title'],
33
                                 res[1]['rels']['rel'],
34
                                 res[1]['children'],
35
                                 res[1]['pid'],
36
                                 res[1]['subject'],
37
                                 res[1]['bestaccessright'],
38
                                 res[1]['collectedfrom'],
39
                                 res[1]['context'],
40
                                 //res[1]['resulttype'],
41
                                 res[0],
42
                                 res[1]['creator'],
43
                                 res[1]['language'],
44
                                 res[1]['country'],
45
                                 res[1]['programmingLanguage'],
46
                                 res[2]
47
                    ]).map(res => this.parseSoftwareInfo(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
  parseSoftwareInfo (data: any):any {
58
    this.softwareInfo = new SoftwareInfo();
59
    this.softwareInfo.record = data[14];
60
    if(data[0] != null) {
61
      var date:string = (data[0].dateofacceptance)+""; // transform to string in case it is an integer
62
      this.softwareInfo.date  = (date && (date).indexOf('-') !== -1)?date.split('-')[0]:date;
63
      this.softwareInfo.dateofacceptance = data[0].dateofacceptance;
64
      this.softwareInfo.publisher = data[0].publisher;
65
      if(!Array.isArray(data[0].description)) {
66
        this.softwareInfo.description = data[0].description;
67
      } else {
68
        this.softwareInfo.description = data[0].description[0];
69
      }
70
      this.softwareInfo.embargoEndDate = data[0].embargoenddate;
71
    }
72
    /*
73
    this.softwareInfo.title = {"name": "", "url": "", "accessMode": ""};
74
    if(data[0]['bestaccessright'] && data[0]['bestaccessright'] && data[0]['bestaccessright'].hasOwnProperty("classid")) {
75
      this.softwareInfo.title.accessMode = data[0]['bestaccessright'].classid;
76
    }
77
    if(data[1] != null) {
78
      if(Array.isArray(data[1])) {
79
        this.softwareInfo.title['name'] = data[1][0].content;
80
      } else {
81
        this.softwareInfo.title['name'] = data[1].content;
82
      }
83
    }
84
    */
85
    if(data[0]['bestaccessright'] && data[0]['bestaccessright'] && data[0]['bestaccessright'].hasOwnProperty("classid")) {
86
      this.softwareInfo.accessMode = data[0]['bestaccessright'].classid;
87
    }
88
    if(data[1] != null) {
89
      if(Array.isArray(data[1])) {
90
        this.softwareInfo.title = data[1][0].content;
91
      } else {
92
        this.softwareInfo.title = data[1].content;
93
      }
94
    }
95

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

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

    
113
            this.softwareInfo.relatedResearchResults = this.parsingFunctions.parseRelatedResearchResults(this.softwareInfo.relatedResearchResults, relation, provenanceAction);
114

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

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

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

    
132
        let counter = 0;
133
        let instance;
134

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

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

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

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

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

    
158
    if(data[4] != null) {
159
      this.softwareInfo.identifiers = this.parsingFunctions.parseIdentifiers(data[4]);
160
    }
161

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

    
169
    // null argument is for journal
170
    this.softwareInfo.hostedBy_collectedFrom = this.parsingFunctions.addPublisherToHostedBy_collectedFrom(
171
                this.softwareInfo.hostedBy_collectedFrom, this.softwareInfo.publisher,
172
                null, this.softwareInfo.identifiers/*, this.softwareInfo.title*/);
173

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

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

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

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

    
195
      let authors = data[10];
196
      let length = Array.isArray(authors) ? authors.length : 1;
197

    
198
      for(let i=0; i<length; i++) {
199
        let author = Array.isArray(authors) ? authors[i] : authors;
200
        this.softwareInfo.authors[author.rank] = author.content;
201
      }
202

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

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

    
215
    if(data[13] != null) {
216
      this.softwareInfo.programmingLanguages = this.parsingFunctions.parseProgrammingLanguages(data[13]);
217
    }
218

    
219
    if(this.softwareInfo.relatedResearchResults) {
220
      let self = this;
221
      this.softwareInfo.relatedResearchResults.forEach(function (value, key, map) {
222
        self.softwareInfo.relatedResearchResults.set(key, self.parsingFunctions.sortByPercentage(value));
223
      });
224
    }
225
    this.softwareInfo.similarResearchResults = this.parsingFunctions.sortByPercentage(this.softwareInfo.similarResearchResults);
226

    
227
    return this.softwareInfo;
228
  }
229
}
(5-5/5)