Project

General

Profile

1
/**
2
 * Created by stefania on 7/13/17.
3
 */
4
import { Injectable } from '@angular/core';
5
import { Http, Response, Headers, RequestOptions } from '@angular/http';
6
import { Observable } from 'rxjs/Rx';
7
import { Page } from "../domain/page";
8
import { PageHelpContent } from "../domain/page-help-content";
9
import { Community } from "../domain/community";
10
import { Entity } from "../domain/entity";
11
import { DivId } from "../domain/divId";
12
import { DivHelpContent } from "../domain/div-help-content";
13
import {COOKIE} from "../openaireLibrary/login/utils/helper.class"
14
import {StatisticsDisplay, StatisticsSummary} from '../openaireLibrary/connect/statistics/statisticsEntities';
15

    
16

    
17
@Injectable()
18
export class HelpContentService {
19

    
20
    constructor(private http:Http) {
21
    }
22

    
23
    static removeNulls(obj){
24
        var isArray = obj instanceof Array;
25
        for (var k in obj){
26
            if (obj[k]===null || obj[k]==='') isArray ? obj.splice(k,1) : delete obj[k];
27
            else if (typeof obj[k]=="object") HelpContentService.removeNulls(obj[k]);
28
        }
29
    }
30

    
31
    getDivIdsFull(page_id: string, helpContentUrl:string) {
32
      if(page_id) {
33
        return this.http.get(helpContentUrl + 'divFull?&page='+page_id)
34
            .map(res => <Array<DivId>> res.json())
35
            .catch(this.handleError);
36
      } else {
37
        return this.http.get(helpContentUrl + 'divFull')
38
            .map(res => <Array<DivId>> res.json())
39
            .catch(this.handleError);
40
      }
41
    }
42

    
43
    updateDivId(divId: DivId, helpContentUrl:string) {
44
        let headers = new Headers({'Content-Type': 'application/json'});
45
        let options = new RequestOptions({headers: headers});
46

    
47
        HelpContentService.removeNulls(divId);
48

    
49
        return this.http.post(helpContentUrl + 'div/update', JSON.stringify(divId), options)
50
            .map(res => <DivId> res.json())
51
            .catch(this.handleError);
52
    }
53

    
54
    getDivId(divId: string, helpContentUrl:string) {
55
        return this.http.get(helpContentUrl + 'div/'+divId)
56
            .map(res => <DivId> res.json())
57
            .catch(this.handleError);
58
    }
59

    
60
    getDivIdFull(divId: string, helpContentUrl:string) {
61
        return this.http.get(helpContentUrl + 'divFull/'+divId)
62
            .map(res => <DivId> res.json())
63
            .catch(this.handleError);
64
    }
65

    
66
    saveDivId(divId: DivId, helpContentUrl:string) {
67
        let headers = new Headers({'Content-Type': 'application/json'});
68
        let options = new RequestOptions({headers: headers});
69

    
70
        HelpContentService.removeNulls(divId);
71

    
72
        return this.http.post(helpContentUrl + 'div/save', JSON.stringify(divId), options)
73
            .map(res => <DivId> res.json())
74
            .catch(this.handleError);
75
    }
76
/*
77
    getCommunitiesWithDivId(helpContentUrl:string) {
78
        return this.http.get(helpContentUrl + 'community?div=true')
79
            .map(res => <Array<Community>> res.json())
80
            .catch(this.handleError);
81
    }
82
*/
83
    getCommunityPagesWithDivId(community_pid: string, helpContentUrl:string) {
84
        return this.http.get(helpContentUrl + 'community/'+community_pid+'/pages?div=true')
85
            .map(res => <Array<Page>> res.json())
86
            .catch(this.handleError);
87
    }
88

    
89
    getCommunityDivHelpContents(community_pid: string, helpContentUrl:string) {
90
      return this.http.get(helpContentUrl + 'divhelpcontent?community='+community_pid)
91
          .map(res => <Array<DivHelpContent>> res.json())
92
          .catch(this.handleError);
93
    }
94

    
95
    getDivHelpContent(id : string, helpContentUrl:string) {
96
      return this.http.get(helpContentUrl + 'divhelpcontent/' + id)
97
          .map(res => <DivHelpContent> res.json())
98
          .catch(this.handleError);
99
    }
100

    
101
    insertOrUpdateDivHelpContent(divHelpContent: DivHelpContent, helpContentUrl:string) {
102

    
103
        let headers = new Headers({'Content-Type': 'application/json'});
104
        let options = new RequestOptions({headers: headers});
105

    
106
        HelpContentService.removeNulls(divHelpContent);
107

    
108
        return this.http.post(helpContentUrl + 'divhelpcontent', JSON.stringify(divHelpContent), options)
109
            .map(res => <DivHelpContent> res.json())
110
            .catch(this.handleError);
111
    }
112

    
113
    deleteDivIds(ids : string[], helpContentUrl:string) {
114
        let headers = new Headers({'Content-Type': 'application/json'});
115
        let options = new RequestOptions({headers: headers});
116

    
117
        return this.http.post(helpContentUrl + 'div/delete',JSON.stringify(ids), options)
118
            .catch(this.handleError);
119
    }
120

    
121
    deleteDivHelpContents(ids : string[], helpContentUrl:string) {
122
        let headers = new Headers({'Content-Type': 'application/json'});
123
        let options = new RequestOptions({headers: headers});
124

    
125
        return this.http.post(helpContentUrl + 'divhelpcontent/delete',JSON.stringify(ids), options)
126
            .catch(this.handleError);
127
    }
128

    
129
    toggleDivHelpContents(ids : string[],status : boolean, helpContentUrl:string) {
130
        let headers = new Headers({'Content-Type': 'application/json'});
131
        let options = new RequestOptions({headers: headers});
132

    
133
        return this.http.post(helpContentUrl + 'divhelpcontent/toggle?status='+ status.toString(), JSON.stringify(ids), options)
134
            //.map( res => <string[]> res.json())
135
            .catch(this.handleError);
136
    }
137

    
138
    getPagesWithDivIds(community_pid: string, helpContentUrl:string) {
139
        return this.http.get(helpContentUrl + 'div/pages')
140
            .map(res => <Map<string, Set<string>>> res.json())
141
            .catch(this.handleError);
142
    }
143

    
144
    getPages(helpContentUrl:string,pid:string) {
145
        return this.http.get(helpContentUrl + 'page'+(pid?("?pid="+pid):""))
146
            .map(res => <Array<Page>> res.json())
147
            .catch(this.handleError);
148
    }
149

    
150
    getPage(pageId:string, helpContentUrl:string) {
151
        return this.http.get(helpContentUrl + 'page/'+pageId)
152
            .map(res => <Page> res.json())
153
            .catch(this.handleError);
154
    }
155

    
156
    getCommunities( helpContentUrl:string) {
157
        return this.http.get(helpContentUrl + 'community')
158
            .map(res => <Array<Community>> res.json())
159
            .catch(this.handleError);
160
    }
161

    
162
    getCommunity(community_pid: string, helpContentUrl:string) {
163
        return this.http.get(helpContentUrl + 'community/'+community_pid)
164
            .map(res => <Community> res.json())
165
            .catch(this.handleError);
166
    }
167

    
168
    getCommunitiesFull( helpContentUrl:string) {
169
        return this.http.get(helpContentUrl + 'communityFull')
170
            .map(res => <Array<Community>> res.json())
171
            .catch(this.handleError);
172
    }
173

    
174
    getCommunityFull(community_pid: string, helpContentUrl:string) {
175
        return this.http.get(helpContentUrl + 'communityFull/'+community_pid)
176
            .map(res => <Community> res.json())
177
            .catch(this.handleError);
178
    }
179

    
180
    saveCommunity(community: Community, helpContentUrl:string) {
181
        // let headers = new Headers({'Content-Type': 'application/json'});
182
        // let options = new RequestOptions({headers: headers});
183

    
184
        HelpContentService.removeNulls(community);
185

    
186
        return this.http.post(helpContentUrl + 'community/save', JSON.stringify(community), this.getAuthOptionsWithBody())
187
            .map(res => <Community> res.json())
188
            .catch(this.handleError);
189
    }
190

    
191
    updateCommunity(community: Community, helpContentUrl:string) {
192
        // let headers = new Headers({'Content-Type': 'application/json'});
193
        // let options = new RequestOptions({headers: headers});
194

    
195
        HelpContentService.removeNulls(community);
196

    
197
        return this.http.post(helpContentUrl + 'community/update', JSON.stringify(community),  this.getAuthOptionsWithBody())
198
            .map(res => <Community> res.json())
199
            .catch(this.handleError);
200
    }
201

    
202
    deleteCommunities(ids : string[], helpContentUrl:string) {
203
        // let headers = new Headers({'Content-Type': 'application/json'});
204
        // let options = new RequestOptions({headers: headers});
205

    
206
        return this.http.post(helpContentUrl + 'community/delete',JSON.stringify(ids), this.getAuthOptionsWithBody())
207
            .catch(this.handleError);
208
    }
209

    
210
    getCommunityPages(community_pid: string, params: string, helpContentUrl:string) {
211
        return this.http.get(helpContentUrl + 'community/'+community_pid+'/pages'+params)
212
            .map(res => <Array<Page>> res.json())
213
            .catch(this.handleError);
214
    }
215

    
216
    getEntities(helpContentUrl:string) {
217
        return this.http.get(helpContentUrl + 'entity')
218
            .map(res => <Array<Entity>> res.json())
219
            .catch(this.handleError);
220
    }
221

    
222
    getCommunityEntities(community_pid: string, helpContentUrl:string) {
223
        return this.http.get(helpContentUrl + 'community/'+community_pid+'/entities')
224
            .map(res => <Array<Entity>> res.json())
225
            .catch(this.handleError);
226
    }
227

    
228

    
229
    saveEntity(entity: Entity, helpContentUrl:string) {
230
        let headers = new Headers({'Content-Type': 'application/json'});
231
        let options = new RequestOptions({headers: headers});
232

    
233
        HelpContentService.removeNulls(entity);
234

    
235
        return this.http.post(helpContentUrl + 'entity/save', JSON.stringify(entity), options)
236
            .map(res => <Entity> res.json())
237
            .catch(this.handleError);
238
    }
239

    
240
    updateEntity(entity: Entity, helpContentUrl:string) {
241
        let headers = new Headers({'Content-Type': 'application/json'});
242
        let options = new RequestOptions({headers: headers});
243

    
244
        HelpContentService.removeNulls(entity);
245

    
246
        return this.http.post(helpContentUrl + 'entity/update', JSON.stringify(entity), options)
247
            .map(res => <Entity> res.json())
248
            .catch(this.handleError);
249
    }
250

    
251
    // toggleEntity(selectedCommunityId: string, id : string,status : boolean) {
252
    //     let headers = new Headers({'Content-Type': 'application/json'});
253
    //     let options = new RequestOptions({headers: headers});
254
    //
255
    //     return this.http.post(helpContentUrl + 'community/'+selectedCommunityId+'/entity/toggle?status='+ status.toString()+'&entityId='+id.toString(), options)
256
    //         .catch(this.handleError);
257
    // }
258

    
259
    toggleEntities(selectedCommunityPid: string, ids : string[],status : boolean, helpContentUrl:string) {
260
        let headers = new Headers({'Content-Type': 'application/json'});
261
        let options = new RequestOptions({headers: headers});
262

    
263
        return this.http.post(helpContentUrl +'community/'+selectedCommunityPid+ '/entity/toggle?status='+ status.toString(), JSON.stringify(ids), options)
264
            //.map( res => <string[]> res.json())
265
            .catch(this.handleError);
266
    }
267

    
268
    deleteEntities(ids : string[], helpContentUrl:string) {
269
        let headers = new Headers({'Content-Type': 'application/json'});
270
        let options = new RequestOptions({headers: headers});
271

    
272
        return this.http.post(helpContentUrl + 'entity/delete',JSON.stringify(ids), options)
273
            .catch(this.handleError);
274
    }
275

    
276

    
277
    toggleEntityOfPage(pageId: string, entityId : string,status : boolean, helpContentUrl:string) {
278
        let headers = new Headers({'Content-Type': 'application/json'});
279
        let options = new RequestOptions({headers: headers});
280

    
281
        return this.http.post(helpContentUrl + 'page/'+pageId+'/entity/toggle?status='+ status.toString()+'&entityId='+entityId.toString(), options)
282
            .catch(this.handleError);
283
    }
284

    
285
    savePage(page: Page, helpContentUrl:string) {
286
        let headers = new Headers({'Content-Type': 'application/json'});
287
        let options = new RequestOptions({headers: headers});
288

    
289
        HelpContentService.removeNulls(page);
290

    
291
        return this.http.post(helpContentUrl + 'page/save', JSON.stringify(page), options)
292
            .map(res => <Page> res.json())
293
            .catch(this.handleError);
294
    }
295

    
296
    updatePage(page: Page, helpContentUrl:string) {
297
        let headers = new Headers({'Content-Type': 'application/json'});
298
        let options = new RequestOptions({headers: headers});
299

    
300
        HelpContentService.removeNulls(page);
301

    
302
        return this.http.post(helpContentUrl + 'page/update', JSON.stringify(page), options)
303
            .map(res => <Page> res.json())
304
            .catch(this.handleError);
305
    }
306

    
307
    togglePages(selectedCommunityPid: string, ids : string[],status : boolean, helpContentUrl:string) {
308
        let headers = new Headers({'Content-Type': 'application/json'});
309
        let options = new RequestOptions({headers: headers});
310

    
311
        return this.http.post(helpContentUrl + 'community/'+selectedCommunityPid+'/page/toggle?status='+ status.toString(),JSON.stringify(ids), options)
312
            .catch(this.handleError);
313
    }
314

    
315
    deletePages(ids : string[], helpContentUrl:string) {
316
        let headers = new Headers({'Content-Type': 'application/json'});
317
        let options = new RequestOptions({headers: headers});
318

    
319
        return this.http.post(helpContentUrl + 'page/delete',JSON.stringify(ids), options)
320
            .catch(this.handleError);
321
    }
322

    
323
    getPageHelpContents(helpContentUrl:string) {
324
        return this.http.get(helpContentUrl + 'pagehelpcontent')
325
            .map(res => <Array<PageHelpContent>> res.json())
326
            .catch(this.handleError);
327
    }
328

    
329
    getCommunityPageHelpContents(community_pid: string, helpContentUrl:string) {
330
        return this.http.get(helpContentUrl + 'pagehelpcontent?community='+community_pid)
331
            .map(res => <Array<PageHelpContent>> res.json())
332
            .catch(this.handleError);
333
    }
334

    
335
    getPageHelpContent(id : string, helpContentUrl:string) {
336
        return this.http.get(helpContentUrl + 'pagehelpcontent/' + id)
337
            .map(res => <PageHelpContent> res.json())
338
            .catch(this.handleError);
339
    }
340

    
341
    savePageHelpContent(pageHelpContent: PageHelpContent, helpContentUrl:string) {
342

    
343
        let headers = new Headers({'Content-Type': 'application/json'});
344
        let options = new RequestOptions({headers: headers});
345

    
346
        HelpContentService.removeNulls(pageHelpContent);
347

    
348
        return this.http.post(helpContentUrl + 'pagehelpcontent/save', JSON.stringify(pageHelpContent), options)
349
            .map(res => <PageHelpContent> res.json())
350
            .catch(this.handleError);
351
    }
352

    
353
    updatePageHelpContent(pageHelpContent: PageHelpContent, helpContentUrl:string) {
354
        let headers = new Headers({'Content-Type': 'application/json'});
355
        let options = new RequestOptions({headers: headers});
356

    
357
        HelpContentService.removeNulls(pageHelpContent);
358

    
359
        return this.http.post(helpContentUrl + 'pagehelpcontent/update', JSON.stringify(pageHelpContent), options)
360
            .map(res => <PageHelpContent> res.json())
361
            .catch(this.handleError);
362
    }
363

    
364
    deletePageHelpContents(ids : string[], helpContentUrl:string) {
365
        let headers = new Headers({'Content-Type': 'application/json'});
366
        let options = new RequestOptions({headers: headers});
367

    
368
        return this.http.post(helpContentUrl + 'pagehelpcontent/delete',JSON.stringify(ids), options)
369
            .catch(this.handleError);
370
    }
371

    
372
    togglePageHelpContents(ids : string[],status : boolean, helpContentUrl:string) {
373
        let headers = new Headers({'Content-Type': 'application/json'});
374
        let options = new RequestOptions({headers: headers});
375

    
376
        return this.http.post(helpContentUrl + 'pagehelpcontent/toggle?status='+ status.toString(), JSON.stringify(ids), options)
377
            //.map( res => <string[]> res.json())
378
            .catch(this.handleError);
379
    }
380

    
381
    private handleError(error: Response) {
382
        // in a real world app, we may send the error to some remote logging infrastructure
383
        // instead of just logging it to the console
384
        console.error(error);
385
        return Observable.throw(error.json().error || 'Server error');
386
    }
387
    public getAuthOptionsWithBody():RequestOptions{
388
    let headers = new Headers();
389
    headers.append('Content-Type', 'application/json');
390
    headers.append('X-XSRF-TOKEN',  COOKIE.getCookie(COOKIE.cookieName_id));
391
    let options = new RequestOptions({ headers: headers, withCredentials:true  });
392
      return options;
393
    }
394

    
395
    getDataProviders() {
396
        return this.http.get('https://beta.services.openaire.eu/search/v2/api/datasources?format=json').map(res => <any> res.json()).map(res => res.results).do(res => {console.log(res)}).catch(this.handleError);
397
    }
398

    
399
    getCommunityStatistics(apiUrl: string, communityId: string): Observable<StatisticsSummary> {
400
        const url = `${apiUrl}communities/${communityId}`;
401
        console.log(`getting statistics summary from: ${url}`);
402
        return this.http.get(url)
403
            .map(res => <any>res.json())
404
            .do(res => { console.log(res); })
405
            .map(res => res.statistics)
406
            .do(res => { console.log(res); });
407
    }
408

    
409
    getCommunityAdminStatisticsChoices(apiUrl: string, communityId: string): Observable<StatisticsDisplay> {
410
        const url = `${apiUrl}statistics/${communityId}`;
411
        console.log(`getting admin choices for statistics from: ${url}`);
412
        return this.http.get(url)
413
            .map(stats => <StatisticsDisplay>stats.json())
414
            .catch(this.handleError);
415
    }
416

    
417
    postCommunityAdminStatisticsChoices(apiUrl: string,
418
                                        communityId: string,
419
                                        entity: string,
420
                                        chartsOrNumbers: string,
421
                                        title: string,
422
                                        status: boolean,
423
                                        monitor: boolean): Observable<any> {
424
        const url = `${apiUrl}statistics/${communityId}/${entity}/${chartsOrNumbers}?status=${status.toString()}&monitor=${monitor.toString()}`;
425
        console.log(`getting admin choices for statistics from: ${url}`);
426

    
427
        const headers = new Headers({'Content-Type': 'application/json'});
428
        const options = new RequestOptions({headers: headers});
429

    
430
        return this.http.post(url, title, options)
431
            .map(stats => <any>stats.json())
432
            .catch(this.handleError);
433
    }
434
}
(2-2/4)