Skip to content

Commit b321a0f

Browse files
author
Gérard Collin
committed
feat: load complete project definition
1 parent 21f5d5d commit b321a0f

4 files changed

Lines changed: 109 additions & 32 deletions

File tree

apps/xt-host/projects/host/src/app/application-model-manager/application-model-manager.service.ts

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { inject, Injectable, signal } from '@angular/core';
2-
import { DcApplicationModel, DcFieldModel } from '../shared/models/dc-application-model';
2+
import {
3+
DcApplicationModel, DcEntityModel,
4+
DcFieldModel,
5+
isOldProjectModel,
6+
OldDcApplicationModel
7+
} from '../shared/models/dc-application-model';
38
import { XtTypeInfo } from 'xt-type';
49
import { Title } from '@angular/platform-browser';
510

@@ -23,17 +28,21 @@ export class ApplicationModelManagerService {
2328
return null;
2429
}
2530

26-
return Object.keys(entities).length>0?entities[Object.keys(entities)[0]].name:null;
31+
return entities.length>0?entities[0].name:null;
2732
}
2833

29-
setModel(value: DcApplicationModel) {
30-
this.model = value;
34+
setModel(value: DcApplicationModel|OldDcApplicationModel) {
35+
if (isOldProjectModel(value)) {
36+
this.model = this.toNewProjectModel (value);
37+
} else
38+
this.model = value;
39+
3140
if (this.model?.content?.creation?.entities!=null){
3241
this.entityNames.set (Object.values(this.model?.content?.creation?.entities).map((entity) => entity.name));
3342
}else {
3443
this.entityNames.set([]);
3544
}
36-
if (this.model.name!=null) {
45+
if (this.model?.name!=null) {
3746
this.projectTitle.set(this.model.name);
3847
this.titleMgr.setTitle(this.model.name);
3948
}
@@ -50,7 +59,7 @@ export class ApplicationModelManagerService {
5059
getApplicationTypes (): XtTypeInfo|null {
5160
if (this.model?.content.creation.entities!=null) {
5261
const ret={} as XtTypeInfo;
53-
for (const entity of Object.values(this.model!.content.creation.entities)) {
62+
for (const entity of this.model!.content.creation.entities) {
5463
if (entity.fields!=null) {
5564
ret[entity.name] = this.getEntityFields (entity.fields);
5665
}
@@ -61,9 +70,9 @@ export class ApplicationModelManagerService {
6170
}
6271
}
6372

64-
getEntityFields(fields: { [key:string]:DcFieldModel}): XtTypeInfo {
73+
getEntityFields(fields: Array<DcFieldModel>): XtTypeInfo {
6574
const ret = {} as XtTypeInfo;
66-
for (const field of Object.values(fields)) {
75+
for (const field of fields) {
6776
ret[field.name]= this.translate (field.type);
6877
}
6978
return ret;
@@ -85,4 +94,25 @@ export class ApplicationModelManagerService {
8594
return type.toLowerCase();
8695
}
8796
}
97+
98+
/**
99+
* Transform the old project model (that uses objects instead of arrays)
100+
* @param value
101+
* @protected
102+
*/
103+
protected toNewProjectModel(value: OldDcApplicationModel): DcApplicationModel {
104+
const ret ={ name:value.name,
105+
description: value.description,
106+
content: { creation: { entities:new Array<DcEntityModel>()}}
107+
} as DcApplicationModel;
108+
109+
for (const entity of Object.values(value.content.creation.entities??{})) {
110+
const newEntity = { name:entity.name, fields:new Array<DcFieldModel>} as DcEntityModel;
111+
for (const field of Object.values(entity.fields??{})) {
112+
newEntity.fields!.push(field);
113+
}
114+
ret.content!.creation!.entities!.push(newEntity);
115+
}
116+
return ret;
117+
}
88118
}

apps/xt-host/projects/host/src/app/project-load/project-load.component.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,23 @@ export class ProjectLoadComponent implements OnInit {
3737
private readonly injector = inject(Injector);
3838

3939
ngOnInit() {
40-
let projectName = this.route.snapshot.paramMap.get('projectName');
41-
if ((projectName==null) || (projectName.length==0)) {
42-
projectName = this.route.snapshot.queryParamMap.get('project')??'Coffee Beans Evaluation';
40+
// Either we get the complete project definition, or only the name to be read from project API
41+
let projectName:string = 'Coffee Beans Evaluation';
42+
let project = this.route.snapshot.queryParamMap.get('prjDef');
43+
if (project==null) {
44+
let givenPrjName = this.route.snapshot.paramMap.get('projectName');
45+
if ((givenPrjName==null) || (givenPrjName.length==0)) {
46+
projectName = this.route.snapshot.queryParamMap.get('project')??projectName;
47+
} else projectName=givenPrjName;
4348
}
49+
4450
const repoName = this.route.snapshot.paramMap.get('repoName') ?? this.route.snapshot.queryParamMap.get('repository')?? 'default';
4551
this.appConfig.updateConfigName(repoName); // Load the default config
46-
this.appConfig.updateProjectName(projectName);
52+
if (project!=null) {
53+
this.appConfig.updateProjectDefinition(project);
54+
}else {
55+
this.appConfig.updateProjectName(projectName);
56+
}
4757
}
4858

4959
combinedloadingStatus = linkedSignal( () => {

apps/xt-host/projects/host/src/app/shared/app-config/app-config.service.ts

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ export class AppConfigService {
1111

1212
protected configResources = {
1313
configName: signal<string|undefined>(undefined),
14-
projectName: signal<string|undefined>(undefined)
14+
projectName: signal<string|undefined>(undefined),
15+
projectDefinition: signal<string|undefined>(undefined)
1516
}
1617

1718
protected resolverService = inject (XtResolverService);
@@ -96,25 +97,30 @@ export class AppConfigService {
9697
const ret= {
9798
pluginsLoaded: (this.plugins.status()=='resolved')||(this.plugins.status()=='local'),
9899
projectUrl:this.config.value()?.projectApiUrl,
99-
projectName: this.configResources.projectName()
100+
projectName: this.configResources.projectName(),
101+
projectDefinition: this.configResources.projectDefinition()
100102
}
101103
return ret;
102104
},
103105
loader: (options) => {
104-
if ((options.params.pluginsLoaded) && (options.params.projectName!=null)) {
105-
let projectUrl = options.params.projectUrl;
106-
if (projectUrl==null) {
107-
projectUrl='assets/projects/'+encodeURIComponent (options.params.projectName)+'.json';
108-
} else {
109-
projectUrl = new URL (options.params.projectName, projectUrl.endsWith('/')?projectUrl:projectUrl+'/').toString();
106+
if (options.params.pluginsLoaded) {
107+
if (options.params.projectDefinition!=null) {
108+
return Promise.resolve(options.params.projectDefinition);
109+
} else if (options.params.projectName!=null) {
110+
let projectUrl = options.params.projectUrl;
111+
if (projectUrl==null) {
112+
projectUrl='assets/projects/'+encodeURIComponent (options.params.projectName)+'.json';
113+
} else {
114+
projectUrl = new URL (options.params.projectName, projectUrl.endsWith('/')?projectUrl:projectUrl+'/').toString();
115+
}
116+
return fetch(projectUrl).then ((response) => {
117+
return response.json();
118+
});
119+
}else {
120+
return Promise.reject('No project name to load');
110121
}
111-
return fetch(projectUrl).then ((response) => {
112-
return response.json();
113-
});
114-
} else if (!options.params.pluginsLoaded) {
115-
return Promise.resolve();
116122
} else {
117-
return Promise.reject('No project name to load');
123+
return Promise.resolve();
118124
}
119125
}
120126
});
@@ -154,4 +160,9 @@ export class AppConfigService {
154160
this.configResources.projectName.set(newName);
155161
}
156162

163+
updateProjectDefinition (prjDef:string) {
164+
this.configResources.projectDefinition.set(prjDef);
165+
}
166+
167+
157168
}

apps/xt-host/projects/host/src/app/shared/models/dc-application-model.ts

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ export type DcApplicationModel = {
66
content: {
77
creation: {
88
type?: string,
9-
entities?: {
10-
[key:string]:DcEntityModel
11-
},
9+
entities?: Array<DcEntityModel>,
1210
sharing?: {
1311
with: 'Dont-code users'|'No-one'|'Volatile'
1412
}
@@ -18,9 +16,7 @@ export type DcApplicationModel = {
1816

1917
export type DcEntityModel = {
2018
name: string,
21-
fields?: {
22-
[key:string]:DcFieldModel
23-
},
19+
fields?: Array<DcFieldModel>,
2420
compatibleWith?: string[]
2521
}
2622

@@ -29,3 +25,33 @@ export type DcFieldModel = {
2925
type: string,
3026
reference?: XtTypeReference
3127
}
28+
29+
export type OldDcApplicationModel = {
30+
name:string,
31+
description?: string,
32+
content: {
33+
creation: {
34+
type?: string,
35+
entities?: {
36+
[key:string]:OldDcEntityModel
37+
},
38+
sharing?: {
39+
with: 'Dont-code users'|'No-one'|'Volatile'
40+
}
41+
}
42+
}
43+
}
44+
45+
export type OldDcEntityModel = {
46+
name: string,
47+
fields?: {
48+
[key:string]:DcFieldModel
49+
},
50+
compatibleWith?: string[]
51+
}
52+
53+
export function isOldProjectModel(prj:DcApplicationModel|OldDcApplicationModel):prj is OldDcApplicationModel {
54+
if (prj.content.creation.entities != null) {
55+
return !Array.isArray(prj.content.creation.entities);
56+
} else return false;
57+
}

0 commit comments

Comments
 (0)