Skip to content

Commit 5462f57

Browse files
committed
Refactoring: Server settings save
1 parent 4ed4d5c commit 5462f57

File tree

3 files changed

+89
-136
lines changed

3 files changed

+89
-136
lines changed

src/main/java/root/javafx/Controller/SettingMenuController.java

Lines changed: 5 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import java.util.Map;
88
import java.util.Optional;
99
import java.util.ResourceBundle;
10-
import java.util.regex.Pattern;
1110

1211
import org.apache.commons.configuration2.PropertiesConfiguration;
1312
import org.apache.log4j.Logger;
@@ -64,8 +63,6 @@ public class SettingMenuController implements Initializable {
6463
* - 따라서, 한 번 생성하두고 이를 재사용하는 것이 효과적이다.
6564
* - 뿐만 아니라, 패턴 객체에 이름을 부여하여 해당 객체의 의미가 명확해진다.
6665
*/
67-
private static Pattern DB_CONNINFO_AP_NAME_PATTERN = Pattern.compile("dbConnInfo(.*)AP");
68-
private static Pattern SERVER_CONNINFO_AP_NAME_PATTERN = Pattern.compile("serverConnInfo(.*)AP");
6966

7067
/* Dependency Injection */
7168
PropertyRepository propertyRepository = PropertyRepositoryImpl.getInstance();
@@ -187,9 +184,9 @@ public void showMonitoringPresetPopup(ActionEvent e) {
187184
}
188185

189186
/**
190-
* [설정] - [접속정보 설정] - .properties 파일을 선택하기 위한 FileChooser를 연다. 사용자가 선택한 파일의 경로에서
191-
* 파일을 읽은 후, 올바른 설정파일이라면 해당 경로를 remember.properties에 저장한다. 그렇지 않다면, '잘못된
192-
* 파일입니다'라는 경고를 띄우고 접속정보를 직접 설정하는 화면으로 이동시킨다.
187+
* [설정] - [접속정보 설정] - .properties 파일을 선택하기 위한 FileChooser를 연다.
188+
* 사용자가 선택한 파일의 경로에서 파일을 읽은 후, 올바른 설정파일이라면 해당 경로를 remember.properties에 저장한다.
189+
* 그렇지 않다면, '잘못된파일입니다'라는 경고를 띄우고 접속정보를 직접 설정하는 화면으로 이동시킨다.
193190
*
194191
* @param e
195192
*/
@@ -291,117 +288,13 @@ public void saveConnInfoSettings(ActionEvent e) {
291288
// TODO 입력값 검사
292289

293290
String configFilePath = fileChooserText.getText();
294-
PropertiesConfiguration config = PropertiesUtils.connInfoConfig;
295291

296292
ConnectionInfoVBox dbConnVBox = (ConnectionInfoVBox) connInfoVBox.lookup("#dbConnVBox");
297-
dbConnVBox.saveConnInfoSettings("configFilePath");
298-
299-
/*
300-
* 서버 접속정보 StackPane에서 얻어내야 할 데이터
301-
*
302-
* (TextField) {ServerName}HostTextField {ServerName}.server.host (TextField)
303-
* {ServerName}PortTextField {ServerName}.server.port (TextField)
304-
* {ServerName}NameTextField {ServerName} (TextField)
305-
* {ServerName}UserNameTextField {ServerName}.server.username (TextField)
306-
* {ServerName}AlertLogFilePathTextField {ServerName}.server.alertlog.filepath
307-
* (PasswordField) {ServerName}PasswordTextField {ServerName}.server.password
308-
* (JFXComboBox) {ServerName}AlertLogDateFormatComboBox
309-
* {ServerName}.server.alertlog.dateformat
310-
*/
293+
dbConnVBox.saveConnInfoSettings(configFilePath);
311294

312-
/*
313-
List<String> serverNames = new ArrayList<String>(Arrays.asList(config.getStringArray("servernames")));
314295
ConnectionInfoVBox serverConnVBox = (ConnectionInfoVBox) connInfoVBox.lookup("#serverConnVBox");
315-
for (AnchorPane ap : serverConnVBox.getConnInfoAPMap().values()) {
316-
String apId = ap.getId();
317-
318-
Matcher m = SERVER_CONNINFO_AP_NAME_PATTERN.matcher(apId);
319-
if (m.matches()) {
320-
String elementId = m.group(1);
321-
String newElementId = elementId;
322-
boolean isNewConnInfo = false;
323-
324-
// 새로 추가된 접속정보
325-
if (!serverNames.contains(elementId)) {
326-
Set<Node> textFields = ap.lookupAll("TextField");
327-
for (Node n : textFields) {
328-
if (((TextField) n).getId().equals(elementId + "NameTextField")) {
329-
isNewConnInfo = true;
330-
newElementId = ((TextField) n).getText();
331-
logger.debug("NEW ServerName : " + newElementId);
332-
break;
333-
}
334-
}
335-
}
336-
337-
String elementIdLower = newElementId.toLowerCase();
338-
339-
// ServerNames 추가
340-
if (isNewConnInfo) {
341-
serverNames.add(newElementId);
342-
config.setProperty("#Server", newElementId);
343-
config.setProperty(elementIdLower + ".server.alertlog.readLine", 500);
344-
}
345-
346-
// TextField Value Update
347-
Set<Node> textFields = ap.lookupAll("TextField");
348-
for (Node n : textFields) {
349-
TextField tf = (TextField) n;
350-
String tfId = tf.getId();
351-
String tfText = tf.getText();
352-
353-
if (tfId.equals(elementId + "HostTextField")) {
354-
config.setProperty(elementIdLower + ".server.host", tfText);
355-
} else if (tfId.equals(elementId + "PortTextField")) {
356-
config.setProperty(elementIdLower + ".server.port", tfText);
357-
} else if (tfId.equals(elementId + "UserNameTextField")) {
358-
config.setProperty(elementIdLower + ".server.username", tfText);
359-
} else if (tfId.equals(elementId + "AlertLogFilePathTextField")) {
360-
config.setProperty(elementIdLower + ".server.alertlog.filepath", tfText);
361-
}
362-
}
363-
364-
// PasswordField Value Update
365-
Set<Node> passwordFields = ap.lookupAll("PasswordField");
366-
for (Node n : passwordFields) {
367-
PasswordField pf = (PasswordField) n;
368-
String pfId = pf.getId();
369-
String pfText = pf.getText();
370-
371-
if (pfId.equals(elementId + "PasswordTextField")) {
372-
config.setProperty(elementIdLower + ".server.password", pfText);
373-
}
374-
}
375-
376-
// JFXComboBox Value Update
377-
Set<Node> comboBoxs = ap.lookupAll("JFXComboBox");
378-
for (Node n : comboBoxs) {
379-
@SuppressWarnings("unchecked")
380-
JFXComboBox<String> cb = (JFXComboBox<String>) n;
381-
String cbId = cb.getId();
382-
String cbSelectedItem = cb.getSelectionModel().getSelectedItem();
383-
384-
if (cbId.equals(elementId + "AlertLogDateFormatComboBox")) {
385-
config.setProperty(elementIdLower + ".server.alertlog.dateformat", cbSelectedItem);
386-
if (isNewConnInfo) {
387-
String dateFormatRegex = "";
388-
if (cbSelectedItem.equals("EEE MMM dd HH:mm:ss yyyy")) {
389-
dateFormatRegex = "...\\s...\\s([0-2][0-9]|1[012])\\s\\d\\d:\\d\\d:\\d\\d\\s\\d{4}";
390-
} else if (cbSelectedItem.equals("yyyy-MM-dd")) {
391-
dateFormatRegex = "\\d{4}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])T";
392-
}
393-
config.setProperty(elementIdLower + ".server.alertlog.dateformatregex", dateFormatRegex);
394-
}
395-
}
396-
}
397-
}
398-
}
399-
// ServerNames Update
400-
config.setProperty("servernames", serverNames);
401-
*/
296+
serverConnVBox.saveConnInfoSettings(configFilePath);
402297

403-
// 변경사항 저장
404-
propertyRepository.save(configFilePath, config);
405298
// 설정파일 ReLoading
406299
loadSelectedConfigFile(configFilePath);
407300
}

src/main/java/root/javafx/CustomView/ConnectionInfoVBox.java

Lines changed: 69 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package root.javafx.CustomView;
22

33
import java.io.IOException;
4+
import java.util.ArrayList;
45
import java.util.HashMap;
6+
import java.util.List;
57
import java.util.Map;
68

79
import org.apache.commons.configuration2.PropertiesConfiguration;
@@ -26,6 +28,7 @@
2628
import lombok.Data;
2729
import lombok.EqualsAndHashCode;
2830
import root.core.domain.JdbcConnectionInfo;
31+
import root.core.domain.JschConnectionInfo;
2932
import root.core.repository.constracts.PropertyRepository;
3033
import root.core.repository.implement.PropertyRepositoryImpl;
3134
import root.javafx.Service.DatabaseConnectService;
@@ -38,7 +41,7 @@ public class ConnectionInfoVBox extends VBox {
3841

3942
/* Dependency Injection */
4043
private PropertyRepository propertyRepository = PropertyRepositoryImpl.getInstance();
41-
44+
4245
@FXML
4346
Label menuTitleLB;
4447

@@ -91,9 +94,13 @@ public void addConnInfo(ActionEvent e) {
9194
connInfoIdx = connInfoAPMap.size();
9295

9396
if (childAPClazz == DBConnectionInfoAnchorPane.class) {
94-
addConnectionInfoAP(new DBConnectionInfoAnchorPane());
97+
DBConnectionInfoAnchorPane dbConnAP = new DBConnectionInfoAnchorPane();
98+
dbConnAP.setInitialValue(new JdbcConnectionInfo());
99+
addConnectionInfoAP(dbConnAP);
95100
} else if (childAPClazz == ServerConnectionInfoAnchorPane.class) {
96-
addConnectionInfoAP(new ServerConnectionInfoAnchorPane());
101+
ServerConnectionInfoAnchorPane serverConnAP = new ServerConnectionInfoAnchorPane();
102+
serverConnAP.setInitialValue(new JschConnectionInfo());
103+
addConnectionInfoAP(serverConnAP);
97104
}
98105
}
99106

@@ -176,33 +183,71 @@ public void testConnection(ActionEvent e) {
176183

177184
dbConnService.start();
178185
} else if (childAPClazz == ServerConnectionInfoAnchorPane.class) {
179-
186+
180187
}
181188
}
182-
189+
183190
public void saveConnInfoSettings(String configFilePath) {
191+
184192
PropertiesConfiguration config = PropertiesUtils.connInfoConfig;
185193

186-
if(childAPClazz == DBConnectionInfoAnchorPane.class) {
187-
188-
}
189-
for (AnchorPane childAP : this.connInfoAPMap.values()) {
190-
DBConnectionInfoAnchorPane dbConnAP = (DBConnectionInfoAnchorPane) childAP;
191-
JdbcConnectionInfo jdbc = dbConnAP.getInputValues();
192-
System.out.println(jdbc);
193-
194-
String dbName = jdbc.getJdbcDBName().toLowerCase();
195-
config.setProperty("#DB", dbName);
196-
config.setProperty(dbName + ".jdbc.alias", jdbc.getJdbcDBName());
197-
config.setProperty(dbName + ".jdbc.id", jdbc.getJdbcId());
198-
config.setProperty(dbName + ".jdbc.pw", jdbc.getJdbcPw());
199-
config.setProperty(dbName + ".jdbc.url", jdbc.getJdbcUrl());
200-
// TODO 선택된 Oracle Driver Type에 따라서, Driver 값 변경하기, 현재는 임시로 모두 동일한 값 입력
201-
config.setProperty(dbName + ".jdbc.driver", "oracle.jdbc.driver.OracleDriver");
202-
config.setProperty(dbName + ".jdbc.validation", jdbc.getJdbcValidation());
203-
config.setProperty(dbName + ".jdbc.connections", jdbc.getJdbcConnections());
194+
if (childAPClazz == DBConnectionInfoAnchorPane.class) {
195+
196+
List<String> dbNames = new ArrayList<>();
197+
for (AnchorPane childAP : this.connInfoAPMap.values()) {
198+
DBConnectionInfoAnchorPane dbConnAP = (DBConnectionInfoAnchorPane) childAP;
199+
JdbcConnectionInfo jdbc = dbConnAP.getInputValues();
200+
201+
String dbName = jdbc.getJdbcDBName().toLowerCase();
202+
config.setProperty("#DB", dbName);
203+
config.setProperty(dbName + ".jdbc.alias", jdbc.getJdbcDBName());
204+
config.setProperty(dbName + ".jdbc.id", jdbc.getJdbcId());
205+
config.setProperty(dbName + ".jdbc.pw", jdbc.getJdbcPw());
206+
config.setProperty(dbName + ".jdbc.url", jdbc.getJdbcUrl());
207+
// TODO 선택된 Oracle Driver Type에 따라서, Driver 값 변경하기, 현재는 임시로 모두 동일한 값 입력
208+
config.setProperty(dbName + ".jdbc.driver", "oracle.jdbc.driver.OracleDriver");
209+
config.setProperty(dbName + ".jdbc.validation", jdbc.getJdbcValidation());
210+
config.setProperty(dbName + ".jdbc.connections", jdbc.getJdbcConnections());
211+
212+
dbNames.add(dbName);
213+
}
214+
215+
config.setProperty("dbnames", dbNames);
216+
} else {
217+
218+
List<String> serverNames = new ArrayList<>();
219+
220+
for (AnchorPane childAP : this.connInfoAPMap.values()) {
221+
ServerConnectionInfoAnchorPane serverConnAP = (ServerConnectionInfoAnchorPane) childAP;
222+
JschConnectionInfo jsch = serverConnAP.getInputValues();
223+
224+
String serverName = jsch.getServerName().toLowerCase();
225+
config.setProperty(serverName + ".server.servername", jsch.getServerName());
226+
config.setProperty(serverName + ".server.host", jsch.getHost());
227+
config.setProperty(serverName + ".server.port", jsch.getPort());
228+
config.setProperty(serverName + ".server.username", jsch.getUserName());
229+
config.setProperty(serverName + ".server.password", jsch.getPassword());
230+
231+
String dateFormat = jsch.getAlc().getDateFormat();
232+
String dateFormatRegex = "";
233+
234+
if (dateFormat.equals("EEE MMM dd HH:mm:ss yyyy")) {
235+
dateFormatRegex = "...\\s...\\s([0-2][0-9]|1[012])\\s\\d\\d:\\d\\d:\\d\\d\\s\\d{4}";
236+
} else if (dateFormat.equals("yyyy-MM-dd")) {
237+
dateFormatRegex = "\\d{4}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])T";
238+
}
239+
240+
config.setProperty(serverName + ".server.alertlog.dateformat", dateFormat);
241+
config.setProperty(serverName + ".server.alertlog.dateformatregex", dateFormatRegex);
242+
config.setProperty(serverName + ".server.alertlog.filepath", jsch.getAlc().getReadFilePath());
243+
config.setProperty(serverName + ".server.alertlog.readLine", 500);
244+
245+
serverNames.add(serverName);
246+
}
247+
248+
config.setProperty("servernames", serverNames);
204249
}
205-
250+
206251
propertyRepository.save(configFilePath, config);
207252
}
208253
}

src/main/java/root/javafx/CustomView/ServerConnectionInfoAnchorPane.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import javafx.scene.layout.AnchorPane;
1212
import lombok.Data;
1313
import lombok.EqualsAndHashCode;
14+
import root.core.domain.AlertLogCommand;
1415
import root.core.domain.JschConnectionInfo;
1516
import root.core.repository.constracts.PropertyRepository;
1617
import root.core.repository.implement.PropertyRepositoryImpl;
@@ -68,4 +69,18 @@ public void setInitialValue(JschConnectionInfo jsch) {
6869
alertLogFilePathTF.setText(jsch.getAlc().getReadFilePath());
6970
alertLogDateFormatCB.getSelectionModel().select(jsch.getAlc().getDateFormat());
7071
}
72+
73+
public JschConnectionInfo getInputValues() {
74+
JschConnectionInfo jsch = new JschConnectionInfo();
75+
jsch.setServerName(this.serverNameTF.getText());
76+
jsch.setHost(this.hostTF.getText());
77+
jsch.setPort(Integer.valueOf(this.portTF.getText()));
78+
jsch.setUserName(this.userTF.getText());
79+
jsch.setPassword(this.passwordPF.getText());
80+
AlertLogCommand alc = new AlertLogCommand();
81+
alc.setReadFilePath(this.alertLogFilePathTF.getText());
82+
alc.setDateFormat(this.alertLogDateFormatCB.getSelectionModel().getSelectedItem());
83+
jsch.setAlc(alc);
84+
return jsch;
85+
}
7186
}

0 commit comments

Comments
 (0)