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
    getPage(pageId:string, helpContentUrl:string) {
136
        return this.http.get<Page>(helpContentUrl + 'page/'+pageId)
137
            //.map(res => <Page> res.json())
138
            .pipe(catchError(this.handleError));
139
    }
140

    
141
    getCommunities( helpContentUrl:string) {
142
        return this.http.get<Array<Community>>(helpContentUrl + 'community')
143
            //.map(res => <Array<Community>> res.json())
144
            .pipe(catchError(this.handleError));
145
    }
146

    
147
    getCommunity(community_pid: string, helpContentUrl:string) {
148
        return this.http.get<Community>(helpContentUrl + 'community/'+community_pid)
149
            //.map(res => <Community> res.json())
150
            .pipe(catchError(this.handleError));
151
    }
152

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

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

    
165
    saveCommunity(community: Community, helpContentUrl:string) {
166
        // let headers = new Headers({'Content-Type': 'application/json'});
167
        // let options = new RequestOptions({headers: headers});
168

    
169
        HelpContentService.removeNulls(community);
170

    
171
        return this.http.post<Community>(helpContentUrl + 'community/save', JSON.stringify(community), CustomOptions.getAuthOptionsWithBody())
172
        //.map(res => <Community> res.json())
173
            .pipe(catchError(this.handleError));
174
    }
175

    
176
    updateCommunity(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/update', JSON.stringify(community),  CustomOptions.getAuthOptionsWithBody())
183
            //.map(res => <Community> res.json())
184
            .pipe(catchError(this.handleError));
185
    }
186

    
187
    deleteCommunities(ids : string[], helpContentUrl:string) {
188
        // let headers = new Headers({'Content-Type': 'application/json'});
189
        // let options = new RequestOptions({headers: headers});
190

    
191
        return this.http.post(helpContentUrl + 'community/delete',JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
192
            .pipe(catchError(this.handleError));
193
    }
194

    
195
    getCommunityPages(community_pid: string, params: string, helpContentUrl:string) {
196
        return this.http.get<Array<Page>>(helpContentUrl + 'community/'+community_pid+'/pages'+params)
197
            //.map(res => <Array<Page>> res.json())
198
            .pipe(catchError(this.handleError));
199
    }
200

    
201
    getEntities(helpContentUrl:string) {
202
        return this.http.get<Array<Entity>>(helpContentUrl + 'entity')
203
            //.map(res => <Array<Entity>> res.json())
204
            .pipe(catchError(this.handleError));
205
    }
206

    
207
    getCommunityEntities(community_pid: string, helpContentUrl:string) {
208
        return this.http.get<Array<Entity>>(helpContentUrl + 'community/'+community_pid+'/entities')
209
            //.map(res => <Array<Entity>> res.json())
210
            .pipe(catchError(this.handleError));
211
    }
212

    
213

    
214
    saveEntity(entity: Entity, helpContentUrl:string) {
215
        HelpContentService.removeNulls(entity);
216

    
217
        return this.http.post<Entity>(helpContentUrl + 'entity/save', JSON.stringify(entity), CustomOptions.getAuthOptionsWithBody())
218
            //.map(res => <Entity> res.json())
219
            .pipe(catchError(this.handleError));
220
    }
221

    
222
    updateEntity(entity: Entity, helpContentUrl:string) {
223
        HelpContentService.removeNulls(entity);
224

    
225
        return this.http.post<Entity>(helpContentUrl + 'entity/update', JSON.stringify(entity), CustomOptions.getAuthOptionsWithBody())
226
            //.map(res => <Entity> res.json())
227
            .pipe(catchError(this.handleError));
228
    }
229

    
230
    // toggleEntity(selectedCommunityId: string, id : string,status : boolean) {
231
    //     let headers = new Headers({'Content-Type': 'application/json'});
232
    //     let options = new RequestOptions({headers: headers});
233
    //
234
    //     return this.http.post(helpContentUrl + 'community/'+selectedCommunityId+'/entity/toggle?status='+ status.toString()+'&entityId='+id.toString(), options)
235
    //         .catch(this.handleError);
236
    // }
237

    
238
    toggleEntities(selectedCommunityPid: string, ids : string[],status : boolean, helpContentUrl:string) {
239

    
240
        return this.http.post(helpContentUrl +'community/'+selectedCommunityPid+ '/entity/toggle?status='+ status.toString(), JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
241
            //.map( res => <string[]> res.json())
242
            .pipe(catchError(this.handleError));
243
    }
244

    
245
    deleteEntities(ids : string[], helpContentUrl:string) {
246
        return this.http.post(helpContentUrl + 'entity/delete',JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
247
            .pipe(catchError(this.handleError));
248
    }
249

    
250

    
251
    toggleEntityOfPage(pageId: string, entityId : string,status : boolean, helpContentUrl:string) {
252
        return this.http.post(helpContentUrl + 'page/'+pageId+'/entity/toggle?status='+ status.toString()+'&entityId='+entityId.toString(), CustomOptions.getAuthOptionsWithBody())
253
            .pipe(catchError(this.handleError));
254
    }
255

    
256
    savePage(page: Page, helpContentUrl:string) {
257
         HelpContentService.removeNulls(page);
258

    
259
        return this.http.post<Page>(helpContentUrl + 'page/save', JSON.stringify(page), CustomOptions.getAuthOptionsWithBody())
260
            //.map(res => <Page> res.json())
261
            .pipe(catchError(this.handleError));
262
    }
263

    
264
    updatePage(page: Page, helpContentUrl:string) {
265

    
266
        HelpContentService.removeNulls(page);
267

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

    
273
    togglePages(selectedCommunityPid: string, ids : string[],status : boolean, helpContentUrl:string) {
274

    
275
        return this.http.post(helpContentUrl + 'community/'+selectedCommunityPid+'/page/toggle?status='+ status.toString(),JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
276
            .pipe(catchError(this.handleError));
277
    }
278

    
279
    deletePages(ids : string[], helpContentUrl:string) {
280

    
281
        return this.http.post(helpContentUrl + 'page/delete',JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
282
            .pipe(catchError(this.handleError));
283
    }
284

    
285
    getPageHelpContents(helpContentUrl:string) {
286
        return this.http.get<Array<PageHelpContent>>(helpContentUrl + 'pagehelpcontent')
287
            //.map(res => <Array<PageHelpContent>> res.json())
288
            .pipe(catchError(this.handleError));
289
    }
290

    
291
    getCommunityPageHelpContents(community_pid: string, helpContentUrl:string) {
292
        return this.http.get<Array<PageHelpContent>>(helpContentUrl + 'pagehelpcontent?community='+community_pid)
293
            //.map(res => <Array<PageHelpContent>> res.json())
294
            .pipe(catchError(this.handleError));
295
    }
296

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

    
303
    savePageHelpContent(pageHelpContent: PageHelpContent, helpContentUrl:string) {
304
        HelpContentService.removeNulls(pageHelpContent);
305

    
306
        return this.http.post<PageHelpContent>(helpContentUrl + 'pagehelpcontent/save', JSON.stringify(pageHelpContent), CustomOptions.getAuthOptionsWithBody())
307
            //.map(res => <PageHelpContent> res.json())
308
            .pipe(catchError(this.handleError));
309
    }
310

    
311
    updatePageHelpContent(pageHelpContent: PageHelpContent, helpContentUrl:string) {
312
        HelpContentService.removeNulls(pageHelpContent);
313

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

    
319
    deletePageHelpContents(ids : string[], helpContentUrl:string) {
320
          return this.http.post(helpContentUrl + 'pagehelpcontent/delete',JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
321
              .pipe(catchError(this.handleError));
322
    }
323

    
324
    togglePageHelpContents(ids : string[],status : boolean, helpContentUrl:string) {
325
        return this.http.post(helpContentUrl + 'pagehelpcontent/toggle?status='+ status.toString(), JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
326
            //.map( res => <string[]> res.json())
327
            .pipe(catchError(this.handleError));
328
    }
329

    
330
    private handleError(error: HttpErrorResponse) {
331
        // in a real world app, we may send the error to some remote logging infrastructure
332
        // instead of just logging it to the console
333
        console.error(error);
334
        return Observable.throw(error.error || 'Server error');
335
    }
336

    
337
    // getDataProviders() {
338
    //     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);
339
    // }
340

    
341
    getCommunityStatistics(apiUrl: string, communityId: string): Observable<StatisticsSummary> {
342
        const url = `${apiUrl}communities/${communityId}`;
343
        //console.log(`getting statistics summary from: ${url}`);
344
        return this.http.get(url)
345
            //.map(res => <any>res.json())
346
            .pipe(map(res => res['statistics']));
347
    }
348

    
349
    getCommunityAdminStatisticsChoices(apiUrl: string, communityId: string): Observable<StatisticsDisplay> {
350
        const url = `${apiUrl}statistics/${communityId}`;
351
        //console.log(`getting admin choices for statistics from: ${url}`);
352
        return this.http.get<StatisticsDisplay>(url)
353
            //.map(stats => <StatisticsDisplay>stats.json())
354
            .pipe(catchError(this.handleError));
355
    }
356

    
357
    postCommunityAdminStatisticsChoices(apiUrl: string,
358
                                        communityId: string,
359
                                        entity: string,
360
                                        chartsOrNumbers: string,
361
                                        title: string,
362
                                        status: boolean,
363
                                        monitor: boolean): Observable<any> {
364
        const url = `${apiUrl}statistics/${communityId}/${entity}/${chartsOrNumbers}?status=${status.toString()}&monitor=${monitor.toString()}`;
365
        //console.log(`getting admin choices for statistics from: ${url}`);
366

    
367
        return this.http.post(url, title, CustomOptions.getAuthOptionsWithBody())
368
            //.map(stats => <any>stats.json())
369
            .pipe(catchError(this.handleError));
370
    }
371
}
(1-1/4)