Project

General

Profile

1
import {Injectable} from '@angular/core';
2
import {Http, Response,Headers, RequestOptions} from '@angular/http';
3
import {Observable}     from 'rxjs/Observable';
4
import {DataProviderInfo} from '../../utils/entities/dataProviderInfo';
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

    
11
@Injectable()
12
export class DataProviderService {
13

    
14
    constructor(private http: Http ) {}
15

    
16
    dataProviderInfo: DataProviderInfo;
17

    
18
    getDataproviderInfo (id: string, properties:EnvProperties):any {
19
        console.info("getDataProviderInfo in service");
20
        let url = properties.searchAPIURLLAst + 'datasources/' +id +"?format=json";
21
        let key = url;
22

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

    
35
    }
36

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

    
41
        let page: number = 0;
42
        let size: number = 1;
43
        return this.http.post(properties.datasourcesAPI+page+"/"+size+"?requestSortBy=id&order=ASCENDING", JSON.stringify({ "id": original_id }), options)
44
            .map(res => <any> res.json())
45
            .map(res => res['datasourceInfo'])
46
            .map(res => this.parseDataproviderAggregationStatus(res));
47
    }
48

    
49
    private handleError (error: Response) {
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 Observable.throw(error  || 'Server error');
54
    }
55

    
56
    parseDataproviderAggregationStatus(data: any): any {
57
        var aggregationStatus: {"fundedContent": string, "indexRecords": string} = null;
58
        if(data != null && data[0] != null) {
59
          aggregationStatus = {"fundedContent": "-1", "indexRecords": "-1"};
60
          aggregationStatus.fundedContent = data[0].fundedContent;
61
          aggregationStatus.indexRecords = data[0].indexRecords;
62
        }
63
        return aggregationStatus;
64
    }
65

    
66
    parseDataProviderInfo (data: any):any {
67
        this.dataProviderInfo = new DataProviderInfo();
68

    
69
        if(data[0] != null) {
70
            this.dataProviderInfo.title = {"name": (data[0].englishname)?data[0].englishname: data[0].officialname, "url": data[0].websiteurl};
71
            this.dataProviderInfo.officialName =  data[0].officialname;
72
            var originalId =(data[0].originalId)?data[0].originalId:"";
73
            if(originalId && originalId != ""){
74
              if(originalId.indexOf("opendoar____::") != -1){
75
                this.dataProviderInfo.openDoarURL = "http://www.opendoar.org/find.php?format=full&rID="+originalId.split("opendoar____::")[1];
76

    
77
              }else if (originalId.indexOf("re3data_____::") != -1){
78
                this.dataProviderInfo.r3DataURL = "http://service.re3data.org/repository/"+originalId.split("re3data_____::")[1];
79
              }
80
              this.dataProviderInfo.originalId = originalId;
81
            }
82

    
83
        }
84

    
85
        if(data[1] != null) {
86
            this.dataProviderInfo.type = data[1].classname;
87

    
88
            if(data[1].classid == "entityregistry" || data[1].classid == "entityregistry::projects" || data[1].classid == "entityregistry::repositories") {
89
                this.dataProviderInfo.registry = true;
90
            } else {
91
                this.dataProviderInfo.registry = false;
92
            }
93

    
94
            if(this.dataProviderInfo.tabs == undefined) {
95
                this.dataProviderInfo.tabs = new Array<{"name": string, "content": string}>();
96
            }
97
            this.dataProviderInfo.tabs = [];
98
            if(this.dataProviderInfo.tabsInTypes.publicationsTab.has(data[1].classid)) {
99
                this.dataProviderInfo.tabs.push({"name": "Publications", "content": "publicationsTab"});
100
                this.dataProviderInfo.tabs2.push("Publications");
101
            }
102
            if(this.dataProviderInfo.tabsInTypes.datasetsTab.has(data[1].classid)) {
103
                this.dataProviderInfo.tabs.push({"name": "Research Data", "content": "datasetsTab"});
104
                this.dataProviderInfo.tabs2.push("Research Data");
105
            }
106

    
107
            if(this.dataProviderInfo.tabsInTypes.projectsTab.has(data[1].classid)) {
108
                this.dataProviderInfo.tabs.push({"name": "Projects", "content": "projectsTab"});
109
                this.dataProviderInfo.tabs2.push("Projects");
110
            }
111
            if(this.dataProviderInfo.tabsInTypes.datasourcesTab.has(data[1].classid)) {
112
                this.dataProviderInfo.tabs.push({"name": "Content Providers", "content": "datasourcesTab"});
113
                this.dataProviderInfo.tabs2.push("Content Providers");
114
            }
115
            this.dataProviderInfo.tabs.push({"name": "Organizations", "content": "organizationsTab"});
116
            this.dataProviderInfo.tabs2.push("Organizations");
117

    
118
            if(this.dataProviderInfo.tabsInTypes.relatedDatasourcesTab.has(data[1].classid)) {
119
                this.dataProviderInfo.tabs.push({"name": "Related Content Providers", "content": "relatedDatasourcesTab"});
120
                this.dataProviderInfo.tabs2.push("Related Content Providers");
121
            }
122

    
123
            if(this.dataProviderInfo.tabsInTypes.statisticsTab.has(data[1].classid)) {
124
                this.dataProviderInfo.tabs.push({"name": "Statistics", "content": "statisticsTab"});
125
                this.dataProviderInfo.tabs2.push("Statistics");
126
            }
127

    
128
            if(this.dataProviderInfo.tabsInTypes.softwareTab.has(data[1].classid)) {
129
                this.dataProviderInfo.tabs.push({"name": "Software", "content": "softwareTab"});
130
                this.dataProviderInfo.tabs2.push("Software");
131
            }
132

    
133
            /*if(this.dataProviderInfo.tabsInTypes.otherTab.has(data[1].classid)) {
134
                this.dataProviderInfo.tabs.push({"name": "Other", "content": "otherTab"});
135
            }*/
136

    
137
            this.dataProviderInfo.tabs.push({"name": "Metrics", "content": "metricsTab"});
138
            this.dataProviderInfo.tabs2.push("Metrics");
139

    
140
            if(this.dataProviderInfo.resultTypes.collectedFrom.has(data[1].classid)) {
141
                this.dataProviderInfo.resultsBy = "collectedFrom";
142
            } else if(this.dataProviderInfo.resultTypes.hostedBy.has(data[1].classid)) {
143
                this.dataProviderInfo.resultsBy = "hostedBy";
144
            }
145
        }
146

    
147
        if(!this.dataProviderInfo.registry) {
148
            if(data[2] != null) {
149
                this.dataProviderInfo.compatibility = {"info": "", "name": "", "id": ""};
150
                this.dataProviderInfo.compatibility.info = data[2].classname;
151
                //this.dataProviderInfo.compatibility = data[2].classname;
152
            }
153

    
154
            if(data[2] != null && data[2].classid == "hostedBy" && data[3] != null) {
155
              this.dataProviderInfo.compatibility.name = data[3].name;
156
              this.dataProviderInfo.compatibility.id = data[3].id;
157

    
158
              if(this.dataProviderInfo.compatibility.name) {
159
                this.dataProviderInfo.compatibility.info = "Collected from ";
160
              }
161
            }
162

    
163
            if(data[4] != null) {
164
                let oaiPmhURL: string;
165
                if(Array.isArray(data[4])) {
166
                    oaiPmhURL = data[4][0];
167
                }
168
                else {
169
                    oaiPmhURL = data[4];
170
                }
171

    
172
                if(oaiPmhURL != '' && oaiPmhURL != 'unknown') {
173
                    this.dataProviderInfo.oaiPmhURL = oaiPmhURL;
174
                }
175
            }
176
        }
177

    
178
        if(data[5] != null) {
179
            let mydata;
180
            let counter = 0;
181
            let countriesSet: Set<string>;
182
            let length = data[5].length!=undefined ? data[5].length : 1;
183

    
184
            for(let i=0; i<length; i++) {
185
                mydata = data[5].length!=undefined ? data[5][i] : data[5];
186
                if(mydata.hasOwnProperty("to")) {
187
                    if(mydata['to'].class == "provides" && mydata['to'].type == "organization") {
188
                        //if(this.dataProviderInfo.organizations == undefined) {
189
                        if(this.dataProviderInfo.organizations.length == 0) {
190
                            //this.dataProviderInfo.organizations = new Array<{"name": string, "url": string}>();
191
                            this.dataProviderInfo.countries = new Array<string>();
192
                            countriesSet = new Set<string>();
193
                        }
194

    
195
                        this.dataProviderInfo.organizations[counter] = {"name": "", "id": ""};
196
                        this.dataProviderInfo.organizations[counter]['name'] = mydata.legalname;
197
                        this.dataProviderInfo.organizations[counter]['id'] = mydata['to'].content;
198

    
199
                        if(mydata.country != '' && mydata['country'].classname != '') {
200
                            if(!countriesSet.has(mydata['country'].classname)) {
201
                                this.dataProviderInfo.countries.push(mydata['country'].classname);
202
                                countriesSet.add(mydata['country'].classname);
203
                            }
204
                        }
205

    
206
                        counter++;
207
                    }
208
                }
209
            }
210
        }
211

    
212
        return this.dataProviderInfo;
213
    }
214
}
(5-5/13)