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} = null;
66
        if(data != null && data[0] != null) {
67
          aggregationStatus = {"fundedContent": "-1", "indexRecords": "-1", "fulltexts": "-1"};
68
          aggregationStatus.fundedContent = data[0].fundedContent;
69
          aggregationStatus.indexRecords = data[0].indexRecords;
70
          aggregationStatus.fulltexts = data[0].fulltexts;
71
        }
72
        return aggregationStatus;
73
    }
74

    
75
    parseDataProviderInfo (data: any):any {
76
        this.dataProviderInfo = new DataProviderInfo();
77

    
78
        if(data[0] != null) {
79
            this.dataProviderInfo.title = {"name": (data[0].englishname)?data[0].englishname: data[0].officialname, "url": data[0].websiteurl};
80
            this.dataProviderInfo.officialName =  data[0].officialname;
81

    
82
            var pattern = /.{12}::.+/;
83
            var originalIds =(data[0].originalId)?data[0].originalId:"";
84

    
85
            let length = Array.isArray(originalIds) ? originalIds.length : 1;
86

    
87
            for(let i=0; i<length; i++) {
88
              var originalId = Array.isArray(originalIds) ? originalIds[i] : originalIds;
89
              var matched = originalId.match(pattern);
90

    
91
              if(matched && originalId && originalId != ""){
92
                if(originalId.indexOf("opendoar____::") != -1){
93
                  this.dataProviderInfo.openDoarId = originalId.split("opendoar____::")[1];
94
                }else if (originalId.indexOf("re3data_____::") != -1){
95
                  this.dataProviderInfo.r3DataId = originalId.split("re3data_____::")[1];
96
                }
97
                this.dataProviderInfo.originalId = originalId;
98
              }
99
            }
100
        }
101

    
102
        if(data[1] != null) {
103
            this.dataProviderInfo.type = data[1].classname;
104

    
105
            if(data[1].classid == "entityregistry" || data[1].classid == "entityregistry::projects" || data[1].classid == "entityregistry::repositories") {
106
                this.dataProviderInfo.registry = true;
107
            } else {
108
                this.dataProviderInfo.registry = false;
109
            }
110

    
111
            if(this.dataProviderInfo.tabs == undefined) {
112
                this.dataProviderInfo.tabs = new Array<{"name": string, "content": string}>();
113
            }
114
            this.dataProviderInfo.tabs = [];
115
            if(this.dataProviderInfo.tabsInTypes.publicationsTab.has(data[1].classid)) {
116
                this.dataProviderInfo.tabs.push({"name": "Publications", "content": "publicationsTab"});
117
                this.dataProviderInfo.tabs2.push("Publications");
118
            }
119
            if(this.dataProviderInfo.tabsInTypes.datasetsTab.has(data[1].classid)) {
120
                this.dataProviderInfo.tabs.push({"name": "Research Data", "content": "datasetsTab"});
121
                this.dataProviderInfo.tabs2.push("Research Data");
122
            }
123

    
124
            if(this.dataProviderInfo.tabsInTypes.projectsTab.has(data[1].classid)) {
125
                this.dataProviderInfo.tabs.push({"name": "Projects", "content": "projectsTab"});
126
                this.dataProviderInfo.tabs2.push("Projects");
127
            }
128
            if(this.dataProviderInfo.tabsInTypes.datasourcesTab.has(data[1].classid)) {
129
                this.dataProviderInfo.tabs.push({"name": "Content Providers", "content": "datasourcesTab"});
130
                this.dataProviderInfo.tabs2.push("Content Providers");
131
            }
132
            this.dataProviderInfo.tabs.push({"name": "Organizations", "content": "organizationsTab"});
133
            this.dataProviderInfo.tabs2.push("Organizations");
134

    
135
            if(this.dataProviderInfo.tabsInTypes.relatedDatasourcesTab.has(data[1].classid)) {
136
                this.dataProviderInfo.tabs.push({"name": "Related Content Providers", "content": "relatedDatasourcesTab"});
137
                this.dataProviderInfo.tabs2.push("Related Content Providers");
138
            }
139

    
140
            if(this.dataProviderInfo.tabsInTypes.statisticsTab.has(data[1].classid)) {
141
                this.dataProviderInfo.tabs.push({"name": "Statistics", "content": "statisticsTab"});
142
                this.dataProviderInfo.tabs2.push("Statistics");
143
            }
144

    
145
            if(this.dataProviderInfo.tabsInTypes.softwareTab.has(data[1].classid)) {
146
                this.dataProviderInfo.tabs.push({"name": "Software", "content": "softwareTab"});
147
                this.dataProviderInfo.tabs2.push("Software");
148
            }
149

    
150
            if(this.dataProviderInfo.tabsInTypes.orpsTab.has(data[1].classid)) {
151
                this.dataProviderInfo.tabs.push({"name": "Other Research Products", "content": "orpsTab"});
152
                this.dataProviderInfo.tabs2.push("Other Research Products");
153
            }
154

    
155
            if(this.dataProviderInfo.tabsInTypes.metricsTab.has(data[1].classid)) {
156
                this.dataProviderInfo.tabs.push({"name": "Metrics", "content": "metricsTab"});
157
                this.dataProviderInfo.tabs2.push("Metrics");
158
            }
159

    
160
            if(this.dataProviderInfo.resultTypes.collectedFrom.has(data[1].classid)) {
161
                this.dataProviderInfo.resultsBy = "collectedFrom";
162
            } else if(this.dataProviderInfo.resultTypes.hostedBy.has(data[1].classid)) {
163
                this.dataProviderInfo.resultsBy = "hostedBy";
164
            }
165
        }
166

    
167
        if(!this.dataProviderInfo.registry) {
168
            if(data[2] != null) {
169
                this.dataProviderInfo.compatibility = {"info": "", "name": "", "id": ""};
170
                this.dataProviderInfo.compatibility.info = data[2].classname;
171
                //this.dataProviderInfo.compatibility = data[2].classname;
172
            }
173

    
174
            if(data[2] != null && data[2].classid == "hostedBy" && data[3] != null) {
175
              this.dataProviderInfo.compatibility.name = data[3].name;
176
              this.dataProviderInfo.compatibility.id = data[3].id;
177

    
178
              if(this.dataProviderInfo.compatibility.name) {
179
                this.dataProviderInfo.compatibility.info = "Collected from ";
180
              }
181
            }
182

    
183
            if(data[4] != null) {
184
                let oaiPmhURL:string;
185
                oaiPmhURL = Array.isArray(data[4]) ? data[4][0]:data[4];
186
                if(oaiPmhURL != '' && oaiPmhURL != 'unknown') {
187
                    this.dataProviderInfo.oaiPmhURL = oaiPmhURL;
188
                }
189
            }
190
        }
191

    
192
        if(data[5] != null) {
193
            let mydata;
194
            let counter = 0;
195
            let countriesSet: Set<string>;
196
            let length = data[5].length!=undefined ? data[5].length : 1;
197

    
198
            for(let i=0; i<length; i++) {
199
                mydata = data[5].length!=undefined ? data[5][i] : data[5];
200
                if(mydata.hasOwnProperty("to")) {
201
                    if(mydata['to'].class == "provides" && mydata['to'].type == "organization") {
202
                        //if(this.dataProviderInfo.organizations == undefined) {
203
                        if(this.dataProviderInfo.organizations.length == 0) {
204
                            //this.dataProviderInfo.organizations = new Array<{"name": string, "url": string}>();
205
                            this.dataProviderInfo.countries = new Array<string>();
206
                            countriesSet = new Set<string>();
207
                        }
208

    
209
                        this.dataProviderInfo.organizations[counter] = {"name": "", "id": ""};
210
                        this.dataProviderInfo.organizations[counter]['name'] = mydata.legalname;
211
                        this.dataProviderInfo.organizations[counter]['id'] = mydata['to'].content;
212

    
213
                        if(mydata.country != '' && mydata['country'].classname != '') {
214
                            if(!countriesSet.has(mydata['country'].classname)) {
215
                                this.dataProviderInfo.countries.push(mydata['country'].classname);
216
                                countriesSet.add(mydata['country'].classname);
217
                            }
218
                        }
219

    
220
                        counter++;
221
                    }
222
                }
223
            }
224
        }
225
        if(data[6] != null) {
226
            this.dataProviderInfo.journal = {"journal": "", "issn": "", "lissn": "", "eissn": ""};
227
            this.dataProviderInfo.journal['journal'] = data[6].content;
228
            this.dataProviderInfo.journal['issn'] = data[6]['issn'];
229
            this.dataProviderInfo.journal['lissn'] = data[6]['lissn'];
230
            this.dataProviderInfo.journal['eissn'] = data[6]['eissn'];
231

    
232
        }else{
233
            this.dataProviderInfo.journal = null;
234
            this.dataProviderInfo.journal = {"journal": "", "issn": "", "lissn": "", "eissn": ""};
235
        }
236

    
237
        return this.dataProviderInfo;
238
    }
239
}
(4-4/13)