Project

General

Profile

1
import {Injectable}                 from '@angular/core';
2
import {Http, Response}             from '@angular/http';
3
import {Headers, RequestOptions}    from '@angular/http';
4
import {Observable}                 from 'rxjs/Rx';
5

    
6
import {CommunityInfo}              from '../community/communityInfo';
7
import{EnvProperties} from '../../utils/properties/env-properties';
8

    
9
@Injectable()
10
export class CommunitiesService {
11

    
12
    constructor(private http:Http) {
13
    }
14

    
15
    getCommunities(properties:EnvProperties, url: string) {
16
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)) : url)
17
                        .map(res => <any> res.json()).map(res => this.parseCommunities(res));
18
    }
19

    
20
    parseCommunities(data: any): CommunityInfo[] {
21
        let communities: CommunityInfo[] = [];
22

    
23
        let length = Array.isArray(data) ? data.length :1;
24

    
25
        for (let i=0; i<length; i++) {
26
            let resData = Array.isArray(data) ? data[i] : data;
27

    
28
            var result: CommunityInfo = new CommunityInfo();
29
            //TODO @Sofia remove this if/else
30
            // if there is case to be array do with resData what you do with data (  let resData = Array.isArray(data) ? data[i] : data;)
31
            //don;t repeat the same code
32
            if(Array.isArray(resData)) {
33
                result['title'] = resData[0].name;
34
                result['shortTitle'] = resData[0].shortName;
35
                result['communityId'] = resData[0].id;
36
                result['queryId'] = resData[0].queryId;
37
                result['logoUrl'] = resData[0].logoUrl;
38
                result['description'] = resData[0].description;
39
                result['date'] = resData[0].creationDate;
40
                result['status'] = "all";
41
                if(resData[0].hasOwnProperty('status')){
42
                  result['status'] = resData[0].status;
43
                  var status = ["all","hidden","manager"];
44
                  if(status.indexOf(result['status']) ==-1){
45
                    result['status'] = "hidden";
46
                  }
47
                }
48

    
49
                if (resData[0].type != null) {
50
                      result['type'] = resData[0].type;
51
                }
52

    
53
                if(resData[0].managers != null) {
54
                    if(result['managers'] == undefined) {
55
                      result['managers'] = new Array<string>();
56
                    }
57

    
58
                    let managers = resData[0].managers;
59
                    let length = Array.isArray(managers) ? managers.length : 1;
60

    
61
                    for(let i=0; i<length; i++) {
62
                      let manager = Array.isArray(managers) ? managers[i] : managers;
63
                      result.managers[i] = manager;
64
                    }
65
                }
66

    
67
                if(resData[0].subjects != null) {
68
                    if(result['subjects'] == undefined) {
69
                      result['subjects'] = new Array<string>();
70
                    }
71

    
72
                    let subjects = resData[0].subjects;
73
                    let length = Array.isArray(subjects) ? subjects.length : 1;
74

    
75
                    for(let i=0; i<length; i++) {
76
                      let subject = Array.isArray(subjects) ? subjects[i] : subjects;
77
                      result.subjects[i] = subject;
78
                    }
79
                }
80

    
81
            } else {
82
                result['title'] = resData.name;
83
                result['shortTitle'] = resData.shortName;
84
                result['communityId'] = resData.id;
85
                result['queryId'] = resData.queryId;
86
                result['logoUrl'] = resData.logoUrl;
87
                result['description'] = resData.description;
88
                result['date'] = resData.creationDate;
89
                result['status'] = "all";
90
                if(resData.hasOwnProperty('status')){
91
                  result['status'] = resData.status;
92
                  var status = ["all","hidden","manager"];
93
                  if(status.indexOf(result['status']) ==-1){
94
                    result['status'] = "hidden";
95
                  }
96
                }
97
                if (resData.type != null) {
98
                      result['type'] = resData.type;
99
                }
100

    
101
                if(resData.managers != null) {
102
                    if(result['managers'] == undefined) {
103
                      result['managers'] = new Array<string>();
104
                    }
105

    
106
                    let managers = resData.managers;
107
                    let length = Array.isArray(managers) ? managers.length : 1;
108

    
109
                    for(let i=0; i<length; i++) {
110
                      let manager = Array.isArray(managers) ? managers[i] : managers;
111
                      result.managers[i] = manager;
112
                    }
113
                }
114

    
115
                if(resData.subjects != null) {
116
                    if(result['subjects'] == undefined) {
117
                      result['subjects'] = new Array<string>();
118
                    }
119

    
120
                    let subjects = resData.subjects;
121
                    let length = Array.isArray(subjects) ? subjects.length : 1;
122

    
123
                    for(let i=0; i<length; i++) {
124
                      let subject = Array.isArray(subjects) ? subjects[i] : subjects;
125
                      result.subjects[i] = subject;
126
                    }
127
                }
128
            }
129
            if (result['type'] == 'community' || result['type'] == "ri") {
130
              communities.push(result);
131
            }
132
        }
133

    
134
        return communities;
135
    }
136
}
    (1-1/1)