Project

General

Profile

1
import {Injectable} from '@angular/core';
2
import {Http, Response} from '@angular/http';
3
import {Observable}     from 'rxjs/Observable';
4
import {DataProviderInfo} from '../../utils/entities/dataProviderInfo';
5
import {OpenaireProperties} from '../../utils/properties/openaireProperties';
6
import 'rxjs/add/observable/of';
7
import 'rxjs/add/operator/do';
8
import 'rxjs/add/operator/share';
9

    
10

    
11
@Injectable()
12
export class DataProviderService {
13

    
14
    constructor(private http: Http ) {}
15

    
16
    dataProviderInfo: DataProviderInfo;
17

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

    
23
        return this.http.get((OpenaireProperties.isCacheEnabled())? (OpenaireProperties.getCacheUrl()+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']['accessinfopackage'],
30
                                 res['oaf:datasource']['rels']['rel']
31
                     ])
32
                     .map(res => this.parseDataProviderInfo(res));
33

    
34
    }
35

    
36
    private handleError (error: Response) {
37
    // in a real world app, we may send the error to some remote logging infrastructure
38
    // instead of just logging it to the console
39
        console.log(error);
40
        return Observable.throw(error  || 'Server error');
41
    }
42

    
43
    parseDataProviderInfo (data: any):any {
44
        this.dataProviderInfo = new DataProviderInfo();
45

    
46
        if(data[0] != null) {
47
            this.dataProviderInfo.title = {"name": (data[0].englishname)?data[0].englishname: data[0].officialname, "url": data[0].websiteurl};
48
            this.dataProviderInfo.officialName =  data[0].officialname;
49
            var originalId =(data[0].originalId)?data[0].originalId:"";
50
            if(originalId && originalId != ""){
51
              if(originalId.indexOf("opendoar____::") != -1){
52
                this.dataProviderInfo.openDoarURL = "http://www.opendoar.org/find.php?format=full&rID="+originalId.split("opendoar____::")[1];
53

    
54
              }else if (originalId.indexOf("re3data_____::") != -1){
55
                this.dataProviderInfo.r3DataURL = "http://service.re3data.org/repository/"+originalId.split("re3data_____::")[1];
56
              }
57
            }
58

    
59
        }
60

    
61
        if(data[1] != null) {
62
            this.dataProviderInfo.type = data[1].classname;
63

    
64
            if(data[1].classid == "entityregistry" || data[1].classid == "entityregistry::projects" || data[1].classid == "entityregistry::repositories") {
65
                this.dataProviderInfo.registry = true;
66
            } else {
67
                this.dataProviderInfo.registry = false;
68
            }
69

    
70
            if(this.dataProviderInfo.tabs == undefined) {
71
                this.dataProviderInfo.tabs = new Array<{"name": string, "content": string}>();
72
            }
73

    
74
            if(this.dataProviderInfo.tabsInTypes.publicationsTab.has(data[1].classid)) {
75
                this.dataProviderInfo.tabs.push({"name": "Publications", "content": "publicationsTab"});
76
            }
77
            if(this.dataProviderInfo.tabsInTypes.datasetsTab.has(data[1].classid)) {
78
                this.dataProviderInfo.tabs.push({"name": "Research Data", "content": "datasetsTab"});
79
            }
80

    
81
            if(this.dataProviderInfo.tabsInTypes.projectsTab.has(data[1].classid)) {
82
                this.dataProviderInfo.tabs.push({"name": "Projects", "content": "projectsTab"});
83
            }
84
            if(this.dataProviderInfo.tabsInTypes.datasourcesTab.has(data[1].classid)) {
85
                this.dataProviderInfo.tabs.push({"name": "Content Providers", "content": "datasourcesTab"});
86
            }
87
            this.dataProviderInfo.tabs.push({"name": "Organizations", "content": "organizationsTab"});
88

    
89
            if(this.dataProviderInfo.tabsInTypes.relatedDatasourcesTab.has(data[1].classid)) {
90
                this.dataProviderInfo.tabs.push({"name": "Related Content Providers", "content": "relatedDatasourcesTab"});
91
            }
92

    
93
            if(this.dataProviderInfo.tabsInTypes.statisticsTab.has(data[1].classid)) {
94
                this.dataProviderInfo.tabs.push({"name": "Statistics", "content": "statisticsTab"});
95
            }
96

    
97
            this.dataProviderInfo.tabs.push({"name": "Metrics", "content": "metricsTab"});
98

    
99
            if(this.dataProviderInfo.resultTypes.collectedFrom.has(data[1].classid)) {
100
                this.dataProviderInfo.resultsBy = "collectedFrom";
101
            } else if(this.dataProviderInfo.resultTypes.hostedBy.has(data[1].classid)) {
102
                this.dataProviderInfo.resultsBy = "hostedBy";
103
            }
104
        }
105

    
106
        if(!this.dataProviderInfo.registry) {
107
            if(data[2] != null) {
108
                this.dataProviderInfo.compatibility = data[2].classname;
109
            }
110

    
111
            if(data[3] != null) {
112
                let oaiPmhURL: string;
113
                if(Array.isArray(data[3])) {
114
                    oaiPmhURL = data[3][0];
115
                }
116
                else {
117
                    oaiPmhURL = data[3];
118
                }
119

    
120
                if(oaiPmhURL != '' && oaiPmhURL != 'unknown') {
121
                    this.dataProviderInfo.oaiPmhURL = oaiPmhURL;
122
                }
123
            }
124
        }
125

    
126
        if(data[4] != null) {
127
            let mydata;
128
            let counter = 0;
129
            let countriesSet: Set<string>;
130
            let length = data[4].length!=undefined ? data[4].length : 1;
131

    
132
            for(let i=0; i<length; i++) {
133
                mydata = data[4].length!=undefined ? data[4][i] : data[4];
134
                if(mydata.hasOwnProperty("to")) {
135
                    if(mydata['to'].class == "provides" && mydata['to'].type == "organization") {
136
                        //if(this.dataProviderInfo.organizations == undefined) {
137
                        if(this.dataProviderInfo.organizations.length == 0) {
138
                            //this.dataProviderInfo.organizations = new Array<{"name": string, "url": string}>();
139
                            this.dataProviderInfo.countries = new Array<string>();
140
                            countriesSet = new Set<string>();
141
                        }
142

    
143
                        this.dataProviderInfo.organizations[counter] = {"name": "", "id": ""};
144
                        this.dataProviderInfo.organizations[counter]['name'] = mydata.legalname;
145
                        this.dataProviderInfo.organizations[counter]['id'] = /*OpenaireProperties.getsearchLinkToOrganization()+*/mydata['to'].content;
146

    
147
                        if(mydata.country != '' && mydata['country'].classname != '') {
148
                            if(!countriesSet.has(mydata['country'].classname)) {
149
                                this.dataProviderInfo.countries.push(mydata['country'].classname);
150
                                countriesSet.add(mydata['country'].classname);
151
                            }
152
                        }
153

    
154
                        counter++;
155
                    }
156
                }
157
            }
158
        }
159

    
160
        return this.dataProviderInfo;
161
    }
162
}
(5-5/12)