From 03933f4c04bbc6c173ae3f1eb0948ae08cea121e Mon Sep 17 00:00:00 2001 From: Eugenio Romano Date: Tue, 6 Sep 2016 18:09:06 +0200 Subject: [PATCH 1/4] start renditions service --- .../src/services/renditions.service.spec.ts | 51 +++++++++++++++++++ .../src/services/renditions.service.ts | 36 +++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 ng2-components/ng2-alfresco-upload/src/services/renditions.service.spec.ts create mode 100644 ng2-components/ng2-alfresco-upload/src/services/renditions.service.ts diff --git a/ng2-components/ng2-alfresco-upload/src/services/renditions.service.spec.ts b/ng2-components/ng2-alfresco-upload/src/services/renditions.service.spec.ts new file mode 100644 index 0000000000..be00030f2d --- /dev/null +++ b/ng2-components/ng2-alfresco-upload/src/services/renditions.service.spec.ts @@ -0,0 +1,51 @@ +/*! + * @license + * Copyright 2016 Alfresco Software, Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { it, describe, inject, beforeEach, beforeEachProviders } from '@angular/core/testing'; +import { EventEmitter } from '@angular/core'; +import { RenditionsService } from './renditions.service'; +import { AlfrescoSettingsService, AlfrescoApiService, AlfrescoAuthenticationService } from 'ng2-alfresco-core'; + +declare let AlfrescoApi: any; +declare let jasmine: any; + +describe('RenditionsService', () => { + let service: any; + + beforeEachProviders(() => { + return [ + AlfrescoSettingsService, + AlfrescoApiService, + AlfrescoAuthenticationService, + RenditionsService + ]; + }); + + beforeEach( inject([RenditionsService, AlfrescoApiService], (renditionsService: RenditionsService, apiService: AlfrescoApiService) => { + jasmine.Ajax.install(); + service = renditionsService; + apiService.setInstance(new AlfrescoApi({})); + })); + + afterEach(() => { + jasmine.Ajax.uninstall(); + }); + + it('', (done) => { + + }); +}); diff --git a/ng2-components/ng2-alfresco-upload/src/services/renditions.service.ts b/ng2-components/ng2-alfresco-upload/src/services/renditions.service.ts new file mode 100644 index 0000000000..7359e0a054 --- /dev/null +++ b/ng2-components/ng2-alfresco-upload/src/services/renditions.service.ts @@ -0,0 +1,36 @@ +/*! + * @license + * Copyright 2016 Alfresco Software, Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { Injectable } from '@angular/core'; +import { AlfrescoApiService } from 'ng2-alfresco-core'; + +/** + * RenditionsService + * + * @returns {RenditionsService} . + */ +@Injectable() +export class RenditionsService { + + constructor(private apiService: AlfrescoApiService) { + + } + + private getRenditionsByNodeId(nodeId: string) { + + } +} From f64883cc4bbaece307780db55832ce820c01f696 Mon Sep 17 00:00:00 2001 From: Eugenio Romano Date: Wed, 7 Sep 2016 11:25:35 +0200 Subject: [PATCH 2/4] get and create renditions --- .../src/services/renditions.service.spec.ts | 39 +++++++++++++++++-- .../src/services/renditions.service.ts | 14 ++++++- 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/ng2-components/ng2-alfresco-upload/src/services/renditions.service.spec.ts b/ng2-components/ng2-alfresco-upload/src/services/renditions.service.spec.ts index be00030f2d..e7c9529708 100644 --- a/ng2-components/ng2-alfresco-upload/src/services/renditions.service.spec.ts +++ b/ng2-components/ng2-alfresco-upload/src/services/renditions.service.spec.ts @@ -16,7 +16,6 @@ */ import { it, describe, inject, beforeEach, beforeEachProviders } from '@angular/core/testing'; -import { EventEmitter } from '@angular/core'; import { RenditionsService } from './renditions.service'; import { AlfrescoSettingsService, AlfrescoApiService, AlfrescoAuthenticationService } from 'ng2-alfresco-core'; @@ -35,7 +34,7 @@ describe('RenditionsService', () => { ]; }); - beforeEach( inject([RenditionsService, AlfrescoApiService], (renditionsService: RenditionsService, apiService: AlfrescoApiService) => { + beforeEach(inject([RenditionsService, AlfrescoApiService], (renditionsService: RenditionsService, apiService: AlfrescoApiService) => { jasmine.Ajax.install(); service = renditionsService; apiService.setInstance(new AlfrescoApi({})); @@ -45,7 +44,41 @@ describe('RenditionsService', () => { jasmine.Ajax.uninstall(); }); - it('', (done) => { + it('Get redition service should call the server with the ID passed', (done) => { + service.getRenditionsByNodeId('fake-node-id').subscribe((res) => { + expect(jasmine.Ajax.requests.mostRecent().url).toBe('http://127.0.0.1:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/fake-node-id/renditions'); + done(); + }); + jasmine.Ajax.requests.mostRecent().respondWith({ + 'status': 200, + contentType: 'application/json' + }); + }); + + it('Create redition service should call the server with the ID passed and the asked encoding', (done) => { + service.createRendition('fake-node-id', 'pdf').subscribe((res) => { + expect(jasmine.Ajax.requests.mostRecent().url).toBe('http://127.0.0.1:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/fake-node-id/renditions'); + done(); + }); + + jasmine.Ajax.requests.mostRecent().respondWith({ + 'status': 200, + contentType: 'application/json' + }); + }); + + it('Get redition service should catch the error', (done) => { + + service.getRenditionsByNodeId('fake-node-id').subscribe((res) => { + }, (res) => { + done(); + } + ); + + jasmine.Ajax.requests.mostRecent().respondWith({ + 'status': 403, + contentType: 'application/json' + }); }); }); diff --git a/ng2-components/ng2-alfresco-upload/src/services/renditions.service.ts b/ng2-components/ng2-alfresco-upload/src/services/renditions.service.ts index 7359e0a054..f94b64bd29 100644 --- a/ng2-components/ng2-alfresco-upload/src/services/renditions.service.ts +++ b/ng2-components/ng2-alfresco-upload/src/services/renditions.service.ts @@ -16,6 +16,7 @@ */ import { Injectable } from '@angular/core'; +import { Observable } from 'rxjs/Rx'; import { AlfrescoApiService } from 'ng2-alfresco-core'; /** @@ -30,7 +31,18 @@ export class RenditionsService { } - private getRenditionsByNodeId(nodeId: string) { + getRenditionsByNodeId(nodeId: string) { + return Observable.fromPromise(this.apiService.getInstance().core.renditionsApi.getRenditions(nodeId)) + .catch(this.handleError); + } + createRendition(nodeId: string, encoding: string) { + return Observable.fromPromise(this.apiService.getInstance().core.renditionsApi.createRendition(nodeId, encoding)) + .catch(this.handleError); + } + + private handleError(error: any): Observable { + console.error(error); + return Observable.throw(error || 'Server error'); } } From 8c76aed11d03f5172ab688f4e1d87664aa828278 Mon Sep 17 00:00:00 2001 From: Eugenio Romano Date: Wed, 7 Sep 2016 12:47:47 +0200 Subject: [PATCH 3/4] move renditions service in the core --- .../ng2-alfresco-core/src/services/index.ts | 1 + .../src/services/renditions.service.spec.ts | 69 ++++++++++++++++--- .../src/services/renditions.service.ts | 10 ++- 3 files changed, 70 insertions(+), 10 deletions(-) rename ng2-components/{ng2-alfresco-upload => ng2-alfresco-core}/src/services/renditions.service.spec.ts (50%) rename ng2-components/{ng2-alfresco-upload => ng2-alfresco-core}/src/services/renditions.service.ts (81%) diff --git a/ng2-components/ng2-alfresco-core/src/services/index.ts b/ng2-components/ng2-alfresco-core/src/services/index.ts index b4d963d6e1..c5eb956dc8 100644 --- a/ng2-components/ng2-alfresco-core/src/services/index.ts +++ b/ng2-components/ng2-alfresco-core/src/services/index.ts @@ -22,3 +22,4 @@ export * from './AlfrescoTranslation.service'; export * from './AlfrescoPipeTranslate.service'; export * from './AlfrescoAuthentication.service'; export * from './AlfrescoContent.service'; +export * from './renditions.service'; diff --git a/ng2-components/ng2-alfresco-upload/src/services/renditions.service.spec.ts b/ng2-components/ng2-alfresco-core/src/services/renditions.service.spec.ts similarity index 50% rename from ng2-components/ng2-alfresco-upload/src/services/renditions.service.spec.ts rename to ng2-components/ng2-alfresco-core/src/services/renditions.service.spec.ts index e7c9529708..750ef256d1 100644 --- a/ng2-components/ng2-alfresco-upload/src/services/renditions.service.spec.ts +++ b/ng2-components/ng2-alfresco-core/src/services/renditions.service.spec.ts @@ -17,7 +17,9 @@ import { it, describe, inject, beforeEach, beforeEachProviders } from '@angular/core/testing'; import { RenditionsService } from './renditions.service'; -import { AlfrescoSettingsService, AlfrescoApiService, AlfrescoAuthenticationService } from 'ng2-alfresco-core'; +import { AlfrescoApiService } from './AlfrescoApi.service'; +import { AlfrescoSettingsService } from './AlfrescoSettings.service'; +import { AlfrescoAuthenticationService } from './AlfrescoAuthentication.service'; declare let AlfrescoApi: any; declare let jasmine: any; @@ -25,6 +27,55 @@ declare let jasmine: any; describe('RenditionsService', () => { let service: any; + let fakeRedition = { + 'list': { + 'pagination': { + 'count': 6, + 'hasMoreItems': false, + 'totalItems': 6, + 'skipCount': 0, + 'maxItems': 100 + }, + 'entries': [{ + 'entry': { + 'id': 'avatar', + 'content': {'mimeType': 'image/png', 'mimeTypeName': 'PNG Image'}, + 'status': 'NOT_CREATED' + } + }, { + 'entry': { + 'id': 'avatar32', + 'content': {'mimeType': 'image/png', 'mimeTypeName': 'PNG Image'}, + 'status': 'NOT_CREATED' + } + }, { + 'entry': { + 'id': 'doclib', + 'content': {'mimeType': 'image/png', 'mimeTypeName': 'PNG Image'}, + 'status': 'NOT_CREATED' + } + }, { + 'entry': { + 'id': 'imgpreview', + 'content': {'mimeType': 'image/jpeg', 'mimeTypeName': 'JPEG Image'}, + 'status': 'NOT_CREATED' + } + }, { + 'entry': { + 'id': 'medium', + 'content': {'mimeType': 'image/jpeg', 'mimeTypeName': 'JPEG Image'}, + 'status': 'NOT_CREATED' + } + }, { + 'entry': { + 'id': 'pdf', + 'content': {'mimeType': 'application/pdf', 'mimeTypeName': 'Adobe PDF Document'}, + 'status': 'NOT_CREATED' + } + }] + } + }; + beforeEachProviders(() => { return [ AlfrescoSettingsService, @@ -44,15 +95,16 @@ describe('RenditionsService', () => { jasmine.Ajax.uninstall(); }); - it('Get redition service should call the server with the ID passed', (done) => { - service.getRenditionsByNodeId('fake-node-id').subscribe((res) => { + it('Get redition list service should call the server with the ID passed', (done) => { + service.getRenditionsListByNodeId('fake-node-id').subscribe((res) => { expect(jasmine.Ajax.requests.mostRecent().url).toBe('http://127.0.0.1:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/fake-node-id/renditions'); done(); }); jasmine.Ajax.requests.mostRecent().respondWith({ 'status': 200, - contentType: 'application/json' + contentType: 'application/json', + responseText: JSON.stringify(fakeRedition) }); }); @@ -64,13 +116,13 @@ describe('RenditionsService', () => { jasmine.Ajax.requests.mostRecent().respondWith({ 'status': 200, - contentType: 'application/json' + contentType: 'application/json', + responseText: '' }); }); it('Get redition service should catch the error', (done) => { - - service.getRenditionsByNodeId('fake-node-id').subscribe((res) => { + service.getRenditionsListByNodeId('fake-node-id').subscribe((res) => { }, (res) => { done(); } @@ -78,7 +130,8 @@ describe('RenditionsService', () => { jasmine.Ajax.requests.mostRecent().respondWith({ 'status': 403, - contentType: 'application/json' + contentType: 'application/json', + responseText: JSON.stringify(fakeRedition) }); }); }); diff --git a/ng2-components/ng2-alfresco-upload/src/services/renditions.service.ts b/ng2-components/ng2-alfresco-core/src/services/renditions.service.ts similarity index 81% rename from ng2-components/ng2-alfresco-upload/src/services/renditions.service.ts rename to ng2-components/ng2-alfresco-core/src/services/renditions.service.ts index f94b64bd29..f8aea82cb6 100644 --- a/ng2-components/ng2-alfresco-upload/src/services/renditions.service.ts +++ b/ng2-components/ng2-alfresco-core/src/services/renditions.service.ts @@ -17,7 +17,7 @@ import { Injectable } from '@angular/core'; import { Observable } from 'rxjs/Rx'; -import { AlfrescoApiService } from 'ng2-alfresco-core'; +import { AlfrescoApiService } from './AlfrescoApi.service'; /** * RenditionsService @@ -31,7 +31,13 @@ export class RenditionsService { } - getRenditionsByNodeId(nodeId: string) { + isReditionsAvailable(nodeId: string, encoding: string) { + this.apiService.getInstance().core.renditionsApi.getRenditions(nodeId).then((res) => { + console.log('res' + res); + }); + } + + getRenditionsListByNodeId(nodeId: string) { return Observable.fromPromise(this.apiService.getInstance().core.renditionsApi.getRenditions(nodeId)) .catch(this.handleError); } From 0c60ff168d7813a1c6e6a9039a1ef13c07ffbb3d Mon Sep 17 00:00:00 2001 From: Eugenio Romano Date: Wed, 7 Sep 2016 14:27:12 +0200 Subject: [PATCH 4/4] renditions available methods --- ng2-components/ng2-alfresco-core/README.md | 6 +++ .../src/services/renditions.service.spec.ts | 39 ++++++++++++++++++- .../src/services/renditions.service.ts | 20 ++++++++-- 3 files changed, 60 insertions(+), 5 deletions(-) diff --git a/ng2-components/ng2-alfresco-core/README.md b/ng2-components/ng2-alfresco-core/README.md index 078df01b91..ca92172b1f 100644 --- a/ng2-components/ng2-alfresco-core/README.md +++ b/ng2-components/ng2-alfresco-core/README.md @@ -154,6 +154,12 @@ bootstrap(MyDemoApp, [ ``` +#### Renditions Service + +* getRenditionsListByNodeId(nodeId: string) +* createRendition(nodeId: string, encoding: string) +* getRendition(nodeId: string, encoding: string) +* isRenditionAvailable(nodeId: string, encoding: string) ## Build from sources diff --git a/ng2-components/ng2-alfresco-core/src/services/renditions.service.spec.ts b/ng2-components/ng2-alfresco-core/src/services/renditions.service.spec.ts index 750ef256d1..fafae1bdb8 100644 --- a/ng2-components/ng2-alfresco-core/src/services/renditions.service.spec.ts +++ b/ng2-components/ng2-alfresco-core/src/services/renditions.service.spec.ts @@ -28,6 +28,14 @@ describe('RenditionsService', () => { let service: any; let fakeRedition = { + 'entry': { + 'id': 'pdf', + 'content': {'mimeType': 'application/pdf', 'mimeTypeName': 'Adobe PDF Document'}, + 'status': 'NOT_CREATED' + } + }; + + let fakeReditionsList = { 'list': { 'pagination': { 'count': 6, @@ -95,12 +103,41 @@ describe('RenditionsService', () => { jasmine.Ajax.uninstall(); }); + it('Get redition list service should call the server with the ID passed', (done) => { service.getRenditionsListByNodeId('fake-node-id').subscribe((res) => { expect(jasmine.Ajax.requests.mostRecent().url).toBe('http://127.0.0.1:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/fake-node-id/renditions'); done(); }); + jasmine.Ajax.requests.mostRecent().respondWith({ + 'status': 200, + contentType: 'application/json', + responseText: JSON.stringify(fakeReditionsList) + }); + }); + + it('Get redition service should call the server with the ID passed', (done) => { + service.getRendition('fake-node-id', 'pdf').subscribe((res) => { + expect(jasmine.Ajax.requests.mostRecent().url).toBe('http://127.0.0.1:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/fake-node-id/renditions/pdf'); + expect(res.entry.status).toBe('NOT_CREATED'); + done(); + }); + + jasmine.Ajax.requests.mostRecent().respondWith({ + 'status': 200, + contentType: 'application/json', + responseText: JSON.stringify(fakeRedition) + }); + }); + + it('isRenditionsAvailable service should call the server with the ID passed and return false if is not created', (done) => { + service.isRenditionAvailable('fake-node-id', 'pdf').subscribe((res) => { + expect(jasmine.Ajax.requests.mostRecent().url).toBe('http://127.0.0.1:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/fake-node-id/renditions/pdf'); + expect(res).toBe(false); + done(); + }); + jasmine.Ajax.requests.mostRecent().respondWith({ 'status': 200, contentType: 'application/json', @@ -131,7 +168,7 @@ describe('RenditionsService', () => { jasmine.Ajax.requests.mostRecent().respondWith({ 'status': 403, contentType: 'application/json', - responseText: JSON.stringify(fakeRedition) + responseText: 'error' }); }); }); diff --git a/ng2-components/ng2-alfresco-core/src/services/renditions.service.ts b/ng2-components/ng2-alfresco-core/src/services/renditions.service.ts index f8aea82cb6..3960c5948d 100644 --- a/ng2-components/ng2-alfresco-core/src/services/renditions.service.ts +++ b/ng2-components/ng2-alfresco-core/src/services/renditions.service.ts @@ -31,19 +31,31 @@ export class RenditionsService { } - isReditionsAvailable(nodeId: string, encoding: string) { - this.apiService.getInstance().core.renditionsApi.getRenditions(nodeId).then((res) => { - console.log('res' + res); + isRenditionAvailable(nodeId: string, encoding: string) { + return Observable.create((observer) => { + this.getRendition(nodeId, encoding).subscribe((res) => { + let isAvailable = true; + if (res.entry.status === 'NOT_CREATED') { + isAvailable = false; + } + observer.next(isAvailable); + observer.complete(); + }); }); } + getRendition(nodeId: string, encoding: string) { + return Observable.fromPromise(this.apiService.getInstance().core.renditionsApi.getRendition(nodeId, encoding)) + .catch(this.handleError); + } + getRenditionsListByNodeId(nodeId: string) { return Observable.fromPromise(this.apiService.getInstance().core.renditionsApi.getRenditions(nodeId)) .catch(this.handleError); } createRendition(nodeId: string, encoding: string) { - return Observable.fromPromise(this.apiService.getInstance().core.renditionsApi.createRendition(nodeId, encoding)) + return Observable.fromPromise(this.apiService.getInstance().core.renditionsApi.createRendition(nodeId, {id: 'pdf'})) .catch(this.handleError); }