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 "../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 {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

    
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) {
34
      if(page_id) {
35
        return this.http.get<Array<DivId>>(helpContentUrl + 'divFull?&page='+page_id)
36
            //.map(res => <Array<DivId>> res.json())
37
            .pipe(catchError(this.handleError));
38
      } else {
39
        return this.http.get<Array<DivId>>(helpContentUrl + 'divFull')
40
            //.map(res => <Array<DivId>> res.json())
41
            .pipe(catchError(this.handleError));
42
      }
43
    }
44

    
45
    updateDivId(divId: DivId, helpContentUrl:string) {
46
        HelpContentService.removeNulls(divId);
47

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

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

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

    
65
    saveDivId(divId: DivId, helpContentUrl:string) {
66
        HelpContentService.removeNulls(divId);
67

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

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

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

    
97
    insertOrUpdateDivHelpContent(divHelpContent: DivHelpContent, helpContentUrl:string) {
98
        HelpContentService.removeNulls(divHelpContent);
99

    
100
        return this.http.post<DivHelpContent>(helpContentUrl + 'divhelpcontent', JSON.stringify(divHelpContent), CustomOptions.getAuthOptionsWithBody())
101
            //.map(res => <DivHelpContent> res.json())
102
            .pipe(catchError(this.handleError));
103
    }
104

    
105
    deleteDivIds(ids : string[], helpContentUrl:string) {
106
        return this.http.post(helpContentUrl + 'div/delete',JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
107
            .pipe(catchError(this.handleError));
108
    }
109

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

    
115
    toggleDivHelpContents(ids : string[],status : boolean, helpContentUrl:string) {
116

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

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

    
129
    getPages(helpContentUrl:string,pid:string) {
130
        return this.http.get<Array<Page>>(helpContentUrl + 'page'+(pid?("?pid="+pid):""))
131
            //.map(res => <Array<Page>> res.json())
132
            .pipe(catchError(this.handleError));
133
    }
134

    
135
    getPagesFull(helpContentUrl:string,pid:string) {
136
        return this.http.get<Array<Page>>(helpContentUrl + 'pageFull'+(pid?("?pid="+pid):""))
137
        //.map(res => <Array<Page>> res.json())
138
          .pipe(catchError(this.handleError));
139
    }
140

    
141
    getPage(pageId:string, helpContentUrl:string) {
142
        return this.http.get<Page>(helpContentUrl + 'page/'+pageId)
143
            //.map(res => <Page> res.json())
144
            .pipe(catchError(this.handleError));
145
    }
146

    
147
    getPageByRoute(route:string, helpContentUrl:string) {
148
      return this.http.get<Page>(helpContentUrl + 'page/?page_route='+route)
149
        .pipe(catchError(this.handleError));
150
    }
151

    
152
    getCommunities( helpContentUrl:string) {
153
        return this.http.get<Array<Community>>(helpContentUrl + 'community')
154
            //.map(res => <Array<Community>> res.json())
155
            .pipe(catchError(this.handleError));
156
    }
157

    
158
    getCommunity(community_pid: string, helpContentUrl:string) {
159
        return this.http.get<Community>(helpContentUrl + 'community/'+community_pid)
160
            //.map(res => <Community> res.json())
161
            .pipe(catchError(this.handleError));
162
    }
163

    
164
    getCommunitiesFull( helpContentUrl:string) {
165
        return this.http.get<Array<Community>>(helpContentUrl + 'communityFull')
166
            //.map(res => <Array<Community>> res.json())
167
            .pipe(catchError(this.handleError));
168
    }
169

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

    
176
    saveCommunity(community: Community, helpContentUrl:string) {
177
        // let headers = new Headers({'Content-Type': 'application/json'});
178
        // let options = new RequestOptions({headers: headers});
179

    
180
        HelpContentService.removeNulls(community);
181

    
182
        return this.http.post<Community>(helpContentUrl + 'community/save', JSON.stringify(community), CustomOptions.getAuthOptionsWithBody())
183
        //.map(res => <Community> res.json())
184
            .pipe(catchError(this.handleError));
185
    }
186

    
187
    updateCommunity(community: Community, helpContentUrl:string) {
188
        // let headers = new Headers({'Content-Type': 'application/json'});
189
        // let options = new RequestOptions({headers: headers});
190

    
191
        HelpContentService.removeNulls(community);
192

    
193
        return this.http.post<Community>(helpContentUrl + 'community/update', JSON.stringify(community),  CustomOptions.getAuthOptionsWithBody())
194
            //.map(res => <Community> res.json())
195
            .pipe(catchError(this.handleError));
196
    }
197

    
198
    deleteCommunities(ids : string[], helpContentUrl:string) {
199
        // let headers = new Headers({'Content-Type': 'application/json'});
200
        // let options = new RequestOptions({headers: headers});
201

    
202
        return this.http.post(helpContentUrl + 'community/delete',JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
203
            .pipe(catchError(this.handleError));
204
    }
205

    
206
    getCommunityPages(community_pid: string, params: string, helpContentUrl:string) {
207
        return this.http.get<Array<Page>>(helpContentUrl + 'community/'+community_pid+'/pages'+params)
208
            //.map(res => <Array<Page>> res.json())
209
            .pipe(catchError(this.handleError));
210
    }
211

    
212
    getEntities(helpContentUrl:string) {
213
        return this.http.get<Array<Entity>>(helpContentUrl + 'entity')
214
            //.map(res => <Array<Entity>> res.json())
215
            .pipe(catchError(this.handleError));
216
    }
217

    
218
    getCommunityEntities(community_pid: string, helpContentUrl:string) {
219
        return this.http.get<Array<Entity>>(helpContentUrl + 'community/'+community_pid+'/entities')
220
            //.map(res => <Array<Entity>> res.json())
221
            .pipe(catchError(this.handleError));
222
    }
223

    
224

    
225
    saveEntity(entity: Entity, helpContentUrl:string) {
226
        HelpContentService.removeNulls(entity);
227

    
228
        return this.http.post<Entity>(helpContentUrl + 'entity/save', JSON.stringify(entity), CustomOptions.getAuthOptionsWithBody())
229
            //.map(res => <Entity> res.json())
230
            .pipe(catchError(this.handleError));
231
    }
232

    
233
    updateEntity(entity: Entity, helpContentUrl:string) {
234
        HelpContentService.removeNulls(entity);
235

    
236
        return this.http.post<Entity>(helpContentUrl + 'entity/update', JSON.stringify(entity), CustomOptions.getAuthOptionsWithBody())
237
            //.map(res => <Entity> res.json())
238
            .pipe(catchError(this.handleError));
239
    }
240

    
241
    // toggleEntity(selectedCommunityId: string, id : string,status : boolean) {
242
    //     let headers = new Headers({'Content-Type': 'application/json'});
243
    //     let options = new RequestOptions({headers: headers});
244
    //
245
    //     return this.http.post(helpContentUrl + 'community/'+selectedCommunityId+'/entity/toggle?status='+ status.toString()+'&entityId='+id.toString(), options)
246
    //         .catch(this.handleError);
247
    // }
248

    
249
    toggleEntities(selectedCommunityPid: string, ids : string[],status : boolean, helpContentUrl:string) {
250

    
251
        return this.http.post(helpContentUrl +'community/'+selectedCommunityPid+ '/entity/toggle?status='+ status.toString(), JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
252
            //.map( res => <string[]> res.json())
253
            .pipe(catchError(this.handleError));
254
    }
255

    
256
    deleteEntities(ids : string[], helpContentUrl:string) {
257
        return this.http.post(helpContentUrl + 'entity/delete',JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
258
            .pipe(catchError(this.handleError));
259
    }
260

    
261

    
262
    toggleEntityOfPage(pageId: string, entityId : string,status : boolean, helpContentUrl:string) {
263
        return this.http.post(helpContentUrl + 'page/'+pageId+'/entity/toggle?status='+ status.toString()+'&entityId='+entityId.toString(), CustomOptions.getAuthOptionsWithBody())
264
            .pipe(catchError(this.handleError));
265
    }
266

    
267
    savePage(page: Page, helpContentUrl:string) {
268
         HelpContentService.removeNulls(page);
269

    
270
        return this.http.post<Page>(helpContentUrl + 'page/save', JSON.stringify(page), CustomOptions.getAuthOptionsWithBody())
271
            //.map(res => <Page> res.json())
272
            .pipe(catchError(this.handleError));
273
    }
274

    
275
    updatePage(page: Page, helpContentUrl:string) {
276

    
277
        HelpContentService.removeNulls(page);
278

    
279
        return this.http.post<Page>(helpContentUrl + 'page/update', JSON.stringify(page), CustomOptions.getAuthOptionsWithBody())
280
            //.map(res => <Page> res.json())
281
            .pipe(catchError(this.handleError));
282
    }
283

    
284
    togglePages(selectedCommunityPid: string, ids : string[],status : boolean, helpContentUrl:string) {
285

    
286
        return this.http.post(helpContentUrl + 'community/'+selectedCommunityPid+'/page/toggle?status='+ status.toString(),JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
287
            .pipe(catchError(this.handleError));
288
    }
289

    
290
    deletePages(ids : string[], helpContentUrl:string) {
291

    
292
        return this.http.post(helpContentUrl + 'page/delete',JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
293
            .pipe(catchError(this.handleError));
294
    }
295

    
296
    getPageHelpContents(helpContentUrl:string) {
297
        return this.http.get<Array<PageHelpContent>>(helpContentUrl + 'pagehelpcontent')
298
            //.map(res => <Array<PageHelpContent>> res.json())
299
            .pipe(catchError(this.handleError));
300
    }
301

    
302
    getCommunityPageHelpContents(community_pid: string, helpContentUrl:string) {
303
        return this.http.get<Array<PageHelpContent>>(helpContentUrl + 'pagehelpcontent?community='+community_pid)
304
            //.map(res => <Array<PageHelpContent>> res.json())
305
            .pipe(catchError(this.handleError));
306
    }
307

    
308
    getPageHelpContent(id : string, helpContentUrl:string) {
309
        return this.http.get<PageHelpContent>(helpContentUrl + 'pagehelpcontent/' + id)
310
            //.map(res => <PageHelpContent> res.json())
311
            .pipe(catchError(this.handleError));
312
    }
313

    
314
    savePageHelpContent(pageHelpContent: PageHelpContent, helpContentUrl:string) {
315
        HelpContentService.removeNulls(pageHelpContent);
316

    
317
        return this.http.post<PageHelpContent>(helpContentUrl + 'pagehelpcontent/save', JSON.stringify(pageHelpContent), CustomOptions.getAuthOptionsWithBody())
318
            //.map(res => <PageHelpContent> res.json())
319
            .pipe(catchError(this.handleError));
320
    }
321

    
322
    updatePageHelpContent(pageHelpContent: PageHelpContent, helpContentUrl:string) {
323
        HelpContentService.removeNulls(pageHelpContent);
324

    
325
        return this.http.post<PageHelpContent>(helpContentUrl + 'pagehelpcontent/update', JSON.stringify(pageHelpContent), CustomOptions.getAuthOptionsWithBody())
326
            //.map(res => <PageHelpContent> res.json())
327
            .pipe(catchError(this.handleError));
328
    }
329

    
330
    deletePageHelpContents(ids : string[], helpContentUrl:string) {
331
          return this.http.post(helpContentUrl + 'pagehelpcontent/delete',JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
332
              .pipe(catchError(this.handleError));
333
    }
334

    
335
    togglePageHelpContents(ids : string[],status : boolean, helpContentUrl:string) {
336
        return this.http.post(helpContentUrl + 'pagehelpcontent/toggle?status='+ status.toString(), JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
337
            //.map( res => <string[]> res.json())
338
            .pipe(catchError(this.handleError));
339
    }
340

    
341
    private handleError(error: HttpErrorResponse) {
342
        // in a real world app, we may send the error to some remote logging infrastructure
343
        // instead of just logging it to the console
344
        console.error(error);
345
        return Observable.throw(error.error || 'Server error');
346
    }
347

    
348
    // getDataProviders() {
349
    //     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);
350
    // }
351

    
352
    getCommunityStatistics(apiUrl: string, communityId: string): Observable<StatisticsSummary> {
353
        const url = `${apiUrl}communities/${communityId}`;
354
        //console.log(`getting statistics summary from: ${url}`);
355
        return this.http.get(url)
356
            //.map(res => <any>res.json())
357
            .pipe(map(res => res['statistics']));
358
    }
359

    
360
    getCommunityAdminStatisticsChoices(apiUrl: string, communityId: string): Observable<StatisticsDisplay> {
361
        const url = `${apiUrl}statistics/${communityId}`;
362
        //console.log(`getting admin choices for statistics from: ${url}`);
363
        return this.http.get<StatisticsDisplay>(url)
364
            //.map(stats => <StatisticsDisplay>stats.json())
365
            .pipe(catchError(this.handleError));
366
    }
367

    
368
    postCommunityAdminStatisticsChoices(apiUrl: string,
369
                                        communityId: string,
370
                                        entity: string,
371
                                        chartsOrNumbers: string,
372
                                        title: string,
373
                                        status: boolean,
374
                                        monitor: boolean): Observable<any> {
375
        const url = `${apiUrl}statistics/${communityId}/${entity}/${chartsOrNumbers}?status=${status.toString()}&monitor=${monitor.toString()}`;
376
        //console.log(`getting admin choices for statistics from: ${url}`);
377

    
378
        return this.http.post(url, title, CustomOptions.getAuthOptionsWithBody())
379
            //.map(stats => <any>stats.json())
380
            .pipe(catchError(this.handleError));
381
    }
382
}
(1-1/4)