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
import { CacheService  } from '../../shared/cache.service';
10

    
11
@Injectable()
12
export class DataProviderService {
13

    
14
    constructor(private http: Http, public _cache: CacheService) {}
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
        if (this._cache.has(key)) {
23
          return Observable.of(this._cache.get(key)).map(res => this.parseDataProviderInfo(res));
24
        }
25
        return this.http.get(url)
26
                    .map(res => <any> res.json())
27
                    .map(res => res['result']['metadata']['oaf:entity'])
28
                    .map(res => [res['oaf:datasource'],
29
                                 res['oaf:datasource']['datasourcetype'],
30
                                 res['oaf:datasource']['openairecompatibility'],
31
                                 res['oaf:datasource']['accessinfopackage'],
32
                                 res['oaf:datasource']['rels']['rel']
33
                     ]).do(res => {
34
                       this._cache.set(key, res);
35
                     })
36
                     .map(res => this.parseDataProviderInfo(res));
37

    
38
    }
39

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

    
47
    parseDataProviderInfo (data: any):any {
48
        this.dataProviderInfo = new DataProviderInfo();
49

    
50
        if(data[0] != null) {
51
            this.dataProviderInfo.title = {"name": data[0].officialname, "url": data[0].websiteurl};
52
        }
53

    
54
        if(data[1] != null) {
55
            this.dataProviderInfo.type = data[1].classname;
56

    
57
            if(data[1].classid == "entityregistry" || data[1].classid == "entityregistry::projects" || data[1].classid == "entityregistry::repositories") {
58
                this.dataProviderInfo.registry = true;
59
                console.info("true");
60
            } else {
61
                this.dataProviderInfo.registry = false;
62
                console.info("false");
63
            }
64
            console.info(this.dataProviderInfo.type);
65

    
66
            if(this.dataProviderInfo.tabs == undefined) {
67
                this.dataProviderInfo.tabs = new Array<{"name": string, "content": string}>();
68
            }
69
            if(this.dataProviderInfo.tabsInTypes.publicationsTab.has(data[1].classid)) {
70
                this.dataProviderInfo.tabs.push({"name": "Publications", "content": "publicationsTab"});
71
            }
72
            if(this.dataProviderInfo.tabsInTypes.datasetsTab.has(data[1].classid)) {
73
                this.dataProviderInfo.tabs.push({"name": "Datasets", "content": "datasetsTab"});
74
            }
75

    
76
            if(this.dataProviderInfo.tabsInTypes.projectsTab.has(data[1].classid)) {
77
                this.dataProviderInfo.tabs.push({"name": "Projects", "content": "projectsTab"});
78
            }
79
            if(this.dataProviderInfo.tabsInTypes.datasourcesTab.has(data[1].classid)) {
80
                this.dataProviderInfo.tabs.push({"name": "Datasources", "content": "datasourcesTab"});
81
            }
82
            this.dataProviderInfo.tabs.push({"name": "Organizations", "content": "organizationsTab"});
83

    
84
            if(this.dataProviderInfo.tabsInTypes.relatedDatasourcesTab.has(data[1].classid)) {
85
                this.dataProviderInfo.tabs.push({"name": "Related Data Providers", "content": "relatedDatasourcesTab"});
86
            }
87

    
88
            if(this.dataProviderInfo.tabsInTypes.statisticsTab.has(data[1].classid)) {
89
                this.dataProviderInfo.tabs.push({"name": "Statistics", "content": "statisticsTab"});
90
            }
91

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

    
94
            if(this.dataProviderInfo.resultTypes.collectedFrom.has(data[1].classid)) {
95
                this.dataProviderInfo.resultsBy = "collectedFrom";
96
            } else if(this.dataProviderInfo.resultTypes.hostedBy.has(data[1].classid)) {
97
                this.dataProviderInfo.resultsBy = "hostedBy";
98
            }
99
        }
100

    
101
        if(!this.dataProviderInfo.registry) {
102
            if(data[2] != null) {
103
                this.dataProviderInfo.compatibility = data[2].classname;
104
            }
105

    
106
            if(data[3] != null) {
107
                let oaiPmhURL: string;
108
                if(Array.isArray(data[3])) {
109
                    oaiPmhURL = data[3][0];
110
                }
111
                else {
112
                    oaiPmhURL = data[3];
113
                }
114

    
115
                if(oaiPmhURL != '' && oaiPmhURL != 'unknown') {
116
                    this.dataProviderInfo.oaiPmhURL = oaiPmhURL;
117
                }
118
            }
119
        }
120

    
121
        if(data[4] != null) {
122
            let mydata;
123
            let counter = 0;
124
            let countriesSet: Set<string>;
125
            let length = data[4].length!=undefined ? data[4].length : 1;
126

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

    
138
                        this.dataProviderInfo.organizations[counter] = {"name": "", "url": ""};
139
                        this.dataProviderInfo.organizations[counter]['name'] = mydata.legalname;
140
                        this.dataProviderInfo.organizations[counter]['url'] = OpenaireProperties.getsearchLinkToOrganization()+mydata['to'].content;
141

    
142
                        if(mydata.country != '' && mydata['country'].classname != '') {
143
                            if(!countriesSet.has(mydata['country'].classname)) {
144
                                this.dataProviderInfo.countries.push(mydata['country'].classname);
145
                                countriesSet.add(mydata['country'].classname);
146
                            }
147
                        }
148

    
149
                        counter++;
150
                    }
151
                }
152
            }
153
        }
154

    
155
        //this.printPublicationInfo();
156
        return this.dataProviderInfo;
157

    
158
    }
159

    
160
    printDataProviderInfo() {
161

    
162
    }
163

    
164
}
(5-5/13)