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
  getDivIdsFullByPageAndPortal(page_id: string, helpContentUrl:string, pid: string) {
115
    let parameters: string = "?&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
  insertOrUpdateDivHelpContent(divHelpContent: DivHelpContent, helpContentUrl:string, pid: string) {
184
      HelpContentService.removeNulls(divHelpContent);
185

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

    
192
  deleteDivHelpContents(ids : string[], helpContentUrl:string, pid: string) {
193
       return this.http.post(helpContentUrl + properties.adminToolsPortalType + "/" + pid + '/divhelpcontent/delete',
194
                                JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
195
          .pipe(catchError(this.handleError));
196
  }
197

    
198
  toggleDivHelpContents(ids : string[],status : boolean, helpContentUrl:string, pid: string) {
199

    
200
      return this.http.post(helpContentUrl + properties.adminToolsPortalType + '/' + pid + '/divhelpcontent/toggle?status='+ status.toString(),
201
                              JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
202
          //.map( res => <string[]> res.json())
203
          .pipe(catchError(this.handleError));
204
  }
205

    
206
  // unused
207
  getPageHelpContents(helpContentUrl:string) {
208
    return this.http.get<Array<PageHelpContent>>(helpContentUrl + 'pagehelpcontent')
209
    //.map(res => <Array<PageHelpContent>> res.json())
210
      .pipe(catchError(this.handleError));
211
  }
212

    
213
  getCommunityPageHelpContents(pid: string, helpContentUrl:string) {
214
    return this.http.get<Array<PageHelpContent>>(helpContentUrl + properties.adminToolsPortalType + '/' + pid + '/pagehelpcontent')
215
    //.map(res => <Array<PageHelpContent>> res.json())
216
      .pipe(catchError(this.handleError));
217
  }
218

    
219
  getPageHelpContent(id : string, helpContentUrl:string, pid: string) {
220
    return this.http.get<PageHelpContent>(helpContentUrl + properties.adminToolsPortalType + '/' + pid + '/pagehelpcontent/' + id)
221
    //.map(res => <PageHelpContent> res.json())
222
      .pipe(catchError(this.handleError));
223
  }
224

    
225
  savePageHelpContent(pageHelpContent: PageHelpContent, helpContentUrl:string, pid: string) {
226
    HelpContentService.removeNulls(pageHelpContent);
227

    
228
    return this.http.post<PageHelpContent>(helpContentUrl + properties.adminToolsPortalType + '/' + pid + '/pagehelpcontent/save',
229
                                          JSON.stringify(pageHelpContent), CustomOptions.getAuthOptionsWithBody())
230
    //.map(res => <PageHelpContent> res.json())
231
      .pipe(catchError(this.handleError));
232
  }
233

    
234
  updatePageHelpContent(pageHelpContent: PageHelpContent, helpContentUrl:string, pid: string) {
235
    HelpContentService.removeNulls(pageHelpContent);
236

    
237
    return this.http.post<PageHelpContent>(helpContentUrl + properties.adminToolsPortalType + '/' + pid +'/pagehelpcontent/update',
238
                                          JSON.stringify(pageHelpContent), CustomOptions.getAuthOptionsWithBody())
239
    //.map(res => <PageHelpContent> res.json())
240
      .pipe(catchError(this.handleError));
241
  }
242

    
243
  deletePageHelpContents(ids : string[], helpContentUrl:string, pid: string) {
244
    return this.http.post(helpContentUrl + properties.adminToolsPortalType + '/' + pid + '/pagehelpcontent/delete',
245
                            JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
246
      .pipe(catchError(this.handleError));
247
  }
248

    
249
  togglePageHelpContents(ids : string[],status : boolean, helpContentUrl:string, pid: string) {
250
    return this.http.post(helpContentUrl + properties.adminToolsPortalType + '/' + pid + '/pagehelpcontent/toggle?status='+ status.toString(),
251
                            JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
252
    //.map( res => <string[]> res.json())
253
      .pipe(catchError(this.handleError));
254
  }
255

    
256
  getCommunityPagesWithDivId(pid: string, helpContentUrl:string) {
257
    return this.http.get<Array<Page>>(helpContentUrl + properties.adminToolsPortalType + '/'+pid+'/pages?div=true')
258
    //.map(res => <Array<Page>> res.json())
259
      .pipe(catchError(this.handleError));
260
  }
261

    
262
  // getCommunityPages(pid: string, params: string, helpContentUrl:string) {
263
  //   return this.http.get<Array<Page>>(helpContentUrl + properties.adminToolsPortalType + '/'+pid+'/pages'+params, CustomOptions.getAuthOptions())
264
  //   //.map(res => <Array<Page>> res.json())
265
  //     .pipe(catchError(this.handleError));
266
  // }
267

    
268
  // Replacing getCommunityPages
269
  getCommunityPagesByType(pid: string, type: string, helpContentUrl:string) {
270
    return this.http.get<Array<Page>>(helpContentUrl + properties.adminToolsPortalType + '/'+pid+'/pages'
271
                                        + (type ? '?page_type='+type : ''))
272
    //.map(res => <Array<Page>> res.json())
273
      .pipe(catchError(this.handleError));
274
  }
275
  // End of replacing getCommunityPages
276

    
277
  // Replacing part of getPages (now getAllPages)
278
  getCommunityPagesWithPositions(pid: string, helpContentUrl:string) {
279
    return this.http.get<Array<Page>>(helpContentUrl + properties.adminToolsPortalType + '/'+pid+'/pages?with_positions=true')
280
    //.map(res => <Array<Page>> res.json())
281
      .pipe(catchError(this.handleError));
282
  }
283
  // End of replacing part of getPages (now getAllPages)
284

    
285
  getAllPages(helpContentUrl:string) {//,pid:string,with_positions:boolean=null) {
286
      // let parameters: string = "";
287
      // if(pid || with_positions == true || with_positions == false) {
288
      //   parameters = "?";
289
      //   if(pid) {
290
      //     parameters += "&pid="+pid;
291
      //   }
292
      //   if(with_positions == true || with_positions == false) {
293
      //     parameters += "&with_positions="+with_positions;
294
      //   }
295
      // }
296
      return this.http.get<Array<Page>>(helpContentUrl + 'page')
297
          //.map(res => <Array<Page>> res.json())
298
          .pipe(catchError(this.handleError));
299
  }
300

    
301
  getAllPagesFull(helpContentUrl:string) {
302
      return this.http.get<Array<Page>>(helpContentUrl + 'page/full')//+(pid?("?pid="+pid):""))
303
      //.map(res => <Array<Page>> res.json())
304
        .pipe(catchError(this.handleError));
305
  }
306

    
307
  getPageByPortal(pageId:string, helpContentUrl:string, pid: string) {
308
      return this.http.get<Page>(helpContentUrl + properties.adminToolsPortalType + '/' + pid + '/page/'+pageId)
309
          //.map(res => <Page> res.json())
310
          .pipe(catchError(this.handleError));
311
  }
312

    
313
  getCommunityPageByRoute(route:string, helpContentUrl:string, pid: string) {
314
    return this.http.get<Page>(helpContentUrl + properties.adminToolsPortalType +'/' + pid + '/page/?page_route='+route)
315
      .pipe(catchError(this.handleError));
316
  }
317

    
318
  savePage(page: Page, helpContentUrl:string) {
319
    HelpContentService.removeNulls(page);
320

    
321
    return this.http.post<Page>(helpContentUrl + 'page/save', JSON.stringify(page), CustomOptions.getAuthOptionsWithBody())
322
    //.map(res => <Page> res.json())
323
      .pipe(catchError(this.handleError));
324
  }
325

    
326
  updatePage(page: Page, helpContentUrl:string) {
327

    
328
    HelpContentService.removeNulls(page);
329

    
330
    return this.http.post<Page>(helpContentUrl + 'page/update', JSON.stringify(page), CustomOptions.getAuthOptionsWithBody())
331
    //.map(res => <Page> res.json())
332
      .pipe(catchError(this.handleError));
333
  }
334

    
335
  togglePages(selectedPortalPid: string, ids : string[],status : boolean, helpContentUrl:string) {
336

    
337
    return this.http.post(helpContentUrl + properties.adminToolsPortalType + '/'+selectedPortalPid+'/page/toggle?status='+ status.toString(),
338
                            JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
339
      .pipe(catchError(this.handleError));
340
  }
341

    
342
  deletePages(ids : string[], helpContentUrl:string) {
343

    
344
    return this.http.post(helpContentUrl + 'page/delete',JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
345
      .pipe(catchError(this.handleError));
346
  }
347

    
348
  // unused
349
  getCommunities( helpContentUrl:string) {
350
      return this.http.get<Array<Portal>>(helpContentUrl + properties.adminToolsPortalType)
351
          //.map(res => <Array<Portal>> res.json())
352
          .pipe(catchError(this.handleError));
353
  }
354

    
355
  getCommunity(pid: string, helpContentUrl:string) {
356
      return this.http.get<Portal>(helpContentUrl + properties.adminToolsPortalType + '/'+pid)
357
          //.map(res => <Portal> res.json())
358
          .pipe(catchError(this.handleError));
359
  }
360

    
361
  getCommunitiesFull( helpContentUrl:string) {
362
      return this.http.get<Array<Portal>>(helpContentUrl + properties.adminToolsPortalType + '/full')
363
          //.map(res => <Array<Portal>> res.json())
364
          .pipe(catchError(this.handleError));
365
  }
366

    
367
  getCommunityFull(portal_pid: string, helpContentUrl:string) {
368
      return this.http.get<Portal>(helpContentUrl + properties.adminToolsPortalType + '/'+portal_pid+'/full')
369
          //.map(res => <Portal> res.json())
370
          .pipe(catchError(this.handleError));
371
  }
372

    
373
  saveCommunity(portal: Portal, helpContentUrl:string) {
374
      // let headers = new Headers({'Content-Type': 'application/json'});
375
      // let options = new RequestOptions({headers: headers});
376

    
377
      HelpContentService.removeNulls(portal);
378

    
379
      return this.http.post<Portal>(helpContentUrl + portal.type + '/save', JSON.stringify(portal), CustomOptions.getAuthOptionsWithBody())
380
      //.map(res => <Portal> res.json())
381
          .pipe(catchError(this.handleError));
382
  }
383

    
384
  updateCommunity(portal: Portal, helpContentUrl:string) {
385
      // let headers = new Headers({'Content-Type': 'application/json'});
386
      // let options = new RequestOptions({headers: headers});
387

    
388
      HelpContentService.removeNulls(portal);
389

    
390
      return this.http.post<Portal>(helpContentUrl + portal.type + '/update', JSON.stringify(portal),  CustomOptions.getAuthOptionsWithBody())
391
          //.map(res => <Portal> res.json())
392
          .pipe(catchError(this.handleError));
393
  }
394

    
395
  deleteCommunities(ids : string[], helpContentUrl:string) {
396
      // let headers = new Headers({'Content-Type': 'application/json'});
397
      // let options = new RequestOptions({headers: headers});
398

    
399
      return this.http.post(helpContentUrl + properties.adminToolsPortalType + '/delete',JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
400
          .pipe(catchError(this.handleError));
401
  }
402

    
403
  private handleError(error: HttpErrorResponse) {
404
      // in a real world app, we may send the error to some remote logging infrastructure
405
      // instead of just logging it to the console
406
      console.error(error);
407
      return Observable.throw(error.error || 'Server error');
408
  }
409

    
410
    // getDataProviders() {
411
    //     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);
412
    // }
413

    
414
    getCommunityStatistics(apiUrl: string, portalId: string): Observable<StatisticsSummary> {
415
        const url = `${apiUrl}communities/${portalId}`;
416
        //console.log(`getting statistics summary from: ${url}`);
417
        return this.http.get(url)
418
            //.map(res => <any>res.json())
419
            .pipe(map(res => res['statistics']));
420
    }
421

    
422
    getCommunityAdminStatisticsChoices(apiUrl: string, portalId: string): Observable<StatisticsDisplay> {
423
        const url = `${apiUrl}statistics/${portalId}`;
424
        //console.log(`getting admin choices for statistics from: ${url}`);
425
        return this.http.get<StatisticsDisplay>(url)
426
            //.map(stats => <StatisticsDisplay>stats.json())
427
            .pipe(catchError(this.handleError));
428
    }
429

    
430
    postCommunityAdminStatisticsChoices(apiUrl: string,
431
                                        portalId: string,
432
                                        entity: string,
433
                                        chartsOrNumbers: string,
434
                                        title: string,
435
                                        status: boolean,
436
                                        monitor: boolean): Observable<any> {
437
        const url = `${apiUrl}statistics/${portalId}/${entity}/${chartsOrNumbers}?status=${status.toString()}&monitor=${monitor.toString()}`;
438
        //console.log(`getting admin choices for statistics from: ${url}`);
439

    
440
        return this.http.post(url, title, CustomOptions.getAuthOptionsWithBody())
441
            //.map(stats => <any>stats.json())
442
            .pipe(catchError(this.handleError));
443
    }
444

    
445
    statisticsIsActiveToggle(apiURL: string, id: string): Observable<boolean> {
446
        const url = apiURL + '/statistics/' + encodeURIComponent(id) + '/toggle';
447
        return this.http.post<boolean>(url, {}, CustomOptions.getAuthOptionsWithBody()).pipe(catchError(this.handleError));
448
    }
449
}
(1-1/4)