22
33import java .io .IOException ;
44import java .util .Collections ;
5- import java .util .HashMap ;
5+ import java .util .List ;
66import java .util .Map ;
77import java .util .stream .Collectors ;
88
1818import javafx .scene .Node ;
1919import javafx .scene .control .Alert .AlertType ;
2020import javafx .scene .control .Label ;
21- import javafx .scene .control .PasswordField ;
22- import javafx .scene .control .TextField ;
2321import javafx .scene .layout .AnchorPane ;
2422import javafx .scene .layout .StackPane ;
2523import javafx .scene .layout .VBox ;
2624import javafx .scene .paint .Paint ;
2725import javafx .scene .text .Text ;
2826import lombok .AllArgsConstructor ;
2927import lombok .Data ;
30- import root .core .domain .JdbcConnectionInfo ;
31- import root .core .domain .JschConnectionInfo ;
32- import root .core .repository .constracts .PropertyRepository ;
33- import root .core .repository .implement .PropertyRepositoryImpl ;
34- import root .javafx .Service .DatabaseConnectService ;
28+ import root .javafx .Service .ConnectionTestService ;
3529import root .utils .AlertUtils ;
3630
37- public class ConnectionInfoVBox extends VBox {
38-
39- /* Dependency Injection */
40- private PropertyRepository propertyRepository = PropertyRepositoryImpl .getInstance ();
31+ public class ConnectionInfoVBox <T > extends VBox {
4132
4233 @ FXML
4334 Label menuTitleLB ;
@@ -68,15 +59,16 @@ public class ConnectionInfoVBox extends VBox {
6859
6960 @ FXML
7061 JFXButton nextConnInfoBtn ;
71-
72- private Class <? extends AnchorPane > childAPClazz ;
62+
63+ private ConnInfoControl < T > connInfoControl ;
7364
7465 private ConnInfoAPMap connInfoAPMap = new ConnInfoAPMap ();
7566
7667 private long connInfoIdx = -1 ;
7768
78- public ConnectionInfoVBox (Class <? extends AnchorPane > childAPClazz ) {
79- this .childAPClazz = childAPClazz ;
69+ public ConnectionInfoVBox (ConnInfoControl <T > connInfoControl ) {
70+ this .connInfoControl = connInfoControl ;
71+
8072 try {
8173 FXMLLoader loader = new FXMLLoader (getClass ().getResource ("/fxml/ConnectionInfoVBox.fxml" ));
8274 loader .setController (this );
@@ -93,7 +85,7 @@ public void clearConnInfoMap() {
9385 }
9486
9587 public void addConnectionInfoAP (int type , Node connInfoAP ) {
96- long newIdx = connInfoAPMap .put (new StatefulAP (type , (AnchorPane ) connInfoAP ));
88+ long newIdx = connInfoAPMap .put (new StatefulAP (type , (ConnectionInfoAP ) connInfoAP ));
9789 connInfoAP .setId (String .valueOf (newIdx ));
9890 connInfoStackPane .getChildren ().add (connInfoAP );
9991
@@ -108,78 +100,56 @@ public void addConnectionInfoAP(int type, Node connInfoAP) {
108100 }
109101 }
110102
111- // TODO 다형성을 이용해 클래스 타입체크 제거하기
112103 public void saveConnInfoSettings (String configFilePath ) {
113- if (childAPClazz == DBConnectionInfoAnchorPane .class ) {
114- Map <String , JdbcConnectionInfo > config = new HashMap <>();
115-
116- for (StatefulAP childAP : this .connInfoAPMap .getActiveAPs ().values ()) {
117- DBConnectionInfoAnchorPane dbConnAP = (DBConnectionInfoAnchorPane ) childAP .getAp ();
118- JdbcConnectionInfo jdbc = dbConnAP .getInputValues ();
119- config .put (jdbc .getJdbcDBName ().toUpperCase (), jdbc );
120- }
121- propertyRepository .saveDBConnectionInfo (configFilePath , config );
122- } else {
123- Map <String , JschConnectionInfo > config = new HashMap <>();
124-
125- for (StatefulAP childAP : this .connInfoAPMap .getActiveAPs ().values ()) {
126- ServerConnectionInfoAnchorPane serverConnAP = (ServerConnectionInfoAnchorPane ) childAP .getAp ();
127- JschConnectionInfo jsch = serverConnAP .getInputValues ();
128- config .put (jsch .getServerName ().toUpperCase (), jsch );
129- }
130- propertyRepository .saveServerConnectionInfo (configFilePath , config );
104+ connInfoControl .save (configFilePath , this .connInfoAPMap .getActiveAPs ().values ());
105+ }
106+
107+ public void addConnInfoList (List <T > connInfoList ) {
108+ if (connInfoList .isEmpty ()) {
109+ addConnectionInfoAP (2 , connInfoControl .getNewConnInfoAP ());
110+ return ;
111+ }
112+
113+ for (T connInfo : connInfoList ) {
114+ ConnectionInfoAP connInfoAP = connInfoControl .getConnInfoAP (connInfo );
115+ addConnectionInfoAP (1 , connInfoAP );
131116 }
132117 }
133118
134119 /* Button Click Listener */
135120
136121 public void testConnection (ActionEvent e ) {
137- if (childAPClazz == DBConnectionInfoAnchorPane .class ) {
122+
123+ // 현재 AP에 작성된 접속정보를 이용해 연결 테스트
124+ ConnectionInfoAP curAP = connInfoAPMap .get (connInfoIdx ).getAp ();
125+
126+ ConnectionTestService testService = connInfoControl .getConnectionTestService (curAP );
127+
128+ if (testService != null ) {
138129 // 아이콘 변경
139130 setConnectionBtnIcon (4 );
140131
141- AnchorPane curAP = connInfoAPMap .get (connInfoIdx ).getAp ();
142-
143- String jdbcUrl = ((TextField ) curAP .lookup ("#urlTF" )).getText ();
144- String jdbcId = ((TextField ) curAP .lookup ("#userTF" )).getText ();
145- String jdbcPw = ((PasswordField ) curAP .lookup ("#passwordPF" )).getText ();
146-
147- // TODO JdbcDriver, Validation Query 하드코딩 변경 - DBMS에 따라 다르게 해야 함
148- JdbcConnectionInfo jdbc = new JdbcConnectionInfo ("oracle.jdbc.driver.OracleDriver" , jdbcUrl , jdbcId , jdbcPw ,
149- "SELECT 1 FROM DUAL" , 1 );
150-
151- DatabaseConnectService dbConnService = new DatabaseConnectService (jdbc );
152- dbConnService .setOnSucceeded (s -> {
153- AlertUtils .showAlert (AlertType .INFORMATION , "DB 연동테스트" ,
154- String .format (DatabaseConnectService .SUCCESS_MSG , jdbc .getJdbcUrl (), jdbc .getJdbcDriver ()));
132+ // 성공시 콜백 이벤트 설정
133+ testService .setOnSucceeded (s -> {
134+ testService .alertSucceed ();
155135 setConnectionBtnIcon (2 );
156136 });
157-
158- dbConnService . setOnFailed ( f -> {
159- AlertUtils . showAlert ( AlertType . ERROR , "DB 연동테스트" ,
160- String . format ( DatabaseConnectService . FAIL_MSG , jdbc . getJdbcUrl (), jdbc . getJdbcDriver ()) );
137+
138+ // 실패시 콜백 이벤트 설정
139+ testService . setOnFailed ( f -> {
140+ testService . alertFailed ( );
161141 setConnectionBtnIcon (3 );
162142 });
163143
164- dbConnService .start ();
165- } else if (childAPClazz == ServerConnectionInfoAnchorPane .class ) {
166-
144+ // 연결테스트 시작
145+ testService .start ();
146+ } else {
147+ AlertUtils .showAlert (AlertType .ERROR , "연결 테스트" , "연결 테스트를 수행하기 위한 정보가 부족합니다.\n 접속정보를 입력해주세요." );
167148 }
168149 }
169150
170151 public void addNewConnInfo (ActionEvent e ) {
171- if (childAPClazz == DBConnectionInfoAnchorPane .class ) {
172- DBConnectionInfoAnchorPane dbConnAP = new DBConnectionInfoAnchorPane ();
173- dbConnAP .init ();
174- dbConnAP .setInitialValue (new JdbcConnectionInfo ());
175- addConnectionInfoAP (2 , dbConnAP );
176-
177- } else if (childAPClazz == ServerConnectionInfoAnchorPane .class ) {
178- ServerConnectionInfoAnchorPane serverConnAP = new ServerConnectionInfoAnchorPane ();
179- serverConnAP .setInitialValue (new JschConnectionInfo ());
180- addConnectionInfoAP (2 , serverConnAP );
181-
182- }
152+ addConnectionInfoAP (2 , connInfoControl .getNewConnInfoAP ());
183153 }
184154
185155 public void removeConnInfo (ActionEvent e ) {
@@ -220,7 +190,6 @@ public void setMenuTitle(String menuTitle, FontAwesomeIcon menuIcon) {
220190 menuIconIV .setIcon (menuIcon );
221191 }
222192
223-
224193 // When connectionInfo index changed, this method always will be invoked.
225194 private void bringFrontConnInfoAnchorPane (long index ) {
226195 connInfoIdx = index ;
@@ -288,9 +257,9 @@ private void setButtonsDisable(boolean disabled) {
288257
289258 @ AllArgsConstructor
290259 @ Data
291- private static class StatefulAP {
260+ public static class StatefulAP {
292261 private int status ; // 1: 기존, 2: 신규, 3: 제거
293- private AnchorPane ap ;
262+ private ConnectionInfoAP ap ;
294263 }
295264
296265 private static class ConnInfoAPMap {
0 commit comments