Project

General

Profile

1
import {Injectable} from '@angular/core';
2
import {Http, Response} from '@angular/http';
3
import {Observable}     from 'rxjs/Observable';
4
import {AutoCompleteValue} from '../searchPages/searchUtils/searchHelperClasses.class';
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 EntitiesSearchService {
13
    private api ="https://beta.services.openaire.eu/provision/mvc/vocabularies/";
14
    constructor(private http: Http, public _cache: CacheService) {}
15

    
16
    searchProjectsByFunder(keyword:string, funderId:string):any {
17
      let url =  OpenaireProperties. getSearchAPIURLLast()+"projects?"+((keyword && keyword.length > 0)?("q=" +keyword):"")+((funderId && funderId.length > 0 )?"&fq=funderid exact " + '"'+funderId+ '"':"")+"&size=10&page=0&format=json";
18
      return this.http.get(url).toPromise()
19
      .then(request =>
20
        {
21
          request = request.json().results;
22
          return this.parse(request,"oaf:project","project");
23
        });
24
      }
25
      searchByDepositType(keyword:string, DepositType:string):any {
26
        console.info("In searchOrganizationsforDeposit");
27

    
28
        let link = OpenaireProperties.getSearchResourcesAPIURL();
29

    
30
        let url = link+"?query=";
31
        if(keyword!= null && keyword != ''  ) {
32
          url += "((oaftype exact organization and deletedbyinference=false and "+
33
            "(reldatasourcecompatibilityid=driver or reldatasourcecompatibilityid=driver-openaire2.0 or reldatasourcecompatibilityid=openaire2.0 or reldatasourcecompatibilityid=openaire3.0 or reldatasourcecompatibilityid=openaire2.0_data or reldatasourcecompatibilityid=hostedBy or relprojectid=*))"+
34
            " and ((organizationlegalname all "+'"'+keyword+'"'+") or (organizationlegalshortname all "+'"'+keyword+'"'+")) " +
35
            // "and " + this.quote(params) + " " +
36
            "and (collectedfromdatasourcename exact "+DepositType+")) "
37

    
38
        }
39

    
40
        url += "&page=0&size=10";
41
        url += "&format=json";
42

    
43
        // let url =  OpenaireProperties. getSearchAPIURLLast()+"projects?"+((keyword && keyword.length > 0)?("q=" +keyword):"")+((funderId && funderId.length > 0 )?"&fq=funderid exact " + '"'+funderId+ '"':"")+"&size=10&page=0&format=json";
44
        return this.http.get(url).toPromise()
45
        .then(request =>
46
          {
47
            request = request.json().results;
48
            console.log(request);
49
            return this.parse(request,"oaf:organization","organization");
50
          });
51
        }
52
    searchByType(keyword:string,type:string){
53
       if (type == "project"){
54
         return  this.searchEntity(keyword,"projects","oaf:project","project");
55
      }else if (type == "person"){
56
        return  this.searchEntity(keyword,"people","oaf:person","person");
57
      }else if (type == "dataset"){
58
        return  this.searchEntity(keyword,"datasets","oaf:result","dataset");
59
      }else if (type == "datasource" || type == "hostedBy" || type== "collectedFrom"){
60
        return  this.searchEntity(keyword,"datasources","oaf:datasource","datasource");
61
      }else if (type == "publication"){
62
        return  this.searchEntity(keyword,"publications","oaf:result","publication");
63
      }else if (type == "organization"){
64
        return  this.searchEntity(keyword,"organizations","oaf:organization","organization");
65

    
66
      }
67

    
68
    }
69
    fetchByType(id:string,type:string){
70
       if (type == "project"){
71
         return  this.fetchEntity(id,"projects","oaf:project","project");
72
      }else if (type == "person"){
73
        return  this.fetchEntity(id,"people","oaf:person","person");
74
      }else if (type == "dataset"){
75
        return  this.fetchEntity(id,"datasets","oaf:result","dataset");
76
      }else if (type == "datasource"  || type == "hostedBy" || type== "collectedFrom"){
77
        return  this.fetchEntity(id,"datasources","oaf:datasource","datasource");
78
      }else if (type == "publication"){
79
        return  this.fetchEntity(id,"publications","oaf:result","publication");
80
      }else if (type == "organization"){
81
        return  this.fetchEntity(id,"organizations","oaf:organization","organization");
82

    
83
      }
84

    
85
    }
86
private searchEntity (keyword: string,APIname:string,oafEntityType:string, type:string):any {
87
      let link = OpenaireProperties. getSearchAPIURLLast()+APIname;
88
      return this.search(link,keyword,oafEntityType,type)
89

    
90
}
91
private fetchEntity (id: string,APIname:string,oafEntityType:string, type:string):any {
92
    let link = OpenaireProperties. getSearchAPIURLLast()+APIname;
93
    return this.fetch(link,id,oafEntityType,type)
94
}
95
private fetch (link,id,oafEntityType,type){
96
  let url = link+"/"+id+"?format=json";
97
  return this.http.get(url)
98
    .map(request => <any> request.json())
99
    // .do(res => console.info(res))
100
    .map(request => <any>  this.parse(request,oafEntityType,type));
101

    
102

    
103

    
104
}
105
    private search (link,keyword,oafEntityType,type){
106
      let url = link+"?";
107
      if(keyword!= null && keyword != ''  ) {
108
          url += "q="+ keyword;
109
      }
110

    
111
      url += "&page=0&size="+10+"&format=json";
112
      return this.http.get(url).toPromise()
113
                 .then(request =>
114
                   {
115
                     request = request.json().results;
116
                     return this.parse(request,oafEntityType,type);
117
                   });
118

    
119

    
120
    }
121

    
122
    private parse(data: any,oafEntityType:string, type:string){
123
      var array:any =[]
124
      let length = Array.isArray(data) ? data.length : 1;
125

    
126
      for(let i=0; i<length; i++) {
127
            let resData = Array.isArray(data) ? data[i]['result']['metadata']['oaf:entity'][oafEntityType] : data['result']['metadata']['oaf:entity'][oafEntityType];
128

    
129
            var value:any={} ;
130
            if(resData['title']){
131
              if(Array.isArray(resData['title'])) {
132
                    value.label = resData['title'][0];
133
              } else {
134
                    value.label = resData['title'];
135
              }
136
            }else if(resData["fullname"]){
137
              if(Array.isArray(resData["fullname"])) {
138
                    value.label = resData["fullname"][0];
139
              } else {
140
                    value.label = resData["fullname"];
141
              }
142
            }else if(resData["legalname"]){
143

    
144
              if(Array.isArray(resData["legalname"])) {
145
                    value.label = resData["legalname"][0];
146
              } else {
147
                    value.label = resData["legalname"];
148
              }
149

    
150
            }else if(resData["officialname"]){
151

    
152
              if(Array.isArray(resData["officialname"])) {
153
                    value.label = resData["officialname"][0];
154
              } else {
155
                    value.label = resData["officialname"];
156
              }
157
            }
158

    
159
           value.id = Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
160

    
161
             if(type=="project"){
162
                  value.projectAcronym = resData['acronym'];
163
                  value.projectName = value.label;
164
                  value.endDate = null;
165
                  value.startDate = null;
166
                  if(resData.hasOwnProperty("startdate")) {
167
                    value.startDate = resData.startdate.split('-')[0];
168
                  }
169
                  if(resData.hasOwnProperty("enddate")) {
170
                    value.endDate = resData.enddate.split('-')[0];
171
                  }
172

    
173
             }
174
              array.push(value);
175
        }
176
        console.info("Parsing results.... Size:"+array.length);
177

    
178
        return array;
179
    }
180

    
181
// http://scoobydoo.di.uoa.gr:8181/dnet-functionality-services-2.0.0-SNAPSHOT/rest/v2/api/projects?refine=true&fields=funderid&page=1&size=0
182

    
183
    private handleError (error: Response) {
184
      // in a real world app, we may send the error to some remote logging infrastructure
185
      // instead of just logging it to the console
186
      console.log(error);
187
      return Observable.throw(error  || 'Server error');
188
    }
189
}
(6-6/24)