import { Injectable, Injector, ReflectiveInjector } from '@angular/core'; import { Http, HttpModule, RequestOptionsArgs, Headers } from '@angular/http'; import { CoreRemoteRestURL } from '../utils/core-remote-rest-urls'; import { TranslateService } from './translate-service'; import { Mediator } from './mediator'; import { CoreMediatorChannels } from '../utils/mediator-channels'; @Injectable() export class HttpService { protected basicUrl: string = CoreRemoteRestURL.SERVER_URL; constructor(private translateService: TranslateService) { $.ajaxSetup({ xhrFields: { withCredentials: true }, crossDomain: true, headers: { "Accept-Language": translateService.currentLang } }); Mediator.subscribe(CoreMediatorChannels.CHANGE_SYSTEM_LANGUAGE, (lang: string) => { $.ajaxSetup({ xhrFields: { withCredentials: true }, crossDomain: true, headers: { "Accept-Language": translateService.currentLang } }); }); } readAll(objectName: string, callback: (data) => void) { $.get(this.basicUrl + objectName + "?localized=true", function (response) { callback(response); }).fail(function (error) { console.log("Error happened" + error); }); } activateInstance(objectName: string, id: number, callback: (data) => void, failureCallback?: (response) => void) { let data = { data: [{ id: id }] }; $.ajax({ type: 'POST', url: this.basicUrl + objectName + CoreRemoteRestURL.ACTIVTION_WITH_NOT_DETAILED_CONTEXT_URL, data: JSON.stringify(data), contentType: "application/json", success: (response) => { console.info(objectName + " Activation action has been done successfully"); callback(response); }, error: (error) => { if (failureCallback) { failureCallback(error.responseJSON); } else callback(error.responseJSON); console.error("An error has been happened while " + objectName + " Activation action: " + error); } }); } deactivateInstance(objectName: string, id: number, callback: (data) => void, failureCallback?: (response) => void) { let data = { data: [{ id: id }] }; $.ajax({ type: 'POST', url: this.basicUrl + objectName + CoreRemoteRestURL.DEACTIVTION_WITH_NOT_DETAILED_CONTEXT_URL, data: JSON.stringify(data), contentType: "application/json", success: (response) => { console.info(objectName + " with id: " + id + " De-activation action has been done successfully") callback(response); }, error: (error) => { if (failureCallback) { failureCallback(error.responseJSON); } else callback(error.responseJSON); console.error("An error has been happened while " + objectName + " with id: " + id + " De-activation action: " + error); } }); } deleteInstances(objectName: string, ids: string[], callback: (data) => void, failureCallback?: (response) => void) { $.ajax({ type: 'POST', url: this.basicUrl + objectName + CoreRemoteRestURL.DELETE_URL, data: JSON.stringify(ids), headers: { "Content-Type": "application/json; charset=utf-8" }, success: (response) => { console.info(objectName + " Deletion action has been done successfully"); callback(response); }, error: (error) => { failureCallback(error.responseJSON); console.error("An error has been happened while " + objectName + " deletion action: " + error); } }); } editEntity(objectName: string, id: number, callback: (data) => void, failureCallback?: (response) => void) { // it just asks for lock token $.ajax({ type: 'GET', url: this.basicUrl + objectName + "/" + id + CoreRemoteRestURL.EDIT_URL, contentType: "application/json", success: (response) => { callback(response); }, error: (error) => { if (failureCallback) { failureCallback(error.responseJSON); } console.log("Error happened while locking" + error); } }); } cancelEditEntity(objectName: string, id: number, tokenId: string, callback: (data) => void, isAsync?: boolean, failureCallback?: (response) => void) { $.ajax({ async: isAsync, type: 'GET', url: this.basicUrl + objectName + "/" + id + CoreRemoteRestURL.UNLOCK_URL, contentType: "application/json", headers: { 'X-BDK-EDIT-TOKEN': tokenId }, success: (response) => { console.info(objectName + " Cancel editing has been done successfully"); callback(response); }, error: (error) => { console.error("An error has been happened while " + objectName + " cancel edititng action: " + error); if (failureCallback) { failureCallback(error.responseJSON); } } }); } }