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

137 lines
5.4 KiB
TypeScript

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';
import{EnvProperties} from '../../utils/properties/env-properties';
@Injectable()
export class CommunitiesService {
constructor(private http:Http) {
}
getCommunities(properties:EnvProperties, url: string) {
return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)) : 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();
//TODO @Sofia remove this if/else
// if there is case to be array do with resData what you do with data ( let resData = Array.isArray(data) ? data[i] : data;)
//don;t repeat the same code
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;
result['status'] = "all";
if(resData[0].hasOwnProperty('status')){
result['status'] = resData[0].status;
var status = ["all","hidden","manager"];
if(status.indexOf(result['status']) ==-1){
result['status'] = "hidden";
}
}
if (resData[0].type != null) {
result['type'] = resData[0].type;
}
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;
result['status'] = "all";
if(resData.hasOwnProperty('status')){
result['status'] = resData.status;
var status = ["all","hidden","manager"];
if(status.indexOf(result['status']) ==-1){
result['status'] = "hidden";
}
}
if (resData.type != null) {
result['type'] = resData.type;
}
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;
}
}
}
if (result['type'] == 'community' || result['type'] == "ri") {
communities.push(result);
}
}
return communities;
}
}