Project

General

Profile

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

    
7

    
8

    
9
import{EnvProperties} from '../../utils/properties/env-properties';
10
import {map} from "rxjs/operators";
11

    
12

    
13
@Injectable()
14
export class DataProviderService {
15

    
16
    constructor(private http: HttpClient ) {}
17

    
18
    dataProviderInfo: DataProviderInfo;
19

    
20
    getDataproviderInfo (id: string, properties:EnvProperties):any {
21
        let url = properties.searchAPIURLLAst + 'datasources/' +id +"?format=json";
22
        let key = url;
23

    
24
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
25
                    //.map(res => <any> res.json())
26
                    .pipe(map(res => res['result']['metadata']['oaf:entity']))
27
                    .pipe(map(res => [res['oaf:datasource'],
28
                                 res['oaf:datasource']['datasourcetype'],
29
                                 res['oaf:datasource']['openairecompatibility'],
30
                                 res['oaf:datasource']['collectedfrom'],
31
                                 res['oaf:datasource']['accessinfopackage'],
32
                                 res['oaf:datasource']['rels']['rel'],
33
                                 res['oaf:datasource']['journal'] //6
34
                     ]))
35
                     .pipe(map(res => this.parseDataProviderInfo(res)));
36

    
37
    }
38

    
39
    getDataproviderAggregationStatus(original_id: string, properties:EnvProperties):any {
40
        //let headers = new Headers({'Content-Type': 'application/json', 'accept': 'application/json'});
41
        //let options = new RequestOptions({headers: headers});
42

    
43
      const options = {
44
        headers: new HttpHeaders({
45
          'Content-Type': 'application/json',
46
          'accept': 'application/json'})
47
      };
48

    
49
      let page: number = 0;
50
        let size: number = 1;
51
        return this.http.post(properties.datasourcesAPI+page+"/"+size+"?requestSortBy=id&order=ASCENDING", JSON.stringify({ "id": original_id }), options)
52
            //.map(res => <any> res.json())
53
            .pipe(map(res => res['datasourceInfo']))
54
            .pipe(map(res => this.parseDataproviderAggregationStatus(res)));
55
    }
56

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

    
64
    parseDataproviderAggregationStatus(data: any): any {
65
        var aggregationStatus: {"fundedContent": string, "indexRecords": string, "fulltexts": string, "lastUpdateDate": string} = null;
66
        if(data != null && data[0] != null) {
67
          aggregationStatus = {"fundedContent": "-1", "indexRecords": "-1", "fulltexts": "-1", "lastUpdateDate": null};
68
          aggregationStatus.fundedContent = data[0].fundedContent;
69
          aggregationStatus.indexRecords = data[0].indexRecords;
70
          aggregationStatus.fulltexts = data[0].fulltexts;
71

    
72
          if(data[0].hasOwnProperty("aggregationHistory")) {
73
            let length = Array.isArray(data[0]["aggregationHistory"]) ? data[0]["aggregationHistory"].length : 1;
74

    
75
            for(let i=0; i<length; i++) {
76
              var aggregationHistory = Array.isArray(data[0]["aggregationHistory"]) ? data[0]["aggregationHistory"][i] : data[0]["aggregationHistory"];
77
              if(aggregationHistory && aggregationHistory.indexedVersion == true) {
78
                aggregationStatus.lastUpdateDate = aggregationHistory.date;
79
                break;
80
              }
81
            }
82
          }
83
        }
84
        return aggregationStatus;
85
    }
86

    
87
    parseDataProviderInfo (data: any):any {
88
        this.dataProviderInfo = new DataProviderInfo();
89

    
90
        if(data[0] != null) {
91
            this.dataProviderInfo.title = {"name": (data[0].englishname)?data[0].englishname: data[0].officialname, "url": data[0].websiteurl};
92
            this.dataProviderInfo.officialName =  data[0].officialname;
93

    
94
            var pattern = /.{12}::.+/;
95
            var originalIds =(data[0].originalId)?data[0].originalId:"";
96

    
97
            let length = Array.isArray(originalIds) ? originalIds.length : 1;
98

    
99
            for(let i=0; i<length; i++) {
100
              var originalId = Array.isArray(originalIds) ? originalIds[i] : originalIds;
101
              var matched = originalId.match(pattern);
102

    
103
              if(matched && originalId && originalId != ""){
104
                if(originalId.indexOf("opendoar____::") != -1){
105
                  this.dataProviderInfo.openDoarId = originalId.split("opendoar____::")[1];
106
                }else if (originalId.indexOf("re3data_____::") != -1){
107
                  this.dataProviderInfo.r3DataId = originalId.split("re3data_____::")[1];
108
                }
109
                this.dataProviderInfo.originalId = originalId;
110
              }
111
            }
112

    
113
            this.dataProviderInfo.subjects = [];
114
            length = Array.isArray(data[0]['subjects']) ? data[0]['subjects'].length : 1;
115
            for(let i=0; i<length; i++) {
116
              let subject = Array.isArray(data[0]['subjects']) ? data[0]['subjects'][i] :data[0]['subjects'];
117
              if(subject && subject.content) {
118
                this.dataProviderInfo.subjects.push(subject.content);
119
              }
120
            }
121

    
122
            if(!Array.isArray(data[0]['description'])) {
123
              this.dataProviderInfo.description = (data[0]['description']) ? String(data[0]['description']) : "";
124
            } else {
125
              this.dataProviderInfo.description = (data[0]['description'][0]) ? String(data[0]['description'][0]) : "";
126
            }
127
        }
128

    
129
        if(data[1] != null) {
130
            this.dataProviderInfo.type = data[1].classname;
131

    
132
            if(data[1].classid == "entityregistry" || data[1].classid == "entityregistry::projects" || data[1].classid == "entityregistry::repositories") {
133
                this.dataProviderInfo.registry = true;
134
            } else {
135
                this.dataProviderInfo.registry = false;
136
            }
137

    
138
            if(this.dataProviderInfo.tabs == undefined) {
139
                this.dataProviderInfo.tabs = new Array<{"name": string, "content": string}>();
140
            }
141
            this.dataProviderInfo.tabs = [];
142
            if(this.dataProviderInfo.tabsInTypes.publicationsTab.has(data[1].classid)) {
143
                this.dataProviderInfo.tabs.push({"name": "Publications", "content": "publicationsTab"});
144
                this.dataProviderInfo.tabs2.push("Publications");
145
            }
146
            if(this.dataProviderInfo.tabsInTypes.datasetsTab.has(data[1].classid)) {
147
                this.dataProviderInfo.tabs.push({"name": "Research Data", "content": "datasetsTab"});
148
                this.dataProviderInfo.tabs2.push("Research Data");
149
            }
150

    
151
            if(this.dataProviderInfo.tabsInTypes.projectsTab.has(data[1].classid)) {
152
                this.dataProviderInfo.tabs.push({"name": "Projects", "content": "projectsTab"});
153
                this.dataProviderInfo.tabs2.push("Projects");
154
            }
155
            if(this.dataProviderInfo.tabsInTypes.datasourcesTab.has(data[1].classid)) {
156
                this.dataProviderInfo.tabs.push({"name": "Content Providers", "content": "datasourcesTab"});
157
                this.dataProviderInfo.tabs2.push("Content Providers");
158
            }
159

    
160
            if(this.dataProviderInfo.tabsInTypes.relatedDatasourcesTab.has(data[1].classid)) {
161
                this.dataProviderInfo.tabs.push({"name": "Related Content Providers", "content": "relatedDatasourcesTab"});
162
                this.dataProviderInfo.tabs2.push("Related Content Providers");
163
            }
164

    
165
            if(this.dataProviderInfo.tabsInTypes.statisticsTab.has(data[1].classid)) {
166
                this.dataProviderInfo.tabs.push({"name": "Statistics", "content": "statisticsTab"});
167
                this.dataProviderInfo.tabs2.push("Statistics");
168
            }
169

    
170
            if(this.dataProviderInfo.tabsInTypes.softwareTab.has(data[1].classid)) {
171
                this.dataProviderInfo.tabs.push({"name": "Software", "content": "softwareTab"});
172
                this.dataProviderInfo.tabs2.push("Software");
173
            }
174

    
175
            if(this.dataProviderInfo.tabsInTypes.orpsTab.has(data[1].classid)) {
176
                this.dataProviderInfo.tabs.push({"name": "Other Research Products", "content": "orpsTab"});
177
                this.dataProviderInfo.tabs2.push("Other Research Products");
178
            }
179

    
180
            if(this.dataProviderInfo.tabsInTypes.metricsTab.has(data[1].classid)) {
181
                this.dataProviderInfo.tabs.push({"name": "Metrics", "content": "metricsTab"});
182
                this.dataProviderInfo.tabs2.push("Metrics");
183
            }
184

    
185
            if(this.dataProviderInfo.resultTypes.collectedFrom.has(data[1].classid)) {
186
                this.dataProviderInfo.resultsBy = "collectedFrom";
187
            } else if(this.dataProviderInfo.resultTypes.hostedBy.has(data[1].classid)) {
188
                this.dataProviderInfo.resultsBy = "hostedBy";
189
            }
190
        }
191

    
192
        if(!this.dataProviderInfo.registry) {
193
            if(data[2] != null) {
194
                this.dataProviderInfo.compatibility = {"info": "", "name": "", "id": ""};
195
                this.dataProviderInfo.compatibility.info = data[2].classname;
196
                //this.dataProviderInfo.compatibility = data[2].classname;
197
            }
198

    
199
            if(data[2] != null && data[2].classid == "hostedBy" && data[3] != null) {
200
              this.dataProviderInfo.compatibility.name = data[3].name;
201
              this.dataProviderInfo.compatibility.id = data[3].id;
202

    
203
              if(this.dataProviderInfo.compatibility.name) {
204
                this.dataProviderInfo.compatibility.info = "Collected from ";
205
              }
206
            }
207

    
208
            if(data[4] != null) {
209
                let oaiPmhURL:string;
210
                oaiPmhURL = Array.isArray(data[4]) ? data[4][0]:data[4];
211
                if(oaiPmhURL != '' && oaiPmhURL != 'unknown') {
212
                    this.dataProviderInfo.oaiPmhURL = oaiPmhURL;
213
                }
214
            }
215
        }
216

    
217
        if(data[5] != null) {
218
            let mydata;
219
            let counter = 0;
220
            let countriesSet: Set<string>;
221
            let length = data[5].length!=undefined ? data[5].length : 1;
222

    
223
            for(let i=0; i<length; i++) {
224
                mydata = data[5].length!=undefined ? data[5][i] : data[5];
225
                if(mydata.hasOwnProperty("to")) {
226
                    if(mydata['to'].class == "provides" && mydata['to'].type == "organization") {
227
                        //if(this.dataProviderInfo.organizations == undefined) {
228
                        if(this.dataProviderInfo.organizations.length == 0) {
229
                            //this.dataProviderInfo.organizations = new Array<{"name": string, "url": string}>();
230
                            this.dataProviderInfo.countries = new Array<string>();
231
                            countriesSet = new Set<string>();
232
                        }
233

    
234
                        this.dataProviderInfo.organizations[counter] = {"acronym": "", "name": "", "id": ""};
235
                        //this.dataProviderInfo.organizations[counter]['name'] = (mydata.legalname ? mydata.legalname : "[no title available");
236
                        this.dataProviderInfo.organizations[counter]['id'] = mydata['to'].content;
237

    
238
                        if(mydata.hasOwnProperty("legalshortname")) {
239
                          this.dataProviderInfo.organizations[counter]['acronym'] = mydata.legalshortname;
240
                        }
241
                        if(mydata.hasOwnProperty("legalname")) {
242
                          this.dataProviderInfo.organizations[counter]['name'] = mydata.legalname;
243
                        }
244
                        if(!this.dataProviderInfo.organizations[counter]['acronym'] && !this.dataProviderInfo.organizations[counter]['name']){
245
                          // acronym is displayed with link and name only in tooltip
246
                          this.dataProviderInfo.organizations[counter]['acronym'] = "[no title available]";
247
                        }
248

    
249
                        if(mydata.country != '' && mydata['country'].classname != '') {
250
                            if(!countriesSet.has(mydata['country'].classname)) {
251
                                this.dataProviderInfo.countries.push(mydata['country'].classname);
252
                                countriesSet.add(mydata['country'].classname);
253
                            }
254
                        }
255

    
256
                        counter++;
257
                    }
258
                }
259
            }
260
        }
261
        if(data[6] != null) {
262
            this.dataProviderInfo.journal = {"journal": "", "issn": "", "lissn": "", "eissn": ""};
263
            this.dataProviderInfo.journal['journal'] = data[6].content;
264
            this.dataProviderInfo.journal['issn'] = data[6]['issn'];
265
            this.dataProviderInfo.journal['lissn'] = data[6]['lissn'];
266
            this.dataProviderInfo.journal['eissn'] = data[6]['eissn'];
267

    
268
        }else{
269
            this.dataProviderInfo.journal = null;
270
            this.dataProviderInfo.journal = {"journal": "", "issn": "", "lissn": "", "eissn": ""};
271
        }
272

    
273
        return this.dataProviderInfo;
274
    }
275
}
(4-4/13)