11package org .finos .calm .model ;
22
3+ import com .fasterxml .jackson .core .JsonProcessingException ;
4+ import com .fasterxml .jackson .databind .JsonNode ;
35import com .fasterxml .jackson .databind .ObjectMapper ;
46import org .finos .calm .model .canonical .CalmMetadataHelper ;
57import org .finos .calm .model .canonical .CalmRelationshipSchema ;
68
79import java .util .Map ;
810import java .util .Optional ;
911
10- public record CalmRelationship (
11- String uniqueId ,
12- CalmRelationshipType relationshipType ,
13- Optional <String > description ,
14- Optional <CalmProtocol > protocol ,
15- Optional <CalmControls > controls ,
16- Map <String , Object > metadata
17- ) {
18- public CalmRelationship {
19- metadata = metadata == null ? Map .of () : Map .copyOf (metadata );
12+ public final class CalmRelationship {
13+ private final String uniqueId ;
14+ private final CalmRelationshipType relationshipType ;
15+ private final Optional <String > description ;
16+ private final Optional <CalmProtocol > protocol ;
17+ private final Optional <CalmControls > controls ;
18+ private final Map <String , Object > metadata ;
19+ private final Map <String , JsonNode > extensions ;
20+ private final ObjectMapper mapper ;
21+
22+ CalmRelationship (String uniqueId , CalmRelationshipType relationshipType ,
23+ Optional <String > description , Optional <CalmProtocol > protocol ,
24+ Optional <CalmControls > controls , Map <String , Object > metadata ,
25+ Map <String , JsonNode > extensions , ObjectMapper mapper ) {
26+ this .uniqueId = uniqueId ;
27+ this .relationshipType = relationshipType ;
28+ this .description = description ;
29+ this .protocol = protocol ;
30+ this .controls = controls ;
31+ this .metadata = Map .copyOf (metadata );
32+ this .extensions = Map .copyOf (extensions );
33+ this .mapper = mapper ;
2034 }
2135
2236 static CalmRelationship from (CalmRelationshipSchema schema , ObjectMapper mapper ) {
@@ -29,11 +43,40 @@ static CalmRelationship from(CalmRelationshipSchema schema, ObjectMapper mapper)
2943 Optional .ofNullable (schema .getDescription ()),
3044 Optional .ofNullable (schema .getProtocol ()),
3145 controls ,
32- CalmMetadataHelper .flatten (schema .getMetadataRaw (), mapper )
46+ CalmMetadataHelper .flatten (schema .getMetadataRaw (), mapper ),
47+ schema .getExtensions (),
48+ mapper
3349 );
3450 }
3551
52+ public String uniqueId () { return uniqueId ; }
53+ public CalmRelationshipType relationshipType () { return relationshipType ; }
54+ public Optional <String > description () { return description ; }
55+ public Optional <CalmProtocol > protocol () { return protocol ; }
56+ public Optional <CalmControls > controls () { return controls ; }
57+
3658 public Optional <Object > getMetadata (String key ) {
3759 return Optional .ofNullable (metadata .get (key ));
3860 }
61+
62+ public <T > Optional <T > parseMetadata (Class <T > type ) {
63+ if (metadata .isEmpty ()) return Optional .empty ();
64+ try {
65+ return Optional .of (mapper .convertValue (metadata , type ));
66+ } catch (Exception e ) {
67+ throw new CalmExtensionParseException (
68+ "Failed to parse relationship metadata as " + type .getSimpleName (), e );
69+ }
70+ }
71+
72+ public <T > Optional <T > parseExtension (String name , Class <T > type ) {
73+ JsonNode node = extensions .get (name );
74+ if (node == null ) return Optional .empty ();
75+ try {
76+ return Optional .of (mapper .treeToValue (node , type ));
77+ } catch (JsonProcessingException e ) {
78+ throw new CalmExtensionParseException (
79+ "Failed to parse extension '" + name + "' as " + type .getSimpleName (), e );
80+ }
81+ }
3982}
0 commit comments