Project

General

Profile

1
import {Injectable} from '@angular/core';
2
import {Http, Response} from '@angular/http';
3
import {HttpClient, HttpErrorResponse} from "@angular/common/http";
4
import {Observable, throwError} from 'rxjs';
5
import {SoftwareInfo} from '../../utils/entities/softwareInfo';
6

    
7

    
8

    
9
import{EnvProperties} from '../../utils/properties/env-properties';
10

    
11
import { ParsingFunctions } from '../landing-utils/parsingFunctions.class';
12
import {map} from "rxjs/operators";
13

    
14
@Injectable()
15
export class SoftwareService {
16

    
17
  constructor(private http: HttpClient ) {
18
    this.parsingFunctions = new ParsingFunctions();
19
  }
20

    
21
  public parsingFunctions: ParsingFunctions;
22
  softwareInfo: SoftwareInfo;
23

    
24
  getSoftwareInfo (id: string, properties:EnvProperties):any {
25

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

    
29

    
30
    return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
31
                    //.map(res => <any> res.json())
32
                    .pipe(map(res => [res['result']['header']['dri:status'], res['result']['metadata']['oaf:entity']['oaf:result'], res]))
33
                    .pipe(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[1]['programmingLanguage'],
48
                                 res[2],
49
                                res[1]['journal'] //15
50
                    ]))
51
                    .pipe(map(res => this.parseSoftwareInfo(res, properties)));
52
  }
53

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

    
61
  parseSoftwareInfo (data: any, properties: EnvProperties):any {
62
    this.softwareInfo = new SoftwareInfo();
63
    this.softwareInfo.record = data[14];
64
    if(data[0] != null) {
65
      var date:string = (data[0].dateofacceptance)+""; // transform to string in case it is an integer
66
      this.softwareInfo.date  = (date && (date).indexOf('-') !== -1)?date.split('-')[0]:date;
67
      this.softwareInfo.dateofacceptance = data[0].dateofacceptance;
68
      this.softwareInfo.publisher = data[0].publisher;
69
      if(!Array.isArray(data[0].description)) {
70
        this.softwareInfo.description = String(data[0].description);
71
      } else {
72
        this.softwareInfo.description = String(data[0].description[0]);
73
      }
74
      this.softwareInfo.embargoEndDate = data[0].embargoenddate;
75
    }
76
    /*
77
    this.softwareInfo.title = {"name": "", "url": "", "accessMode": ""};
78
    if(data[0]['bestaccessright'] && data[0]['bestaccessright'] && data[0]['bestaccessright'].hasOwnProperty("classid")) {
79
      this.softwareInfo.title.accessMode = data[0]['bestaccessright'].classid;
80
    }
81
    if(data[1] != null) {
82
      if(Array.isArray(data[1])) {
83
        this.softwareInfo.title['name'] = data[1][0].content;
84
      } else {
85
        this.softwareInfo.title['name'] = data[1].content;
86
      }
87
    }
88
    */
89
    if(data[0]['bestaccessright'] && data[0]['bestaccessright'] && data[0]['bestaccessright'].hasOwnProperty("classid")) {
90
      this.softwareInfo.accessMode = data[0]['bestaccessright'].classid;
91
    }
92
    if(data[1] != null) {
93
      if(Array.isArray(data[1])) {
94
        this.softwareInfo.title = String(data[1][0].content);
95
      } else {
96
        this.softwareInfo.title = String(data[1].content);
97
      }
98
    }
99

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

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

    
117
            this.softwareInfo.relatedResearchResults = this.parsingFunctions.parseRelatedResearchResults(this.softwareInfo.relatedResearchResults, relation, provenanceAction);
118

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

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

    
133
        this.softwareInfo.types = new Array<string>();
134
        let types = new Set<string>();
135

    
136
        let counter = 0;
137
        let instance;
138

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

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

    
144
          this.parsingFunctions.parseTypes(this.softwareInfo.types, types, instance);
145

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

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

    
162
    if(data[4] != null) {
163
      this.softwareInfo.identifiers = this.parsingFunctions.parseIdentifiers(data[4]);
164
    }
165

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

    
173
    // null argument is for journal
174
    this.softwareInfo.hostedBy_collectedFrom = this.parsingFunctions.addPublisherToHostedBy_collectedFrom(
175
                this.softwareInfo.hostedBy_collectedFrom, this.softwareInfo.publisher,
176
                null, this.softwareInfo.identifiers/*, this.softwareInfo.title*/);
177

    
178
    if(data[8] != null) {
179
      this.softwareInfo.contexts = this.parsingFunctions.parseContexts(data[8]);
180
    }
181

    
182
    // if(data[9] != null && this.softwareInfo.type == undefined) {
183
    //   if(data[9].hasOwnProperty('classname')) {
184
    //     this.softwareInfo.type = data[9].classname;
185
    //   }
186
    // }
187

    
188
    if(data[9] != null && data[9] == "under curation") {
189
      this.softwareInfo.underCurationMessage = true;
190
    } else {
191
      this.softwareInfo.underCurationMessage = false;
192
    }
193

    
194
    if(data[10] != null) {
195
      if(this.softwareInfo.authors == undefined) {
196
        this.softwareInfo.authors = new Array<{"fullName": string, "orcid": string}>();
197
      }
198

    
199
      let authors = data[10];
200
      let length = Array.isArray(authors) ? authors.length : 1;
201

    
202
      for(let i=0; i<length; i++) {
203
        let author = Array.isArray(authors) ? authors[i] : authors;
204
        if(author) {
205
          /*if (author.ORCID && author.ORCID.indexOf(properties.orcidURL) != -1) {
206
            author.ORCID = author.ORCID.substr(properties.orcidURL.length);
207
          }*/
208
          this.softwareInfo['authors'][author.rank] = {"fullName": author.content, "orcid": author.ORCID};
209
        }
210
      }
211
      this.softwareInfo.authors = this.softwareInfo.authors.filter(function (item) {
212
        return (item != undefined && item.fullName != undefined);
213
      });
214
    }
215

    
216
    if(data[11] != null) {
217
      this.softwareInfo.languages = this.parsingFunctions.parseLanguages(data[11]);
218
    }
219
    if(data[12] != null) {
220
      this.softwareInfo.countries = this.parsingFunctions.parseCountries(data[12]);
221
    }
222

    
223
    if(data[13] != null) {
224
      this.softwareInfo.programmingLanguages = this.parsingFunctions.parseProgrammingLanguages(data[13]);
225
    }
226

    
227
    if(this.softwareInfo.relatedResearchResults) {
228
      let self = this;
229
      this.softwareInfo.relatedResearchResults.forEach(function (value, key, map) {
230
        self.softwareInfo.relatedResearchResults.set(key, self.parsingFunctions.sortByPercentage(value));
231
      });
232
    }
233
    this.softwareInfo.similarResearchResults = this.parsingFunctions.sortByPercentage(this.softwareInfo.similarResearchResults);
234
    if(data[15] != null) {
235
      this.softwareInfo.journal = {"journal": "", "issn": "", "lissn": "", "eissn": "", "issue": "", "volume": "", "start_page": "", "end_page": ""}
236

    
237
      this.softwareInfo.journal['journal'] = data[15].content;
238
      this.softwareInfo.journal['issn'] = data[15].issn;
239
      this.softwareInfo.journal['lissn'] = data[15].lissn;
240
      this.softwareInfo.journal['eissn'] = data[15].eissn;
241
      this.softwareInfo.journal['issue'] = data[15].iss;
242
      this.softwareInfo.journal['volume'] = data[15].vol;
243
      this.softwareInfo.journal['start_page'] = data[15].sp;
244
      this.softwareInfo.journal['end_page'] = data[15].ep;
245
    }
246
    return this.softwareInfo;
247
  }
248
}
(5-5/5)