Project

General

Profile

1
/**
2
 * Created by stefania on 7/13/17.
3
 */
4
import { Injectable } from '@angular/core';
5
import { Http, Response, Headers, RequestOptions } from '@angular/http';
6
import { Observable } from 'rxjs/Rx';
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

    
16

    
17
@Injectable()
18
export class HelpContentService {
19

    
20
    constructor(private http:Http) {
21
    }
22

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

    
31
    getDivIdsFull(page_id: string, helpContentUrl:string) {
32
      if(page_id) {
33
        return this.http.get(helpContentUrl + 'divFull?&page='+page_id)
34
            .map(res => <Array<DivId>> res.json())
35
            .catch(this.handleError);
36
      } else {
37
        return this.http.get(helpContentUrl + 'divFull')
38
            .map(res => <Array<DivId>> res.json())
39
            .catch(this.handleError);
40
      }
41
    }
42

    
43
    updateDivId(divId: DivId, helpContentUrl:string) {
44
        HelpContentService.removeNulls(divId);
45

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

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

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

    
63
    saveDivId(divId: DivId, helpContentUrl:string) {
64
        HelpContentService.removeNulls(divId);
65

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

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

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

    
95
    insertOrUpdateDivHelpContent(divHelpContent: DivHelpContent, helpContentUrl:string) {
96
        HelpContentService.removeNulls(divHelpContent);
97

    
98
        return this.http.post(helpContentUrl + 'divhelpcontent', JSON.stringify(divHelpContent), CustomOptions.getAuthOptionsWithBody())
99
            .map(res => <DivHelpContent> res.json())
100
            .catch(this.handleError);
101
    }
102

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

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

    
113
    toggleDivHelpContents(ids : string[],status : boolean, helpContentUrl:string) {
114

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

    
120
    getPagesWithDivIds(community_pid: string, helpContentUrl:string) {
121
        return this.http.get(helpContentUrl + 'div/pages')
122
            .map(res => <Map<string, Set<string>>> res.json())
123
            .catch(this.handleError);
124
    }
125

    
126
    getPages(helpContentUrl:string,pid:string) {
127
        return this.http.get(helpContentUrl + 'page'+(pid?("?pid="+pid):""))
128
            .map(res => <Array<Page>> res.json())
129
            .catch(this.handleError);
130
    }
131

    
132
    getPage(pageId:string, helpContentUrl:string) {
133
        return this.http.get(helpContentUrl + 'page/'+pageId)
134
            .map(res => <Page> res.json())
135
            .catch(this.handleError);
136
    }
137

    
138
    getCommunities( helpContentUrl:string) {
139
        return this.http.get(helpContentUrl + 'community')
140
            .map(res => <Array<Community>> res.json())
141
            .catch(this.handleError);
142
    }
143

    
144
    getCommunity(community_pid: string, helpContentUrl:string) {
145
        return this.http.get(helpContentUrl + 'community/'+community_pid)
146
            .map(res => <Community> res.json())
147
            .catch(this.handleError);
148
    }
149

    
150
    getCommunitiesFull( helpContentUrl:string) {
151
        return this.http.get(helpContentUrl + 'communityFull')
152
            .map(res => <Array<Community>> res.json())
153
            .catch(this.handleError);
154
    }
155

    
156
    getCommunityFull(community_pid: string, helpContentUrl:string) {
157
        return this.http.get(helpContentUrl + 'communityFull/'+community_pid)
158
            .map(res => <Community> res.json())
159
            .catch(this.handleError);
160
    }
161

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

    
166
        HelpContentService.removeNulls(community);
167

    
168
        return this.http.post(helpContentUrl + 'community/save', JSON.stringify(community),CustomOptions.getAuthOptionsWithBody())
169
            .map(res => <Community> res.json())
170
            .catch(this.handleError);
171
    }
172

    
173
    updateCommunity(community: Community, helpContentUrl:string) {
174
        // let headers = new Headers({'Content-Type': 'application/json'});
175
        // let options = new RequestOptions({headers: headers});
176

    
177
        HelpContentService.removeNulls(community);
178

    
179
        return this.http.post(helpContentUrl + 'community/update', JSON.stringify(community),  CustomOptions.getAuthOptionsWithBody())
180
            .map(res => <Community> res.json())
181
            .catch(this.handleError);
182
    }
183

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

    
188
        return this.http.post(helpContentUrl + 'community/delete',JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
189
            .catch(this.handleError);
190
    }
191

    
192
    getCommunityPages(community_pid: string, params: string, helpContentUrl:string) {
193
        return this.http.get(helpContentUrl + 'community/'+community_pid+'/pages'+params)
194
            .map(res => <Array<Page>> res.json())
195
            .catch(this.handleError);
196
    }
197

    
198
    getEntities(helpContentUrl:string) {
199
        return this.http.get(helpContentUrl + 'entity')
200
            .map(res => <Array<Entity>> res.json())
201
            .catch(this.handleError);
202
    }
203

    
204
    getCommunityEntities(community_pid: string, helpContentUrl:string) {
205
        return this.http.get(helpContentUrl + 'community/'+community_pid+'/entities')
206
            .map(res => <Array<Entity>> res.json())
207
            .catch(this.handleError);
208
    }
209

    
210

    
211
    saveEntity(entity: Entity, helpContentUrl:string) {
212
        HelpContentService.removeNulls(entity);
213

    
214
        return this.http.post(helpContentUrl + 'entity/save', JSON.stringify(entity), CustomOptions.getAuthOptionsWithBody())
215
            .map(res => <Entity> res.json())
216
            .catch(this.handleError);
217
    }
218

    
219
    updateEntity(entity: Entity, helpContentUrl:string) {
220
        HelpContentService.removeNulls(entity);
221

    
222
        return this.http.post(helpContentUrl + 'entity/update', JSON.stringify(entity), CustomOptions.getAuthOptionsWithBody())
223
            .map(res => <Entity> res.json())
224
            .catch(this.handleError);
225
    }
226

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

    
235
    toggleEntities(selectedCommunityPid: string, ids : string[],status : boolean, helpContentUrl:string) {
236

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

    
242
    deleteEntities(ids : string[], helpContentUrl:string) {
243
        return this.http.post(helpContentUrl + 'entity/delete',JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
244
            .catch(this.handleError);
245
    }
246

    
247

    
248
    toggleEntityOfPage(pageId: string, entityId : string,status : boolean, helpContentUrl:string) {
249
        return this.http.post(helpContentUrl + 'page/'+pageId+'/entity/toggle?status='+ status.toString()+'&entityId='+entityId.toString(), CustomOptions.getAuthOptionsWithBody())
250
            .catch(this.handleError);
251
    }
252

    
253
    savePage(page: Page, helpContentUrl:string) {
254
         HelpContentService.removeNulls(page);
255

    
256
        return this.http.post(helpContentUrl + 'page/save', JSON.stringify(page), CustomOptions.getAuthOptionsWithBody())
257
            .map(res => <Page> res.json())
258
            .catch(this.handleError);
259
    }
260

    
261
    updatePage(page: Page, helpContentUrl:string) {
262

    
263
        HelpContentService.removeNulls(page);
264

    
265
        return this.http.post(helpContentUrl + 'page/update', JSON.stringify(page), CustomOptions.getAuthOptionsWithBody())
266
            .map(res => <Page> res.json())
267
            .catch(this.handleError);
268
    }
269

    
270
    togglePages(selectedCommunityPid: string, ids : string[],status : boolean, helpContentUrl:string) {
271

    
272
        return this.http.post(helpContentUrl + 'community/'+selectedCommunityPid+'/page/toggle?status='+ status.toString(),JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
273
            .catch(this.handleError);
274
    }
275

    
276
    deletePages(ids : string[], helpContentUrl:string) {
277

    
278
        return this.http.post(helpContentUrl + 'page/delete',JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
279
            .catch(this.handleError);
280
    }
281

    
282
    getPageHelpContents(helpContentUrl:string) {
283
        return this.http.get(helpContentUrl + 'pagehelpcontent')
284
            .map(res => <Array<PageHelpContent>> res.json())
285
            .catch(this.handleError);
286
    }
287

    
288
    getCommunityPageHelpContents(community_pid: string, helpContentUrl:string) {
289
        return this.http.get(helpContentUrl + 'pagehelpcontent?community='+community_pid)
290
            .map(res => <Array<PageHelpContent>> res.json())
291
            .catch(this.handleError);
292
    }
293

    
294
    getPageHelpContent(id : string, helpContentUrl:string) {
295
        return this.http.get(helpContentUrl + 'pagehelpcontent/' + id)
296
            .map(res => <PageHelpContent> res.json())
297
            .catch(this.handleError);
298
    }
299

    
300
    savePageHelpContent(pageHelpContent: PageHelpContent, helpContentUrl:string) {
301
        HelpContentService.removeNulls(pageHelpContent);
302

    
303
        return this.http.post(helpContentUrl + 'pagehelpcontent/save', JSON.stringify(pageHelpContent), CustomOptions.getAuthOptionsWithBody())
304
            .map(res => <PageHelpContent> res.json())
305
            .catch(this.handleError);
306
    }
307

    
308
    updatePageHelpContent(pageHelpContent: PageHelpContent, helpContentUrl:string) {
309
        HelpContentService.removeNulls(pageHelpContent);
310

    
311
        return this.http.post(helpContentUrl + 'pagehelpcontent/update', JSON.stringify(pageHelpContent), CustomOptions.getAuthOptionsWithBody())
312
            .map(res => <PageHelpContent> res.json())
313
            .catch(this.handleError);
314
    }
315

    
316
    deletePageHelpContents(ids : string[], helpContentUrl:string) {
317
          return this.http.post(helpContentUrl + 'pagehelpcontent/delete',JSON.stringify(ids), CustomOptions.getAuthOptionsWithBody())
318
            .catch(this.handleError);
319
    }
320

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

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

    
334
    // getDataProviders() {
335
    //     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);
336
    // }
337

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

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

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

    
364
        return this.http.post(url, title, CustomOptions.getAuthOptionsWithBody())
365
            .map(stats => <any>stats.json())
366
            .catch(this.handleError);
367
    }
368
}
(2-2/5)