openaire-library/connect/communities/communities.service.ts

108 lines
3.9 KiB
TypeScript
Raw Normal View History

import {Injectable} from '@angular/core';
import {Http, Response} from '@angular/http';
import {Headers, RequestOptions} from '@angular/http';
import {Observable} from 'rxjs/Rx';
import {CommunityInfo} from '../community/communityInfo';
@Injectable()
export class CommunitiesService {
constructor(private http:Http) {
}
getCommunities(url: string) {
return this.http.get(url).map(res => <any> res.json()).map(res => this.parseCommunities(res));
}
parseCommunities(data: any): CommunityInfo[] {
let communities: CommunityInfo[] = [];
let length = Array.isArray(data) ? data.length :1;
for (let i=0; i<length; i++) {
let resData = Array.isArray(data) ? data[i] : data;
var result: CommunityInfo = new CommunityInfo();
if(Array.isArray(resData)) {
result['title'] = resData[0].name;
result['shortTitle'] = resData[0].shortName;
result['communityId'] = resData[0].id;
result['queryId'] = resData[0].queryId;
result['logoUrl'] = resData[0].logoUrl;
result['description'] = resData[0].description;
result['date'] = resData[0].creationDate;
if(resData[0].managers != null) {
if(result['managers'] == undefined) {
result['managers'] = new Array<string>();
}
let managers = resData[0].managers;
let length = Array.isArray(managers) ? managers.length : 1;
for(let i=0; i<length; i++) {
let manager = Array.isArray(managers) ? managers[i] : managers;
result.managers[i] = manager;
}
}
if(resData[0].subjects != null) {
if(result['subjects'] == undefined) {
result['subjects'] = new Array<string>();
}
let subjects = resData[0].subjects;
let length = Array.isArray(subjects) ? subjects.length : 1;
for(let i=0; i<length; i++) {
let subject = Array.isArray(subjects) ? subjects[i] : subjects;
result.subjects[i] = subject;
}
}
} else {
result['title'] = resData.name;
result['shortTitle'] = resData.shortName;
result['communityId'] = resData.id;
result['queryId'] = resData.queryId;
result['logoUrl'] = resData.logoUrl;
result['description'] = resData.description;
result['date'] = resData.creationDate;
if(resData.managers != null) {
if(result['managers'] == undefined) {
result['managers'] = new Array<string>();
}
let managers = resData.managers;
let length = Array.isArray(managers) ? managers.length : 1;
for(let i=0; i<length; i++) {
let manager = Array.isArray(managers) ? managers[i] : managers;
result.managers[i] = manager;
}
}
if(resData.subjects != null) {
if(result['subjects'] == undefined) {
result['subjects'] = new Array<string>();
}
let subjects = resData.subjects;
let length = Array.isArray(subjects) ? subjects.length : 1;
for(let i=0; i<length; i++) {
let subject = Array.isArray(subjects) ? subjects[i] : subjects;
result.subjects[i] = subject;
}
}
}
communities.push(result);
}
return communities;
}
}