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 } from 'rxjs';
7
import { Page } from "../utils/entities/adminTool/page";
8
import { PageHelpContent } from "../utils/entities/adminTool/page-help-content";
9
import { Portal } from "../utils/entities/adminTool/portal";
10
import { Entity } from "../utils/entities/adminTool/entity";
11
import { DivId } from "../utils/entities/adminTool/divId";
12
import { DivHelpContent } from "../utils/entities/adminTool/div-help-content";
13
import {StatisticsDisplay, StatisticsSummary} from '../connect/statistics/statisticsEntities';
14
import { CustomOptions } from './servicesUtils/customOptions.class';
15
import {catchError, map} from "rxjs/operators";
16
import {properties} from "../../../environments/environment";
17

    
18

    
19
@Injectable()
20
export class HelpContentService {
21

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

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

    
33
  getDivIdsFull(page_id: string, helpContentUrl:string, pid: string = null) {
34
    let parameters: string = "";
35
    if(page_id || pid) {
36
      parameters = "?";
37
      if(page_id) {
38
        parameters += "&page="+page_id;
39
      }
40
      if(pid) {
41
        parameters += "&portal="+pid;
42
      }
43
    }
44

    
45
    return this.http.get<Array<DivId>>(helpContentUrl + 'divFull'+parameters)
46
    //.map(res => <Array<DivId>> res.json())
47
      .pipe(catchError(this.handleError));
48
  }
49

    
50
  updateDivId(divId: DivId, helpContentUrl:string) {
51
    HelpContentService.removeNulls(divId);
52

    
53
    return this.http.post<DivId>(helpContentUrl + 'div/update', JSON.stringify(divId), CustomOptions.getAuthOptionsWithBody())
54
    //.map(res => <DivId> res.json())
55
      .pipe(catchError(this.handleError));
56
  }
57

    
58
  getDivId(divId: string, helpContentUrl:string) {
59
    return this.http.get<DivId>(helpContentUrl + 'div/'+divId)
60
    //.map(res => <DivId> res.json())
61
      .pipe(catchError(this.handleError));
62
  }
63

    
64
  getDivIdFull(divId: string, helpContentUrl:string) {
65
    return this.http.get<DivId>(helpContentUrl + 'divFull/'+divId)
66
    //.map(res => <DivId> res.json())
67
      .pipe(catchError(this.handleError));
68
  }
69

    
70
  saveDivId(divId: DivId, helpContentUrl:string) {
71
    HelpContentService.removeNulls(divId);
72

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

    
90
  getCommunityDivHelpContents(portal_pid: string, helpContentUrl:string) {
91
    return this.http.get<Array<DivHelpContent>>(helpContentUrl + 'divhelpcontent?portal='+portal_pid)
92
    //.map(res => <Array<DivHelpContent>> res.json())
93
      .pipe(catchError(this.handleError));
94
  }
95

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

    
102
  insertOrUpdateDivHelpContent(divHelpContent: DivHelpContent, helpContentUrl:string) {
103
    HelpContentService.removeNulls(divHelpContent);
104

    
105
    return this.http.post<DivHelpContent>(helpContentUrl + 'divhelpcontent', JSON.stringify(divHelpContent), CustomOptions.getAuthOptionsWithBody())
106
    //.map(res => <DivHelpContent> res.json())
107
      .pipe(catchError(this.handleError));
108
  }
109

    
110
  deleteDivIds(ids : string[], helpContentUrl:string) {
111
    return this.http.post(helpContentUrl + 'div/delete',JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
112
      .pipe(catchError(this.handleError));
113
  }
114

    
115
  deleteDivHelpContents(ids : string[], helpContentUrl:string) {
116
    return this.http.post(helpContentUrl + 'divhelpcontent/delete',JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
117
      .pipe(catchError(this.handleError));
118
  }
119

    
120
  toggleDivHelpContents(ids : string[],status : boolean, helpContentUrl:string) {
121

    
122
    return this.http.post(helpContentUrl + 'divhelpcontent/toggle?status='+ status.toString(), JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
123
    //.map( res => <string[]> res.json())
124
      .pipe(catchError(this.handleError));
125
  }
126

    
127
  getPagesWithDivIds(portal_pid: string, helpContentUrl:string) {
128
    let parameters = (portal_pid ? "?portal="+portal_pid : "");
129
    return this.http.get<Array<string>>(helpContentUrl + 'div/pages'+parameters)
130
    //.map(res => <Map<string, Set<string>>> res.json())
131
      .pipe(catchError(this.handleError));
132
  }
133

    
134
  getPages(helpContentUrl:string,pid:string,with_positions:boolean=null) {
135
    let parameters: string = "";
136
    if(pid || with_positions == true || with_positions == false) {
137
      parameters = "?";
138
      if(pid) {
139
        parameters += "&pid="+pid;
140
      }
141
      if(with_positions == true || with_positions == false) {
142
        parameters += "&with_positions="+with_positions;
143
      }
144
    }
145
    return this.http.get<Array<Page>>(helpContentUrl + 'page'+parameters)
146
    //.map(res => <Array<Page>> res.json())
147
      .pipe(catchError(this.handleError));
148
  }
149

    
150
  getPagesFull(helpContentUrl:string,pid:string) {
151
    return this.http.get<Array<Page>>(helpContentUrl + 'pageFull'+(pid?("?pid="+pid):""))
152
    //.map(res => <Array<Page>> res.json())
153
      .pipe(catchError(this.handleError));
154
  }
155

    
156
  getPage(pageId:string, helpContentUrl:string) {
157
    return this.http.get<Page>(helpContentUrl + 'page/'+pageId)
158
    //.map(res => <Page> res.json())
159
      .pipe(catchError(this.handleError));
160
  }
161

    
162
  getPageByRoute(route:string, helpContentUrl:string) {
163
    return this.http.get<Page>(helpContentUrl + 'page/?page_route='+route)
164
      .pipe(catchError(this.handleError));
165
  }
166

    
167
  getCommunities( helpContentUrl:string) {
168
    return this.http.get<Array<Portal>>(helpContentUrl + properties.adminToolsPortalType)
169
    //.map(res => <Array<Portal>> res.json())
170
      .pipe(catchError(this.handleError));
171
  }
172

    
173
  getCommunity(portal_pid: string, helpContentUrl:string) {
174
    return this.http.get<Portal>(helpContentUrl + properties.adminToolsPortalType + '/'+portal_pid)
175
    //.map(res => <Portal> res.json())
176
      .pipe(catchError(this.handleError));
177
  }
178

    
179
  getCommunitiesFull( helpContentUrl:string) {
180
    return this.http.get<Array<Portal>>(helpContentUrl + properties.adminToolsPortalType + 'Full')
181
    //.map(res => <Array<Portal>> res.json())
182
      .pipe(catchError(this.handleError));
183
  }
184

    
185
  getCommunityFull(portal_pid: string, helpContentUrl:string) {
186
    return this.http.get<Portal>(helpContentUrl + properties.adminToolsPortalType + 'Full/'+portal_pid)
187
    //.map(res => <Portal> res.json())
188
      .pipe(catchError(this.handleError));
189
  }
190

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

    
195
    HelpContentService.removeNulls(portal);
196

    
197
    return this.http.post<Portal>(helpContentUrl + portal.type + '/save', JSON.stringify(portal), CustomOptions.getAuthOptionsWithBody())
198
    //.map(res => <Portal> res.json())
199
      .pipe(catchError(this.handleError));
200
  }
201

    
202
  updateCommunity(portal: Portal, helpContentUrl:string) {
203
    // let headers = new Headers({'Content-Type': 'application/json'});
204
    // let options = new RequestOptions({headers: headers});
205

    
206
    HelpContentService.removeNulls(portal);
207

    
208
    return this.http.post<Portal>(helpContentUrl + portal.type + '/update', JSON.stringify(portal),  CustomOptions.getAuthOptionsWithBody())
209
    //.map(res => <Portal> res.json())
210
      .pipe(catchError(this.handleError));
211
  }
212

    
213
  deleteCommunities(ids : string[], helpContentUrl:string) {
214
    // let headers = new Headers({'Content-Type': 'application/json'});
215
    // let options = new RequestOptions({headers: headers});
216

    
217
    return this.http.post(helpContentUrl + properties.adminToolsPortalType + '/delete',JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
218
      .pipe(catchError(this.handleError));
219
  }
220

    
221
  getCommunityPages(portal_pid: string, params: string, helpContentUrl:string) {
222
    return this.http.get<Array<Page>>(helpContentUrl + properties.adminToolsPortalType + '/'+portal_pid+'/pages'+params)
223
    //.map(res => <Array<Page>> res.json())
224
      .pipe(catchError(this.handleError));
225
  }
226

    
227
  getEntities(helpContentUrl:string) {
228
    return this.http.get<Array<Entity>>(helpContentUrl + 'entity')
229
    //.map(res => <Array<Entity>> res.json())
230
      .pipe(catchError(this.handleError));
231
  }
232

    
233
  getCommunityEntities(portal_pid: string, helpContentUrl:string) {
234
    return this.http.get<Array<Entity>>(helpContentUrl + properties.adminToolsPortalType + '/'+portal_pid+'/entities')
235
    //.map(res => <Array<Entity>> res.json())
236
      .pipe(catchError(this.handleError));
237
  }
238

    
239

    
240
  saveEntity(entity: Entity, helpContentUrl:string) {
241
    HelpContentService.removeNulls(entity);
242

    
243
    return this.http.post<Entity>(helpContentUrl + 'entity/save', JSON.stringify(entity), CustomOptions.getAuthOptionsWithBody())
244
    //.map(res => <Entity> res.json())
245
      .pipe(catchError(this.handleError));
246
  }
247

    
248
  updateEntity(entity: Entity, helpContentUrl:string) {
249
    HelpContentService.removeNulls(entity);
250

    
251
    return this.http.post<Entity>(helpContentUrl + 'entity/update', JSON.stringify(entity), CustomOptions.getAuthOptionsWithBody())
252
    //.map(res => <Entity> res.json())
253
      .pipe(catchError(this.handleError));
254
  }
255

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

    
264
  toggleEntities(selectedPortalPid: string, ids : string[],status : boolean, helpContentUrl:string) {
265

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

    
271
  deleteEntities(ids : string[], helpContentUrl:string) {
272
    return this.http.post(helpContentUrl + 'entity/delete',JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
273
      .pipe(catchError(this.handleError));
274
  }
275

    
276

    
277
  toggleEntityOfPage(pageId: string, entityId : string,status : boolean, helpContentUrl:string) {
278
    return this.http.post(helpContentUrl + 'page/'+pageId+'/entity/toggle?status='+ status.toString()+'&entityId='+entityId.toString(), CustomOptions.getAuthOptionsWithBody())
279
      .pipe(catchError(this.handleError));
280
  }
281

    
282
  savePage(page: Page, helpContentUrl:string) {
283
    HelpContentService.removeNulls(page);
284

    
285
    return this.http.post<Page>(helpContentUrl + 'page/save', JSON.stringify(page), CustomOptions.getAuthOptionsWithBody())
286
    //.map(res => <Page> res.json())
287
      .pipe(catchError(this.handleError));
288
  }
289

    
290
  updatePage(page: Page, helpContentUrl:string) {
291

    
292
    HelpContentService.removeNulls(page);
293

    
294
    return this.http.post<Page>(helpContentUrl + 'page/update', JSON.stringify(page), CustomOptions.getAuthOptionsWithBody())
295
    //.map(res => <Page> res.json())
296
      .pipe(catchError(this.handleError));
297
  }
298

    
299
  togglePages(selectedPortalPid: string, ids : string[],status : boolean, helpContentUrl:string) {
300

    
301
    return this.http.post(helpContentUrl + properties.adminToolsPortalType + '/'+selectedPortalPid+'/page/toggle?status='+ status.toString(),JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
302
      .pipe(catchError(this.handleError));
303
  }
304

    
305
  deletePages(ids : string[], helpContentUrl:string) {
306

    
307
    return this.http.post(helpContentUrl + 'page/delete',JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
308
      .pipe(catchError(this.handleError));
309
  }
310

    
311
  getPageHelpContents(helpContentUrl:string) {
312
    return this.http.get<Array<PageHelpContent>>(helpContentUrl + 'pagehelpcontent')
313
    //.map(res => <Array<PageHelpContent>> res.json())
314
      .pipe(catchError(this.handleError));
315
  }
316

    
317
  getCommunityPageHelpContents(portal_pid: string, helpContentUrl:string) {
318
    return this.http.get<Array<PageHelpContent>>(helpContentUrl + 'pagehelpcontent?portal='+portal_pid)
319
    //.map(res => <Array<PageHelpContent>> res.json())
320
      .pipe(catchError(this.handleError));
321
  }
322

    
323
  getPageHelpContent(id : string, helpContentUrl:string) {
324
    return this.http.get<PageHelpContent>(helpContentUrl + 'pagehelpcontent/' + id)
325
    //.map(res => <PageHelpContent> res.json())
326
      .pipe(catchError(this.handleError));
327
  }
328

    
329
  savePageHelpContent(pageHelpContent: PageHelpContent, helpContentUrl:string) {
330
    HelpContentService.removeNulls(pageHelpContent);
331

    
332
    return this.http.post<PageHelpContent>(helpContentUrl + 'pagehelpcontent/save', JSON.stringify(pageHelpContent), CustomOptions.getAuthOptionsWithBody())
333
    //.map(res => <PageHelpContent> res.json())
334
      .pipe(catchError(this.handleError));
335
  }
336

    
337
  updatePageHelpContent(pageHelpContent: PageHelpContent, helpContentUrl:string) {
338
    HelpContentService.removeNulls(pageHelpContent);
339

    
340
    return this.http.post<PageHelpContent>(helpContentUrl + 'pagehelpcontent/update', JSON.stringify(pageHelpContent), CustomOptions.getAuthOptionsWithBody())
341
    //.map(res => <PageHelpContent> res.json())
342
      .pipe(catchError(this.handleError));
343
  }
344

    
345
  deletePageHelpContents(ids : string[], helpContentUrl:string) {
346
    return this.http.post(helpContentUrl + 'pagehelpcontent/delete',JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
347
      .pipe(catchError(this.handleError));
348
  }
349

    
350
  togglePageHelpContents(ids : string[],status : boolean, helpContentUrl:string) {
351
    return this.http.post(helpContentUrl + 'pagehelpcontent/toggle?status='+ status.toString(), JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
352
    //.map( res => <string[]> res.json())
353
      .pipe(catchError(this.handleError));
354
  }
355

    
356
  private handleError(error: HttpErrorResponse) {
357
    // in a real world app, we may send the error to some remote logging infrastructure
358
    // instead of just logging it to the console
359
    console.error(error);
360
    return Observable.throw(error.error || 'Server error');
361
  }
362

    
363
  // getDataProviders() {
364
  //     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);
365
  // }
366

    
367
  getCommunityStatistics(apiUrl: string, portalId: string): Observable<StatisticsSummary> {
368
    const url = `${apiUrl}communities/${portalId}`;
369
    //console.log(`getting statistics summary from: ${url}`);
370
    return this.http.get(url)
371
    //.map(res => <any>res.json())
372
      .pipe(map(res => res['statistics']));
373
  }
374

    
375
  getCommunityAdminStatisticsChoices(apiUrl: string, portalId: string): Observable<StatisticsDisplay> {
376
    const url = `${apiUrl}statistics/${portalId}`;
377
    //console.log(`getting admin choices for statistics from: ${url}`);
378
    return this.http.get<StatisticsDisplay>(url)
379
    //.map(stats => <StatisticsDisplay>stats.json())
380
      .pipe(catchError(this.handleError));
381
  }
382

    
383
  postCommunityAdminStatisticsChoices(apiUrl: string,
384
                                      portalId: string,
385
                                      entity: string,
386
                                      chartsOrNumbers: string,
387
                                      title: string,
388
                                      status: boolean,
389
                                      monitor: boolean): Observable<any> {
390
    const url = `${apiUrl}statistics/${portalId}/${entity}/${chartsOrNumbers}?status=${status.toString()}&monitor=${monitor.toString()}`;
391
    //console.log(`getting admin choices for statistics from: ${url}`);
392

    
393
    return this.http.post(url, title, CustomOptions.getAuthOptionsWithBody())
394
    //.map(stats => <any>stats.json())
395
      .pipe(catchError(this.handleError));
396
  }
397

    
398
  statisticsIsActiveToggle(apiURL: string, id: string): Observable<boolean> {
399
    const url = apiURL + '/statistics/' + encodeURIComponent(id) + '/toggle';
400
    return this.http.post<boolean>(url, {}, CustomOptions.getAuthOptionsWithBody()).pipe(catchError(this.handleError));
401
  }
402
}
(4-4/23)