1
1
package com .linkedin .datahub .graphql .resolvers .structuredproperties ;
2
2
3
3
import static com .linkedin .datahub .graphql .resolvers .ResolverUtils .bindArgument ;
4
- import static com .linkedin .metadata .Constants .STRUCTURED_PROPERTY_ENTITY_NAME ;
4
+ import static com .linkedin .datahub .graphql .resolvers .mutate .MutationUtils .buildMetadataChangeProposalWithUrn ;
5
+ import static com .linkedin .metadata .Constants .*;
5
6
6
7
import com .linkedin .common .urn .Urn ;
7
8
import com .linkedin .data .template .SetMode ;
12
13
import com .linkedin .datahub .graphql .exception .AuthorizationException ;
13
14
import com .linkedin .datahub .graphql .generated .CreateStructuredPropertyInput ;
14
15
import com .linkedin .datahub .graphql .generated .StructuredPropertyEntity ;
16
+ import com .linkedin .datahub .graphql .generated .StructuredPropertySettingsInput ;
15
17
import com .linkedin .datahub .graphql .types .structuredproperty .StructuredPropertyMapper ;
16
18
import com .linkedin .entity .EntityResponse ;
17
19
import com .linkedin .entity .client .EntityClient ;
18
20
import com .linkedin .metadata .aspect .patch .builder .StructuredPropertyDefinitionPatchBuilder ;
21
+ import com .linkedin .metadata .models .StructuredPropertyUtils ;
19
22
import com .linkedin .metadata .utils .EntityKeyUtils ;
20
23
import com .linkedin .mxe .MetadataChangeProposal ;
21
24
import com .linkedin .structured .PrimitivePropertyValue ;
22
25
import com .linkedin .structured .PropertyCardinality ;
23
26
import com .linkedin .structured .PropertyValue ;
24
27
import com .linkedin .structured .StructuredPropertyKey ;
28
+ import com .linkedin .structured .StructuredPropertySettings ;
25
29
import graphql .schema .DataFetcher ;
26
30
import graphql .schema .DataFetchingEnvironment ;
31
+ import java .util .ArrayList ;
32
+ import java .util .List ;
27
33
import java .util .Objects ;
28
- import java .util .UUID ;
29
34
import java .util .concurrent .CompletableFuture ;
30
35
import javax .annotation .Nonnull ;
31
36
@@ -54,40 +59,28 @@ public CompletableFuture<StructuredPropertyEntity> get(final DataFetchingEnviron
54
59
"Unable to create structured property. Please contact your admin." );
55
60
}
56
61
final StructuredPropertyKey key = new StructuredPropertyKey ();
57
- final String id = input .getId () != null ? input .getId () : UUID .randomUUID ().toString ();
62
+ final String id =
63
+ StructuredPropertyUtils .getPropertyId (input .getId (), input .getQualifiedName ());
58
64
key .setId (id );
59
65
final Urn propertyUrn =
60
66
EntityKeyUtils .convertEntityKeyToUrn (key , STRUCTURED_PROPERTY_ENTITY_NAME );
61
- StructuredPropertyDefinitionPatchBuilder builder =
62
- new StructuredPropertyDefinitionPatchBuilder ().urn (propertyUrn );
63
-
64
- builder .setQualifiedName (input .getQualifiedName ());
65
- builder .setValueType (input .getValueType ());
66
- input .getEntityTypes ().forEach (builder ::addEntityType );
67
- if (input .getDisplayName () != null ) {
68
- builder .setDisplayName (input .getDisplayName ());
69
- }
70
- if (input .getDescription () != null ) {
71
- builder .setDescription (input .getDescription ());
72
- }
73
- if (input .getImmutable () != null ) {
74
- builder .setImmutable (input .getImmutable ());
75
- }
76
- if (input .getTypeQualifier () != null ) {
77
- buildTypeQualifier (input , builder );
78
- }
79
- if (input .getAllowedValues () != null ) {
80
- buildAllowedValues (input , builder );
67
+
68
+ if (_entityClient .exists (context .getOperationContext (), propertyUrn )) {
69
+ throw new IllegalArgumentException (
70
+ "A structured property already exists with this urn" );
81
71
}
82
- if (input .getCardinality () != null ) {
83
- builder .setCardinality (
84
- PropertyCardinality .valueOf (input .getCardinality ().toString ()));
72
+
73
+ List <MetadataChangeProposal > mcps = new ArrayList <>();
74
+
75
+ // first, create the property definition itself
76
+ mcps .add (createPropertyDefinition (context , propertyUrn , id , input ));
77
+
78
+ // then add the settings aspect if we're adding any settings inputs
79
+ if (input .getSettings () != null ) {
80
+ mcps .add (createPropertySettings (context , propertyUrn , input .getSettings ()));
85
81
}
86
- builder .setCreated (context .getOperationContext ().getAuditStamp ());
87
- builder .setLastModified (context .getOperationContext ().getAuditStamp ());
88
82
89
- MetadataChangeProposal mcp = builder .build ();
90
- _entityClient .ingestProposal (context .getOperationContext (), mcp , false );
83
+ _entityClient .batchIngestProposals (context .getOperationContext (), mcps , false );
91
84
92
85
EntityResponse response =
93
86
_entityClient .getV2 (
@@ -103,6 +96,72 @@ public CompletableFuture<StructuredPropertyEntity> get(final DataFetchingEnviron
103
96
});
104
97
}
105
98
99
+ private MetadataChangeProposal createPropertySettings (
100
+ @ Nonnull final QueryContext context ,
101
+ @ Nonnull final Urn propertyUrn ,
102
+ final StructuredPropertySettingsInput settingsInput )
103
+ throws Exception {
104
+ StructuredPropertySettings settings = new StructuredPropertySettings ();
105
+
106
+ if (settingsInput .getIsHidden () != null ) {
107
+ settings .setIsHidden (settingsInput .getIsHidden ());
108
+ }
109
+ if (settingsInput .getShowInSearchFilters () != null ) {
110
+ settings .setShowInSearchFilters (settingsInput .getShowInSearchFilters ());
111
+ }
112
+ if (settingsInput .getShowInAssetSummary () != null ) {
113
+ settings .setShowInAssetSummary (settingsInput .getShowInAssetSummary ());
114
+ }
115
+ if (settingsInput .getShowAsAssetBadge () != null ) {
116
+ settings .setShowAsAssetBadge (settingsInput .getShowAsAssetBadge ());
117
+ }
118
+ if (settingsInput .getShowInColumnsTable () != null ) {
119
+ settings .setShowInColumnsTable (settingsInput .getShowInColumnsTable ());
120
+ }
121
+ settings .setLastModified (context .getOperationContext ().getAuditStamp ());
122
+
123
+ StructuredPropertyUtils .validatePropertySettings (settings , true );
124
+
125
+ return buildMetadataChangeProposalWithUrn (
126
+ propertyUrn , STRUCTURED_PROPERTY_SETTINGS_ASPECT_NAME , settings );
127
+ }
128
+
129
+ private MetadataChangeProposal createPropertyDefinition (
130
+ @ Nonnull final QueryContext context ,
131
+ @ Nonnull final Urn propertyUrn ,
132
+ @ Nonnull final String id ,
133
+ final CreateStructuredPropertyInput input )
134
+ throws Exception {
135
+ StructuredPropertyDefinitionPatchBuilder builder =
136
+ new StructuredPropertyDefinitionPatchBuilder ().urn (propertyUrn );
137
+
138
+ builder .setQualifiedName (id );
139
+ builder .setValueType (input .getValueType ());
140
+ input .getEntityTypes ().forEach (builder ::addEntityType );
141
+ if (input .getDisplayName () != null ) {
142
+ builder .setDisplayName (input .getDisplayName ());
143
+ }
144
+ if (input .getDescription () != null ) {
145
+ builder .setDescription (input .getDescription ());
146
+ }
147
+ if (input .getImmutable () != null ) {
148
+ builder .setImmutable (input .getImmutable ());
149
+ }
150
+ if (input .getTypeQualifier () != null ) {
151
+ buildTypeQualifier (input , builder );
152
+ }
153
+ if (input .getAllowedValues () != null ) {
154
+ buildAllowedValues (input , builder );
155
+ }
156
+ if (input .getCardinality () != null ) {
157
+ builder .setCardinality (PropertyCardinality .valueOf (input .getCardinality ().toString ()));
158
+ }
159
+ builder .setCreated (context .getOperationContext ().getAuditStamp ());
160
+ builder .setLastModified (context .getOperationContext ().getAuditStamp ());
161
+
162
+ return builder .build ();
163
+ }
164
+
106
165
private void buildTypeQualifier (
107
166
@ Nonnull final CreateStructuredPropertyInput input ,
108
167
@ Nonnull final StructuredPropertyDefinitionPatchBuilder builder ) {
0 commit comments