Project

General

Profile

1
import {Injectable} from '@angular/core';
2
import {HttpClient, HttpErrorResponse} from "@angular/common/http";
3
import {throwError} from 'rxjs';
4
import {SoftwareInfo} from '../../utils/entities/softwareInfo';
5
import {EnvProperties} from '../../utils/properties/env-properties';
6
import {ParsingFunctions} from '../landing-utils/parsingFunctions.class';
7
import {map} from "rxjs/operators";
8

    
9
@Injectable()
10
export class SoftwareService {
11

    
12
  constructor(private http: HttpClient ) {
13
    this.parsingFunctions = new ParsingFunctions();
14
  }
15

    
16
  public parsingFunctions: ParsingFunctions;
17
  softwareInfo: SoftwareInfo;
18

    
19
  getSoftwareInfo (id: string, properties:EnvProperties):any {
20

    
21
    let url = properties.searchAPIURLLAst+'software/'+id+"?format=json";
22
    let key = url;
23

    
24

    
25
    return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
26
                    //.map(res => <any> res.json())
27
                    .pipe(map(res => [res['result']['header']['dri:status'], res['result']['metadata']['oaf:entity']['oaf:result'], res]))
28
                    .pipe(map(res => [res[1],
29
                                 res[1]['title'],
30
                                 res[1]['rels']['rel'],
31
                                 res[1]['children'],
32
                                 res[1]['pid'],
33
                                 res[1]['subject'],
34
                                 res[1]['bestaccessright'],
35
                                 res[1]['collectedfrom'],
36
                                 res[1]['context'],
37
                                 //res[1]['resulttype'],
38
                                 res[0],
39
                                 res[1]['creator'],
40
                                 res[1]['language'],
41
                                 res[1]['country'],
42
                                 res[1]['programmingLanguage'],
43
                                 res[2],
44
                                res[1]['journal'] //15
45
                    ]))
46
                    .pipe(map(res => this.parseSoftwareInfo(res, properties)));
47
  }
48

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

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

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

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

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

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

    
121
    if(data[3] != null) {
122
      if(data[3].hasOwnProperty("result")) {
123
        this.softwareInfo.deletedByInferenceIds = [];
124
        let length = Array.isArray(data[3]['result']) ? data[3]['result'].length : 1;
125

    
126
        for(let i=0; i<length; i++) {
127
          let result = Array.isArray(data[3]['result']) ? data[3]['result'][i] : data[3]['result'];
128
          this.softwareInfo.deletedByInferenceIds.push(result.objidentifier);
129
        }
130
      }
131

    
132
      if(data[3].hasOwnProperty("instance")) {
133
        this.softwareInfo.hostedBy_collectedFrom = new Array<{  "downloadName": string, "downloadUrl": string[],
134
                                                                "collectedName": string, "collectedId": string,
135
                                                                "accessMode": string[], "bestAccessMode": string,
136
                                                                "type": string, "year":string}>();
137

    
138
        this.softwareInfo.types = new Array<string>();
139
        let types = new Set<string>();
140

    
141
        let counter = 0;
142
        let instance;
143

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

    
146
        for(let i=0; i<length; i++) {
147
          instance = data[3]['instance'].length!=undefined ? data[3]['instance'][i] : data[3]['instance'];
148

    
149
          this.parsingFunctions.parseTypes(this.softwareInfo.types, types, instance);
150

    
151
          if(instance.hasOwnProperty("webresource")) {
152
            let url;
153
            if(instance['webresource'].length == undefined) {
154
              url = instance['webresource'].url;
155
            } else{
156
              url = instance['webresource'][0].url;
157
            }
158

    
159
            if(instance.hasOwnProperty("hostedby")) {
160
              counter = this.parsingFunctions.parseHostedBy_collectedFrom(this.softwareInfo.hostedBy_collectedFrom, instance, data[0], url, counter/*, this.softwareInfo.title*/, this.softwareInfo.accessMode);
161
            }
162
          }
163
        }
164
      }
165
    }
166

    
167
    if(data[4] != null) {
168
      this.softwareInfo.identifiers = this.parsingFunctions.parseIdentifiers(data[4]);
169
    }
170

    
171
    if(data[5] != null) {
172
      let subjectResults: [string[], Map<string, string[]>, Map<string, string[]>] = this.parsingFunctions.parseAllSubjects(data[5]);
173
      this.softwareInfo.subjects = subjectResults[0];
174
      this.softwareInfo.otherSubjects = subjectResults[1];
175
      this.softwareInfo.classifiedSubjects = subjectResults[2];
176
    }
177

    
178
    // null argument is for journal
179
    this.softwareInfo.hostedBy_collectedFrom = this.parsingFunctions.addPublisherToHostedBy_collectedFrom(
180
                this.softwareInfo.hostedBy_collectedFrom, this.softwareInfo.publisher,
181
                null, this.softwareInfo.identifiers/*, this.softwareInfo.title*/);
182

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

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

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

    
199
    if(data[10] != null) {
200
      if(this.softwareInfo.authors == undefined) {
201
        this.softwareInfo.authors = new Array<{"fullName": string, "orcid": string}>();
202
      }
203

    
204
      let authors = data[10];
205
      let length = Array.isArray(authors) ? authors.length : 1;
206

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

    
221
    if(data[11] != null) {
222
      this.softwareInfo.languages = this.parsingFunctions.parseLanguages(data[11]);
223
    }
224
    if(data[12] != null) {
225
      this.softwareInfo.countries = this.parsingFunctions.parseCountries(data[12]);
226
    }
227

    
228
    if(data[13] != null) {
229
      this.softwareInfo.programmingLanguages = this.parsingFunctions.parseProgrammingLanguages(data[13]);
230
    }
231

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

    
242
      this.softwareInfo.journal['journal'] = data[15].content;
243
      this.softwareInfo.journal['issn'] = data[15].issn;
244
      this.softwareInfo.journal['lissn'] = data[15].lissn;
245
      this.softwareInfo.journal['eissn'] = data[15].eissn;
246
      this.softwareInfo.journal['issue'] = data[15].iss;
247
      this.softwareInfo.journal['volume'] = data[15].vol;
248
      this.softwareInfo.journal['start_page'] = data[15].sp;
249
      this.softwareInfo.journal['end_page'] = data[15].ep;
250
    }
251
    return this.softwareInfo;
252
  }
253
}
(5-5/5)