2018-03-07 12:08:03 +01:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import { Http, Response, Headers, RequestOptions } from '@angular/http';
|
|
|
|
import { Observable } from 'rxjs/Rx';
|
|
|
|
|
|
|
|
import { ResultInfo } from '../results/resultInfo';
|
|
|
|
import { CommunityInfo } from './communityInfo';
|
2018-04-04 16:05:26 +02:00
|
|
|
import{EnvProperties} from '../../utils/properties/env-properties';
|
2018-03-07 12:08:03 +01:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class CommunityService {
|
|
|
|
|
|
|
|
constructor(private http:Http) {
|
|
|
|
}
|
|
|
|
|
2018-04-04 16:05:26 +02:00
|
|
|
getCommunity(properties:EnvProperties, url: string) {
|
|
|
|
return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)) : url)
|
|
|
|
.map(res => <any> res.json()).map(res => this.parseCommunity(res));
|
2018-03-07 12:08:03 +01:00
|
|
|
}
|
2018-03-20 12:40:39 +01:00
|
|
|
|
|
|
|
updateCommunity(url: string, community:any) {
|
2018-03-20 13:09:51 +01:00
|
|
|
let headers = new Headers({'Content-Type': 'application/json'});
|
|
|
|
let options = new RequestOptions({headers: headers});
|
|
|
|
|
2018-03-20 12:40:39 +01:00
|
|
|
let body = JSON.stringify(community);
|
|
|
|
|
2018-03-20 13:09:51 +01:00
|
|
|
console.log(body);
|
|
|
|
|
|
|
|
return this.http.post(url, body, options)
|
|
|
|
/*.map(res => res.json())*/
|
2018-03-20 12:40:39 +01:00
|
|
|
.do(request => console.log("Insert Response:"+request.status));
|
|
|
|
}
|
|
|
|
|
2018-04-04 16:05:26 +02:00
|
|
|
iscommunityManager(properties:EnvProperties, url: string, manager:string){
|
|
|
|
return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)) : url)
|
|
|
|
.map(res => <any> res.json()).map(res => this.parseCommunity(res)).map(community => community.managers.indexOf(manager)!=-1);
|
2018-03-15 10:53:07 +01:00
|
|
|
}
|
2018-04-04 16:05:26 +02:00
|
|
|
iscommunityRI(properties:EnvProperties, url: string){
|
|
|
|
return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)) : url)
|
|
|
|
.map(res => <any> res.json()).map(res => this.parseCommunity(res)).map(community => (community && community.type && community.type !="community"));
|
2018-03-27 10:22:55 +02:00
|
|
|
}
|
2018-04-16 13:49:35 +02:00
|
|
|
isSubscribedToCommunity(pid:string, email:string, url:string){
|
|
|
|
return this.http.get(url+"/community/"+pid+"/subscribers")
|
|
|
|
.map(res => ((<any>res =="")?{}:<any> res.json()))
|
|
|
|
|
|
|
|
.map(res => {
|
|
|
|
if(res.subscribers && res.subscribers != null){
|
|
|
|
|
|
|
|
for(var i =0; i< res.subscribers.length; i++ ){
|
|
|
|
if(res.subscribers[i]!=null && res.subscribers[i].email == email){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}).do(res => {console.log("Response is "+res)});
|
|
|
|
}
|
2018-03-07 12:08:03 +01:00
|
|
|
parseCommunity(data:any): 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 community: CommunityInfo = new CommunityInfo();
|
|
|
|
|
|
|
|
if(Array.isArray(resData)) {
|
|
|
|
|
|
|
|
community['title'] = resData[0].name;
|
|
|
|
community['shortTitle'] = resData[0].shortName;
|
|
|
|
community['communityId'] = resData[0].id;
|
|
|
|
community['queryId'] = resData[0].queryId;
|
|
|
|
community['logoUrl'] = resData[0].logoUrl;
|
|
|
|
community['description'] = resData[0].description;
|
|
|
|
community['date'] = resData[0].creationDate;
|
2018-05-03 13:27:42 +02:00
|
|
|
community['status'] = "all";
|
|
|
|
if(resData[0].hasOwnProperty('status')){
|
|
|
|
community['status'] = resData[0].status;
|
|
|
|
var status = ["all","hidden","manager"];
|
|
|
|
if(status.indexOf(community['status']) ==-1){
|
|
|
|
community['status'] = "hidden";
|
|
|
|
}
|
|
|
|
}
|
2018-03-26 16:13:28 +02:00
|
|
|
if (resData[0].type != null) {
|
|
|
|
community['type'] = resData[0].type;
|
|
|
|
}
|
|
|
|
|
2018-03-07 12:08:03 +01:00
|
|
|
if(resData[0].managers != null) {
|
|
|
|
if(community['managers'] == undefined) {
|
|
|
|
community['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;
|
|
|
|
community.managers[i] = manager;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(resData[0].subjects != null) {
|
|
|
|
if(community['subjects'] == undefined) {
|
|
|
|
community['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;
|
|
|
|
community.subjects[i] = subject;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if(!Array.isArray(resData)) {
|
|
|
|
|
|
|
|
community['title'] = resData.name;
|
|
|
|
community['shortTitle'] = resData.shortName;
|
|
|
|
community['communityId'] = resData.id;
|
|
|
|
community['queryId'] = resData.queryId;
|
|
|
|
community['logoUrl'] = resData.logoUrl;
|
|
|
|
community['description'] = resData.description;
|
|
|
|
community['date'] = resData.creationDate;
|
2018-05-03 13:27:42 +02:00
|
|
|
community['status'] = "all";
|
|
|
|
if(resData.hasOwnProperty('status')){
|
|
|
|
community['status'] = resData.status;
|
|
|
|
var status = ["all","hidden","manager"];
|
|
|
|
if(status.indexOf(community['status']) ==-1){
|
|
|
|
community['status'] = "hidden";
|
|
|
|
}
|
|
|
|
}
|
2018-03-26 16:13:28 +02:00
|
|
|
if (resData.type != null) {
|
|
|
|
community['type'] = resData.type;
|
|
|
|
}
|
|
|
|
|
2018-03-07 12:08:03 +01:00
|
|
|
if(resData.managers != null) {
|
|
|
|
if(community['managers'] == undefined) {
|
|
|
|
community['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;
|
|
|
|
community.managers[i] = manager;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(resData.subjects != null) {
|
|
|
|
if(community['subjects'] == undefined) {
|
|
|
|
community['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;
|
|
|
|
community.subjects[i] = subject;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return community;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|