-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathedmx-to-vdm.ts
186 lines (172 loc) · 5.25 KB
/
edmx-to-vdm.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import { ServiceNameFormatter } from '../../../service-name-formatter';
import {
VdmNavigationProperty,
VdmComplexType,
VdmEntity,
VdmFunctionImport
} from '../../../vdm-types';
import { JoinedEntityMetadata, ParsedServiceMetadata } from '../common';
import {
joinEntityMetadata,
createEntityClassNames,
transformEntity,
navigationPropertyBase,
swaggerDefinitionForFunctionImport,
parseReturnType,
transformFunctionImportBase
} from '../common/edmx-to-vdm';
import { stripNamespace } from '../../../edmx-to-vdm/edmx-to-vdm-util';
import {
EdmxAssociationSet,
EdmxAssociation,
EdmxEntityType,
EdmxMetadata,
EdmxFunctionImport
} from './parser-types';
/* eslint-disable valid-jsdoc */
/**
* @deprecated Since version 1.25.0 due to major generator refactoring.
*/
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
};
});
}
/**
* @deprecated Since version 1.25.0 due to major generator refactoring.
*/
export function transformEntitiesV2(
serviceMetadata: ParsedServiceMetadata,
complexTypes: VdmComplexType[],
formatter: ServiceNameFormatter
): Omit<VdmEntity, 'entityTypeNamespace'>[] {
const edmxMetadata = serviceMetadata.edmx as EdmxMetadata;
const entitiesMetadata = joinEntityMetadata(serviceMetadata);
const classNames = createEntityClassNames(entitiesMetadata, formatter);
const associations = joinAssociationMetadata(
edmxMetadata.associationSets,
edmxMetadata.associations
);
return entitiesMetadata.map(entityMetadata => ({
...transformEntity(entityMetadata, classNames, complexTypes, formatter),
navigationProperties: navigationProperties(
entityMetadata,
associations,
classNames,
formatter
)
}));
}
/**
* @deprecated Since version 1.25.0 due to major generator refactoring.
*/
export function transformFunctionImportsV2(
serviceMetadata: ParsedServiceMetadata,
entities: VdmEntity[],
complexTypes: VdmComplexType[],
formatter: ServiceNameFormatter
): VdmFunctionImport[] {
const edmxFunctionImports = serviceMetadata.edmx
.functionImports as EdmxFunctionImport[];
return edmxFunctionImports.map(f => {
const httpMethod = f['m:HttpMethod'].toLowerCase();
const swaggerDefinition = swaggerDefinitionForFunctionImport(
serviceMetadata,
f.Name,
httpMethod
);
return {
...transformFunctionImportBase(
f,
f.Parameter,
swaggerDefinition,
formatter
),
httpMethod,
returnType: parseReturnType(f.ReturnType, entities, complexTypes),
returnTypeEdmx: f.ReturnType
};
});
}
function navigationProperties(
entity: JoinedEntityMetadata,
associations: JoinedAssociationMetadata[],
classNames: { [originalName: string]: string },
formatter: ServiceNameFormatter
): VdmNavigationProperty[] {
const entityType = entity.entityType as EdmxEntityType;
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('*')
};
});
}
/**
* @deprecated Since version 1.25.0 due to major generator refactoring.
*/
export interface JoinedAssociationMetadata {
Name: string;
'sap:creatable': string;
'sap:updatable': string;
'sap:deletable': string;
'sap:content-version': string;
Ends: End[];
}
interface End {
EntitySet: string;
Type: string;
Multiplicity: string;
Role: string;
}