moving in services

This commit is contained in:
Mario Romano 2016-08-24 11:54:29 +01:00
parent ac1a919abb
commit 4f29a8e315
6 changed files with 246 additions and 179 deletions

View File

@ -25,9 +25,9 @@ import {
} from '@angular/core';
import { MATERIAL_DESIGN_DIRECTIVES, AlfrescoAuthenticationService, AlfrescoSettingsService } from 'ng2-alfresco-core';
import { Observable } from 'rxjs/Rx';
import { Response, Http, Headers, RequestOptions } from '@angular/http';
import { EcmModelService } from './../services/ecm-model.service'
import { FormService } from './../services/form.service';
import { NodeService } from './../services/node.service'
import { FormModel, FormOutcomeModel, FormValues, FormFieldModel, FormOutcomeEvent } from './widgets/core/index';
import { TabsWidget } from './widgets/tabs/tabs.widget';
@ -74,7 +74,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: [FormService, WidgetVisibilityService]
providers: [FormService, WidgetVisibilityService, EcmModelService, NodeService]
})
export class ActivitiForm implements OnInit, AfterViewChecked, OnChanges {
@ -134,8 +134,8 @@ export class ActivitiForm implements OnInit, AfterViewChecked, OnChanges {
constructor(private formService: FormService,
private visibilityService: WidgetVisibilityService,
private authService: AlfrescoAuthenticationService,
private http: Http,
public alfrescoSettingsService: AlfrescoSettingsService) {
private ecmModelService: EcmModelService,
private nodeService: NodeService) {
}
hasForm(): boolean {
@ -378,32 +378,19 @@ export class ActivitiForm implements OnInit, AfterViewChecked, OnChanges {
}
private loadFormForEcmMetadata(): void {
let metadata = {};
let self = this;
this.authService.getAlfrescoApi().nodes.getNodeInfo(this.nodeId).then(function (data) {
if (data && data.properties) {
for (let key in data.properties) {
if (key) {
console.log(key + ' => ' + data.properties[key]);
metadata [key.split(':')[1]] = data.properties[key];
}
}
this.nodeService.getNodeMetadata(this.nodeId).subscribe(data => {
this.isFormDefinedInActiviti(data.nodeType, data.metadata);
},
this.handleError);
}
self.data = metadata;
self.isFormDefinedInActiviti(data.nodeType, self, metadata);
}, function (error) {
console.log('This node does not exist');
});
}
public isFormDefinedInActiviti(nodeType: string, ctx: any, metadata: any): Observable<any> {
public isFormDefinedInActiviti(nodeType: string, metadata: any): Observable<any> {
let opts = {
'modelType': 2
};
return ctx.authService.getAlfrescoApi().activiti.modelsApi.getModels(opts).then(function (forms) {
let ctx = this;
return this.authService.getAlfrescoApi().activiti.modelsApi.getModels(opts).then(function (forms) {
let form = forms.data.find(formdata => formdata.name === nodeType);
if (!form) {
@ -514,9 +501,9 @@ export class ActivitiForm implements OnInit, AfterViewChecked, OnChanges {
let modelName = 'activitiForms';
this.getEcmModels().subscribe(
this.ecmModelService.getEcmModels().subscribe(
models => {
if (!this.isAnEcmModelExistingForThisForm(models, modelName)) {
if (!this.ecmModelService.isAnEcmModelExistingForThisForm(models, modelName)) {
let modelNamespace = 'activitiFormsModel';
this.createAndActiveEcmModel(modelName, modelNamespace);
} else {
@ -530,11 +517,11 @@ export class ActivitiForm implements OnInit, AfterViewChecked, OnChanges {
private createAndActiveEcmModel(modelName: string, modelNamespace: string) {
this.createEcmModel(modelName, modelNamespace).subscribe(
this.ecmModelService.createEcmModel(modelName, modelNamespace).subscribe(
model => {
console.log('model created', model);
this.activeEcmModel(modelName).subscribe(
this.ecmModelService.activeEcmModel(modelName).subscribe(
modelActive => {
console.log('model active', modelActive);
this.createModelType(modelName);
@ -547,18 +534,18 @@ export class ActivitiForm implements OnInit, AfterViewChecked, OnChanges {
}
private createModelType(modelName: string) {
this.getEcmTypes(modelName).subscribe(
this.ecmModelService.getEcmTypes(modelName).subscribe(
customTypes => {
console.log('custom types', customTypes);
let customType = customTypes.list.entries.find(type => type.entry.name === this.formName);
if (!customType) {
let typeName = this.formName;
this.createEcmType(this.formName, modelName, 'cm:folder').subscribe(
this.ecmModelService.createEcmType(this.formName, modelName, 'cm:folder').subscribe(
typeCreated => {
console.log('type Created', typeCreated);
this.addPropertyToAType(modelName, typeName, this.form).subscribe(
this.ecmModelService.addPropertyToAType(modelName, typeName, this.form).subscribe(
properyAdded => {
console.log('property Added', properyAdded);
},
@ -570,133 +557,4 @@ export class ActivitiForm implements OnInit, AfterViewChecked, OnChanges {
this.handleError
);
}
private isAnEcmModelExistingForThisForm(ecmModels: any, modelName: string) {
let formEcmModel = ecmModels.list.entries.find(model => model.entry.name === modelName);
if (!formEcmModel) {
return false;
} else {
return true;
}
}
private activeEcmModel(modelName: string): Observable<any> {
let url = `${this.alfrescoSettingsService.ecmHost}/alfresco/api/-default-/private/alfresco/versions/1/cmm/${modelName}?select=status`;
let options = this.getRequestOptions();
let body = {status: 'ACTIVE'};
return this.http
.put(url, body, options)
.map(this.toJson)
.catch(this.handleError);
}
private createEcmModel(modelName: string, nameSpace: string): Observable<any> {
let url = `${this.alfrescoSettingsService.ecmHost}/alfresco/api/-default-/private/alfresco/versions/1/cmm`;
let options = this.getRequestOptions();
let body = {
status: 'DRAFT', namespaceUri: modelName, namespacePrefix: nameSpace, name: modelName, description: '', author: ''
};
return this.http
.post(url, body, options)
.map(this.toJson)
.catch(this.handleError);
}
private getEcmModels(): Observable<any> {
let url = `${this.alfrescoSettingsService.ecmHost}/alfresco/api/-default-/private/alfresco/versions/1/cmm`;
let options = this.getRequestOptions();
return this.http
.get(url, options)
.map(this.toJson)
.catch(this.handleError);
}
private getEcmTypes(modelName: string): Observable<any> {
let url = `${this.alfrescoSettingsService.ecmHost}/alfresco/api/-default-/private/alfresco/versions/1/cmm/${modelName}/types`;
let options = this.getRequestOptions();
return this.http
.get(url, options)
.map(this.toJson)
.catch(this.handleError);
}
private createEcmType(typeName: string, modelName: string, parentType: string): Observable<any> {
let url = `${this.alfrescoSettingsService.ecmHost}/alfresco/api/-default-/private/alfresco/versions/1/cmm/${modelName}/types`;
let options = this.getRequestOptions();
let body = {
name: typeName,
parentName: parentType,
title: typeName,
description: ''
};
return this.http
.post(url, body, options)
.map(this.toJson)
.catch(this.handleError);
}
private addPropertyToAType(modelName: string, typeName: string, formFields: any) {
let url = `${this.alfrescoSettingsService.ecmHost}/alfresco/api/-default-/private/alfresco/versions/1/cmm/${modelName}/types/${typeName}?select=props`;
let options = this.getRequestOptions();
let properties = [];
if (formFields && formFields.values) {
for (let key in formFields.values) {
if (key) {
properties.push({
name: key,
title: key,
description: key,
dataType: 'd:text',
multiValued: false,
mandatory: false,
mandatoryEnforced: false
});
}
}
}
let body = {
name: typeName,
properties: properties
};
return this.http
.put(url, body, options)
.map(this.toJson)
.catch(this.handleError);
}
private getHeaders(): Headers {
return new Headers({
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': this.authService.getTicketEcmBase64()
});
}
private getRequestOptions(): RequestOptions {
let headers = this.getHeaders();
return new RequestOptions({headers: headers});
}
toJson(res: Response) {
if (res) {
let body = res.json();
return body || {};
}
return {};
}
}

View File

@ -0,0 +1,163 @@
/*!
* @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 { AlfrescoAuthenticationService, AlfrescoSettingsService } from 'ng2-alfresco-core';
import { Observable } from 'rxjs/Rx';
import { Response, Http, Headers, RequestOptions } from '@angular/http';
@Injectable()
export class EcmModelService {
constructor(private authService: AlfrescoAuthenticationService,
private http: Http,
public alfrescoSettingsService: AlfrescoSettingsService) {
}
public isAnEcmModelExistingForThisForm(ecmModels: any, modelName: string) {
let formEcmModel = ecmModels.list.entries.find(model => model.entry.name === modelName);
if (!formEcmModel) {
return false;
} else {
return true;
}
}
public activeEcmModel(modelName: string): Observable<any> {
let url = `${this.alfrescoSettingsService.ecmHost}/alfresco/api/-default-/private/alfresco/versions/1/cmm/${modelName}?select=status`;
let options = this.getRequestOptions();
let body = {status: 'ACTIVE'};
return this.http
.put(url, body, options)
.map(this.toJson)
.catch(this.handleError);
}
public createEcmModel(modelName: string, nameSpace: string): Observable<any> {
let url = `${this.alfrescoSettingsService.ecmHost}/alfresco/api/-default-/private/alfresco/versions/1/cmm`;
let options = this.getRequestOptions();
let body = {
status: 'DRAFT', namespaceUri: modelName, namespacePrefix: nameSpace, name: modelName, description: '', author: ''
};
return this.http
.post(url, body, options)
.map(this.toJson)
.catch(this.handleError);
}
public getEcmModels(): Observable<any> {
let url = `${this.alfrescoSettingsService.ecmHost}/alfresco/api/-default-/private/alfresco/versions/1/cmm`;
let options = this.getRequestOptions();
return this.http
.get(url, options)
.map(this.toJson)
.catch(this.handleError);
}
public getEcmTypes(modelName: string): Observable<any> {
let url = `${this.alfrescoSettingsService.ecmHost}/alfresco/api/-default-/private/alfresco/versions/1/cmm/${modelName}/types`;
let options = this.getRequestOptions();
return this.http
.get(url, options)
.map(this.toJson)
.catch(this.handleError);
}
public createEcmType(typeName: string, modelName: string, parentType: string): Observable<any> {
let url = `${this.alfrescoSettingsService.ecmHost}/alfresco/api/-default-/private/alfresco/versions/1/cmm/${modelName}/types`;
let options = this.getRequestOptions();
let body = {
name: typeName,
parentName: parentType,
title: typeName,
description: ''
};
return this.http
.post(url, body, options)
.map(this.toJson)
.catch(this.handleError);
}
public addPropertyToAType(modelName: string, typeName: string, formFields: any) {
let url = `${this.alfrescoSettingsService.ecmHost}/alfresco/api/-default-/private/alfresco/versions/1/cmm/${modelName}/types/${typeName}?select=props`;
let options = this.getRequestOptions();
let properties = [];
if (formFields && formFields.values) {
for (let key in formFields.values) {
if (key) {
properties.push({
name: key,
title: key,
description: key,
dataType: 'd:text',
multiValued: false,
mandatory: false,
mandatoryEnforced: false
});
}
}
}
let body = {
name: typeName,
properties: properties
};
return this.http
.put(url, body, options)
.map(this.toJson)
.catch(this.handleError);
}
public getHeaders(): Headers {
return new Headers({
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': this.authService.getTicketEcmBase64()
});
}
public getRequestOptions(): RequestOptions {
let headers = this.getHeaders();
return new RequestOptions({headers: headers});
}
toJson(res: Response) {
if (res) {
let body = res.json();
return body || {};
}
return {};
}
handleError(err: any): any {
console.log(err);
}
}

View File

@ -29,24 +29,12 @@ export class FormService {
constructor(private authService: AlfrescoAuthenticationService) {
}
getProcessDefinitions(): Observable<any> {
return Observable.fromPromise(this.authService.getAlfrescoApi().activiti.processApi.getProcessDefinitions({}))
.map(this.toJsonArray)
.catch(this.handleError);
}
getTasks(): Observable<any> {
return Observable.fromPromise(this.authService.getAlfrescoApi().activiti.taskApi.listTasks({}))
.map(this.toJsonArray)
.catch(this.handleError);
}
getTask(taskId: string): Observable<any> {
return Observable.fromPromise(this.authService.getAlfrescoApi().activiti.taskApi.getTask(taskId))
.map(this.toJson)
.catch(this.handleError);
}
/**
* Save Task Form
* @param taskId Task Id
* @param formValues Form Values
* @returns {any}
*/
saveTaskForm(taskId: string, formValues: FormValues): Observable<any> {
let body = JSON.stringify({values: formValues});
@ -72,12 +60,22 @@ export class FormService {
.catch(this.handleError);
}
/**
* Get Form related to a taskId
* @param taskId Task Id
* @returns {any}
*/
getTaskForm(taskId: string): Observable<any> {
return Observable.fromPromise(this.authService.getAlfrescoApi().activiti.taskApi.getTaskForm(taskId))
.map(this.toJson)
.catch(this.handleError);
}
/**
* Get Form Definition
* @param formId Form Id
* @returns {any}
*/
getFormDefinitionById(formId: string): Observable<any> {
return Observable.fromPromise(this.authService.getAlfrescoApi().activiti.editorApi.getForm(formId))
.map(this.toJson)

View File

@ -0,0 +1,48 @@
/*!
* @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 { AlfrescoAuthenticationService } from 'ng2-alfresco-core';
import { Observable } from 'rxjs/Rx';
@Injectable()
export class NodeService {
constructor(private authService: AlfrescoAuthenticationService) {
}
public getNodeMetadata(nodeId: string): Observable<any> {
return Observable.fromPromise(this.authService.getAlfrescoApi().nodes.getNodeInfo(nodeId).map(this.cleanMetadataFromSemicolon));
}
private cleanMetadataFromSemicolon(data: any): any {
let metadata = {};
if (data && data.properties) {
for (let key in data.properties) {
if (key) {
metadata [key.split(':')[1]] = data.properties[key];
}
}
}
return {
metadata: metadata,
nodeType: data.nodeType
};
}
}