11import { 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' ;
38import { XtTypeInfo } from 'xt-type' ;
49import { 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,29 @@ 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 : {
107+ type :value . content . creation . type ,
108+ entities :new Array < DcEntityModel > ( ) ,
109+ sharing : value . content . creation . sharing
110+ } }
111+ } as DcApplicationModel ;
112+
113+ for ( const entity of Object . values ( value . content . creation . entities ?? { } ) ) {
114+ const newEntity = { name :entity . name , fields :new Array < DcFieldModel > } as DcEntityModel ;
115+ for ( const field of Object . values ( entity . fields ?? { } ) ) {
116+ newEntity . fields ! . push ( field ) ;
117+ }
118+ ret . content ! . creation ! . entities ! . push ( newEntity ) ;
119+ }
120+ return ret ;
121+ }
88122}
0 commit comments