Project

General

Profile

1 53600 argiro.kok
/**
2
 * Created by stefania on 7/13/17.
3
 */
4
import { Injectable } from '@angular/core';
5 55972 argiro.kok
import {HttpClient, HttpErrorResponse, HttpHeaders} from "@angular/common/http";
6
import { Observable } from 'rxjs';
7 57916 argiro.kok
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 55972 argiro.kok
import {catchError, map} from "rxjs/operators";
16 53600 argiro.kok
17
18
@Injectable()
19
export class HelpContentService {
20
21 55972 argiro.kok
    constructor(private http:HttpClient) {
22 53600 argiro.kok
    }
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 56740 konstantin
    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 53600 argiro.kok
      }
43 56740 konstantin
44 57916 argiro.kok
      return this.http.get<Array<DivId>>(helpContentUrl + '/divFull'+parameters)
45 56740 konstantin
          //.map(res => <Array<DivId>> res.json())
46
          .pipe(catchError(this.handleError));
47 53600 argiro.kok
    }
48
49
    updateDivId(divId: DivId, helpContentUrl:string) {
50
        HelpContentService.removeNulls(divId);
51
52 57916 argiro.kok
        return this.http.post<DivId>(helpContentUrl + '/div/update', JSON.stringify(divId), CustomOptions.getAuthOptionsWithBody())
53 55972 argiro.kok
            //.map(res => <DivId> res.json())
54
            .pipe(catchError(this.handleError));
55 53600 argiro.kok
    }
56
57
    getDivId(divId: string, helpContentUrl:string) {
58 57916 argiro.kok
        return this.http.get<DivId>(helpContentUrl + '/div/'+divId)
59 55972 argiro.kok
            //.map(res => <DivId> res.json())
60
            .pipe(catchError(this.handleError));
61 53600 argiro.kok
    }
62
63
    getDivIdFull(divId: string, helpContentUrl:string) {
64 57916 argiro.kok
        return this.http.get<DivId>(helpContentUrl + '/divFull/'+divId)
65 55972 argiro.kok
            //.map(res => <DivId> res.json())
66
            .pipe(catchError(this.handleError));
67 53600 argiro.kok
    }
68
69
    saveDivId(divId: DivId, helpContentUrl:string) {
70
        HelpContentService.removeNulls(divId);
71
72 57916 argiro.kok
        return this.http.post<DivId>(helpContentUrl + '/div/save', JSON.stringify(divId), CustomOptions.getAuthOptionsWithBody())
73 55972 argiro.kok
            //.map(res => <DivId> res.json())
74
            .pipe(catchError(this.handleError));
75 53600 argiro.kok
    }
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 57916 argiro.kok
        return this.http.get<Array<Page>>(helpContentUrl + '/community/'+community_pid+'/pages?div=true')
85 55972 argiro.kok
            //.map(res => <Array<Page>> res.json())
86
            .pipe(catchError(this.handleError));
87 53600 argiro.kok
    }
88
89
    getCommunityDivHelpContents(community_pid: string, helpContentUrl:string) {
90 57916 argiro.kok
      return this.http.get<Array<DivHelpContent>>(helpContentUrl + '/divhelpcontent?community='+community_pid)
91 55972 argiro.kok
          //.map(res => <Array<DivHelpContent>> res.json())
92
          .pipe(catchError(this.handleError));
93 53600 argiro.kok
    }
94
95
    getDivHelpContent(id : string, helpContentUrl:string) {
96 57916 argiro.kok
      return this.http.get<DivHelpContent>(helpContentUrl + '/divhelpcontent/' + id)
97 55972 argiro.kok
          //.map(res => <DivHelpContent> res.json())
98
          .pipe(catchError(this.handleError));
99 53600 argiro.kok
    }
100
101
    insertOrUpdateDivHelpContent(divHelpContent: DivHelpContent, helpContentUrl:string) {
102
        HelpContentService.removeNulls(divHelpContent);
103
104 57916 argiro.kok
        return this.http.post<DivHelpContent>(helpContentUrl + '/divhelpcontent', JSON.stringify(divHelpContent), CustomOptions.getAuthOptionsWithBody())
105 55972 argiro.kok
            //.map(res => <DivHelpContent> res.json())
106
            .pipe(catchError(this.handleError));
107 53600 argiro.kok
    }
108
109
    deleteDivIds(ids : string[], helpContentUrl:string) {
110 57916 argiro.kok
        return this.http.post(helpContentUrl + '/div/delete',JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
111 55972 argiro.kok
            .pipe(catchError(this.handleError));
112 53600 argiro.kok
    }
113
114
    deleteDivHelpContents(ids : string[], helpContentUrl:string) {
115 57916 argiro.kok
         return this.http.post(helpContentUrl + '/divhelpcontent/delete',JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
116 55972 argiro.kok
            .pipe(catchError(this.handleError));
117 53600 argiro.kok
    }
118
119
    toggleDivHelpContents(ids : string[],status : boolean, helpContentUrl:string) {
120
121 57916 argiro.kok
        return this.http.post(helpContentUrl + '/divhelpcontent/toggle?status='+ status.toString(), JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
122 53600 argiro.kok
            //.map( res => <string[]> res.json())
123 55972 argiro.kok
            .pipe(catchError(this.handleError));
124 53600 argiro.kok
    }
125
126
    getPagesWithDivIds(community_pid: string, helpContentUrl:string) {
127 56294 konstantin
        let parameters = (community_pid ? "?communityId="+community_pid : "");
128 57916 argiro.kok
        return this.http.get<Array<string>>(helpContentUrl + '/div/pages'+parameters)
129 55972 argiro.kok
            //.map(res => <Map<string, Set<string>>> res.json())
130
            .pipe(catchError(this.handleError));
131 53600 argiro.kok
    }
132
133 56740 konstantin
    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 57916 argiro.kok
        return this.http.get<Array<Page>>(helpContentUrl + '/page'+parameters)
145 55972 argiro.kok
            //.map(res => <Array<Page>> res.json())
146
            .pipe(catchError(this.handleError));
147 53600 argiro.kok
    }
148
149 56670 k.triantaf
    getPagesFull(helpContentUrl:string,pid:string) {
150 57916 argiro.kok
        return this.http.get<Array<Page>>(helpContentUrl + '/pageFull'+(pid?("?pid="+pid):""))
151 56670 k.triantaf
        //.map(res => <Array<Page>> res.json())
152
          .pipe(catchError(this.handleError));
153
    }
154
155 53600 argiro.kok
    getPage(pageId:string, helpContentUrl:string) {
156 57916 argiro.kok
        return this.http.get<Page>(helpContentUrl + '/page/'+pageId)
157 55972 argiro.kok
            //.map(res => <Page> res.json())
158
            .pipe(catchError(this.handleError));
159 53600 argiro.kok
    }
160
161 56403 konstantin
    getPageByRoute(route:string, helpContentUrl:string) {
162 57916 argiro.kok
      return this.http.get<Page>(helpContentUrl + '/page/?page_route='+route)
163 56403 konstantin
        .pipe(catchError(this.handleError));
164
    }
165
166 53600 argiro.kok
    getCommunities( helpContentUrl:string) {
167 57916 argiro.kok
        return this.http.get<Array<Community>>(helpContentUrl + '/community')
168 55972 argiro.kok
            //.map(res => <Array<Community>> res.json())
169
            .pipe(catchError(this.handleError));
170 53600 argiro.kok
    }
171
172
    getCommunity(community_pid: string, helpContentUrl:string) {
173 57916 argiro.kok
        return this.http.get<Community>(helpContentUrl + '/community/'+community_pid)
174 55972 argiro.kok
            //.map(res => <Community> res.json())
175
            .pipe(catchError(this.handleError));
176 53600 argiro.kok
    }
177
178
    getCommunitiesFull( helpContentUrl:string) {
179 57916 argiro.kok
        return this.http.get<Array<Community>>(helpContentUrl + '/communityFull')
180 55972 argiro.kok
            //.map(res => <Array<Community>> res.json())
181
            .pipe(catchError(this.handleError));
182 53600 argiro.kok
    }
183
184
    getCommunityFull(community_pid: string, helpContentUrl:string) {
185 57916 argiro.kok
        return this.http.get<Community>(helpContentUrl + '/communityFull/'+community_pid)
186 55972 argiro.kok
            //.map(res => <Community> res.json())
187
            .pipe(catchError(this.handleError));
188 53600 argiro.kok
    }
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 57916 argiro.kok
        return this.http.post<Community>(helpContentUrl + '/community/save', JSON.stringify(community), CustomOptions.getAuthOptionsWithBody())
197 55972 argiro.kok
        //.map(res => <Community> res.json())
198
            .pipe(catchError(this.handleError));
199 53600 argiro.kok
    }
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 57916 argiro.kok
        return this.http.post<Community>(helpContentUrl + '/community/update', JSON.stringify(community),  CustomOptions.getAuthOptionsWithBody())
208 55972 argiro.kok
            //.map(res => <Community> res.json())
209
            .pipe(catchError(this.handleError));
210 53600 argiro.kok
    }
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 57916 argiro.kok
        return this.http.post(helpContentUrl + '/community/delete',JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
217 55972 argiro.kok
            .pipe(catchError(this.handleError));
218 53600 argiro.kok
    }
219
220
    getCommunityPages(community_pid: string, params: string, helpContentUrl:string) {
221 57916 argiro.kok
        return this.http.get<Array<Page>>(helpContentUrl + '/community/'+community_pid+'/pages'+params)
222 55972 argiro.kok
            //.map(res => <Array<Page>> res.json())
223
            .pipe(catchError(this.handleError));
224 53600 argiro.kok
    }
225
226
    getEntities(helpContentUrl:string) {
227 57916 argiro.kok
        return this.http.get<Array<Entity>>(helpContentUrl + '/entity')
228 55972 argiro.kok
            //.map(res => <Array<Entity>> res.json())
229
            .pipe(catchError(this.handleError));
230 53600 argiro.kok
    }
231
232
    getCommunityEntities(community_pid: string, helpContentUrl:string) {
233 57916 argiro.kok
        return this.http.get<Array<Entity>>(helpContentUrl + '/community/'+community_pid+'/entities')
234 55972 argiro.kok
            //.map(res => <Array<Entity>> res.json())
235
            .pipe(catchError(this.handleError));
236 53600 argiro.kok
    }
237
238
239
    saveEntity(entity: Entity, helpContentUrl:string) {
240
        HelpContentService.removeNulls(entity);
241
242 57916 argiro.kok
        return this.http.post<Entity>(helpContentUrl + '/entity/save', JSON.stringify(entity), CustomOptions.getAuthOptionsWithBody())
243 55972 argiro.kok
            //.map(res => <Entity> res.json())
244
            .pipe(catchError(this.handleError));
245 53600 argiro.kok
    }
246
247
    updateEntity(entity: Entity, helpContentUrl:string) {
248
        HelpContentService.removeNulls(entity);
249
250 57916 argiro.kok
        return this.http.post<Entity>(helpContentUrl + '/entity/update', JSON.stringify(entity), CustomOptions.getAuthOptionsWithBody())
251 55972 argiro.kok
            //.map(res => <Entity> res.json())
252
            .pipe(catchError(this.handleError));
253 53600 argiro.kok
    }
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 57916 argiro.kok
        return this.http.post(helpContentUrl +'/community/'+selectedCommunityPid+ '/entity/toggle?status='+ status.toString(), JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
266 53600 argiro.kok
            //.map( res => <string[]> res.json())
267 55972 argiro.kok
            .pipe(catchError(this.handleError));
268 53600 argiro.kok
    }
269
270
    deleteEntities(ids : string[], helpContentUrl:string) {
271 57916 argiro.kok
        return this.http.post(helpContentUrl + '/entity/delete',JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
272 55972 argiro.kok
            .pipe(catchError(this.handleError));
273 53600 argiro.kok
    }
274
275
276
    toggleEntityOfPage(pageId: string, entityId : string,status : boolean, helpContentUrl:string) {
277 57916 argiro.kok
        return this.http.post(helpContentUrl + '/page/'+pageId+'/entity/toggle?status='+ status.toString()+'&entityId='+entityId.toString(), CustomOptions.getAuthOptionsWithBody())
278 55972 argiro.kok
            .pipe(catchError(this.handleError));
279 53600 argiro.kok
    }
280
281
    savePage(page: Page, helpContentUrl:string) {
282 53669 argiro.kok
         HelpContentService.removeNulls(page);
283 53600 argiro.kok
284 57916 argiro.kok
        return this.http.post<Page>(helpContentUrl + '/page/save', JSON.stringify(page), CustomOptions.getAuthOptionsWithBody())
285 55972 argiro.kok
            //.map(res => <Page> res.json())
286
            .pipe(catchError(this.handleError));
287 53600 argiro.kok
    }
288
289
    updatePage(page: Page, helpContentUrl:string) {
290
291
        HelpContentService.removeNulls(page);
292
293 57916 argiro.kok
        return this.http.post<Page>(helpContentUrl + '/page/update', JSON.stringify(page), CustomOptions.getAuthOptionsWithBody())
294 55972 argiro.kok
            //.map(res => <Page> res.json())
295
            .pipe(catchError(this.handleError));
296 53600 argiro.kok
    }
297
298
    togglePages(selectedCommunityPid: string, ids : string[],status : boolean, helpContentUrl:string) {
299
300 57916 argiro.kok
        return this.http.post(helpContentUrl + '/community/'+selectedCommunityPid+'/page/toggle?status='+ status.toString(),JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
301 55972 argiro.kok
            .pipe(catchError(this.handleError));
302 53600 argiro.kok
    }
303
304
    deletePages(ids : string[], helpContentUrl:string) {
305
306 57916 argiro.kok
        return this.http.post(helpContentUrl + '/page/delete',JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
307 55972 argiro.kok
            .pipe(catchError(this.handleError));
308 53600 argiro.kok
    }
309
310
    getPageHelpContents(helpContentUrl:string) {
311 55972 argiro.kok
        return this.http.get<Array<PageHelpContent>>(helpContentUrl + 'pagehelpcontent')
312
            //.map(res => <Array<PageHelpContent>> res.json())
313
            .pipe(catchError(this.handleError));
314 53600 argiro.kok
    }
315
316
    getCommunityPageHelpContents(community_pid: string, helpContentUrl:string) {
317 57916 argiro.kok
        return this.http.get<Array<PageHelpContent>>(helpContentUrl + '/pagehelpcontent?community='+community_pid)
318 55972 argiro.kok
            //.map(res => <Array<PageHelpContent>> res.json())
319
            .pipe(catchError(this.handleError));
320 53600 argiro.kok
    }
321
322
    getPageHelpContent(id : string, helpContentUrl:string) {
323 57916 argiro.kok
        return this.http.get<PageHelpContent>(helpContentUrl + '/pagehelpcontent/' + id)
324 55972 argiro.kok
            //.map(res => <PageHelpContent> res.json())
325
            .pipe(catchError(this.handleError));
326 53600 argiro.kok
    }
327
328
    savePageHelpContent(pageHelpContent: PageHelpContent, helpContentUrl:string) {
329
        HelpContentService.removeNulls(pageHelpContent);
330
331 57916 argiro.kok
        return this.http.post<PageHelpContent>(helpContentUrl + '/pagehelpcontent/save', JSON.stringify(pageHelpContent), CustomOptions.getAuthOptionsWithBody())
332 55972 argiro.kok
            //.map(res => <PageHelpContent> res.json())
333
            .pipe(catchError(this.handleError));
334 53600 argiro.kok
    }
335
336
    updatePageHelpContent(pageHelpContent: PageHelpContent, helpContentUrl:string) {
337
        HelpContentService.removeNulls(pageHelpContent);
338
339 57916 argiro.kok
        return this.http.post<PageHelpContent>(helpContentUrl + '/pagehelpcontent/update', JSON.stringify(pageHelpContent), CustomOptions.getAuthOptionsWithBody())
340 55972 argiro.kok
            //.map(res => <PageHelpContent> res.json())
341
            .pipe(catchError(this.handleError));
342 53600 argiro.kok
    }
343
344
    deletePageHelpContents(ids : string[], helpContentUrl:string) {
345 53669 argiro.kok
          return this.http.post(helpContentUrl + 'pagehelpcontent/delete',JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
346 55972 argiro.kok
              .pipe(catchError(this.handleError));
347 53600 argiro.kok
    }
348
349
    togglePageHelpContents(ids : string[],status : boolean, helpContentUrl:string) {
350 57916 argiro.kok
        return this.http.post(helpContentUrl + '/pagehelpcontent/toggle?status='+ status.toString(), JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
351 53600 argiro.kok
            //.map( res => <string[]> res.json())
352 55972 argiro.kok
            .pipe(catchError(this.handleError));
353 53600 argiro.kok
    }
354
355 55972 argiro.kok
    private handleError(error: HttpErrorResponse) {
356 53600 argiro.kok
        // 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 55972 argiro.kok
        return Observable.throw(error.error || 'Server error');
360 53600 argiro.kok
    }
361
362 53669 argiro.kok
    // 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 53600 argiro.kok
366
    getCommunityStatistics(apiUrl: string, communityId: string): Observable<StatisticsSummary> {
367
        const url = `${apiUrl}communities/${communityId}`;
368 54788 konstantin
        //console.log(`getting statistics summary from: ${url}`);
369 53600 argiro.kok
        return this.http.get(url)
370 55972 argiro.kok
            //.map(res => <any>res.json())
371
            .pipe(map(res => res['statistics']));
372 53600 argiro.kok
    }
373
374
    getCommunityAdminStatisticsChoices(apiUrl: string, communityId: string): Observable<StatisticsDisplay> {
375 57916 argiro.kok
        const url = `${apiUrl}/statistics/${communityId}`;
376 54788 konstantin
        //console.log(`getting admin choices for statistics from: ${url}`);
377 55972 argiro.kok
        return this.http.get<StatisticsDisplay>(url)
378
            //.map(stats => <StatisticsDisplay>stats.json())
379
            .pipe(catchError(this.handleError));
380 53600 argiro.kok
    }
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 57916 argiro.kok
        const url = `${apiUrl}/statistics/${communityId}/${entity}/${chartsOrNumbers}?status=${status.toString()}&monitor=${monitor.toString()}`;
390 54788 konstantin
        //console.log(`getting admin choices for statistics from: ${url}`);
391 53600 argiro.kok
392 53669 argiro.kok
        return this.http.post(url, title, CustomOptions.getAuthOptionsWithBody())
393 55972 argiro.kok
            //.map(stats => <any>stats.json())
394
            .pipe(catchError(this.handleError));
395 53600 argiro.kok
    }
396
}