Project

General

Profile

1
/**
2
 * Created by stefania on 7/13/17.
3
 */
4
import { Injectable } from '@angular/core';
5
import {HttpClient, HttpErrorResponse, HttpHeaders} from "@angular/common/http";
6
import {Observable, Subscription} from 'rxjs';
7
import { Page } from "../domain/page";
8
import { PageHelpContent } from "../domain/page-help-content";
9
import { Portal } from "../domain/portal";
10
import { Entity } from "../domain/entity";
11
import { DivId } from "../domain/divId";
12
import { DivHelpContent } from "../domain/div-help-content";
13
import {StatisticsDisplay, StatisticsSummary} from '../openaireLibrary/connect/statistics/statisticsEntities';
14
import { CustomOptions } from '../openaireLibrary/services/servicesUtils/customOptions.class';
15
import {catchError, map} from "rxjs/operators";
16
import {COOKIE} from "../openaireLibrary/login/utils/helper.class";
17
import {properties} from "../../environments/environment";
18

    
19

    
20
@Injectable()
21
export class HelpContentService {
22

    
23
  constructor(private http:HttpClient) {
24
  }
25

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

    
34
  getEntities(helpContentUrl:string) {
35
    return this.http.get<Array<Entity>>(helpContentUrl + 'entity')
36
    //.map(res => <Array<Entity>> res.json())
37
      .pipe(catchError(this.handleError));
38
  }
39

    
40
  saveEntity(entity: Entity, helpContentUrl:string) {
41
    HelpContentService.removeNulls(entity);
42

    
43
    return this.http.post<Entity>(helpContentUrl + 'entity/save', JSON.stringify(entity), CustomOptions.getAuthOptionsWithBody())
44
    //.map(res => <Entity> res.json())
45
      .pipe(catchError(this.handleError));
46
  }
47

    
48
  updateEntity(entity: Entity, helpContentUrl:string) {
49
    HelpContentService.removeNulls(entity);
50

    
51
    return this.http.post<Entity>(helpContentUrl + 'entity/update', JSON.stringify(entity), CustomOptions.getAuthOptionsWithBody())
52
    //.map(res => <Entity> res.json())
53
      .pipe(catchError(this.handleError));
54
  }
55

    
56
  deleteEntities(ids : string[], helpContentUrl:string) {
57
    return this.http.post(helpContentUrl + 'entity/delete',JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
58
      .pipe(catchError(this.handleError));
59
  }
60

    
61
  // toggleEntity(selectedCommunityId: string, id : string,status : boolean) {
62
  //     let headers = new Headers({'Content-Type': 'application/json'});
63
  //     let options = new RequestOptions({headers: headers});
64
  //
65
  //     return this.http.post(helpContentUrl + 'community/'+selectedCommunityId+'/entity/toggle?status='+ status.toString()+'&entityId='+id.toString(), options)
66
  //         .catch(this.handleError);
67
  // }
68

    
69
  getCommunityEntities(pid: string, helpContentUrl:string) {
70
    return this.http.get<Array<Entity>>(helpContentUrl + properties.adminToolsPortalType + '/'+pid+'/entities')
71
    //.map(res => <Array<Entity>> res.json())
72
      .pipe(catchError(this.handleError));
73
  }
74

    
75
  toggleEntities(pid: string, ids : string[],status : boolean, helpContentUrl:string) {
76

    
77
    return this.http.post(helpContentUrl + properties.adminToolsPortalType + '/'+pid+ '/entity/toggle?status='+ status.toString(),
78
                            JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
79
    //.map( res => <string[]> res.json())
80
      .pipe(catchError(this.handleError));
81
  }
82

    
83
  // unused
84
  toggleEntityOfPage(pageId: string, entityId : string,status : boolean, helpContentUrl:string) {
85
    return this.http.post(helpContentUrl + 'page/'+pageId+'/entity/toggle?status='+ status.toString()+'&entityId='+entityId.toString(),
86
                            CustomOptions.getAuthOptionsWithBody())
87
      .pipe(catchError(this.handleError));
88
  }
89

    
90
  // getDivIdsFull(page_id: string, helpContentUrl:string, pid: string = null) {
91
  //   let parameters: string = "";
92
  //   if(page_id || pid) {
93
  //     parameters = "?";
94
  //     if(page_id) {
95
  //       parameters += "&page="+page_id;
96
  //     }
97
  //     if(pid) {
98
  //       parameters += "&portal="+pid;
99
  //     }
100
  //   }
101
  //
102
  //   return this.http.get<Array<DivId>>(helpContentUrl + 'div/full'+parameters)
103
  //       //.map(res => <Array<DivId>> res.json())
104
  //       .pipe(catchError(this.handleError));
105
  // }
106

    
107
  // Replacing getDivIdsFull
108
  getAllDivIdsFull(helpContentUrl:string) {
109
    return this.http.get<Array<DivId>>(helpContentUrl + 'div/full')
110
    //.map(res => <Array<DivId>> res.json())
111
      .pipe(catchError(this.handleError));
112
  }
113

    
114
  getDivIdsFullByPortal(page_id: string, helpContentUrl:string, pid: string) {
115
    let parameters: string = page_id ? "?&page="+page_id : "";
116

    
117
    return this.http.get<Array<DivId>>(helpContentUrl + properties.adminToolsPortalType + '/'+pid + '/div/full'+parameters)
118
      .pipe(catchError(this.handleError));
119
  }
120
  // End of replacing getDivIdsFull
121

    
122
  // unused
123
  getDivId(divId: string, helpContentUrl:string) {
124
      return this.http.get<DivId>(helpContentUrl + 'div/'+divId)
125
          //.map(res => <DivId> res.json())
126
          .pipe(catchError(this.handleError));
127
  }
128

    
129
  getDivIdFull(divId: string, helpContentUrl:string, pid: string) {
130
      return this.http.get<DivId>(helpContentUrl + properties.adminToolsPortalType + "/" + pid + '/div/'+divId+"/full")
131
          //.map(res => <DivId> res.json())
132
          .pipe(catchError(this.handleError));
133
  }
134

    
135
  updateDivId(divId: DivId, helpContentUrl:string) {
136
    HelpContentService.removeNulls(divId);
137

    
138
    return this.http.post<DivId>(helpContentUrl + 'div/update', JSON.stringify(divId), CustomOptions.getAuthOptionsWithBody())
139
    //.map(res => <DivId> res.json())
140
      .pipe(catchError(this.handleError));
141
  }
142

    
143
  saveDivId(divId: DivId, helpContentUrl:string) {
144
      HelpContentService.removeNulls(divId);
145

    
146
      return this.http.post<DivId>(helpContentUrl + 'div/save', JSON.stringify(divId), CustomOptions.getAuthOptionsWithBody())
147
          //.map(res => <DivId> res.json())
148
          .pipe(catchError(this.handleError));
149
  }
150

    
151
  deleteDivIds(ids : string[], helpContentUrl:string) {
152
    return this.http.post(helpContentUrl + 'div/delete',JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
153
      .pipe(catchError(this.handleError));
154
  }
155

    
156
  getPageIdsFromDivIds(pid: string, helpContentUrl:string) {
157
    //let parameters = (pid ? "?portal="+pid : "");
158
    return this.http.get<Array<string>>(helpContentUrl + properties.adminToolsPortalType + "/" + pid + '/div/pages')
159
    //.map(res => <Map<string, Set<string>>> res.json())
160
      .pipe(catchError(this.handleError));
161
  }
162

    
163
/*
164
  getCommunitiesWithDivId(helpContentUrl:string) {
165
      return this.http.get(helpContentUrl + 'community?div=true')
166
          .map(res => <Array<Portal>> res.json())
167
          .catch(this.handleError);
168
  }
169
*/
170

    
171
  getCommunityDivHelpContents(pid: string, helpContentUrl:string) {
172
    return this.http.get<Array<DivHelpContent>>(helpContentUrl + properties.adminToolsPortalType + "/" + pid + '/divhelpcontent')
173
        //.map(res => <Array<DivHelpContent>> res.json())
174
        .pipe(catchError(this.handleError));
175
  }
176

    
177
  getDivHelpContent(id : string, helpContentUrl:string, pid: string) {
178
    return this.http.get<DivHelpContent>(helpContentUrl + properties.adminToolsPortalType + "/" + pid + '/divhelpcontent/' + id)
179
        //.map(res => <DivHelpContent> res.json())
180
        .pipe(catchError(this.handleError));
181
  }
182

    
183
  // unused
184
  insertOrUpdateDivHelpContent(divHelpContent: DivHelpContent, helpContentUrl:string, pid: string) {
185
      HelpContentService.removeNulls(divHelpContent);
186

    
187
      return this.http.post<DivHelpContent>(helpContentUrl + properties.adminToolsPortalType + "/" + pid + '/divhelpcontent',
188
                                              JSON.stringify(divHelpContent), CustomOptions.getAuthOptionsWithBody())
189
          //.map(res => <DivHelpContent> res.json())
190
          .pipe(catchError(this.handleError));
191
  }
192

    
193
  saveDivHelpContent(divHelpContent: DivHelpContent, helpContentUrl:string, pid: string) {
194
    HelpContentService.removeNulls(divHelpContent);
195

    
196
    return this.http.post<DivHelpContent>(helpContentUrl + properties.adminToolsPortalType + '/' + pid + '/divhelpcontent/save',
197
                                            JSON.stringify(divHelpContent), CustomOptions.getAuthOptionsWithBody())
198
      .pipe(catchError(this.handleError));
199
  }
200

    
201
  updateDivHelpContent(divHelpContent: DivHelpContent, helpContentUrl:string, pid: string) {
202
    HelpContentService.removeNulls(divHelpContent);
203

    
204
    return this.http.post<DivHelpContent>(helpContentUrl + properties.adminToolsPortalType + '/' + pid +'/divhelpcontent/update',
205
                                            JSON.stringify(divHelpContent), CustomOptions.getAuthOptionsWithBody())
206
      .pipe(catchError(this.handleError));
207
  }
208

    
209
  deleteDivHelpContents(ids : string[], helpContentUrl:string, pid: string) {
210
       return this.http.post(helpContentUrl + properties.adminToolsPortalType + "/" + pid + '/divhelpcontent/delete',
211
                                JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
212
          .pipe(catchError(this.handleError));
213
  }
214

    
215
  toggleDivHelpContents(ids : string[],status : boolean, helpContentUrl:string, pid: string) {
216

    
217
      return this.http.post(helpContentUrl + properties.adminToolsPortalType + '/' + pid + '/divhelpcontent/toggle?status='+ status.toString(),
218
                              JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
219
          //.map( res => <string[]> res.json())
220
          .pipe(catchError(this.handleError));
221
  }
222

    
223
  // unused
224
  getPageHelpContents(helpContentUrl:string) {
225
    return this.http.get<Array<PageHelpContent>>(helpContentUrl + 'pagehelpcontent')
226
    //.map(res => <Array<PageHelpContent>> res.json())
227
      .pipe(catchError(this.handleError));
228
  }
229

    
230
  getCommunityPageHelpContents(pid: string, helpContentUrl:string) {
231
    return this.http.get<Array<PageHelpContent>>(helpContentUrl + properties.adminToolsPortalType + '/' + pid + '/pagehelpcontent')
232
    //.map(res => <Array<PageHelpContent>> res.json())
233
      .pipe(catchError(this.handleError));
234
  }
235

    
236
  getPageHelpContent(id : string, helpContentUrl:string, pid: string) {
237
    return this.http.get<PageHelpContent>(helpContentUrl + properties.adminToolsPortalType + '/' + pid + '/pagehelpcontent/' + id)
238
    //.map(res => <PageHelpContent> res.json())
239
      .pipe(catchError(this.handleError));
240
  }
241

    
242
  savePageHelpContent(pageHelpContent: PageHelpContent, helpContentUrl:string, pid: string) {
243
    HelpContentService.removeNulls(pageHelpContent);
244

    
245
    return this.http.post<PageHelpContent>(helpContentUrl + properties.adminToolsPortalType + '/' + pid + '/pagehelpcontent/save',
246
                                          JSON.stringify(pageHelpContent), CustomOptions.getAuthOptionsWithBody())
247
    //.map(res => <PageHelpContent> res.json())
248
      .pipe(catchError(this.handleError));
249
  }
250

    
251
  updatePageHelpContent(pageHelpContent: PageHelpContent, helpContentUrl:string, pid: string) {
252
    HelpContentService.removeNulls(pageHelpContent);
253

    
254
    return this.http.post<PageHelpContent>(helpContentUrl + properties.adminToolsPortalType + '/' + pid +'/pagehelpcontent/update',
255
                                          JSON.stringify(pageHelpContent), CustomOptions.getAuthOptionsWithBody())
256
    //.map(res => <PageHelpContent> res.json())
257
      .pipe(catchError(this.handleError));
258
  }
259

    
260
  deletePageHelpContents(ids : string[], helpContentUrl:string, pid: string) {
261
    return this.http.post(helpContentUrl + properties.adminToolsPortalType + '/' + pid + '/pagehelpcontent/delete',
262
                            JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
263
      .pipe(catchError(this.handleError));
264
  }
265

    
266
  togglePageHelpContents(ids : string[],status : boolean, helpContentUrl:string, pid: string) {
267
    return this.http.post(helpContentUrl + properties.adminToolsPortalType + '/' + pid + '/pagehelpcontent/toggle?status='+ status.toString(),
268
                            JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
269
    //.map( res => <string[]> res.json())
270
      .pipe(catchError(this.handleError));
271
  }
272

    
273
  getCommunityPagesWithDivId(pid: string, helpContentUrl:string) {
274
    return this.http.get<Array<Page>>(helpContentUrl + properties.adminToolsPortalType + '/'+pid+'/pages?div=true')
275
    //.map(res => <Array<Page>> res.json())
276
      .pipe(catchError(this.handleError));
277
  }
278

    
279
  // getCommunityPages(pid: string, params: string, helpContentUrl:string) {
280
  //   return this.http.get<Array<Page>>(helpContentUrl + properties.adminToolsPortalType + '/'+pid+'/pages'+params, CustomOptions.getAuthOptions())
281
  //   //.map(res => <Array<Page>> res.json())
282
  //     .pipe(catchError(this.handleError));
283
  // }
284
  
285
  getCommunityPagesByRoute(pid: string, route: string, helpContentUrl:string) {
286
    return this.http.get<Array<Page>>(helpContentUrl + properties.adminToolsPortalType + '/'+pid+'/pages'
287
      + (route ? '?page_route='+route : ''))
288
      .pipe(map(pages => (pages.length>0?pages[0]:null)), catchError(this.handleError));
289
  }
290
  
291
  // Replacing getCommunityPages
292
  getCommunityPagesByType(pid: string, type: string, helpContentUrl:string) {
293
    return this.http.get<Array<Page>>(helpContentUrl + properties.adminToolsPortalType + '/'+pid+'/pages'
294
                                        + (type ? '?page_type='+type : ''))
295
      .pipe(catchError(this.handleError));
296
  }
297
  // End of replacing getCommunityPages
298

    
299
  // Replacing part of getPages (now getAllPages)
300
  getCommunityPagesWithPositions(pid: string, helpContentUrl:string) {
301
    return this.http.get<Array<Page>>(helpContentUrl + properties.adminToolsPortalType + '/'+pid+'/pages?with_positions=true')
302
    //.map(res => <Array<Page>> res.json())
303
      .pipe(catchError(this.handleError));
304
  }
305
  // End of replacing part of getPages (now getAllPages)
306

    
307
  getAllPages(helpContentUrl:string) {//,pid:string,with_positions:boolean=null) {
308
      // let parameters: string = "";
309
      // if(pid || with_positions == true || with_positions == false) {
310
      //   parameters = "?";
311
      //   if(pid) {
312
      //     parameters += "&pid="+pid;
313
      //   }
314
      //   if(with_positions == true || with_positions == false) {
315
      //     parameters += "&with_positions="+with_positions;
316
      //   }
317
      // }
318
      return this.http.get<Array<Page>>(helpContentUrl + 'page')
319
          //.map(res => <Array<Page>> res.json())
320
          .pipe(catchError(this.handleError));
321
  }
322

    
323
  getAllPagesFull(helpContentUrl:string) {
324
      return this.http.get<Array<Page>>(helpContentUrl + 'page/full')//+(pid?("?pid="+pid):""))
325
      //.map(res => <Array<Page>> res.json())
326
        .pipe(catchError(this.handleError));
327
  }
328

    
329
  getPageByPortal(pageId:string, helpContentUrl:string, pid: string) {
330
      return this.http.get<Page>(helpContentUrl + properties.adminToolsPortalType + '/' + pid + '/page/'+pageId)
331
          //.map(res => <Page> res.json())
332
          .pipe(catchError(this.handleError));
333
  }
334

    
335
  getCommunityPageByRoute(route:string, helpContentUrl:string, pid: string) {
336
    return this.http.get<Page>(helpContentUrl + properties.adminToolsPortalType +'/' + pid + '/page/?page_route='+route)
337
      .pipe(catchError(this.handleError));
338
  }
339

    
340
  savePage(page: Page, helpContentUrl:string) {
341
    HelpContentService.removeNulls(page);
342

    
343
    return this.http.post<Page>(helpContentUrl + 'page/save', JSON.stringify(page), CustomOptions.getAuthOptionsWithBody())
344
    //.map(res => <Page> res.json())
345
      .pipe(catchError(this.handleError));
346
  }
347

    
348
  updatePage(page: Page, helpContentUrl:string) {
349

    
350
    HelpContentService.removeNulls(page);
351

    
352
    return this.http.post<Page>(helpContentUrl + 'page/update', JSON.stringify(page), CustomOptions.getAuthOptionsWithBody())
353
    //.map(res => <Page> res.json())
354
      .pipe(catchError(this.handleError));
355
  }
356

    
357
  togglePages(selectedPortalPid: string, ids : string[],status : boolean, helpContentUrl:string) {
358

    
359
    return this.http.post(helpContentUrl + properties.adminToolsPortalType + '/'+selectedPortalPid+'/page/toggle?status='+ status.toString(),
360
                            JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
361
      .pipe(catchError(this.handleError));
362
  }
363

    
364
  deletePages(ids : string[], helpContentUrl:string) {
365

    
366
    return this.http.post(helpContentUrl + 'page/delete',JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
367
      .pipe(catchError(this.handleError));
368
  }
369

    
370
  // unused
371
  getCommunities( helpContentUrl:string) {
372
      return this.http.get<Array<Portal>>(helpContentUrl + properties.adminToolsPortalType)
373
          //.map(res => <Array<Portal>> res.json())
374
          .pipe(catchError(this.handleError));
375
  }
376

    
377
  getCommunity(pid: string, helpContentUrl:string) {
378
      return this.http.get<Portal>(helpContentUrl + properties.adminToolsPortalType + '/'+pid)
379
          //.map(res => <Portal> res.json())
380
          .pipe(catchError(this.handleError));
381
  }
382

    
383
  getCommunitiesFull( helpContentUrl:string) {
384
      return this.http.get<Array<Portal>>(helpContentUrl + properties.adminToolsPortalType + '/full')
385
          //.map(res => <Array<Portal>> res.json())
386
          .pipe(catchError(this.handleError));
387
  }
388

    
389
  getCommunityFull(portal_pid: string, helpContentUrl:string) {
390
      return this.http.get<Portal>(helpContentUrl + properties.adminToolsPortalType + '/'+portal_pid+'/full')
391
          //.map(res => <Portal> res.json())
392
          .pipe(catchError(this.handleError));
393
  }
394

    
395
  saveCommunity(portal: Portal, helpContentUrl:string) {
396
      // let headers = new Headers({'Content-Type': 'application/json'});
397
      // let options = new RequestOptions({headers: headers});
398

    
399
      HelpContentService.removeNulls(portal);
400

    
401
      return this.http.post<Portal>(helpContentUrl + portal.type + '/save', JSON.stringify(portal), CustomOptions.getAuthOptionsWithBody())
402
      //.map(res => <Portal> res.json())
403
          .pipe(catchError(this.handleError));
404
  }
405

    
406
  updateCommunity(portal: Portal, helpContentUrl:string) {
407
      // let headers = new Headers({'Content-Type': 'application/json'});
408
      // let options = new RequestOptions({headers: headers});
409

    
410
      HelpContentService.removeNulls(portal);
411

    
412
      return this.http.post<Portal>(helpContentUrl + portal.type + '/update', JSON.stringify(portal),  CustomOptions.getAuthOptionsWithBody())
413
          //.map(res => <Portal> res.json())
414
          .pipe(catchError(this.handleError));
415
  }
416

    
417
  deleteCommunities(ids : string[], helpContentUrl:string) {
418
      // let headers = new Headers({'Content-Type': 'application/json'});
419
      // let options = new RequestOptions({headers: headers});
420

    
421
      return this.http.post(helpContentUrl + properties.adminToolsPortalType + '/delete',JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
422
          .pipe(catchError(this.handleError));
423
  }
424

    
425
  private handleError(error: HttpErrorResponse) {
426
      // in a real world app, we may send the error to some remote logging infrastructure
427
      // instead of just logging it to the console
428
      console.error(error);
429
      return Observable.throw(error.error || 'Server error');
430
  }
431

    
432
    // getDataProviders() {
433
    //     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);
434
    // }
435

    
436
    getCommunityStatistics(apiUrl: string, portalId: string): Observable<StatisticsSummary> {
437
        const url = `${apiUrl}communities/${portalId}`;
438
        //console.log(`getting statistics summary from: ${url}`);
439
        return this.http.get(url)
440
            //.map(res => <any>res.json())
441
            .pipe(map(res => res['statistics']));
442
    }
443

    
444
    getCommunityAdminStatisticsChoices(apiUrl: string, portalId: string): Observable<StatisticsDisplay> {
445
        const url = `${apiUrl}statistics/${portalId}`;
446
        //console.log(`getting admin choices for statistics from: ${url}`);
447
        return this.http.get<StatisticsDisplay>(url)
448
            //.map(stats => <StatisticsDisplay>stats.json())
449
            .pipe(catchError(this.handleError));
450
    }
451

    
452
    postCommunityAdminStatisticsChoices(apiUrl: string,
453
                                        portalId: string,
454
                                        entity: string,
455
                                        chartsOrNumbers: string,
456
                                        title: string,
457
                                        status: boolean,
458
                                        monitor: boolean): Observable<any> {
459
        const url = `${apiUrl}statistics/${portalId}/${entity}/${chartsOrNumbers}?status=${status.toString()}&monitor=${monitor.toString()}`;
460
        //console.log(`getting admin choices for statistics from: ${url}`);
461

    
462
        return this.http.post(url, title, CustomOptions.getAuthOptionsWithBody())
463
            //.map(stats => <any>stats.json())
464
            .pipe(catchError(this.handleError));
465
    }
466

    
467
    statisticsIsActiveToggle(apiURL: string, id: string): Observable<boolean> {
468
        const url = apiURL + '/statistics/' + encodeURIComponent(id) + '/toggle';
469
        return this.http.post<boolean>(url, {}, CustomOptions.getAuthOptionsWithBody()).pipe(catchError(this.handleError));
470
    }
471
}
(1-1/4)