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
    getPublicationInfo (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));
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
                     ]).map(res => this.parseDataProviderInfo(res)).do(res => {
34
                      this._cache.set(key, res);
35
                    })
36
                    .do(res => {
37
                      this._cache.set(key, res);
38
                    });
39

    
40
    }
41

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

    
49
    parseDataProviderInfo (data: any):any {
50
        this.dataProviderInfo = new DataProviderInfo();
51

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

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

    
59
            if(this.dataProviderInfo.tabs == undefined) {
60
                this.dataProviderInfo.tabs = new Array<{"name": string, "content": string}>();
61
            }
62

    
63
            if(this.dataProviderInfo.tabsInTypes.publicationsTab.has(data[1].classid)) {
64
                this.dataProviderInfo.tabs.push({"name": "Publications", "content": "publicationsTab"});
65
            }
66
            if(this.dataProviderInfo.tabsInTypes.datasetsTab.has(data[1].classid)) {
67
                this.dataProviderInfo.tabs.push({"name": "Datasets", "content": "datasetsTab"});
68
            }
69
            if(this.dataProviderInfo.tabsInTypes.statisticsTab.has(data[1].classid)) {
70
                this.dataProviderInfo.tabs.push({"name": "Statistics", "content": "statisticsTab"});
71
            }
72
            if(this.dataProviderInfo.tabsInTypes.projectsTab.has(data[1].classid)) {
73
                this.dataProviderInfo.tabs.push({"name": "Projects", "content": "projectsTab"});
74
            }
75
            if(this.dataProviderInfo.tabsInTypes.datasourcesTab.has(data[1].classid)) {
76
                this.dataProviderInfo.tabs.push({"name": "Datasources", "content": "datasourcesTab"});
77
            }
78

    
79
            if(this.dataProviderInfo.resultTypes.collectedFrom.has(data[1].classid)) {
80
                this.dataProviderInfo.resultsBy = "collectedFrom";
81
            } else if(this.dataProviderInfo.resultTypes.hostedBy.has(data[1].classid)) {
82
                this.dataProviderInfo.resultsBy = "hostedBy";
83
            }
84
        }
85

    
86
        if(data[2] != null) {
87
            this.dataProviderInfo.compatibility = data[2].classname;
88
        }
89

    
90
        if(data[3] != null) {
91
            let oaiPmhURL: string;
92
            if(Array.isArray(data[3])) {
93
                oaiPmhURL = data[3][0];
94
            }
95
            else {
96
                oaiPmhURL = data[3];
97
            }
98

    
99
            if(oaiPmhURL != '' && oaiPmhURL != 'unknown') {
100
                this.dataProviderInfo.oaiPmhURL = oaiPmhURL;
101
            }
102
        }
103

    
104
        if(data[4] != null) {
105
            let mydata;
106
            let counter = 0;
107
            let countriesSet: Set<string>;
108
            let length = data[4].length!=undefined ? data[4].length : 1;
109

    
110
            for(let i=0; i<length; i++) {
111
                mydata = length > 1 ? data[4][i] : data[4];
112
                if(mydata.hasOwnProperty("to")) {
113
                    if(mydata['to'].class == "provides" && mydata['to'].type == "organization") {
114
                        if(this.dataProviderInfo.organizations == undefined) {
115
                            this.dataProviderInfo.organizations = new Array<{"name": string, "url": string}>();
116
                            this.dataProviderInfo.countries = new Array<string>();
117
                            countriesSet = new Set<string>();
118
                        }
119

    
120
                        this.dataProviderInfo.organizations[counter] = {"name": "", "url": ""};
121
                        this.dataProviderInfo.organizations[counter]['name'] = mydata.legalname;
122
                        this.dataProviderInfo.organizations[counter]['url'] = OpenaireProperties.getsearchLinkToOrganization()+mydata['to'].content;
123

    
124
                        if(mydata.country != '' && mydata['country'].classname != '') {
125
                            if(!countriesSet.has(mydata['country'].classname)) {
126
                                this.dataProviderInfo.countries.push(mydata['country'].classname);
127
                                countriesSet.add(mydata['country'].classname);
128
                            }
129
                        }
130

    
131
                        counter++;
132
                    }
133
                }
134
            }
135
        }
136

    
137
        //this.printPublicationInfo();
138
        return this.dataProviderInfo;
139

    
140
    }
141

    
142
    printDataProviderInfo() {
143

    
144
    }
145

    
146
}
(4-4/22)