44import com .dataengine .collection .domain .model .DataSource ;
55import com .dataengine .collection .interfaces .api .DataSourceApi ;
66import com .dataengine .collection .interfaces .dto .*;
7+ import com .fasterxml .jackson .core .type .TypeReference ;
8+ import com .fasterxml .jackson .databind .ObjectMapper ;
79import lombok .RequiredArgsConstructor ;
10+ import lombok .extern .slf4j .Slf4j ;
811import org .springframework .http .ResponseEntity ;
912import org .springframework .validation .annotation .Validated ;
1013import org .springframework .web .bind .annotation .RestController ;
1720@ RestController
1821@ RequiredArgsConstructor
1922@ Validated
23+ @ Slf4j
2024public class DataSourceController implements DataSourceApi {
2125
2226 private final DataSourceService dataSourceService ;
27+ private final ObjectMapper objectMapper ;
2328
2429 private DataSourceResponse toResponse (DataSource ds ) {
30+ if (ds == null ) {
31+ return null ;
32+ }
33+
2534 DataSourceResponse r = new DataSourceResponse ();
2635 r .setId (ds .getId ());
2736 r .setName (ds .getName ());
2837 r .setDescription (ds .getDescription ());
29- r .setType (DataSourceType .fromValue (ds .getType ().name ()));
30- r .setStatus (DataSourceResponse .StatusEnum .fromValue (ds .getStatus ().name ()));
31- r .setConfig (Map .of ());
38+
39+ // 安全处理枚举转换
40+ if (ds .getType () != null ) {
41+ try {
42+ r .setType (DataSourceType .fromValue (ds .getType ().name ()));
43+ } catch (Exception e ) {
44+ log .error ("Failed to convert data source type: {} for datasource id: {}, error: {}" ,
45+ ds .getType (), ds .getId (), e .getMessage ());
46+ // 设置一个默认值或者抛出异常
47+ r .setType (DataSourceType .MYSQL );
48+ }
49+ }
50+
51+ if (ds .getStatus () != null ) {
52+ try {
53+ r .setStatus (DataSourceResponse .StatusEnum .fromValue (ds .getStatus ().name ()));
54+ } catch (Exception e ) {
55+ log .error ("Failed to convert data source status: {} for datasource id: {}, error: {}" ,
56+ ds .getStatus (), ds .getId (), e .getMessage ());
57+ r .setStatus (DataSourceResponse .StatusEnum .ACTIVE );
58+ }
59+ }
60+
61+ r .setConfig (parseConfigJson (ds .getConfig ()));
3262 return r ;
3363 }
3464
65+ private Map <String , Object > parseConfigJson (String configJson ) {
66+ if (configJson == null || configJson .trim ().isEmpty ()) {
67+ return Map .of ();
68+ }
69+
70+ String trimmedJson = configJson .trim ();
71+ if (trimmedJson .equals ("{}" )) {
72+ return Map .of ();
73+ }
74+
75+ try {
76+ return objectMapper .readValue (trimmedJson , new TypeReference <Map <String , Object >>() {});
77+ } catch (Exception e ) {
78+ // 记录详细的错误信息用于调试
79+ log .warn ("Failed to parse config JSON: {}, error: {}" , trimmedJson , e .getMessage ());
80+ // 返回包含错误信息的Map,便于前端处理
81+ return Map .of (
82+ "error" , "Invalid JSON format" ,
83+ "message" , e .getMessage (),
84+ "raw" , trimmedJson
85+ );
86+ }
87+ }
88+
3589 @ Override
3690 public ResponseEntity <PagedDataSources > datasourcesGet (Integer page , Integer size , DataSourceType type , String status ) {
3791 List <DataSource > list = dataSourceService .list (page , size ,
@@ -56,24 +110,59 @@ public ResponseEntity<Void> datasourcesIdDelete(String id) {
56110
57111 @ Override
58112 public ResponseEntity <DataSourceResponse > datasourcesIdGet (String id ) {
113+ log .debug ("Getting data source by id: {}" , id );
59114 DataSource ds = dataSourceService .get (id );
60115 return ds == null ? ResponseEntity .notFound ().build () : ResponseEntity .ok (toResponse (ds ));
61116 }
62117
63118 @ Override
64119 public ResponseEntity <DataSourceResponse > datasourcesIdPut (String id , UpdateDataSourceRequest body ) {
65- DataSource ds = new DataSource ();
66- ds .setId (id );
67- ds .setName (body .getName ());
68- ds .setDescription (body .getDescription ());
69- ds .setType (com .dataengine .collection .domain .model .DataSourceType .MYSQL );
70- ds .setConfig ("{}" );
71- ds = dataSourceService .update (ds );
72- return ResponseEntity .ok (toResponse (ds ));
120+ log .debug ("Updating data source: {}" , id );
121+
122+ // 1. 先从数据库查询现有数据源
123+ DataSource existingDs = dataSourceService .get (id );
124+ if (existingDs == null ) {
125+ log .warn ("Data source not found for update: {}" , id );
126+ return ResponseEntity .notFound ().build ();
127+ }
128+
129+ // 2. 只更新传入的非空字段,保持其他字段不变
130+ if (body .getName () != null && !body .getName ().trim ().isEmpty ()) {
131+ log .debug ("Updating name for datasource {}: {} -> {}" , id , existingDs .getName (), body .getName ().trim ());
132+ existingDs .setName (body .getName ().trim ());
133+ }
134+
135+ if (body .getDescription () != null ) {
136+ existingDs .setDescription (body .getDescription ());
137+ }
138+
139+ // 3. 处理config配置 - 只有在传入config时才更新
140+ if (body .getConfig () != null ) {
141+ if (body .getConfig ().isEmpty ()) {
142+ // 如果传入空的config,设置为空JSON对象
143+ existingDs .setConfig ("{}" );
144+ } else {
145+ try {
146+ String configJson = objectMapper .writeValueAsString (body .getConfig ());
147+ existingDs .setConfig (configJson );
148+ } catch (Exception e ) {
149+ log .error ("Failed to serialize config to JSON for datasource {}: {}" , id , e .getMessage ());
150+ return ResponseEntity .badRequest ().build ();
151+ }
152+ }
153+ }
154+ // 如果没有传入config,保持原有配置不变
155+
156+ // 4. 更新数据源
157+ log .debug ("Saving updated data source: {}" , id );
158+ DataSource updatedDs = dataSourceService .update (existingDs );
159+ log .info ("Successfully updated data source: {}" , id );
160+ return ResponseEntity .ok (toResponse (updatedDs ));
73161 }
74162
75163 @ Override
76164 public ResponseEntity <ConnectionTestResult > datasourcesIdTestPost (String id ) {
165+ //TODO
77166 ConnectionTestResult r = new ConnectionTestResult ();
78167 r .setSuccess (true );
79168 r .setMessage ("OK" );
@@ -85,12 +174,54 @@ public ResponseEntity<ConnectionTestResult> datasourcesIdTestPost(String id) {
85174
86175 @ Override
87176 public ResponseEntity <DataSourceResponse > datasourcesPost (CreateDataSourceRequest body ) {
177+ log .debug ("Creating new data source: {}" , body .getName ());
178+
179+ // 1. 验证必填字段
180+ if (body .getName () == null || body .getName ().trim ().isEmpty ()) {
181+ log .warn ("Data source name is required" );
182+ return ResponseEntity .badRequest ().build ();
183+ }
184+
185+ if (body .getType () == null ) {
186+ log .warn ("Data source type is required" );
187+ return ResponseEntity .badRequest ().build ();
188+ }
189+
190+ // 2. 创建数据源对象
88191 DataSource ds = new DataSource ();
89- ds .setName (body .getName ());
192+ ds .setName (body .getName (). trim () );
90193 ds .setDescription (body .getDescription ());
91- ds .setType (com .dataengine .collection .domain .model .DataSourceType .valueOf (body .getType ().getValue ()));
92- ds .setConfig ("{}" );
93- ds = dataSourceService .create (ds );
94- return ResponseEntity .status (201 ).body (toResponse (ds ));
194+
195+ // 3. 处理数据源类型
196+ try {
197+ ds .setType (com .dataengine .collection .domain .model .DataSourceType .valueOf (body .getType ().getValue ()));
198+ } catch (IllegalArgumentException e ) {
199+ log .error ("Invalid data source type: {}" , body .getType ().getValue ());
200+ return ResponseEntity .badRequest ().build ();
201+ }
202+
203+ // 4. 处理config配置
204+ if (body .getConfig () != null && !body .getConfig ().isEmpty ()) {
205+ try {
206+ String configJson = objectMapper .writeValueAsString (body .getConfig ());
207+ ds .setConfig (configJson );
208+ } catch (Exception e ) {
209+ log .error ("Failed to serialize config to JSON: {}" , e .getMessage ());
210+ return ResponseEntity .badRequest ().build ();
211+ }
212+ } else {
213+ ds .setConfig ("{}" );
214+ }
215+
216+ // 5. 创建数据源
217+ try {
218+ log .debug ("Saving new data source: {}" , ds .getName ());
219+ DataSource createdDs = dataSourceService .create (ds );
220+ log .info ("Successfully created data source: {} with id: {}" , createdDs .getName (), createdDs .getId ());
221+ return ResponseEntity .status (201 ).body (toResponse (createdDs ));
222+ } catch (Exception e ) {
223+ log .error ("Failed to create data source: {}" , e .getMessage (), e );
224+ return ResponseEntity .internalServerError ().build ();
225+ }
95226 }
96227}
0 commit comments