mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-05-19 17:14:57 +00:00
clean code and store metadata
This commit is contained in:
parent
b1a88acf76
commit
17f805f2c4
@ -1,4 +1,11 @@
|
||||
<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>-->
|
||||
<!--<activiti-form [formName]="'activitiForms:patientFolder'"
|
||||
[saveMetadata]="true"
|
||||
[path]="'/Sites/swsdp/documentLibrary'"
|
||||
[nameNode]="'test'"></activiti-form>-->
|
||||
<activiti-form [nodeId]="'e280be3a-6584-45a1-8bb5-89bfe070262e'"
|
||||
[saveMetadata]="true"
|
||||
[path]="'/Sites/swsdp/documentLibrary'"
|
||||
<!--[nameNode]="'test2'"-->
|
||||
></activiti-form>
|
||||
</div>
|
||||
|
@ -17,7 +17,7 @@
|
||||
|
||||
import { Component, OnInit, OnDestroy, AfterViewChecked } from '@angular/core';
|
||||
import { ActivatedRoute, Router } from '@angular/router';
|
||||
import { ActivitiForm, FormService } from 'ng2-activiti-form';
|
||||
import { ActivitiForm, FormService, EcmModelService, NodeService } from 'ng2-activiti-form';
|
||||
import { Subscription } from 'rxjs/Rx';
|
||||
|
||||
declare let __moduleName: string;
|
||||
@ -29,7 +29,7 @@ declare var componentHandler;
|
||||
templateUrl: './form-viewer.component.html',
|
||||
styleUrls: ['./form-viewer.component.css'],
|
||||
directives: [ActivitiForm],
|
||||
providers: [FormService]
|
||||
providers: [FormService, EcmModelService, NodeService]
|
||||
})
|
||||
export class FormViewer implements OnInit, OnDestroy, AfterViewChecked {
|
||||
|
||||
|
@ -18,3 +18,5 @@
|
||||
export * from './src/components/activiti-form.component';
|
||||
export * from './src/services/form.service';
|
||||
export * from './src/components/widgets/index';
|
||||
export * from './src/services/ecm-model.service';
|
||||
export * from './src/services/node.service';
|
||||
|
@ -40,7 +40,7 @@ describe('ActivitiForm', () => {
|
||||
]);
|
||||
window['componentHandler'] = componentHandler;
|
||||
|
||||
formService = new FormService(null);
|
||||
formService = new FormService(null, null);
|
||||
formComponent = new ActivitiForm(formService, visibilityService, null, null, null);
|
||||
});
|
||||
|
||||
|
@ -73,7 +73,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, EcmModelService, NodeService]
|
||||
providers: [EcmModelService, FormService, WidgetVisibilityService, NodeService]
|
||||
})
|
||||
export class ActivitiForm implements OnInit, AfterViewChecked, OnChanges {
|
||||
|
||||
@ -99,6 +99,12 @@ export class ActivitiForm implements OnInit, AfterViewChecked, OnChanges {
|
||||
@Input()
|
||||
data: FormValues;
|
||||
|
||||
@Input()
|
||||
path: string;
|
||||
|
||||
@Input()
|
||||
nameNode: string;
|
||||
|
||||
@Input()
|
||||
showTitle: boolean = true;
|
||||
|
||||
@ -165,7 +171,7 @@ export class ActivitiForm implements OnInit, AfterViewChecked, OnChanges {
|
||||
|
||||
ngOnInit() {
|
||||
if (this.nodeId) {
|
||||
this.loadFormForEcmMetadata();
|
||||
this.loadActivitiFormForEcmNode();
|
||||
} else {
|
||||
this.loadForm();
|
||||
}
|
||||
@ -290,7 +296,7 @@ export class ActivitiForm implements OnInit, AfterViewChecked, OnChanges {
|
||||
.getFormDefinitionById(formId)
|
||||
.subscribe(
|
||||
form => {
|
||||
// console.log('Get Form By definition Id', form);
|
||||
this.formName = form.name;
|
||||
this.form = this.parseForm(form);
|
||||
this.formLoaded.emit(this.form);
|
||||
},
|
||||
@ -376,18 +382,19 @@ export class ActivitiForm implements OnInit, AfterViewChecked, OnChanges {
|
||||
}
|
||||
}
|
||||
|
||||
private loadFormForEcmMetadata(): void {
|
||||
private loadActivitiFormForEcmNode(): void {
|
||||
this.nodeService.getNodeMetadata(this.nodeId).subscribe(data => {
|
||||
this.loadFormFromActiviti(data.nodeType, data.metadata);
|
||||
this.data = data.metadata;
|
||||
this.loadFormFromActiviti(data.nodeType);
|
||||
},
|
||||
this.handleError);
|
||||
}
|
||||
|
||||
public loadFormFromActiviti(nodeType: string, metadata: any): any {
|
||||
public loadFormFromActiviti(nodeType: string): any {
|
||||
this.formService.searchFrom(nodeType).subscribe(
|
||||
form => {
|
||||
if (!form) {
|
||||
this.formService.createFormFromMetadaProperties(nodeType, metadata).subscribe(formMetadata => {
|
||||
this.formService.createFormFromNodeType(nodeType).subscribe(formMetadata => {
|
||||
this.loadFormFromFormId(formMetadata.id);
|
||||
});
|
||||
} else {
|
||||
@ -404,64 +411,11 @@ export class ActivitiForm implements OnInit, AfterViewChecked, OnChanges {
|
||||
}
|
||||
|
||||
private storeFormAsMetadata() {
|
||||
if (this.saveMetadata) {
|
||||
let modelName = 'activitiForms';
|
||||
|
||||
|
||||
this.ecmModelService.getEcmModels().subscribe(
|
||||
models => {
|
||||
if (!this.ecmModelService.isAnEcmModelExistingForThisForm(models, modelName)) {
|
||||
let modelNamespace = 'activitiFormsModel';
|
||||
this.createAndActiveEcmModel(modelName, modelNamespace);
|
||||
} else {
|
||||
this.createModelType(modelName);
|
||||
}
|
||||
},
|
||||
this.handleError
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private createAndActiveEcmModel(modelName: string, modelNamespace: string) {
|
||||
this.ecmModelService.createEcmModel(modelName, modelNamespace).subscribe(
|
||||
model => {
|
||||
console.log('model created', model);
|
||||
|
||||
this.ecmModelService.activeEcmModel(modelName).subscribe(
|
||||
modelActive => {
|
||||
console.log('model active', modelActive);
|
||||
this.createModelType(modelName);
|
||||
},
|
||||
this.handleError
|
||||
);
|
||||
},
|
||||
this.handleError
|
||||
);
|
||||
}
|
||||
|
||||
private createModelType(modelName: string) {
|
||||
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.ecmModelService.createEcmType(this.formName, modelName, 'cm:folder').subscribe(
|
||||
typeCreated => {
|
||||
console.log('type Created', typeCreated);
|
||||
|
||||
this.ecmModelService.addPropertyToAType(modelName, typeName, this.form).subscribe(
|
||||
properyAdded => {
|
||||
console.log('property Added', properyAdded);
|
||||
},
|
||||
this.handleError);
|
||||
},
|
||||
this.handleError);
|
||||
this.ecmModelService.createEcmTypeForActivitiForm(this.formName, this.form).subscribe(type => {
|
||||
if (this.saveMetadata) {
|
||||
this.nodeService.createNodeMetadata(type.nodeType || type.entry.prefixedName, EcmModelService.MODEL_NAMESPACE, this.form.values, this.path, this.nameNode);
|
||||
}
|
||||
},
|
||||
this.handleError
|
||||
}, this.handleError
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -62,12 +62,12 @@ export class FormDefinitionModel {
|
||||
private metadataToFields(metadata: any): any[] {
|
||||
let fields = [];
|
||||
if (metadata) {
|
||||
for (let key in metadata) {
|
||||
if (key) {
|
||||
metadata.forEach(function(property) {
|
||||
if (property) {
|
||||
let field = {
|
||||
type: 'text',
|
||||
id: key,
|
||||
name: key,
|
||||
id: property.name,
|
||||
name: property.name,
|
||||
required: false,
|
||||
readOnly: false,
|
||||
sizeX: 1,
|
||||
@ -87,7 +87,7 @@ export class FormDefinitionModel {
|
||||
};
|
||||
fields.push(field);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return fields;
|
||||
|
@ -19,22 +19,112 @@ 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';
|
||||
import { FormModel } from '../components/widgets/core/form.model';
|
||||
import { NodeService } from './node.service';
|
||||
|
||||
@Injectable()
|
||||
export class EcmModelService {
|
||||
|
||||
public static MODEL_NAMESPACE: string = 'activitiForms';
|
||||
public static MODEL_NAME: string = 'activitiFormsModel';
|
||||
public static TYPE_MODEL: string = 'cm:folder';
|
||||
|
||||
constructor(private authService: AlfrescoAuthenticationService,
|
||||
private http: Http,
|
||||
public alfrescoSettingsService: AlfrescoSettingsService) {
|
||||
public alfrescoSettingsService: AlfrescoSettingsService,
|
||||
private nodeService: NodeService) {
|
||||
}
|
||||
|
||||
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 createEcmTypeForActivitiForm(formName: string, form: FormModel): Observable<any> {
|
||||
return Observable.create(observer => {
|
||||
this.seachActivitiEcmModel().subscribe(
|
||||
model => {
|
||||
if (!model) {
|
||||
this.createActivitiEcmModel(formName, form).subscribe(typeForm => {
|
||||
observer.next(typeForm);
|
||||
observer.complete();
|
||||
});
|
||||
} else {
|
||||
this.createFomType(formName, form).subscribe(typeForm => {
|
||||
observer.next(typeForm);
|
||||
observer.complete();
|
||||
});
|
||||
}
|
||||
},
|
||||
this.handleError
|
||||
);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
private seachActivitiEcmModel() {
|
||||
return this.getEcmModels().map(function (ecmModels: any) {
|
||||
return ecmModels.list.entries.find(model => model.entry.name === EcmModelService.MODEL_NAME);
|
||||
});
|
||||
}
|
||||
|
||||
private createActivitiEcmModel(formName: string, form: FormModel): Observable<any> {
|
||||
return Observable.create(observer => {
|
||||
this.createEcmModel(EcmModelService.MODEL_NAME, EcmModelService.MODEL_NAMESPACE).subscribe(
|
||||
model => {
|
||||
console.log('model created', model);
|
||||
this.activeEcmModel(EcmModelService.MODEL_NAME).subscribe(
|
||||
modelActive => {
|
||||
console.log('model active', modelActive);
|
||||
this.createEcmTypeWithProperties(formName, form).subscribe(typeCreated => {
|
||||
observer.next(typeCreated);
|
||||
observer.complete();
|
||||
});
|
||||
},
|
||||
this.handleError
|
||||
);
|
||||
},
|
||||
this.handleError
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
private createFomType(formName: string, form: FormModel): Observable<any> {
|
||||
return Observable.create(observer => {
|
||||
this.searchFormType(formName).subscribe(
|
||||
ecmType => {
|
||||
console.log('custom types', ecmType);
|
||||
if (!ecmType) {
|
||||
this.createEcmTypeWithProperties(formName, form).subscribe(typeCreated => {
|
||||
observer.next(typeCreated);
|
||||
observer.complete();
|
||||
});
|
||||
} else {
|
||||
observer.next(ecmType);
|
||||
observer.complete();
|
||||
}
|
||||
},
|
||||
this.handleError
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
public createEcmTypeWithProperties(formName: string, form: FormModel): Observable<any> {
|
||||
return Observable.create(observer => {
|
||||
this.createEcmType(formName, EcmModelService.MODEL_NAME, EcmModelService.TYPE_MODEL).subscribe(
|
||||
typeCreated => {
|
||||
console.log('type Created', typeCreated);
|
||||
this.addPropertyToAType(EcmModelService.MODEL_NAME, formName, form).subscribe(
|
||||
properyAdded => {
|
||||
console.log('property Added', properyAdded);
|
||||
observer.next(typeCreated);
|
||||
observer.complete();
|
||||
},
|
||||
this.handleError);
|
||||
},
|
||||
this.handleError);
|
||||
});
|
||||
}
|
||||
|
||||
public searchFormType(formName: string): Observable<any> {
|
||||
return this.getEcmType(EcmModelService.MODEL_NAME).map(function (customTypes: any) {
|
||||
return customTypes.list.entries.find(type => type.entry.prefixedName === formName);
|
||||
});
|
||||
}
|
||||
|
||||
public activeEcmModel(modelName: string): Observable<any> {
|
||||
@ -76,7 +166,7 @@ export class EcmModelService {
|
||||
}
|
||||
|
||||
|
||||
public getEcmTypes(modelName: string): Observable<any> {
|
||||
public getEcmType(modelName: string): Observable<any> {
|
||||
let url = `${this.alfrescoSettingsService.ecmHost}/alfresco/api/-default-/private/alfresco/versions/1/cmm/${modelName}/types`;
|
||||
let options = this.getRequestOptions();
|
||||
|
||||
|
@ -20,6 +20,7 @@ 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';
|
||||
import { EcmModelService } from './ecm-model.service';
|
||||
|
||||
@Injectable()
|
||||
export class FormService {
|
||||
@ -27,23 +28,27 @@ export class FormService {
|
||||
static UNKNOWN_ERROR_MESSAGE: string = 'Unknown error';
|
||||
static GENERIC_ERROR_MESSAGE: string = 'Server error';
|
||||
|
||||
constructor(private authService: AlfrescoAuthenticationService) {
|
||||
constructor(private authService: AlfrescoAuthenticationService,
|
||||
private ecmModelService: EcmModelService) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a Form with a fields for each metadata properties
|
||||
* @returns {Observable<any>}
|
||||
*/
|
||||
public createFormFromMetadaProperties(formName: string, metadata: any): Observable<any> {
|
||||
public createFormFromNodeType(formName: string): Observable<any> {
|
||||
return Observable.create(observer => {
|
||||
this.createFormModel(formName).subscribe(
|
||||
this.createForm(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.ecmModelService.searchFormType(formName).subscribe(
|
||||
customType => {
|
||||
let formDefinitionModel = new FormDefinitionModel(form.id, form.name, form.lastUpdatedByFullName, form.lastUpdated, customType.entry.properties);
|
||||
this.addFieldsNodeTypePropertiesToTheForm(form.id, formDefinitionModel).subscribe(formData => {
|
||||
observer.next(formData);
|
||||
observer.complete();
|
||||
}, this.handleError);
|
||||
},
|
||||
this.handleError);
|
||||
}, this.handleError);
|
||||
});
|
||||
}
|
||||
@ -52,8 +57,7 @@ export class FormService {
|
||||
* Create a Form
|
||||
* @returns {Observable<any>}
|
||||
*/
|
||||
public createFormModel(formName: string): Observable<any> {
|
||||
|
||||
public createForm(formName: string): Observable<any> {
|
||||
let dataModel = {
|
||||
name: formName,
|
||||
description: '',
|
||||
@ -68,7 +72,7 @@ export class FormService {
|
||||
* Add Fields to A form from a metadata properties
|
||||
* @returns {Observable<any>}
|
||||
*/
|
||||
public addFieldsToAFormFromMetadata(formId: string, formDefinitionModel: FormDefinitionModel): Observable<any> {
|
||||
public addFieldsNodeTypePropertiesToTheForm(formId: string, formDefinitionModel: FormDefinitionModel): Observable<any> {
|
||||
return this.addFieldsToAForm(formId, formDefinitionModel);
|
||||
}
|
||||
|
||||
|
@ -35,6 +35,51 @@ export class NodeService {
|
||||
return Observable.fromPromise(this.authService.getAlfrescoApi().nodes.getNodeInfo(nodeId)).map(this.cleanMetadataFromSemicolon);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new Node from form metadata
|
||||
* @param path path
|
||||
* @param nodeType node type
|
||||
* @param nameSpace namespace node
|
||||
* @param data data to store
|
||||
* @returns NodeMetadata
|
||||
*/
|
||||
public createNodeMetadata(nodeType: string, nameSpace: any, data: any, path: string, name?: string): Observable<any> {
|
||||
let properties = {};
|
||||
for (let key in data) {
|
||||
if (data[key]) {
|
||||
properties[nameSpace + ':' + key] = data[key];
|
||||
}
|
||||
}
|
||||
|
||||
return this.createNode(name || this.generateUuid(), nodeType, properties, path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new Node from form metadata
|
||||
* @param name path
|
||||
* @param nodeType node type
|
||||
* @param properties namespace node
|
||||
* @param path path
|
||||
* @returns NodeMetadata
|
||||
*/
|
||||
public createNode(name: string, nodeType: string, properties: any, path: string): Observable<any> {
|
||||
let body = {
|
||||
name: name,
|
||||
nodeType: nodeType,
|
||||
properties: properties,
|
||||
relativePath: path
|
||||
};
|
||||
|
||||
return Observable.fromPromise(this.authService.getAlfrescoApi().nodes.addNode('-root-', body, {}));
|
||||
}
|
||||
|
||||
private generateUuid() {
|
||||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
|
||||
let r = Math.random() * 16 | 0, v = c === 'x' ? r : (r & 0x3 | 0x8);
|
||||
return v.toString(16);
|
||||
});
|
||||
}
|
||||
|
||||
private cleanMetadataFromSemicolon(data: any): NodeMetadata {
|
||||
let metadata = {};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user