1818 import lombok .extern .slf4j .Slf4j ;
1919 import org .springframework .boot .CommandLineRunner ;
2020 import org .springframework .stereotype .Component ;
21+ import org .springframework .transaction .annotation .Transactional ;
2122
2223 @ Slf4j
2324 @ RequiredArgsConstructor
@@ -52,35 +53,29 @@ public void run(String... args) {
5253 }
5354
5455 private List <CatalogConnector > extractCatalog (JsonNode rootNode ) {
55- List <CatalogConnector > catalogConnectors = new ArrayList <>();
56-
57- JsonNode contracts = rootNode .get ("contracts" );
58- if (contracts != null && contracts .isArray ()) {
59- for (JsonNode contract : contracts ) {
60- Optional <CatalogConnector > catalog = buildCatalogConnector (contract );
61- catalog .ifPresent (catalogConnectors ::add );
56+ JsonNode contracts = rootNode .get ("contracts" );
57+ if (contracts == null ) {
58+ // TODO throw error ?
59+ return List .of ();
6260 }
63- }
64- List <CatalogConnector > savedConnectors = catalogConnectorService .upsertAll (catalogConnectors );
6561
66- if (contracts != null && contracts .isArray ()) {
67- int index = 0 ;
68- for (JsonNode contract : contracts ) {
69- CatalogConnector savedConnector = savedConnectors .get (index );
70-
71- List <CatalogConnectorConfiguration > configs = buildConnectorConfigurations (contract ,
72- savedConnector );
73- catalogConnectorConfigurationService .upsertAll (configs );
62+ List <CatalogConnector > catalogConnectorList = new ArrayList <>();
7463
75- index ++;
64+ for (JsonNode contract : contracts ) {
65+ CatalogConnector catalogConnector = buildCatalogConnector (contract );
66+ catalogConnectorList .add (catalogConnector );
7667 }
77- }
68+ List < CatalogConnector > savedConnectors = catalogConnectorService . saveAll ( catalogConnectorList );
7869
7970 return savedConnectors ;
8071 }
8172
82- private Optional <CatalogConnector > buildCatalogConnector (JsonNode contract ) {
83- CatalogConnector connector = new CatalogConnector ();
73+ private CatalogConnector buildCatalogConnector (JsonNode contract ) {
74+
75+ CatalogConnector connector = catalogConnectorService
76+ .findBySlug (contract .get ("slug" ).asText ())
77+ .orElseGet (CatalogConnector ::new );
78+
8479
8580 List <String > useCases = new ArrayList <>();
8681 JsonNode arrUseCases = contract .get ("use_cases" );
@@ -125,14 +120,18 @@ private Optional<CatalogConnector> buildCatalogConnector(JsonNode contract) {
125120 connector .setContainerType (CatalogConnector .CONNECTOR_TYPE .valueOf (containerType .trim ().toUpperCase ()));
126121 } else {
127122 log .error ("container_type is null" );
128- return Optional .empty ();
123+ //TODO : return empty
124+ connector .setContainerType (CatalogConnector .CONNECTOR_TYPE .COLLECTOR );
129125 }
130- return Optional .of (connector );
126+
127+ Set <CatalogConnectorConfiguration > conf = buildConnectorConfigurations (contract , connector );
128+ connector .setCatalogConnectorConfigurations (conf );
129+
130+ return connector ;
131131 }
132132
133- private List <CatalogConnectorConfiguration > buildConnectorConfigurations (JsonNode contract ,
134- CatalogConnector connector ) {
135- List <CatalogConnectorConfiguration > configs = new ArrayList <>();
133+ private Set <CatalogConnectorConfiguration > buildConnectorConfigurations (JsonNode contract , CatalogConnector connector ) {
134+ Set <CatalogConnectorConfiguration > configs = new HashSet <>();
136135
137136 JsonNode schema = contract .get ("config_schema" );
138137 if (schema == null || schema .isNull ()) return configs ;
@@ -146,7 +145,9 @@ private List<CatalogConnectorConfiguration> buildConnectorConfigurations(JsonNod
146145 String key = it .next ();
147146 JsonNode prop = properties .get (key );
148147
149- CatalogConnectorConfiguration conf = new CatalogConnectorConfiguration ();
148+ CatalogConnectorConfiguration conf = connector .getCatalogConnectorConfigurations ().stream ()
149+ .filter (c ->key .equals (c .getConnectorConfigurationKey ())).findFirst ()
150+ .orElse ( new CatalogConnectorConfiguration ());
150151 conf .setCatalogConnector (connector );
151152 conf .setConnectorConfigurationKey (key );
152153
0 commit comments