Project

General

Profile

1
import {Injectable} from '@angular/core';
2
import {Http, Response} from '@angular/http';
3
import {HttpClient} from "@angular/common/http";
4
import {Observable}     from 'rxjs';
5

    
6

    
7

    
8
import {SearchResult}     from '../utils/entities/searchResult';
9
import {RefineResultsUtils} from './servicesUtils/refineResults.class';
10
import{EnvProperties} from '../utils/properties/env-properties';
11
import {StringUtils} from '../utils/string-utils.class';
12
import {map} from "rxjs/operators";
13
@Injectable()
14
export class SearchProjectsService {
15
    private sizeOfDescription: number = 270;
16

    
17
    constructor(private http: HttpClient ) {}
18

    
19
    searchProjects (params: string, refineParams:string, page: number, size: number, refineFields:string[] , properties:EnvProperties ):any {
20

    
21
        let link = properties.searchAPIURLLAst+"projects";
22

    
23
        let url = link+"?";
24
        if(params!= null && params != ''  ) {
25
            url += params;
26
        }
27
        if(refineParams!= null && refineParams != ''  ) {
28
            url += refineParams;
29
        }
30
        url += "&page="+(page-1)+"&size="+size + "&format=json";
31

    
32

    
33
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
34
                    //.map(res => <any> res.json())
35
                    .pipe(map(res => [res['meta'].total, this.parseResults(res['results']),RefineResultsUtils.parse(res['refineResults'],refineFields, "project")]));
36
    }
37
    getProjectsforDataProvider (datasourceId: string, page: number, size: number, properties:EnvProperties  ):any {
38
      let url = properties.searchResourcesAPIURL;
39
      var basicQuery = "(oaftype exact project) "
40
      url += "?query=";
41
      if(datasourceId!= null && datasourceId != ''  ) {
42
        url +=" ( "+basicQuery+ " ) " +" and (collectedfromdatasourceid exact \"" + datasourceId + "\")";
43
      }else{
44
        url +=" ( "+basicQuery+ " ) ";
45
      }
46

    
47
      url += "&page="+(page-1)+"&size="+size;
48
      url += "&format=json";
49

    
50
      return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
51
      //.map(res => <any> res.json())
52
      .pipe(map(res => [res['meta'].total, this.parseResults(res['results'])]));
53
    }
54
    advancedSearchProjects (params: string, page: number, size: number, properties:EnvProperties  ):any {
55
      let url = properties.searchResourcesAPIURL;
56
      var basicQuery = "(oaftype exact project) "
57
      url += "?query=";
58
      if(params!= null && params != ''  ) {
59
        url +=" ( "+basicQuery+ " ) " +" and (" + params + ")";
60
      }else{
61
        url +=" ( "+basicQuery+ " ) ";
62
      }
63

    
64
      url += "&page="+(page-1)+"&size="+size;
65
      url += "&format=json";
66
      return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
67
      //.map(res => <any> res.json())
68
      .pipe(map(res => [res['meta'].total, this.parseResults(res['results'])]));
69
    }
70
    getProjectsForOrganizations (organizationId: string, filterquery: string, page: number, size: number, refineFields:string[] , properties:EnvProperties  ):any {
71
      let url = properties.searchResourcesAPIURL;
72
      var basicQuery = "(oaftype exact project) "
73
      url += "?query=";
74
      if(filterquery!= null && filterquery != ''  ) {
75
        url +="( ( "+basicQuery+ " )  and (relorganizationid exact \"" + organizationId + "\")"+"  " + filterquery + ")";
76
      }else{
77
        url +=" (( "+basicQuery+ " ) " +" and (relorganizationid exact \"" + organizationId + "\"))";
78
      }
79
      if(refineFields!= null && refineFields.length > 0 ) {
80
        url +="&refine=true";
81
        for(let i=0; i< refineFields.length ; i++ ){
82
          url +="&fields="+refineFields[i];
83
        }
84
      }
85
      url += "&page="+(page-1)+"&size="+size;
86
      url += "&format=json";
87

    
88
      return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
89
                  //.map(res => <any> res.json())
90
                  .pipe(map(res => [res['meta'].total, this.parseResults(res['results']),RefineResultsUtils.parse(res['refineResults'],refineFields, "project")]));
91
    }
92
    getFunders(properties:EnvProperties ):any {
93
       let url = properties.searchAPIURLLAst+"projects?refine=true&fields=funder&size=0"+ "&format=json";;
94

    
95
       return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
96
                   //.map(res => <any> res.json())
97
                   .pipe(map(res => [res['meta'].total, res['refineResults']['funder']]));
98

    
99

    
100
      }
101

    
102
      searchForProjectsObs(keyword:string, funderId:string, properties:EnvProperties ):any {
103
         let url = 'search?action=search&sTransformer=projects_openaire&query='+
104
         '%28oaftype+exact+project%29+and+%28%28projecttitle+%3D+%22'+keyword+'%22%29+or+%28projectacronym+%3D+%22'+keyword+'%22%29+or+%28projectcode+%3D+%22'+keyword+'%22%29%29+and+%28funder+exact+'+funderId+'%29&size=10&locale=en_GB&format=json';
105

    
106
         return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url).toPromise()
107
                    .then(request =>{
108
                      //return (request.json().response.results)?request.json().response.results.result:request.json().response.result;
109
                      return (request['response'].results)?request['response'].results.result:request['response'].result;
110

    
111
                    }) ;
112
        }
113
    parseResults(data: any): SearchResult[] {
114
        let results: SearchResult[] = [];
115

    
116
        let length = Array.isArray(data) ? data.length : 1;
117

    
118
        for(let i=0; i<length; i++) {
119
            let resData = Array.isArray(data) ? data[i]['result']['metadata']['oaf:entity']['oaf:project'] : data['result']['metadata']['oaf:entity']['oaf:project'];
120

    
121
            var result: SearchResult = new SearchResult();
122
            result.openAccessMandatePublications = (resData['oamandatepublications'])?true:false;
123
            result.openAccessMandateDatasets = (resData['ecarticle29_3'])?true:false;
124
            result['title'] = {"name": '', "accessMode": '', "sc39": ''};
125
            // if(resData['acronym'] != undefined && resData['acronym'] != "") {
126
            //     result['title'].name = resData['acronym'] + " - ";
127
            // }
128
            if(Array.isArray(resData['title'])) {
129
                result['title'].name += resData['title'][0];
130
            } else {
131
                result['title'].name += resData['title'];
132
            }
133

    
134
            // if(result['title'].name != '') {
135
            //     result['title'].name += " ("+resData['code']+")"
136
            // }
137

    
138
            if(resData['ecsc39'] == true) {
139
                result['title'].sc39 = "sc39";
140
            }
141

    
142
            result.acronym = resData['acronym'];
143
            result.code = resData['code'];
144

    
145
            result.id = Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
146

    
147
            if(resData['rels'].hasOwnProperty("rel")) {
148
                let relLength = Array.isArray(resData['rels']['rel']) ? resData['rels']['rel'].length : 1;
149

    
150
                for(let j=0; j<relLength; j++) {
151
                    let relation = Array.isArray(resData['rels']['rel']) ? resData['rels']['rel'][j] : resData['rels']['rel'];
152

    
153
                    if(relation.hasOwnProperty("to")) {
154
                         if(relation['to'].class == "hasParticipant") {
155
                            if(result['organizations'] == undefined) {
156
                                result['organizations'] = new Array<
157
                                    { "name": string, "id": string}>();
158
                            }
159

    
160
                            let countOrganizations = result['organizations'].length;
161

    
162
                            result['organizations'][countOrganizations] = { "name": "", "id": "" }
163

    
164
                            result['organizations'][countOrganizations]['id'] =
165
                                /*OpenaireProperties.getsearchLinkToOrganization() + */relation['to'].content;
166
                            if(relation.legalshortname) {
167
                              result['organizations'][countOrganizations]['name'] = relation.legalshortname;
168
                            } else {
169
                              result['organizations'][countOrganizations]['name'] = relation.legalname;
170
                            }
171
                        }
172
                    }
173
                }
174
            }
175
            if(resData.hasOwnProperty("fundingtree")) {
176
              /*let funderSet: Set<string>;
177
              if(result['funders'] == undefined) {
178
                  result['funders'] = new Array<
179
                      {"funderShortname": string, "funderName": string}>();
180
                  funderSet = new Set<string>();
181
              }
182

    
183
                let fundingLength = Array.isArray(resData['fundingtree']) ? resData['fundingtree'].length : 1;
184

    
185
                for(let z=0; z<fundingLength; z++) {
186
                    let fundingData = Array.isArray(resData['fundingtree']) ? resData['fundingtree'][z] : resData['fundingtree'];
187
                    if(fundingData.hasOwnProperty("funder")) {
188
                        if(!funderSet.has(fundingData['funder'].shortname)) {
189
                          let countFunders = result['funders'].length;
190

    
191
                          result['funders'][countFunders] =   {"funderShortname": "", "funderName": ""};
192
                          result['funders'][countFunders]['funderShortname'] = fundingData['funder'].shortname;
193
                          result['funders'][countFunders]['funderName'] = fundingData['funder'].name;
194
                          funderSet.add(fundingData['funder'].shortname);
195
                        }
196
                    }
197
                }*/
198

    
199
                let fundingLength = Array.isArray(resData['fundingtree']) ? resData['fundingtree'].length : 1;
200

    
201
                for(let z=0; z<fundingLength; z++) {
202
                  let fundingData = Array.isArray(resData['fundingtree']) ? resData['fundingtree'][z] : resData['fundingtree'];
203
                  if(fundingData.hasOwnProperty("funder")) {
204
                    result['funderShortname'] = fundingData['funder'].shortname;
205
                    break;
206
                  }
207
                }
208
            }
209
            if(resData.hasOwnProperty("startdate")) {
210
              result.startYear = resData.startdate.split('-')[0];
211
            }
212
            if(resData.hasOwnProperty("enddate")) {
213
              result.endYear = resData.enddate.split('-')[0];
214
            }
215

    
216
            results.push(result);
217
        }
218

    
219
        return results;
220
    }
221

    
222
    numOfProjects(url: string, properties:EnvProperties ):any {
223

    
224
      return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
225
                  //.map(res => <any> res.json())
226
                  .pipe(map(res => res['total']));
227
    }
228

    
229
    numOfEntityProjects(id: string, entity: string, properties:EnvProperties ):any {
230
        //let url = properties.searchAPIURLLAst+params+(params.indexOf("?") == -1 ?"?":"&")+"format=json";
231
        var parameters: string = "";
232
        if(entity == "organization") {
233
          parameters = "organizations/"+id+"/projects/count";
234
        }
235

    
236
        let url = properties.searchAPIURLLAst+parameters+ "?format=json";
237

    
238
        return this.numOfProjects(url, properties);
239
    }
240

    
241
    numOfSearchProjects(params: string, properties:EnvProperties ):any {
242
        let url = properties.searchAPIURLLAst+"projects/count?format=json";
243
        if(params != "") {
244
          url += "&q=" + StringUtils.URIEncode(params);
245
        }
246
        return this.numOfProjects(url, properties);
247
    }
248
}
(19-19/22)