1
|
import {Injectable} from '@angular/core';
|
2
|
import {Http, Response} from '@angular/http';
|
3
|
import {Observable} from 'rxjs/Observable';
|
4
|
import 'rxjs/add/observable/of';
|
5
|
import 'rxjs/add/operator/do';
|
6
|
import 'rxjs/add/operator/share';
|
7
|
import { } from '../shared/cache.service';
|
8
|
import {OpenaireProperties} from '../utils/properties/openaireProperties';
|
9
|
import {SearchResult} from '../utils/entities/searchResult';
|
10
|
import {RefineResultsUtils} from './servicesUtils/refineResults.class';
|
11
|
|
12
|
@Injectable()
|
13
|
export class SearchOrganizationsService {
|
14
|
|
15
|
constructor(private http: Http ) {}
|
16
|
|
17
|
parseResultsForDeposit(data: any): {"name": string, "id": string}[] {
|
18
|
let results: {"name": string, "id": string}[] = [];
|
19
|
|
20
|
let length = Array.isArray(data) ? data.length : 1;
|
21
|
|
22
|
for(let i=0; i<length; i++) {
|
23
|
let name: string = '';
|
24
|
let id: string = '';
|
25
|
let resData = Array.isArray(data) ? data[i]['result']['metadata']['oaf:entity']['oaf:organization'] : data['result']['metadata']['oaf:entity']['oaf:organization'];
|
26
|
name = resData.legalname;
|
27
|
if(name == '') {
|
28
|
name = resData.legalshortname;
|
29
|
}
|
30
|
|
31
|
id = Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
|
32
|
|
33
|
results.push({"name": name, "id": id});
|
34
|
}
|
35
|
console.info(results);
|
36
|
return results;
|
37
|
}
|
38
|
|
39
|
searchOrganizations (params: string, refineParams:string, page: number, size: number, refineFields:string[] ):any {
|
40
|
|
41
|
console.info("In searchOrganizations");
|
42
|
|
43
|
let link = OpenaireProperties.getSearchAPIURLLast()+"organizations";
|
44
|
|
45
|
let url = link+"?";
|
46
|
if(params!= null && params != '' ) {
|
47
|
url += params;
|
48
|
}
|
49
|
if(refineParams!= null && refineParams != '' ) {
|
50
|
url += refineParams;
|
51
|
}
|
52
|
url += "&page="+(page-1)+"&size="+size + "&format=json";
|
53
|
|
54
|
|
55
|
return this.http.get((OpenaireProperties.isCacheEnabled())? (OpenaireProperties.getCacheUrl()+encodeURIComponent(url)): url)
|
56
|
.map(res => <any> res.json())
|
57
|
//.do(res => console.info(res))
|
58
|
|
59
|
.map(res => [res['meta'].total, this.parseResults(res['results']),RefineResultsUtils.parse(res['refineResults'],refineFields, "organization")]);
|
60
|
}
|
61
|
advancedSearchOrganizations (params: string, page: number, size: number ):any {
|
62
|
let url = OpenaireProperties.getSearchResourcesAPIURL();
|
63
|
var basicQuery = "(oaftype exact organization) "
|
64
|
url += "?query=";
|
65
|
if(params!= null && params != '' ) {
|
66
|
url +=" ( "+basicQuery+ " ) " +" and (" + params + ")";
|
67
|
}else{
|
68
|
url +=" ( "+basicQuery+ " ) ";
|
69
|
}
|
70
|
|
71
|
url += "&page="+(page-1)+"&size="+size;
|
72
|
url += "&format=json";
|
73
|
|
74
|
return this.http.get((OpenaireProperties.isCacheEnabled())? (OpenaireProperties.getCacheUrl()+encodeURIComponent(url)): url)
|
75
|
.map(res => <any> res.json())
|
76
|
//.do(res => console.info(res))
|
77
|
|
78
|
.map(res => [res['meta'].total, this.parseResults(res['results'])]);
|
79
|
}
|
80
|
parseResults(data: any): SearchResult[] {
|
81
|
let results: SearchResult[] = [];
|
82
|
|
83
|
let length = Array.isArray(data) ? data.length : 1;
|
84
|
|
85
|
for(let i=0; i<length; i++) {
|
86
|
let resData = Array.isArray(data) ? data[i]['result']['metadata']['oaf:entity']['oaf:organization'] : data['result']['metadata']['oaf:entity']['oaf:organization'];
|
87
|
|
88
|
var result: SearchResult = new SearchResult();
|
89
|
|
90
|
result['title'] = {"name": '', "accessMode": '', "sc39": ''};
|
91
|
|
92
|
result['title'].name = resData.legalshortname;
|
93
|
if(result['title'].name == '') {
|
94
|
result['title'].name = resData.legalname;
|
95
|
}
|
96
|
|
97
|
//result['title'].url = OpenaireProperties.getsearchLinkToOrganization();
|
98
|
//result['title'].url += Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
|
99
|
result['id'] = Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
|
100
|
|
101
|
if(resData['rels'].hasOwnProperty("rel")) {
|
102
|
let relLength = Array.isArray(resData['rels']['rel']) ? resData['rels']['rel'].length : 1;
|
103
|
|
104
|
for(let j=0; j<relLength; j++) {
|
105
|
let relation = Array.isArray(resData['rels']['rel']) ? resData['rels']['rel'][j] : resData['rels']['rel'];
|
106
|
|
107
|
if(relation.hasOwnProperty("to")) {
|
108
|
if(relation['to'].class == "isParticipant") {
|
109
|
if(result['projects'] == undefined) {
|
110
|
result['projects'] = new Array<
|
111
|
{ "id": string, "acronym": string, "title": string,
|
112
|
"funderShortname": string, "funderName": string,
|
113
|
"code": string
|
114
|
}>();
|
115
|
}
|
116
|
|
117
|
let countProjects = result['projects'].length;
|
118
|
|
119
|
result['projects'][countProjects] = {
|
120
|
"id": "", "acronym": "", "title": "",
|
121
|
"funderShortname": "", "funderName": "",
|
122
|
"code": ""
|
123
|
}
|
124
|
|
125
|
if(relation.title != 'unidentified') {
|
126
|
result['projects'][countProjects]['id'] =
|
127
|
/*OpenaireProperties.getsearchLinkToProject() + */relation['to'].content;
|
128
|
result['projects'][countProjects]['acronym'] = relation.acronym;
|
129
|
result['projects'][countProjects]['title'] = relation.title;
|
130
|
result['projects'][countProjects]['code'] = relation.code;
|
131
|
} else {
|
132
|
result['projects'][countProjects]['id'] = "";
|
133
|
result['projects'][countProjects]['acronym'] = "";
|
134
|
result['projects'][countProjects]['title'] = "";
|
135
|
result['projects'][countProjects]['code'] = "";
|
136
|
}
|
137
|
|
138
|
if(relation.hasOwnProperty("funding")) {
|
139
|
let fundingLength = Array.isArray(relation['funding']) ? relation['funding'].length : 1;
|
140
|
|
141
|
for(let z=0; z<fundingLength; z++) {
|
142
|
let fundingData = Array.isArray(relation['funding']) ? relation['funding'][z] : relation['funding'];
|
143
|
|
144
|
if(fundingData.hasOwnProperty("funder")) {
|
145
|
result['projects'][countProjects]['funderShortname'] = fundingData['funder'].shortname;
|
146
|
result['projects'][countProjects]['funderName'] = fundingData['funder'].name;
|
147
|
}
|
148
|
}
|
149
|
}
|
150
|
|
151
|
}
|
152
|
}
|
153
|
}
|
154
|
}
|
155
|
|
156
|
if(resData.country.hasOwnProperty("classname")) {
|
157
|
result.country = resData.country.classname;
|
158
|
}
|
159
|
|
160
|
results.push(result);
|
161
|
}
|
162
|
|
163
|
return results;
|
164
|
}
|
165
|
|
166
|
numOfOrganizations(url: string): any {
|
167
|
|
168
|
return this.http.get((OpenaireProperties.isCacheEnabled())? (OpenaireProperties.getCacheUrl()+encodeURIComponent(url)): url)
|
169
|
.map(res => <any> res.json())
|
170
|
.map(res => res.total);
|
171
|
}
|
172
|
|
173
|
numOfEntityOrganizations(id: string, entity: string):any {
|
174
|
// currently not used - if used fix entity comparison below
|
175
|
var parameters: string = "";
|
176
|
if(entity == "organization") {
|
177
|
parameters = "organizations/"+id+"/organizations/count";
|
178
|
}
|
179
|
|
180
|
let url = OpenaireProperties.getSearchAPIURLLast()+parameters+"?format=json";
|
181
|
return this.numOfOrganizations(url);
|
182
|
}
|
183
|
|
184
|
numOfSearchOrganizations(params: string):any {
|
185
|
let url = OpenaireProperties.getSearchAPIURLLast()+"organizations/count?format=json";
|
186
|
if(params != "") {
|
187
|
url += "&q=" + params;
|
188
|
}
|
189
|
return this.numOfOrganizations(url);
|
190
|
}
|
191
|
}
|