Project

General

Profile

1 50448 sofia.balt
import { Injectable } from '@angular/core';
2
import { Http, Response, Headers, RequestOptions } from '@angular/http';
3
import { Observable } from 'rxjs/Rx';
4
5 51076 sofia.balt
import { ResultInfo } from '../results/resultInfo';
6
import { CommunityInfo } from './communityInfo';
7 50448 sofia.balt
8
@Injectable()
9
export class CommunityService {
10
11 50449 sofia.balt
    constructor(private http:Http) {
12
      }
13
14 50974 sofia.balt
    getCommunity(url: string) {
15
        return this.http.get(url).map(res => <any> res.json()).map(res => this.parseCommunity(res));
16 50965 sofia.balt
    }
17
18 50974 sofia.balt
    parseCommunity(data:any): CommunityInfo {
19 50965 sofia.balt
20
      let length = Array.isArray(data) ? data.length :1;
21
22 50974 sofia.balt
      for (let i=0; i<length; i++) {
23 50965 sofia.balt
24
          let resData = Array.isArray(data) ? data[i] : data;
25
26 50974 sofia.balt
          var community: CommunityInfo = new CommunityInfo();
27 50965 sofia.balt
28 50974 sofia.balt
          if(Array.isArray(resData)) {
29 50965 sofia.balt
30 50974 sofia.balt
              community['title'] = resData[0].name;
31
              community['shortTitle'] = resData[0].shortName;
32
              community['communityId'] = resData[0].id;
33
              community['queryId'] = resData[0].queryId;
34
              community['logoUrl'] = resData[0].logoUrl;
35
              community['description'] = resData[0].description;
36 51042 sofia.balt
              community['date'] = resData[0].creationDate;
37 50965 sofia.balt
38 51025 sofia.balt
              if(resData[0].managers != null) {
39
                  if(community['managers'] == undefined) {
40
                    community['managers'] = new Array<string>();
41
                  }
42
43
                  let managers = resData[0].managers;
44
                  let length = Array.isArray(managers) ? managers.length : 1;
45
46
                  for(let i=0; i<length; i++) {
47
                    let manager = Array.isArray(managers) ? managers[i] : managers;
48
                    community.managers[i] = manager;
49
                  }
50
              }
51
52 51036 sofia.balt
              if(resData[0].subjects != null) {
53
                  if(community['subjects'] == undefined) {
54
                    community['subjects'] = new Array<string>();
55
                  }
56
57
                  let subjects = resData[0].subjects;
58
                  let length = Array.isArray(subjects) ? subjects.length : 1;
59
60
                  for(let i=0; i<length; i++) {
61
                    let subject = Array.isArray(subjects) ? subjects[i] : subjects;
62
                    community.subjects[i] = subject;
63
                  }
64
              }
65
66 50974 sofia.balt
          } else if(!Array.isArray(resData)) {
67 50965 sofia.balt
68 50974 sofia.balt
              community['title'] = resData.name;
69
              community['shortTitle'] = resData.shortName;
70
              community['communityId'] = resData.id;
71
              community['queryId'] = resData.queryId;
72
              community['logoUrl'] = resData.logoUrl;
73
              community['description'] = resData.description;
74 51042 sofia.balt
              community['date'] = resData.creationDate;
75 51025 sofia.balt
76
              if(resData.managers != null) {
77
                  if(community['managers'] == undefined) {
78
                    community['managers'] = new Array<string>();
79
                  }
80
81
                  let managers = resData.managers;
82
                  let length = Array.isArray(managers) ? managers.length : 1;
83
84
                  for(let i=0; i<length; i++) {
85
                    let manager = Array.isArray(managers) ? managers[i] : managers;
86
                    community.managers[i] = manager;
87
                  }
88
              }
89 51036 sofia.balt
90
              if(resData.subjects != null) {
91
                  if(community['subjects'] == undefined) {
92
                    community['subjects'] = new Array<string>();
93
                  }
94
95
                  let subjects = resData.subjects;
96
                  let length = Array.isArray(subjects) ? subjects.length : 1;
97
98
                  for(let i=0; i<length; i++) {
99
                    let subject = Array.isArray(subjects) ? subjects[i] : subjects;
100
                    community.subjects[i] = subject;
101
                  }
102
              }
103 50965 sofia.balt
          }
104
      }
105
      return community;
106
    }
107
108 50448 sofia.balt
}