-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathentity.ts
145 lines (134 loc) · 4.18 KB
/
entity.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* Copyright (c) 2020 SAP SE or an SAP affiliate company. All rights reserved. */
import {
createEntityClassNames,
joinEntityMetadata,
navigationPropertyBase,
transformEntityBase
} from '../common';
import {
VdmComplexType,
VdmEntity,
VdmNavigationProperty
} from '../../vdm-types';
import { ServiceNameFormatter } from '../../service-name-formatter';
import {
EdmxEntitySetBase,
JoinedEntityMetadata
} from '../../edmx-parser/common';
import {
EdmxAssociation,
EdmxAssociationSet,
EdmxEntityType,
End,
JoinedAssociationMetadata,
parseAssociation,
parseAssociationSets,
parseEntitySets,
parseEntityTypes
} from '../../edmx-parser/v2';
import { ServiceMetadata } from '../../edmx-parser/edmx-file-reader';
import { stripNamespace } from '../edmx-to-vdm-util';
export function generateEntitiesV2(
serviceMetadata: ServiceMetadata,
complexTypes: Omit<VdmComplexType, 'factoryName'>[],
formatter: ServiceNameFormatter
): VdmEntity[] {
const entitySets = parseEntitySets(serviceMetadata.edmx.root);
const entityTypes = parseEntityTypes(serviceMetadata.edmx.root);
const entitiesMetadata = joinEntityMetadata(
entitySets,
entityTypes,
serviceMetadata.edmx.namespace,
serviceMetadata.swagger
);
const classNames = createEntityClassNames(entitiesMetadata, formatter);
const associations = joinAssociationMetadata(
parseAssociationSets(serviceMetadata.edmx.root),
parseAssociation(serviceMetadata.edmx.root)
);
return entitiesMetadata.map(entityMetadata => ({
...transformEntityBase(
entityMetadata,
classNames,
complexTypes,
[],
formatter
),
navigationProperties: navigationProperties(
entityMetadata,
associations,
classNames,
formatter
)
}));
}
function navigationProperties(
entity: JoinedEntityMetadata<EdmxEntitySetBase, EdmxEntityType>,
associations: JoinedAssociationMetadata[],
classNames: { [originalName: string]: string },
formatter: ServiceNameFormatter
): VdmNavigationProperty[] {
const entityType = entity.entityType;
return entityType.NavigationProperty.map(navProp => {
const relationship = navProp.Relationship.split('.').pop();
const association = associations
.filter(ass => ass.Name === relationship)
.pop();
if (!association) {
throw Error(
`Unable to find the association with the name: ${relationship}`
);
}
const from = association.Ends.find(end => end.Role === navProp.FromRole);
const to = association.Ends.find(end => end.Role === navProp.ToRole);
if (!from) {
throw Error(
`Unable to get the role property of the association ends: ${association.Ends} with the name: ${navProp.FromRole}`
);
}
if (!to) {
throw Error(
`Unable to get the role property of the association ends: ${association.Ends} with the name: ${navProp.ToRole}`
);
}
return {
...navigationPropertyBase(navProp.Name, entity.entitySet.Name, formatter),
from: entity.entityType.Name,
to: to.EntitySet,
toEntityClassName: classNames[to.EntitySet],
multiplicity: from.Multiplicity + ' - ' + to.Multiplicity,
isMultiLink: to.Multiplicity.endsWith('*'),
isCollection: to.Multiplicity.endsWith('*')
};
});
}
export function joinAssociationMetadata(
associationSets: EdmxAssociationSet[],
associations: EdmxAssociation[]
): JoinedAssociationMetadata[] {
return associationSets.map(assocSet => {
const matchingAssoc = associations.find(
a => a.Name === stripNamespace(assocSet.Association)
);
if (!matchingAssoc) {
throw Error(
`Unable to match the association set: ${assocSet.Association} with associations: ${associations}.`
);
}
const ends = assocSet.End.map(
assocSetEnd =>
({
...assocSetEnd,
...matchingAssoc.End.find(end => end.Role === assocSetEnd.Role)
} as End)
);
return {
Name: matchingAssoc.Name,
'sap:creatable': assocSet['sap:creatable'],
'sap:updatable': assocSet['sap:updatable'],
'sap:deletable': assocSet['sap:deletable'],
'sap:content-version': assocSet['sap:content-version'],
Ends: ends
};
});
}