Merge pull request #1225 from Alfresco/dev-mvitale-992

Move the Report API inside the js API
This commit is contained in:
Mario Romano 2016-12-14 10:23:53 +00:00 committed by GitHub
commit e357e92f29
4 changed files with 72 additions and 119 deletions

View File

@ -82,8 +82,26 @@ describe('Test ng2-activiti-analytics Report list', () => {
}); });
it('should return the default reports when the report list is empty', (done) => { it('should return the default reports when the report list is empty', (done) => {
jasmine.Ajax.stubRequest('http://localhost:9999/activiti-app/app/rest/reporting/reports').andReturn({
status: 200,
contentType: 'json',
responseText: []
});
fixture.detectChanges(); fixture.detectChanges();
jasmine.Ajax.stubRequest('http://localhost:9999/activiti-app/app/rest/reporting/default-reports').andReturn({
status: 200,
contentType: 'json',
responseText: []
});
jasmine.Ajax.stubRequest('http://localhost:9999/activiti-app/app/rest/reporting/reports').andReturn({
status: 200,
contentType: 'json',
responseText: reportList
});
component.onSuccess.subscribe(() => { component.onSuccess.subscribe(() => {
fixture.detectChanges(); fixture.detectChanges();
expect(element.querySelector('#report-list-0 > i').innerHTML).toBe('assignment'); expect(element.querySelector('#report-list-0 > i').innerHTML).toBe('assignment');
@ -96,23 +114,6 @@ describe('Test ng2-activiti-analytics Report list', () => {
done(); done();
}); });
jasmine.Ajax.requests.mostRecent().respondWith({
status: 200,
contentType: 'json',
responseText: []
});
jasmine.Ajax.requests.mostRecent().respondWith({
status: 200,
contentType: 'json',
responseText: []
});
jasmine.Ajax.requests.mostRecent().respondWith({
status: 200,
contentType: 'json',
responseText: reportList
});
}); });
it('Report render the report list relative to a single app', (done) => { it('Report render the report list relative to a single app', (done) => {

View File

@ -280,21 +280,22 @@ describe('Test ng2-analytics-report-parameters Report Parameters ', () => {
done(); done();
}); });
let reportId = 1; jasmine.Ajax.stubRequest('http://localhost:9999/activiti-app/app/rest/reporting/report-params/1').andReturn({
let change = new SimpleChange(null, reportId);
component.ngOnChanges({ 'reportId': change });
jasmine.Ajax.requests.first().respondWith({
status: 200, status: 200,
contentType: 'json', contentType: 'json',
responseText: analyticParamsMock.reportDefParamProcessDef responseText: analyticParamsMock.reportDefParamProcessDef
}); });
jasmine.Ajax.requests.mostRecent().respondWith({ jasmine.Ajax.stubRequest('http://localhost:9999/activiti-app/app/rest/reporting/process-definitions').andReturn({
status: 200, status: 200,
contentType: 'json', contentType: 'json',
responseText: analyticParamsMock.reportDefParamProcessDefOptionsNoApp responseText: analyticParamsMock.reportDefParamProcessDefOptionsNoApp
}); });
let reportId = 1;
let change = new SimpleChange(null, reportId);
component.ngOnChanges({ 'reportId': change });
}); });
it('Should render a dropdown with all the process definition when the definition parameter type is \'processDefinition\' and the' + it('Should render a dropdown with all the process definition when the definition parameter type is \'processDefinition\' and the' +
@ -310,22 +311,24 @@ describe('Test ng2-analytics-report-parameters Report Parameters ', () => {
done(); done();
}); });
let appId = 1; jasmine.Ajax.stubRequest('http://localhost:9999/activiti-app/app/rest/reporting/report-params/1').andReturn({
component.appId = appId;
let change = new SimpleChange(null, appId);
component.ngOnChanges({ 'appId': change });
jasmine.Ajax.requests.first().respondWith({
status: 200, status: 200,
contentType: 'json', contentType: 'json',
responseText: analyticParamsMock.reportDefParamProcessDef responseText: analyticParamsMock.reportDefParamProcessDef
}); });
jasmine.Ajax.requests.mostRecent().respondWith({ jasmine.Ajax.stubRequest('http://localhost:9999/activiti-app/api/enterprise/process-definitions').andReturn({
status: 200, status: 200,
contentType: 'json', contentType: 'json',
responseText: analyticParamsMock.reportDefParamProcessDefOptionsApp responseText: analyticParamsMock.reportDefParamProcessDefOptionsApp
}); });
let appId = 1;
component.appId = appId;
component.reportId = 1;
let change = new SimpleChange(null, appId);
component.ngOnChanges({ 'appId': change });
}); });
it('Should load the task list when a process definition is selected', () => { it('Should load the task list when a process definition is selected', () => {
@ -355,21 +358,22 @@ describe('Test ng2-analytics-report-parameters Report Parameters ', () => {
done(); done();
}); });
let reportId = 1; jasmine.Ajax.stubRequest('http://localhost:9999/activiti-app/app/rest/reporting/report-params/1').andReturn({
let change = new SimpleChange(null, reportId);
component.ngOnChanges({ 'reportId': change });
jasmine.Ajax.requests.first().respondWith({
status: 200, status: 200,
contentType: 'json', contentType: 'json',
responseText: analyticParamsMock.reportDefParamProcessDef responseText: analyticParamsMock.reportDefParamProcessDef
}); });
jasmine.Ajax.requests.mostRecent().respondWith({ jasmine.Ajax.stubRequest('http://localhost:9999/activiti-app/app/rest/reporting/process-definitions').andReturn({
status: 404, status: 404,
contentType: 'json', contentType: 'json',
responseText: [] responseText: []
}); });
let reportId = 1;
let change = new SimpleChange(null, reportId);
component.ngOnChanges({ 'reportId': change });
}); });
it('Should emit an error with a 404 response when the report parameters response is not found', (done) => { it('Should emit an error with a 404 response when the report parameters response is not found', (done) => {

View File

@ -190,6 +190,7 @@ describe('Test ng2-activiti-analytics Report ', () => {
}); });
let reportParamQuery = new ReportQuery({status: 'All'}); let reportParamQuery = new ReportQuery({status: 'All'});
component.reportId = 1;
component.showReport(reportParamQuery); component.showReport(reportParamQuery);
jasmine.Ajax.requests.mostRecent().respondWith({ jasmine.Ajax.requests.mostRecent().respondWith({
@ -214,6 +215,7 @@ describe('Test ng2-activiti-analytics Report ', () => {
}); });
let reportParamQuery = new ReportQuery({status: 'All'}); let reportParamQuery = new ReportQuery({status: 'All'});
component.reportId = 1;
component.showReport(reportParamQuery); component.showReport(reportParamQuery);
jasmine.Ajax.requests.mostRecent().respondWith({ jasmine.Ajax.requests.mostRecent().respondWith({

View File

@ -16,9 +16,9 @@
*/ */
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { AlfrescoAuthenticationService, AlfrescoSettingsService } from 'ng2-alfresco-core'; import { AlfrescoAuthenticationService, AlfrescoSettingsService, AlfrescoApiService } from 'ng2-alfresco-core';
import { Observable } from 'rxjs/Rx'; import { Observable } from 'rxjs/Rx';
import { Response, Http, Headers, RequestOptions, URLSearchParams } from '@angular/http'; import { Response } from '@angular/http';
import { ReportParametersModel, ParameterValueModel } from '../models/report.model'; import { ReportParametersModel, ParameterValueModel } from '../models/report.model';
import { Chart, PieChart, TableChart, BarChart, HeatMapChart, MultiBarChart } from '../models/chart.model'; import { Chart, PieChart, TableChart, BarChart, HeatMapChart, MultiBarChart } from '../models/chart.model';
@ -26,7 +26,7 @@ import { Chart, PieChart, TableChart, BarChart, HeatMapChart, MultiBarChart } fr
export class AnalyticsService { export class AnalyticsService {
constructor(private authService: AlfrescoAuthenticationService, constructor(private authService: AlfrescoAuthenticationService,
private http: Http, public apiService: AlfrescoApiService,
private alfrescoSettingsService: AlfrescoSettingsService) { private alfrescoSettingsService: AlfrescoSettingsService) {
} }
@ -35,14 +35,10 @@ export class AnalyticsService {
* @returns {Observable<any>} * @returns {Observable<any>}
*/ */
getReportList(): Observable<any> { getReportList(): Observable<any> {
let url = `${this.alfrescoSettingsService.getBPMApiBaseUrl()}/app/rest/reporting/reports`; return Observable.fromPromise(this.apiService.getInstance().activiti.reportApi.getReportList())
let options = this.getRequestOptions();
return this.http
.get(url, options)
.map((res: any) => { .map((res: any) => {
let reports: ReportParametersModel[] = []; let reports: ReportParametersModel[] = [];
let body = res.json(); res.forEach((report: ReportParametersModel) => {
body.forEach((report: ReportParametersModel) => {
let reportModel = new ReportParametersModel(report); let reportModel = new ReportParametersModel(report);
reports.push(reportModel); reports.push(reportModel);
}); });
@ -51,13 +47,9 @@ export class AnalyticsService {
} }
getReportParams(reportId: string): Observable<any> { getReportParams(reportId: string): Observable<any> {
let url = `${this.alfrescoSettingsService.getBPMApiBaseUrl()}/app/rest/reporting/report-params/${reportId}`; return Observable.fromPromise(this.apiService.getInstance().activiti.reportApi.getReportParams(reportId))
let options = this.getRequestOptions();
return this.http
.get(url, options)
.map((res: any) => { .map((res: any) => {
let body = res.json(); return new ReportParametersModel(res);
return new ReportParametersModel(body);
}).catch(this.handleError); }).catch(this.handleError);
} }
@ -72,7 +64,7 @@ export class AnalyticsService {
} }
} else if (type === 'dateInterval') { } else if (type === 'dateInterval') {
return this.getDateIntervalValues(); return this.getDateIntervalValues();
} else if (type === 'task') { } else if (type === 'task' && reportId && processDefinitionId) {
return this.getTasksByProcessDefinitionId(reportId, processDefinitionId); return this.getTasksByProcessDefinitionId(reportId, processDefinitionId);
} else { } else {
return Observable.create(observer => { return Observable.create(observer => {
@ -124,14 +116,10 @@ export class AnalyticsService {
} }
getProcessDefinitionsValuesNoApp(): Observable<any> { getProcessDefinitionsValuesNoApp(): Observable<any> {
let url = `${this.alfrescoSettingsService.getBPMApiBaseUrl()}/app/rest/reporting/process-definitions`; return Observable.fromPromise(this.apiService.getInstance().activiti.reportApi.getProcessDefinitions())
let options = this.getRequestOptions();
return this.http
.get(url, options)
.map((res: any) => { .map((res: any) => {
let paramOptions: ParameterValueModel[] = []; let paramOptions: ParameterValueModel[] = [];
let body = res.json(); res.forEach((opt) => {
body.forEach((opt) => {
paramOptions.push(new ParameterValueModel(opt)); paramOptions.push(new ParameterValueModel(opt));
}); });
return paramOptions; return paramOptions;
@ -139,17 +127,10 @@ export class AnalyticsService {
} }
getProcessDefinitionsValues(appId: string): Observable<any> { getProcessDefinitionsValues(appId: string): Observable<any> {
let url = `${this.alfrescoSettingsService.getBPMApiBaseUrl()}/app/rest/process-definitions`; return Observable.fromPromise(this.apiService.getInstance().activiti.processDefinitionsApi.getProcessDefinitions(appId))
let params: URLSearchParams;
params = new URLSearchParams();
params.set('appDefinitionId', appId);
let options = this.getRequestOptions(params);
return this.http
.get(url, options)
.map((res: any) => { .map((res: any) => {
let paramOptions: ParameterValueModel[] = []; let paramOptions: ParameterValueModel[] = [];
let body = res.json(); res.data.forEach((opt) => {
body.data.forEach((opt) => {
paramOptions.push(new ParameterValueModel(opt)); paramOptions.push(new ParameterValueModel(opt));
}); });
return paramOptions; return paramOptions;
@ -157,42 +138,21 @@ export class AnalyticsService {
} }
getTasksByProcessDefinitionId(reportId: string, processDefinitionId: string): Observable<any> { getTasksByProcessDefinitionId(reportId: string, processDefinitionId: string): Observable<any> {
if (reportId && processDefinitionId) { return Observable.fromPromise(this.apiService.getInstance().activiti.reportApi.getTasksByProcessDefinitionId(reportId, processDefinitionId))
let url = `${this.alfrescoSettingsService.getBPMApiBaseUrl()}/app/rest/reporting/report-params/${reportId}/tasks`; .map((res: any) => {
let params: URLSearchParams; let paramOptions: ParameterValueModel[] = [];
if (processDefinitionId) { res.forEach((opt) => {
params = new URLSearchParams(); paramOptions.push(new ParameterValueModel({ id: opt, name: opt }));
params.set('processDefinitionId', processDefinitionId); });
} return paramOptions;
let options = this.getRequestOptions(params); }).catch(this.handleError);
return this.http
.get(url, options)
.map((res: any) => {
let paramOptions: ParameterValueModel[] = [];
let body = res.json();
body.forEach((opt) => {
paramOptions.push(new ParameterValueModel({ id: opt, name: opt }));
});
return paramOptions;
}).catch(this.handleError);
} else {
return Observable.create(observer => {
observer.next(null);
observer.complete();
});
}
} }
getReportsByParams(reportId: number, paramsQuery: any): Observable<any> { getReportsByParams(reportId: number, paramsQuery: any): Observable<any> {
let url = `${this.alfrescoSettingsService.getBPMApiBaseUrl()}/app/rest/reporting/report-params/${reportId}`; return Observable.fromPromise(this.apiService.getInstance().activiti.reportApi.getReportsByParams(reportId, paramsQuery))
let body = paramsQuery ? JSON.stringify(paramsQuery) : {};
let options = this.getRequestOptions();
return this.http
.post(url, body, options)
.map((res: any) => { .map((res: any) => {
let elements: Chart[] = []; let elements: Chart[] = [];
let bodyRes = res.json(); res.elements.forEach((chartData) => {
bodyRes.elements.forEach((chartData) => {
if (chartData.type === 'pieChart') { if (chartData.type === 'pieChart') {
elements.push(new PieChart(chartData)); elements.push(new PieChart(chartData));
} else if (chartData.type === 'table') { } else if (chartData.type === 'table') {
@ -213,31 +173,17 @@ export class AnalyticsService {
} }
public createDefaultReports(): Observable<any> { public createDefaultReports(): Observable<any> {
let url = `${this.alfrescoSettingsService.getBPMApiBaseUrl()}/app/rest/reporting/default-reports`; return Observable.fromPromise(this.apiService.getInstance().activiti.reportApi.createDefaultReports())
let options = this.getRequestOptions(); .map(this.toJson)
let body = {}; .catch(this.handleError);
return this.http
.post(url, body, options)
.map((res: any) => {
return res;
}).catch(this.handleError);
}
public getHeaders(): Headers {
return new Headers({
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': this.authService.getTicketBpm()
});
}
public getRequestOptions(param?: any): RequestOptions {
let headers = this.getHeaders();
return new RequestOptions({ headers: headers, withCredentials: true, search: param });
} }
private handleError(error: Response) { private handleError(error: Response) {
console.error(error); console.error(error);
return Observable.throw(error.json().error || 'Server error'); return Observable.throw(error.json().error || 'Server error');
} }
toJson(res: any) {
return res || {};
}
} }