Commit f8178222 authored by Alexander Philipp Nowosad's avatar Alexander Philipp Nowosad
Browse files

Add list wrapper to feature models

parent 8e439c26
Pipeline #137922 failed with stages
in 11 minutes and 4 seconds
......@@ -4,22 +4,21 @@ import { CompanyModel } from './company-model';
import PouchDB from 'pouchdb-browser';
import { CanvasDefinition } from './canvas-definition';
import { Observable } from 'rxjs';
import { ElementService } from '../database/element-service';
@Injectable({
providedIn: 'root',
})
export class CompanyModelService {
export class CompanyModelService implements ElementService<CompanyModel> {
constructor(private pouchdbService: PouchdbService) {}
async createCompanyModel(
createCompanyModel(
model: Partial<CompanyModel>,
definition: CanvasDefinition
) {
const { id } = await this.add(model);
const companyModel = await this.get(id);
): CompanyModel {
const companyModel = new CompanyModel(model);
companyModel.definition = definition;
await this.save(companyModel);
return this.get(id);
return companyModel;
}
add(companyModel: Partial<CompanyModel>) {
......
......@@ -4,29 +4,31 @@ import PouchDB from 'pouchdb-browser';
import { ExpertModel } from './expert-model';
import { CanvasDefinition } from './canvas-definition';
import { Observable } from 'rxjs';
import { ElementService } from '../database/element-service';
import { Domain } from '../development-process-registry/knowledge/domain';
import { Author } from '../model/author';
import { Instance } from './instance';
@Injectable({
providedIn: 'root',
})
export class ExpertModelService {
export class ExpertModelService implements ElementService<ExpertModel> {
constructor(private pouchdbService: PouchdbService) {}
async createExpertModel(
createExpertModel(
model: Partial<ExpertModel>,
definition: CanvasDefinition
) {
const { id } = await this.add(model);
const expertModel = await this.get(id);
): ExpertModel {
const expertModel = new ExpertModel(model);
expertModel.definition = definition;
await this.save(expertModel);
return this.get(id);
return expertModel;
}
add(expertModel: Partial<ExpertModel>) {
return this.pouchdbService.post(new ExpertModel(expertModel));
}
async remove(id: string) {
async delete(id: string) {
const result = await this.pouchdbService.get(id);
return this.pouchdbService.remove(result);
}
......@@ -55,11 +57,36 @@ export class ExpertModelService {
* Update an expert model
*
* @param id the id of the expert model to update
* @param update this method is called to update the expert model
* @param element the new data of the expert model
*/
async update(id: string, update: (current: ExpertModel) => ExpertModel) {
async update(id: string, element: Partial<ExpertModel>) {
const expertModel = await this.get(id);
expertModel.update(element);
return this.save(expertModel);
}
async updateDomains(id: string, domains: Partial<Domain>[]) {
const expertModel = await this.get(id);
expertModel.updateDomains(domains);
return this.save(expertModel);
}
async updateAuthor(id: string, author: Partial<Author>) {
const expertModel = await this.get(id);
expertModel.updateAuthor(author);
return this.save(expertModel);
}
async addInstance(id: string, instance: Partial<Instance>) {
const expertModel = await this.get(id);
expertModel.addInstance(instance);
return this.save(expertModel);
}
async removeInstance(id: string, instanceId: number) {
const expertModel = await this.get(id);
return this.save(update(expertModel));
expertModel.removeInstance(instanceId);
return this.save(expertModel);
}
save(expertModel: ExpertModel): Promise<PouchDB.Core.Response> {
......
......@@ -62,10 +62,11 @@ export class CreateCanvasConfigurationComponent
const canvasDefinition = await this.canvasDefinitionService.get(
this.predefinedInput.definitionId
);
const companyModel = await this.companyModelService.createCompanyModel(
const companyModel = this.companyModelService.createCompanyModel(
model,
canvasDefinition
);
await this.companyModelService.add(companyModel.toPouchDb());
this.forceUpdate.emit({
companyModelId: companyModel._id,
automaticCreation: true,
......
......@@ -18,7 +18,6 @@
<app-model-list
modelListTitle="List of Company Business Knowledge Models"
modelFormTitle="Add Company Business Knowledge Model"
[modelList]="companyModelList"
(viewModel)="viewCompanyModel($event)"
(editModel)="editCompanyModel($event)"
(deleteModel)="openDeleteCompanyModelModal($event)"
......
import { Component, OnInit, ViewChild } from '@angular/core';
import { Component, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import { CompanyModel } from '../../../canvas-meta-model/company-model';
import { CompanyModelService } from '../../../canvas-meta-model/company-model.service';
import { CanvasDefinition } from '../../../canvas-meta-model/canvas-definition';
import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
import { ELEMENT_SERVICE, ListService } from '../../../shared/list.service';
@Component({
selector: 'app-company-models',
templateUrl: './company-models.component.html',
styleUrls: ['./company-models.component.css'],
providers: [
ListService,
{ provide: ELEMENT_SERVICE, useExisting: CompanyModelService },
],
})
export class CompanyModelsComponent implements OnInit {
companyModelList: CompanyModel[];
export class CompanyModelsComponent {
modalCompanyModel: CompanyModel;
private modalReference: NgbModalRef;
......@@ -21,24 +24,22 @@ export class CompanyModelsComponent implements OnInit {
constructor(
private companyModelService: CompanyModelService,
private listService: ListService<CompanyModel>,
private modalService: NgbModal,
private router: Router
) {}
ngOnInit() {
this.loadCompanyModelList().then();
}
async addCompanyModel(
definition: CanvasDefinition,
name: string,
description: string
) {
await this.companyModelService.createCompanyModel(
{ name, description },
definition
await this.listService.add(
this.companyModelService.createCompanyModel(
{ name, description },
definition
)
);
this.loadCompanyModelList().then();
}
viewCompanyModel(companyModelId: string): void {
......@@ -57,12 +58,6 @@ export class CompanyModelsComponent implements OnInit {
}
async deleteCompanyModel(featureModelId: string) {
await this.companyModelService.delete(featureModelId);
this.loadCompanyModelList().then();
}
async loadCompanyModelList() {
const result = await this.companyModelService.getList();
this.companyModelList = result.docs;
await this.listService.delete(featureModelId);
}
}
......@@ -6,7 +6,6 @@ import { Instance, InstanceType } from '../../../canvas-meta-model/instance';
import { FormGroup } from '@angular/forms';
import { Domain } from '../../../development-process-registry/knowledge/domain';
import { ExpertModelLoaderService } from '../expert-model-loader.service';
import { ExpertModel } from '../../../canvas-meta-model/expert-model';
@Component({
selector: 'app-expert-model',
......@@ -27,33 +26,27 @@ export class ExpertModelComponent {
}
async update(description: any) {
await this.updateExpertModel((current) => {
current.update(description);
return current;
});
await this.expertModelService.update(this.expertModel._id, description);
}
async updateDomains(domains: Domain[]) {
await this.updateExpertModel((current) => {
current.updateDomains(domains);
return current;
});
await this.expertModelService.updateDomains(this.expertModel._id, domains);
}
async updateFeatureModelAuthor(authorInfo: any) {
await this.updateExpertModel((current) => {
current.updateAuthor(authorInfo);
return current;
});
await this.expertModelService.updateAuthor(
this.expertModel._id,
authorInfo
);
}
/**
* Add a new instance.
*/
async addInstance(instanceForm: FormGroup, type = InstanceType.EXAMPLE) {
await this.updateExpertModel((current) => {
current.addInstance({ name: instanceForm.value.name, type });
return current;
await this.expertModelService.addInstance(this.expertModel._id, {
name: instanceForm.value.name,
type,
});
}
......@@ -90,14 +83,10 @@ export class ExpertModelComponent {
* @param instanceId id of the instance
*/
async deleteInstance(instanceId: number) {
await this.updateExpertModel((current) => {
current.removeInstance(instanceId);
return current;
});
}
async updateExpertModel(update: (current: ExpertModel) => ExpertModel) {
await this.expertModelService.update(this.expertModel._id, update);
await this.expertModelService.removeInstance(
this.expertModel._id,
instanceId
);
}
getExampleInstances(): Instance[] {
......
......@@ -20,7 +20,6 @@
class="expert-model-list"
modelListTitle="List of Expert Business Knowledge Models"
modelFormTitle="Add Expert Business Knowledge Model"
[modelList]="expertModelList"
(viewModel)="viewExpertModel($event)"
(editModel)="editExpertModel($event)"
(deleteModel)="openDeleteExpertModelModal($event)"
......@@ -32,7 +31,7 @@
<app-model-import-view
formTitle="Import Expert Model"
[importExpertModel]="true"
(imported)="loadExpertModelList()"
(imported)="reload()"
></app-model-import-view>
</div>
</main>
import { Component, OnInit, ViewChild } from '@angular/core';
import { Component, ViewChild } from '@angular/core';
import { ExpertModelService } from '../../../canvas-meta-model/expert-model.service';
import { ExpertModel } from '../../../canvas-meta-model/expert-model';
import { Router } from '@angular/router';
import { CanvasDefinition } from '../../../canvas-meta-model/canvas-definition';
import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
import { ELEMENT_SERVICE, ListService } from '../../../shared/list.service';
@Component({
selector: 'app-expert-models',
templateUrl: './expert-models.component.html',
styleUrls: ['./expert-models.component.css'],
providers: [
ListService,
{ provide: ELEMENT_SERVICE, useExisting: ExpertModelService },
],
})
export class ExpertModelsComponent implements OnInit {
expertModelList: ExpertModel[];
export class ExpertModelsComponent {
modalExpertModel: ExpertModel;
private modalReference: NgbModalRef;
......@@ -21,32 +24,29 @@ export class ExpertModelsComponent implements OnInit {
constructor(
private expertModelService: ExpertModelService,
private listService: ListService<ExpertModel>,
private modalService: NgbModal,
private router: Router
) {}
ngOnInit() {
this.loadExpertModelList();
}
async addExpertModel(
definition: CanvasDefinition,
name: string,
description: string
) {
await this.expertModelService.createExpertModel(
const expertModel = await this.expertModelService.createExpertModel(
{ name, description },
definition
);
this.loadExpertModelList();
await this.listService.add(expertModel.toPouchDb());
}
viewExpertModel(expertModelId: string): void {
this.router.navigate(['/expertModels', expertModelId]);
this.router.navigate(['/expertModels', expertModelId]).then();
}
editExpertModel(expertModelId: string): void {
this.router.navigate(['/expertModels', expertModelId, 'edit']);
this.router.navigate(['/expertModels', expertModelId, 'edit']).then();
}
async openDeleteExpertModelModal(featureModelId: string) {
......@@ -56,13 +56,11 @@ export class ExpertModelsComponent implements OnInit {
});
}
async deleteExpertModel(featureModelId: string) {
await this.expertModelService.remove(featureModelId);
this.loadExpertModelList();
async reload() {
await this.listService.load();
}
async loadExpertModelList() {
const result = await this.expertModelService.getList();
this.expertModelList = result.docs;
async deleteExpertModel(featureModelId: string) {
await this.listService.delete(featureModelId);
}
}
<!-- model list -->
<div class="my-3 p-3 bg-white rounded shadow-sm">
<h6 class="border-bottom border-gray pb-2 mb-0">{{ modelListTitle }}</h6>
<ul class="list-unstyled mb-0">
<app-list-wrapper
[listTitle]="modelListTitle"
[reloading]="reloading"
[loading]="loading"
[noResults]="noResults"
>
<div noResults>No entries.</div>
<ul results class="list-unstyled mb-0">
<li
*ngFor="let model of modelList"
class="model-list-item media text-muted pt-2 pb-2 border-bottom"
......@@ -80,7 +85,7 @@
</div>
</li>
</ul>
</div>
</app-list-wrapper>
<!-- Add model -->
<div class="my-3 p-3 bg-white rounded shadow-sm">
......
......@@ -3,6 +3,7 @@ import { FormBuilder, Validators } from '@angular/forms';
import { FeatureModel } from '../../../canvas-meta-model/feature-model';
import { CanvasDefinition } from '../../../canvas-meta-model/canvas-definition';
import { CanvasDefinitionService } from '../../../canvas-meta-model/canvas-definition.service';
import { ListService } from '../../../shared/list.service';
@Component({
selector: 'app-model-list',
......@@ -11,7 +12,6 @@ import { CanvasDefinitionService } from '../../../canvas-meta-model/canvas-defin
})
export class ModelListComponent implements OnInit {
@Input() modelListTitle: string;
@Input() modelList: FeatureModel[];
@Input() modelFormTitle: string;
@Output() viewModel = new EventEmitter<string>();
......@@ -33,7 +33,8 @@ export class ModelListComponent implements OnInit {
constructor(
private canvasDefinitionService: CanvasDefinitionService,
private fb: FormBuilder
private fb: FormBuilder,
private listService: ListService<FeatureModel>
) {}
ngOnInit() {
......@@ -57,4 +58,20 @@ export class ModelListComponent implements OnInit {
get definitionControl() {
return this.modelForm.get('definition');
}
get modelList(): FeatureModel[] {
return this.listService.elements;
}
get noResults(): boolean {
return this.listService.noResults;
}
get loading(): boolean {
return this.listService.loading;
}
get reloading(): boolean {
return this.listService.reloading;
}
}
......@@ -51,7 +51,11 @@ export class ListService<T extends PouchdbModel> {
await this.load();
}
private async load() {
/**
* Loads the elements list.
* Calling this method should be necessary only in some edge cases.
*/
async load() {
this._reloading = true;
this._elements = (await this.elementService.getList()).docs;
this._reloading = false;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment