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 { Community } from "../utils/entities/adminTool/community";
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

    
17

    
18
@Injectable()
19
export class HelpContentService {
20

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

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

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

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

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

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

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

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

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

    
72
        return this.http.post<DivId>(helpContentUrl + '/div/save', JSON.stringify(divId), CustomOptions.getAuthOptionsWithBody())
73
            //.map(res => <DivId> res.json())
74
            .pipe(catchError(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<Array<Page>>(helpContentUrl + '/community/'+community_pid+'/pages?div=true')
85
            //.map(res => <Array<Page>> res.json())
86
            .pipe(catchError(this.handleError));
87
    }
88

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

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

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

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

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

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

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

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

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

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

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

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

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

    
166
    getCommunities( helpContentUrl:string) {
167
        return this.http.get<Array<Community>>(helpContentUrl + '/community')
168
            //.map(res => <Array<Community>> res.json())
169
            .pipe(catchError(this.handleError));
170
    }
171

    
172
    getCommunity(community_pid: string, helpContentUrl:string) {
173
        return this.http.get<Community>(helpContentUrl + '/community/'+community_pid)
174
            //.map(res => <Community> res.json())
175
            .pipe(catchError(this.handleError));
176
    }
177

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

    
184
    getCommunityFull(community_pid: string, helpContentUrl:string) {
185
        return this.http.get<Community>(helpContentUrl + '/communityFull/'+community_pid)
186
            //.map(res => <Community> res.json())
187
            .pipe(catchError(this.handleError));
188
    }
189

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

    
194
        HelpContentService.removeNulls(community);
195

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

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

    
205
        HelpContentService.removeNulls(community);
206

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

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

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

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

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

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

    
238

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

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

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

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

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

    
263
    toggleEntities(selectedCommunityPid: string, ids : string[],status : boolean, helpContentUrl:string) {
264

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

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

    
275

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

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

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

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

    
291
        HelpContentService.removeNulls(page);
292

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

    
298
    togglePages(selectedCommunityPid: string, ids : string[],status : boolean, helpContentUrl:string) {
299

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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