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
    console.info("getSoftwareInfo in service");
24

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

    
28

    
29
    return this.http.get((properties.useCache)? (properties.cacheUrl+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'], res])
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
                                 res[1]['language'],
46
                                 res[1]['country'],
47
                                 res[1]['programmingLanguage'],
48
                                 res[2]
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
    this.softwareInfo.record = data[14];
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
    /*
75
    this.softwareInfo.title = {"name": "", "url": "", "accessMode": ""};
76
    if(data[0]['bestaccessright'] && data[0]['bestaccessright'] && data[0]['bestaccessright'].hasOwnProperty("classid")) {
77
      this.softwareInfo.title.accessMode = data[0]['bestaccessright'].classid;
78
    }
79
    if(data[1] != null) {
80
      if(Array.isArray(data[1])) {
81
        this.softwareInfo.title['name'] = data[1][0].content;
82
      } else {
83
        this.softwareInfo.title['name'] = data[1].content;
84
      }
85
    }
86
    */
87
    if(data[0]['bestaccessright'] && data[0]['bestaccessright'] && data[0]['bestaccessright'].hasOwnProperty("classid")) {
88
      this.softwareInfo.accessMode = data[0]['bestaccessright'].classid;
89
    }
90
    if(data[1] != null) {
91
      if(Array.isArray(data[1])) {
92
        this.softwareInfo.title = data[1][0].content;
93
      } else {
94
        this.softwareInfo.title = data[1].content;
95
      }
96
    }
97

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

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

    
115
            this.softwareInfo.relatedResearchResults = this.parsingFunctions.parseRelatedResearchResults(this.softwareInfo.relatedResearchResults, relation, provenanceAction);
116

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

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

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

    
134
        let counter = 0;
135
        let instance;
136

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
229
    return this.softwareInfo;
230
  }
231
}
(5-5/5)