From 25f475d742255458eaeb7ff2ee7d0c049a3e3159 Mon Sep 17 00:00:00 2001 From: Will Abson Date: Thu, 8 Sep 2016 01:44:50 +0100 Subject: [PATCH] Use JS API to fetch node info and set related attached content - Add new service - Remove old HTTP-based methods from FormService - Refactoring Refs #621 --- .../activiti-form.component.spec.ts | 2 +- .../src/components/activiti-form.component.ts | 3 +- .../widgets/attach/attach.widget.ts | 8 +- .../widgets/dropdown/dropdown.widget.spec.ts | 2 +- .../typeahead/typeahead.widget.spec.ts | 2 +- .../src/services/activiti-alfresco.service.ts | 90 +++++++++++++++++++ .../src/services/form.service.ts | 69 +------------- 7 files changed, 101 insertions(+), 75 deletions(-) create mode 100644 ng2-components/ng2-activiti-form/src/services/activiti-alfresco.service.ts diff --git a/ng2-components/ng2-activiti-form/src/components/activiti-form.component.spec.ts b/ng2-components/ng2-activiti-form/src/components/activiti-form.component.spec.ts index 6602497019..37204d11d6 100644 --- a/ng2-components/ng2-activiti-form/src/components/activiti-form.component.spec.ts +++ b/ng2-components/ng2-activiti-form/src/components/activiti-form.component.spec.ts @@ -40,7 +40,7 @@ describe('ActivitiForm', () => { ]); window['componentHandler'] = componentHandler; - formService = new FormService(null, null, null, null); + formService = new FormService(null, null); formComponent = new ActivitiForm(formService, visibilityService, null, null); }); diff --git a/ng2-components/ng2-activiti-form/src/components/activiti-form.component.ts b/ng2-components/ng2-activiti-form/src/components/activiti-form.component.ts index 2c5151d877..5ef0780417 100644 --- a/ng2-components/ng2-activiti-form/src/components/activiti-form.component.ts +++ b/ng2-components/ng2-activiti-form/src/components/activiti-form.component.ts @@ -26,6 +26,7 @@ import { import { MATERIAL_DESIGN_DIRECTIVES } from 'ng2-alfresco-core'; import { EcmModelService } from './../services/ecm-model.service'; import { FormService } from './../services/form.service'; +import { ActivitiAlfrescoContentService } from './../services/activiti-alfresco.service'; import { NodeService } from './../services/node.service'; import { FormModel, FormOutcomeModel, FormValues, FormFieldModel, FormOutcomeEvent } from './widgets/core/index'; @@ -85,7 +86,7 @@ import { WidgetVisibilityService } from './../services/widget-visibility.servic templateUrl: './activiti-form.component.html', styleUrls: ['./activiti-form.component.css'], directives: [MATERIAL_DESIGN_DIRECTIVES, ContainerWidget, TabsWidget], - providers: [EcmModelService, FormService, WidgetVisibilityService, NodeService] + providers: [EcmModelService, FormService, ActivitiAlfrescoContentService, WidgetVisibilityService, NodeService] }) export class ActivitiForm implements OnInit, AfterViewChecked, OnChanges { diff --git a/ng2-components/ng2-activiti-form/src/components/widgets/attach/attach.widget.ts b/ng2-components/ng2-activiti-form/src/components/widgets/attach/attach.widget.ts index 2477a5ad94..fbb2293625 100644 --- a/ng2-components/ng2-activiti-form/src/components/widgets/attach/attach.widget.ts +++ b/ng2-components/ng2-activiti-form/src/components/widgets/attach/attach.widget.ts @@ -17,7 +17,7 @@ import { Component, OnInit, Input, Output, EventEmitter, ViewChild } from '@angular/core'; import { WidgetComponent } from './../widget.component'; -import { FormService } from '../../../services/form.service'; +import { ActivitiAlfrescoContentService } from '../../../services/activiti-alfresco.service'; import { ExternalContent } from '../core/external-content'; import { ExternalContentLink } from '../core/external-content-link'; import { FormFieldModel } from '../core/form-field.model'; @@ -51,7 +51,7 @@ export class AttachWidget extends WidgetComponent implements OnInit { @ViewChild('dialog') dialog: any; - constructor(private formService: FormService) { + constructor(private contentService: ActivitiAlfrescoContentService) { super(); } @@ -94,7 +94,7 @@ export class AttachWidget extends WidgetComponent implements OnInit { private getExternalContentNodes() { - this.formService.getAlfrescoNodes(this.selectedFolderAccountId, this.selectedFolderPathId) + this.contentService.getAlfrescoNodes(this.selectedFolderAccountId, this.selectedFolderPathId) .subscribe( (nodes) => { this.selectedFolderNodes = nodes; @@ -103,7 +103,7 @@ export class AttachWidget extends WidgetComponent implements OnInit { } selectFile(node: ExternalContent, $event: any) { - this.formService.linkAlfrescoNode(this.selectedFolderAccountId, node, this.selectedFolderSiteId).subscribe( + this.contentService.linkAlfrescoNode(this.selectedFolderAccountId, node, this.selectedFolderSiteId).subscribe( (link: ExternalContentLink) => { this.selectedFile = node; this.field.value = [link]; diff --git a/ng2-components/ng2-activiti-form/src/components/widgets/dropdown/dropdown.widget.spec.ts b/ng2-components/ng2-activiti-form/src/components/widgets/dropdown/dropdown.widget.spec.ts index fd80f8dc23..292ffd54dc 100644 --- a/ng2-components/ng2-activiti-form/src/components/widgets/dropdown/dropdown.widget.spec.ts +++ b/ng2-components/ng2-activiti-form/src/components/widgets/dropdown/dropdown.widget.spec.ts @@ -28,7 +28,7 @@ describe('DropdownWidget', () => { let widget: DropdownWidget; beforeEach(() => { - formService = new FormService(null, null, null, null); + formService = new FormService(null, null); widget = new DropdownWidget(formService); widget.field = new FormFieldModel(new FormModel()); }); diff --git a/ng2-components/ng2-activiti-form/src/components/widgets/typeahead/typeahead.widget.spec.ts b/ng2-components/ng2-activiti-form/src/components/widgets/typeahead/typeahead.widget.spec.ts index fac9461a8b..5dbfdd09eb 100644 --- a/ng2-components/ng2-activiti-form/src/components/widgets/typeahead/typeahead.widget.spec.ts +++ b/ng2-components/ng2-activiti-form/src/components/widgets/typeahead/typeahead.widget.spec.ts @@ -29,7 +29,7 @@ describe('TypeaheadWidget', () => { let widget: TypeaheadWidget; beforeEach(() => { - formService = new FormService(null, null, null, null); + formService = new FormService(null, null); widget = new TypeaheadWidget(formService); widget.field = new FormFieldModel(new FormModel()); }); diff --git a/ng2-components/ng2-activiti-form/src/services/activiti-alfresco.service.ts b/ng2-components/ng2-activiti-form/src/services/activiti-alfresco.service.ts new file mode 100644 index 0000000000..dec92c19e3 --- /dev/null +++ b/ng2-components/ng2-activiti-form/src/services/activiti-alfresco.service.ts @@ -0,0 +1,90 @@ +/*! + * @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 { Observable } from 'rxjs/Rx'; +import { AlfrescoAuthenticationService } from 'ng2-alfresco-core'; +import { ExternalContent } from '../components/widgets/core/external-content'; +import { ExternalContentLink } from '../components/widgets/core/external-content-link'; + +@Injectable() +export class ActivitiAlfrescoContentService { + + static UNKNOWN_ERROR_MESSAGE: string = 'Unknown error'; + static GENERIC_ERROR_MESSAGE: string = 'Server error'; + + constructor(private authService: AlfrescoAuthenticationService) { + } + + /** + * Returns a list of child nodes below the specified folder + * + * @param accountId + * @param folderId + * @returns {null} + */ + getAlfrescoNodes(accountId: string, folderId: string): Observable<[ExternalContent]> { + let apiService: any = this.authService.getAlfrescoApi(); + let accountShortId = accountId.replace('alfresco-', ''); + return Observable.fromPromise(apiService.activiti.alfrescoApi.getContentInFolder(accountShortId, folderId)) + .map(this.toJsonArray) + .catch(this.handleError); + } + + /** + * Returns a list of child nodes below the specified folder + * + * @param accountId + * @param node + * @param siteId + * @returns {null} + */ + linkAlfrescoNode(accountId: string, node: ExternalContent, siteId: string): Observable { + let apiService: any = this.authService.getAlfrescoApi(); + return Observable.fromPromise(apiService.activiti.contentApi.createTemporaryRelatedContent({ + link: true, + name: node.title, + simpleType: node.simpleType, + source: accountId, + sourceId: node.id + '@' + siteId + })).map(this.toJson).catch(this.handleError); + } + + toJson(res: any) { + if (res) { + return res || {}; + } + return {}; + } + + toJsonArray(res: any) { + if (res) { + return res.data || []; + } + return []; + } + + handleError(error: any): Observable { + let errMsg = ActivitiAlfrescoContentService.UNKNOWN_ERROR_MESSAGE; + if (error) { + errMsg = (error.message) ? error.message : + error.status ? `${error.status} - ${error.statusText}` : ActivitiAlfrescoContentService.GENERIC_ERROR_MESSAGE; + } + console.error(errMsg); + return Observable.throw(errMsg); + } +} diff --git a/ng2-components/ng2-activiti-form/src/services/form.service.ts b/ng2-components/ng2-activiti-form/src/services/form.service.ts index a7164fc307..e04d3f8d93 100644 --- a/ng2-components/ng2-activiti-form/src/services/form.service.ts +++ b/ng2-components/ng2-activiti-form/src/services/form.service.ts @@ -18,16 +18,13 @@ import { Injectable } from '@angular/core'; import { Observable } from 'rxjs/Rx'; import 'rxjs/add/operator/map'; -import { AlfrescoApiService, AlfrescoSettingsService } from 'ng2-alfresco-core'; +import { AlfrescoApiService } from 'ng2-alfresco-core'; import { FormValues } from './../components/widgets/core/index'; import { FormDefinitionModel } from '../models/form-definition.model'; import { EcmModelService } from './ecm-model.service'; import { GroupModel } from './../components/widgets/core/group.model'; import { GroupUserModel } from './../components/widgets/core/group-user.model'; -import { ExternalContent } from '../components/widgets/core/external-content'; -import { ExternalContentLink } from '../components/widgets/core/external-content-link'; -import {Http, Response, RequestOptions, Headers} from '@angular/http'; @Injectable() export class FormService { @@ -35,8 +32,7 @@ export class FormService { static GENERIC_ERROR_MESSAGE: string = 'Server error'; constructor(private ecmModelService: EcmModelService, - private apiService: AlfrescoApiService, - private alfrescoSettingsService: AlfrescoSettingsService, private http: Http) { + private apiService: AlfrescoApiService) { } /** @@ -278,67 +274,6 @@ export class FormService { }); } - /** - * Returns a list of child nodes below the specified folder - * - * @param accountId - * @param nodeId - * @returns {null} - */ - getAlfrescoNodes(accountId: string, nodeId: string): Observable<[ExternalContent]> { - let accountPath = accountId.replace('-', '/'); - let headers = new Headers(); - headers.append('Authorization', this.authService.getTicketBpm()); - return this.http.get( - `${this.alfrescoSettingsService.bpmHost}/activiti-app/` + - `app/rest/integration/${accountPath}/folders/${nodeId}/content`, - new RequestOptions({ - headers: headers, - withCredentials: true - })) - .map(this.extractData) - .catch(this.handleError); - } - - /** - * Returns a list of child nodes below the specified folder - * - * @param accountId - * @param node - * @param siteId - * @returns {null} - */ - linkAlfrescoNode(accountId: string, node: ExternalContent, siteId: string): Observable { - let headers = new Headers(); - headers.append('Content-Type', 'application/json'); - headers.append('Authorization', this.authService.getTicketBpm()); - return this.http.post( - `${this.alfrescoSettingsService.bpmHost}/activiti-app/app/rest/content`, - JSON.stringify({ - link: true, - name: node.title, - simpleType: node.simpleType, - source: accountId, - sourceId: node.id + '@' + siteId - }), - new RequestOptions({ - headers: headers, - withCredentials: true - })) - .map(this.extractBody) - .catch(this.handleError); - } - - private extractBody(res: Response) { - let body = res.json(); - return body || { }; - } - - private extractData(res: Response) { - let body = res.json(); - return body.data || { }; - } - getFormId(res: any) { let result = null;