1313import java .util .ArrayList ;
1414import java .util .List ;
1515import java .util .Objects ;
16+ import java .util .Optional ;
1617import java .util .stream .Collectors ;
1718
1819/**
@@ -30,14 +31,8 @@ public class DatasourceConfigComponent implements PersistentStateComponent<Datas
3031 if (Objects .isNull (config )) {
3132
3233 config = new DatasourceConfig4Save ();
33-
3434 List <DatasourceConfiguration > configurations = new ArrayList <>();
35-
36- DatasourceConfiguration configuration = initConfiguration ();
37-
38- configurations .add (configuration );
39-
40- config .setCurrent ("default" );
35+ config .setCurrent (StringUtils .EMPTY );
4136 config .setConfigurations (configurations );
4237
4338 }
@@ -50,34 +45,14 @@ public DatasourceConfiguration getConfig() {
5045 getState ();
5146 }
5247
53- if (StringUtils .isBlank (config .getCurrent ())) {
54- config .setCurrent ("default" );
55- }
56-
57- DatasourceConfiguration configuration = this .config .getConfigurations ().stream ()
48+ return this .config .getConfigurations ().stream ()
5849 .filter (temp -> StringUtils .equals (this .config .getCurrent (), temp .getName ()))
5950 .findFirst ()
6051 .orElse (null );
61-
62- if (configuration == null ) {
63- configuration = initConfiguration ();
64- config .getConfigurations ().add (configuration );
65- }
66-
67- return configuration ;
68-
6952 }
7053
71- private DatasourceConfiguration initConfiguration () {
72-
73- return new DatasourceConfiguration ()
74- .name ("default" )
75- .host ("localhost" )
76- .port ("3306" )
77- .user ("root" )
78- .password (StringUtils .EMPTY )
79- .database (StringUtils .EMPTY );
80-
54+ public void addDatasourceConfiguration (DatasourceConfiguration configuration ) {
55+ this .config .getConfigurations ().add (configuration );
8156 }
8257
8358 @ Override
@@ -94,30 +69,65 @@ public void setCurrent(String name) {
9469 }
9570
9671 public String getHost () {
72+ if (this .getConfig () == null ) {
73+ return StringUtils .EMPTY ;
74+ }
9775 return this .getConfig ().getHost ();
9876 }
9977
10078 public String getPort () {
79+ if (this .getConfig () == null ) {
80+ return StringUtils .EMPTY ;
81+ }
10182 return this .getConfig ().getPort ();
10283 }
10384
10485 public String getUser () {
86+ if (this .getConfig () == null ) {
87+ return StringUtils .EMPTY ;
88+ }
10589 return this .getConfig ().getUser ();
10690 }
10791
10892 public String getPassword () {
93+ if (this .getConfig () == null ) {
94+ return StringUtils .EMPTY ;
95+ }
10996 return this .getConfig ().getPassword ();
11097 }
11198
11299 public String getDatabase () {
100+ if (this .getConfig () == null ) {
101+ return StringUtils .EMPTY ;
102+ }
113103 return this .getConfig ().getDatabase ();
114104 }
115105
116106 public String getName () {
107+ if (this .getConfig () == null ) {
108+ return StringUtils .EMPTY ;
109+ }
117110 return this .getConfig ().getName ();
118111 }
119112
120113 public List <String > getAllDatasourceNames () {
121114 return this .config .getConfigurations ().stream ().map (DatasourceConfiguration ::getName ).collect (Collectors .toList ());
122115 }
116+
117+ public void remove () {
118+ Optional <DatasourceConfiguration > optional = this .config .getConfigurations ().stream ()
119+ .filter (temp -> StringUtils .equals (this .config .getCurrent (), temp .getName ()))
120+ .findFirst ();
121+
122+ if (!optional .isPresent ()) {
123+ return ;
124+ }
125+ List <DatasourceConfiguration > configurations = this .config .getConfigurations ();
126+ configurations .remove (optional .get ());
127+ if (configurations .size () > 0 ) {
128+ this .config .setCurrent (configurations .get (0 ).getName ());
129+ } else {
130+ this .config .setCurrent (StringUtils .EMPTY );
131+ }
132+ }
123133}
0 commit comments