diff --git a/demo-shell-ng2/app/components/activiti/form-viewer.component.html b/demo-shell-ng2/app/components/activiti/form-viewer.component.html index cd9c53283f..e26465dae5 100644 --- a/demo-shell-ng2/app/components/activiti/form-viewer.component.html +++ b/demo-shell-ng2/app/components/activiti/form-viewer.component.html @@ -1,3 +1,4 @@
+
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 645515202b..6d043e8b1b 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 @@ -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 { - 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() { diff --git a/ng2-components/ng2-activiti-form/src/models/form-definition.model.ts b/ng2-components/ng2-activiti-form/src/models/form-definition.model.ts new file mode 100644 index 0000000000..88d20c4517 --- /dev/null +++ b/ng2-components/ng2-activiti-form/src/models/form-definition.model.ts @@ -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; + } +} diff --git a/ng2-components/ng2-activiti-form/src/models/node-metadata.model.ts b/ng2-components/ng2-activiti-form/src/models/node-metadata.model.ts new file mode 100644 index 0000000000..35254e8a7b --- /dev/null +++ b/ng2-components/ng2-activiti-form/src/models/node-metadata.model.ts @@ -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; + } +} diff --git a/ng2-components/ng2-activiti-form/src/services/ecm-model.service.spec.ts b/ng2-components/ng2-activiti-form/src/services/ecm-model.service.spec.ts index e69de29bb2..58ad8b656b 100644 --- a/ng2-components/ng2-activiti-form/src/services/ecm-model.service.spec.ts +++ b/ng2-components/ng2-activiti-form/src/services/ecm-model.service.spec.ts @@ -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. + */ diff --git a/ng2-components/ng2-activiti-form/src/services/ecm-model.service.ts b/ng2-components/ng2-activiti-form/src/services/ecm-model.service.ts index bfbe30e49d..5dad4a9829 100644 --- a/ng2-components/ng2-activiti-form/src/services/ecm-model.service.ts +++ b/ng2-components/ng2-activiti-form/src/services/ecm-model.service.ts @@ -87,12 +87,12 @@ export class EcmModelService { } public createEcmType(typeName: string, modelName: string, parentType: string): Observable { + 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', 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 0415b79f95..7b5507fad1 100644 --- a/ng2-components/ng2-activiti-form/src/services/form.service.ts +++ b/ng2-components/ng2-activiti-form/src/services/form.service.ts @@ -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} + */ + public createFormFromMetadaProperties(formName: string, metadata: any): Observable { + 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} + */ + public createFormModel(formName: string): Observable { + + 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} + */ + public addFieldsToAFormFromMetadata(formId: string, formDefinitionModel: FormDefinitionModel): Observable { + return this.addFieldsToAForm(formId, formDefinitionModel); + } + + /** + * Add Fileds to A form + * @returns {Observable} + */ + public addFieldsToAForm(formId: string, formModel: FormDefinitionModel): Observable { + return Observable.fromPromise(this.authService.getAlfrescoApi().activiti.editorApi.saveForm(formId, formModel)); + } + + /** + * Search For A Form by name + * @returns {Observable} + */ + public searchFrom(name: string): Observable { + 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} + */ + public getForms(): Observable { + let opts = { + 'modelType': 2 + }; + + return Observable.fromPromise(this.authService.getAlfrescoApi().activiti.modelsApi.getModels(opts)); + } + + /** + * Get Process Definition + * @returns {Observable} + */ + public getProcessDefinitions(): Observable { + 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} + */ + public getTasks(): Observable { + return Observable.fromPromise(this.authService.getAlfrescoApi().activiti.taskApi.listTasks({})) + .map(this.toJsonArray) + .catch(this.handleError); + } + + /** + * Get Task + * @param taskId Task Id + * @returns {Observable} + */ + public getTask(taskId: string): Observable { + 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} */ - saveTaskForm(taskId: string, formValues: FormValues): Observable { + public saveTaskForm(taskId: string, formValues: FormValues): Observable { 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} */ - completeTaskForm(taskId: string, formValues: FormValues, outcome?: string): Observable { + public completeTaskForm(taskId: string, formValues: FormValues, outcome?: string): Observable { 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} */ - getTaskForm(taskId: string): Observable { + public getTaskForm(taskId: string): Observable { 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} */ - getFormDefinitionById(formId: string): Observable { + public getFormDefinitionById(formId: string): Observable { 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|Promise} */ - getFormDefinitionByName(name: string): Observable { + public getFormDefinitionByName(name: string): Observable { let opts = { 'filter': 'myReusableForms', 'filterText': name, diff --git a/ng2-components/ng2-activiti-form/src/services/node.service.spec.ts b/ng2-components/ng2-activiti-form/src/services/node.service.spec.ts index e69de29bb2..58ad8b656b 100644 --- a/ng2-components/ng2-activiti-form/src/services/node.service.spec.ts +++ b/ng2-components/ng2-activiti-form/src/services/node.service.spec.ts @@ -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. + */ diff --git a/ng2-components/ng2-activiti-form/src/services/node.service.ts b/ng2-components/ng2-activiti-form/src/services/node.service.ts index 74e798b139..f71469c5f6 100644 --- a/ng2-components/ng2-activiti-form/src/services/node.service.ts +++ b/ng2-components/ng2-activiti-form/src/services/node.service.ts @@ -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 { - 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 { + 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); } }