clean code

This commit is contained in:
Mario Romano 2016-08-24 18:15:40 +01:00
parent 4f29a8e315
commit b1a88acf76
9 changed files with 319 additions and 137 deletions

View File

@ -1,3 +1,4 @@
<div class="activiti-form-viewer" *ngIf="taskId">
<activiti-form [formName]="'hc:patientFolder'" [saveMetadata]="true"></activiti-form>
<!--<activiti-form [nodeId]="'35c6dc13-810d-4264-b5a2-9a391a225010'"></activiti-form>-->
</div>

View File

@ -23,11 +23,10 @@ import {
Output,
EventEmitter
} from '@angular/core';
import { MATERIAL_DESIGN_DIRECTIVES, AlfrescoAuthenticationService, AlfrescoSettingsService } from 'ng2-alfresco-core';
import { Observable } from 'rxjs/Rx';
import { EcmModelService } from './../services/ecm-model.service'
import { MATERIAL_DESIGN_DIRECTIVES, AlfrescoAuthenticationService } from 'ng2-alfresco-core';
import { EcmModelService } from './../services/ecm-model.service';
import { FormService } from './../services/form.service';
import { NodeService } from './../services/node.service'
import { NodeService } from './../services/node.service';
import { FormModel, FormOutcomeModel, FormValues, FormFieldModel, FormOutcomeEvent } from './widgets/core/index';
import { TabsWidget } from './widgets/tabs/tabs.widget';
@ -379,121 +378,29 @@ export class ActivitiForm implements OnInit, AfterViewChecked, OnChanges {
private loadFormForEcmMetadata(): void {
this.nodeService.getNodeMetadata(this.nodeId).subscribe(data => {
this.isFormDefinedInActiviti(data.nodeType, data.metadata);
this.loadFormFromActiviti(data.nodeType, data.metadata);
},
this.handleError);
}
public isFormDefinedInActiviti(nodeType: string, metadata: any): Observable<any> {
let opts = {
'modelType': 2
};
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) {
let dataModel = {
name: nodeType,
description: '',
modelType: 2,
stencilSet: 0
};
ctx.authService.getAlfrescoApi().activiti.modelsApi.createModel(dataModel).then(function (representation) {
console.log('created', representation.id);
ctx.formId = representation.id;
let formRepresentation = {
id: representation.id,
name: representation.name,
description: '',
version: 1,
lastUpdatedBy: 1,
lastUpdatedByFullName: representation.lastUpdatedByFullName,
lastUpdated: representation.lastUpdated,
stencilSetId: 0,
referenceId: null,
formDefinition: {}
};
let fields = [];
for (let key in metadata) {
if (key) {
let field = {
type: 'text',
id: key,
name: key,
required: false,
readOnly: false,
sizeX: 1,
sizeY: 1,
row: -1,
col: -1,
colspan: 1,
params: {
existingColspan: 1,
maxColspan: 2
},
layout: {
colspan: 1,
row: -1,
column: -1
}
};
fields.push(field);
}
}
formRepresentation.formDefinition = {
fields: [{
name: 'Label',
type: 'container',
fieldType: 'ContainerRepresentation',
numberOfColumns: 2,
required: false,
readOnly: false,
sizeX: 2,
sizeY: 1,
row: -1,
col: -1,
fields: {'1': fields}
}],
gridsterForm: false,
javascriptEvents: [],
metadata: {},
outcomes: [],
className: '',
style: '',
tabs: [],
variables: []
};
let data = {
reusable: false,
newVersion: false,
formRepresentation: formRepresentation,
formImageBase64: ''
};
ctx.authService.getAlfrescoApi().activiti.editorApi.saveForm(formRepresentation.id, data).then(function (response) {
console.log(response);
ctx.loadForm();
}, function (error) {
console.log('Form not created');
public loadFormFromActiviti(nodeType: string, metadata: any): any {
this.formService.searchFrom(nodeType).subscribe(
form => {
if (!form) {
this.formService.createFormFromMetadaProperties(nodeType, metadata).subscribe(formMetadata => {
this.loadFormFromFormId(formMetadata.id);
});
} else {
this.loadFormFromFormId(form.id);
}
},
this.handleError
);
}
}, function (error) {
console.log('Form not created');
});
} else {
ctx.formId = form.id;
ctx.loadForm();
}
}, function (error) {
console.log('This node does not exist');
});
private loadFormFromFormId(formId: string) {
this.formId = formId;
this.loadForm();
}
private storeFormAsMetadata() {

View File

@ -0,0 +1,95 @@
/*!
* @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.
*/
export class FormDefinitionModel {
reusable: boolean = false;
newVersion: boolean = false;
formRepresentation: any;
formImageBase64: string = '';
constructor(id: string, name: any, lastUpdatedByFullName: string, lastUpdated: string, metadata: any) {
this.formRepresentation = {
id: id,
name: name,
description: '',
version: 1,
lastUpdatedBy: 1,
lastUpdatedByFullName: lastUpdatedByFullName,
lastUpdated: lastUpdated,
stencilSetId: 0,
referenceId: null,
formDefinition: {
fields: [{
name: 'Label',
type: 'container',
fieldType: 'ContainerRepresentation',
numberOfColumns: 2,
required: false,
readOnly: false,
sizeX: 2,
sizeY: 1,
row: -1,
col: -1,
fields: {'1': this.metadataToFields(metadata)}
}],
gridsterForm: false,
javascriptEvents: [],
metadata: {},
outcomes: [],
className: '',
style: '',
tabs: [],
variables: []
}
};
}
private metadataToFields(metadata: any): any[] {
let fields = [];
if (metadata) {
for (let key in metadata) {
if (key) {
let field = {
type: 'text',
id: key,
name: key,
required: false,
readOnly: false,
sizeX: 1,
sizeY: 1,
row: -1,
col: -1,
colspan: 1,
params: {
existingColspan: 1,
maxColspan: 2
},
layout: {
colspan: 1,
row: -1,
column: -1
}
};
fields.push(field);
}
}
}
return fields;
}
}

View File

@ -0,0 +1,26 @@
/*!
* @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.
*/
export class NodeMetadata {
metadata: any;
nodeType: string;
constructor(metadata: any, nodeType: string) {
this.metadata = metadata;
this.nodeType = nodeType;
}
}

View File

@ -0,0 +1,16 @@
/*!
* @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.
*/

View File

@ -87,12 +87,12 @@ export class EcmModelService {
}
public createEcmType(typeName: string, modelName: string, parentType: string): Observable<any> {
let name = this.cleanNameType(typeName);
let url = `${this.alfrescoSettingsService.ecmHost}/alfresco/api/-default-/private/alfresco/versions/1/cmm/${modelName}/types`;
let options = this.getRequestOptions();
let body = {
name: typeName,
name: name,
parentName: parentType,
title: typeName,
description: ''
@ -105,7 +105,8 @@ export class EcmModelService {
}
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 name = this.cleanNameType(typeName);
let url = `${this.alfrescoSettingsService.ecmHost}/alfresco/api/-default-/private/alfresco/versions/1/cmm/${modelName}/types/${name}?select=props`;
let options = this.getRequestOptions();
let properties = [];
@ -126,7 +127,7 @@ export class EcmModelService {
}
let body = {
name: typeName,
name: name,
properties: properties
};
@ -136,6 +137,14 @@ export class EcmModelService {
.catch(this.handleError);
}
public cleanNameType(name: string): string {
let cleanName = name;
if (name.indexOf(':') !== -1) {
cleanName = name.split(':')[1];
}
return cleanName.replace(/[^a-zA-Z ]/g, '');
}
public getHeaders(): Headers {
return new Headers({
'Accept': 'application/json',

View File

@ -15,10 +15,11 @@
* limitations under the License.
*/
import {Injectable} from '@angular/core';
import {Observable} from 'rxjs/Rx';
import {AlfrescoAuthenticationService} from 'ng2-alfresco-core';
import {FormValues} from './../components/widgets/core/index';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Rx';
import { AlfrescoAuthenticationService } from 'ng2-alfresco-core';
import { FormValues } from './../components/widgets/core/index';
import { FormDefinitionModel } from '../models/form-definition.model';
@Injectable()
export class FormService {
@ -29,13 +30,121 @@ export class FormService {
constructor(private authService: AlfrescoAuthenticationService) {
}
/**
* Create a Form with a fields for each metadata properties
* @returns {Observable<any>}
*/
public createFormFromMetadaProperties(formName: string, metadata: any): Observable<any> {
return Observable.create(observer => {
this.createFormModel(formName).subscribe(
form => {
let formDefinitionModel = new FormDefinitionModel(form.id, form.name, form.lastUpdatedByFullName, form.lastUpdated, metadata);
this.addFieldsToAFormFromMetadata(form.id, formDefinitionModel).subscribe(formData => {
observer.next(formData);
observer.complete();
}, this.handleError);
}, this.handleError);
});
}
/**
* Create a Form
* @returns {Observable<any>}
*/
public createFormModel(formName: string): Observable<any> {
let dataModel = {
name: formName,
description: '',
modelType: 2,
stencilSet: 0
};
return Observable.fromPromise(this.authService.getAlfrescoApi().activiti.modelsApi.createModel(dataModel));
}
/**
* Add Fields to A form from a metadata properties
* @returns {Observable<any>}
*/
public addFieldsToAFormFromMetadata(formId: string, formDefinitionModel: FormDefinitionModel): Observable<any> {
return this.addFieldsToAForm(formId, formDefinitionModel);
}
/**
* Add Fileds to A form
* @returns {Observable<any>}
*/
public addFieldsToAForm(formId: string, formModel: FormDefinitionModel): Observable<any> {
return Observable.fromPromise(this.authService.getAlfrescoApi().activiti.editorApi.saveForm(formId, formModel));
}
/**
* Search For A Form by name
* @returns {Observable<any>}
*/
public searchFrom(name: string): Observable<any> {
let opts = {
'modelType': 2
};
return Observable.fromPromise(this.authService.getAlfrescoApi().activiti.modelsApi.getModels(opts)).map(function (forms: any) {
return forms.data.find(formdata => formdata.name === name);
}).catch(this.handleError);
}
/**
* Get All the forms
* @returns {Observable<any>}
*/
public getForms(): Observable<any> {
let opts = {
'modelType': 2
};
return Observable.fromPromise(this.authService.getAlfrescoApi().activiti.modelsApi.getModels(opts));
}
/**
* Get Process Definition
* @returns {Observable<any>}
*/
public getProcessDefinitions(): Observable<any> {
return Observable.fromPromise(this.authService.getAlfrescoApi().activiti.processApi.getProcessDefinitions({}))
.map(this.toJsonArray)
.catch(this.handleError);
}
/**
* Get All the Tasks
* @param taskId Task Id
* @returns {Observable<any>}
*/
public getTasks(): Observable<any> {
return Observable.fromPromise(this.authService.getAlfrescoApi().activiti.taskApi.listTasks({}))
.map(this.toJsonArray)
.catch(this.handleError);
}
/**
* Get Task
* @param taskId Task Id
* @returns {Observable<any>}
*/
public 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}
* @returns {Observable<any>}
*/
saveTaskForm(taskId: string, formValues: FormValues): Observable<any> {
public saveTaskForm(taskId: string, formValues: FormValues): Observable<any> {
let body = JSON.stringify({values: formValues});
return Observable.fromPromise(this.authService.getAlfrescoApi().activiti.taskApi.saveTaskForm(taskId, body))
@ -47,9 +156,9 @@ export class FormService {
* @param taskId Task Id
* @param formValues Form Values
* @param outcome Form Outcome
* @returns {any}
* @returns {Observable<any>}
*/
completeTaskForm(taskId: string, formValues: FormValues, outcome?: string): Observable<any> {
public completeTaskForm(taskId: string, formValues: FormValues, outcome?: string): Observable<any> {
let data: any = {values: formValues};
if (outcome) {
data.outcome = outcome;
@ -63,9 +172,9 @@ export class FormService {
/**
* Get Form related to a taskId
* @param taskId Task Id
* @returns {any}
* @returns {Observable<any>}
*/
getTaskForm(taskId: string): Observable<any> {
public getTaskForm(taskId: string): Observable<any> {
return Observable.fromPromise(this.authService.getAlfrescoApi().activiti.taskApi.getTaskForm(taskId))
.map(this.toJson)
.catch(this.handleError);
@ -74,9 +183,9 @@ export class FormService {
/**
* Get Form Definition
* @param formId Form Id
* @returns {any}
* @returns {Observable<any>}
*/
getFormDefinitionById(formId: string): Observable<any> {
public getFormDefinitionById(formId: string): Observable<any> {
return Observable.fromPromise(this.authService.getAlfrescoApi().activiti.editorApi.getForm(formId))
.map(this.toJson)
.catch(this.handleError);
@ -87,7 +196,7 @@ export class FormService {
* @param name
* @returns {Promise<T>|Promise<ErrorObservable>}
*/
getFormDefinitionByName(name: string): Observable<any> {
public getFormDefinitionByName(name: string): Observable<any> {
let opts = {
'filter': 'myReusableForms',
'filterText': name,

View File

@ -0,0 +1,16 @@
/*!
* @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.
*/

View File

@ -18,6 +18,7 @@
import { Injectable } from '@angular/core';
import { AlfrescoAuthenticationService } from 'ng2-alfresco-core';
import { Observable } from 'rxjs/Rx';
import { NodeMetadata } from '../models/node-metadata.model';
@Injectable()
export class NodeService {
@ -25,11 +26,16 @@ 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));
/**
* Get All the metadata and the nodeType for a nodeId cleaned by the prefix
* @param nodeId Node Id
* @returns NodeMetadata
*/
public getNodeMetadata(nodeId: string): Observable<NodeMetadata> {
return Observable.fromPromise(this.authService.getAlfrescoApi().nodes.getNodeInfo(nodeId)).map(this.cleanMetadataFromSemicolon);
}
private cleanMetadataFromSemicolon(data: any): any {
private cleanMetadataFromSemicolon(data: any): NodeMetadata {
let metadata = {};
if (data && data.properties) {
@ -40,9 +46,6 @@ export class NodeService {
}
}
return {
metadata: metadata,
nodeType: data.nodeType
};
return new NodeMetadata(metadata, data.nodeType);
}
}