import {Injectable} from '@angular/core'; import {URLSearchParams } from '@angular/http'; import {Http, Response} from '@angular/http'; import {Observable} from 'rxjs/Observable'; // import {Claim} from '../claim'; import 'rxjs/add/observable/of'; import 'rxjs/add/operator/do'; import 'rxjs/add/operator/share'; import { CustomOptions } from './customOptions.class'; @Injectable() export class ClaimsService { constructor(private http: Http ) { } private getClaimRequest(size : number, page : number, url :string, fromCache:boolean):any { console.info('ClaimsService: Claims request: '+url); let key = url; return this.http.get(url, CustomOptions.getAuthOptions()) .map(request => request.json()) .do(request => console.info("Get claims: offset = "+(size*(page-1)) + " limit ="+size )) .catch(this.handleError); } getClaims( size : number, page : number, keyword:string, sortby: string, descending: boolean, types: string, apiUrl:string):any { console.info('ClaimsService: getClaims ' ); console.info('ClaimsService: Types : '+types ); let url = apiUrl +"claims"+"?offset="+(size*(page-1) + "&limit="+size)+"&keyword="+keyword+"&sortby="+sortby+"&descending="+descending+"&"+types; return this.getClaimRequest(size,page,url,true); } getClaimsByUser( size : number, page : number, user:string, keyword:string, sortby: string, descending: boolean, types: string, apiUrl:string):any { console.info('ClaimsService: getClaims for user : '+user); let url = apiUrl +"users/claims"+"?offset="+(size*(page-1) + "&limit="+size)+"&keyword="+keyword+"&sortby="+sortby+"&descending="+descending+"&"+types; return this.getClaimRequest(size,page,url,false); } getClaimsBycontext( size : number, page : number, contextId:string, keyword:string, sortby: string, descending: boolean, types: string , apiUrl:string):any { console.info('ClaimsService: getClaims for context : '+contextId); let url = apiUrl +"contexts/"+contextId+"/claims"+"?offset="+(size*(page-1) + "&limit="+size)+"&keyword="+keyword+"&sortby="+sortby+"&descending="+descending+"&"+types; return this.getClaimRequest(size,page,url,true); } getClaimsByResult( size : number, page : number, resultId:string, keyword:string, sortby: string, descending: boolean, types: string, apiUrl:string ):any { console.info('ClaimsService: getClaims for result : '+resultId); let url = apiUrl +"results/"+resultId+"/claims"+"?offset="+(size*(page-1) + "&limit="+size)+"&keyword="+keyword+"&sortby="+sortby+"&descending="+descending+"&"+types; return this.getClaimRequest(size,page,url,true); } getClaimsByProject( size : number, page : number, projectId:string, keyword:string, sortby: string, descending: boolean, types: string, apiUrl:string ):any { console.info('ClaimsService: getClaims for project : '+projectId); let url = apiUrl +"projects/"+projectId+"/claims"+"?offset="+(size*(page-1) + "&limit="+size)+"&keyword="+keyword+"&sortby="+sortby+"&descending="+descending+"&"+types; return this.getClaimRequest(size,page,url,true); } deleteClaimById(claimId:string , apiUrl:string):any{ console.warn('Trying to delete claim with id : '+claimId); let url = apiUrl +"claims/"+claimId; // let headers = new Headers({ 'Content-Type': 'application/json' }); // let options = new RequestOptions({ headers: headers }); return this.http.delete( url, CustomOptions.getAuthOptionsWithBody()).map(request => request.json()) // .do(request => console.info("After delete" )) .catch(this.handleError); } deleteBulk(claimIds:string[], apiUrl:string):any{ console.warn('Trying to delete claims with ids : '+claimIds); var url = ""; for(var claimId of claimIds){ url=url+(url.length >0 ?"&":"")+"claimId="+claimId; } url= apiUrl +"claims/bulk?"+url; // let headers = new Headers({ 'Content-Type': 'application/json' }); // let options = new RequestOptions({ headers: headers }); return this.http.delete( url, CustomOptions.getAuthOptions()).map(request => request.json()) // .do(request => console.info("After delete" )) .catch(this.handleError); } insertBulkClaims(claims, apiUrl:string):any{ console.warn('Trying toinsert claims : '+claims); let url = apiUrl +"claims/bulk"; let body = JSON.stringify( claims ); console.warn('Json body: : '+body); // let headers = new Headers({ 'Content-Type': 'application/json' }); // let options = new RequestOptions({ headers: headers }); return this.http.post(url, body, CustomOptions.getAuthOptionsWithBody()) .map(res => res.json()) .do(request => console.info("Insert Response:"+request.status) ) .catch(this.handleError); } insertClaim(claim, apiUrl:string):any{ console.warn('Trying toinsert claim : '+claim); let url = apiUrl +"claims"; let body = JSON.stringify( claim ); // let headers = new Headers({ 'Content-Type': 'application/json' }); // let options = new RequestOptions({ headers: headers }); return this.http.post(url, body, CustomOptions.getAuthOptionsWithBody()) .map(res => res.json()) .do(request => console.info("Insert Response:"+request.status) ) .catch(this.handleError); } insertDirectRecords(records, apiUrl:string):any{ console.warn('Trying to feedrecords : '+records); let url = apiUrl +"feed/bulk"; let body = JSON.stringify( records ); console.warn('Json body: : '+body); // let headers = new Headers({ 'Content-Type': 'application/json' }); // let options = new RequestOptions({ headers: headers }); return this.http.post(url, body, CustomOptions.getAuthOptionsWithBody()) .map(res => res.json()) .do(request => console.info("Insert Response:"+request) ) .catch(this.handleError); } private handleError (error: Response) { // in a real world app, we may send the error to some remote logging infrastructure // instead of just logging it to the console console.log(error); return Observable.throw(error || 'Server error'); } getClaim(id:string, apiUrl:string):any { let url = apiUrl+"claims/"+id; return new Promise((resolve, reject) => { this.http.get(url) .map(res => res.json()) .subscribe( data => { resolve(data.data); }, err => { reject(err); } ) ; }); } }