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
This commit is contained in:
Will Abson
2016-09-08 01:44:50 +01:00
parent 3463481829
commit 25f475d742
7 changed files with 101 additions and 75 deletions

View File

@@ -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);
});

View File

@@ -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 {

View File

@@ -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];

View File

@@ -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());
});

View File

@@ -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());
});

View File

@@ -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<ExternalContentLink> {
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<any> {
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);
}
}

View File

@@ -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<ExternalContentLink> {
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;