Skip to content

Commit

Permalink
Added One-Click database export option
Browse files Browse the repository at this point in the history
  • Loading branch information
afsalashyana committed Dec 16, 2018
1 parent c6ed54f commit 49ae2ee
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 14 deletions.
17 changes: 14 additions & 3 deletions src/library/assistant/alert/AlertMaker.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import javafx.scene.Node;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Alert;
import javafx.scene.control.DialogPane;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.effect.BoxBlur;
Expand All @@ -32,8 +33,7 @@ public static void showSimpleAlert(String title, String content) {
alert.setTitle(title);
alert.setHeaderText(null);
alert.setContentText(content);
Stage stage = (Stage) alert.getDialogPane().getScene().getWindow();
LibraryAssistantUtil.setStageIcon(stage);
styleAlert(alert);
alert.showAndWait();
}

Expand All @@ -42,7 +42,7 @@ public static void showErrorMessage(String title, String content) {
alert.setTitle("Error");
alert.setHeaderText(title);
alert.setContentText(content);

styleAlert(alert);
alert.showAndWait();
}

Expand Down Expand Up @@ -74,6 +74,8 @@ public static void showErrorMessage(Exception ex) {
expContent.add(textArea, 0, 1);

alert.getDialogPane().setExpandableContent(expContent);

styleAlert(alert);
alert.showAndWait();
}

Expand Down Expand Up @@ -147,4 +149,13 @@ public static void showTrayMessage(String title, String message) {
exp.printStackTrace();
}
}

private static void styleAlert(Alert alert) {
Stage stage = (Stage) alert.getDialogPane().getScene().getWindow();
LibraryAssistantUtil.setStageIcon(stage);

DialogPane dialogPane = alert.getDialogPane();
dialogPane.getStylesheets().add(AlertMaker.class.getResource("/resources/dark-theme.css").toExternalForm());
dialogPane.getStyleClass().add("custom-alert");
}
}
46 changes: 46 additions & 0 deletions src/library/assistant/database/export/DatabaseExporter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package library.assistant.database.export;

import java.awt.Desktop;
import java.io.File;
import java.sql.CallableStatement;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import javafx.concurrent.Task;
import library.assistant.alert.AlertMaker;
import library.assistant.database.DatabaseHandler;
import library.assistant.util.LibraryAssistantUtil;

/**
*
* @author Villan
*/
public class DatabaseExporter extends Task<Boolean> {

private final File backupDirectory;

public DatabaseExporter(File backupDirectory) {
this.backupDirectory = backupDirectory;
}

@Override
protected Boolean call() {
try {
createBackup();
return true;
} catch (Exception exp) {
AlertMaker.showErrorMessage(exp);
}
return false;
}

private void createBackup() throws Exception {
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy_MM_dd_hh_mm_ss");
String backupdirectory = backupDirectory.getAbsolutePath() + File.separator + LocalDateTime.now().format(dateFormat);
try (CallableStatement cs = DatabaseHandler.getInstance().getConnection().prepareCall("CALL SYSCS_UTIL.SYSCS_BACKUP_DATABASE(?)")) {
cs.setString(1, backupdirectory);
cs.execute();
}
File file = new File(backupdirectory);
LibraryAssistantUtil.openFileWithDesktop(file);
}
}
23 changes: 21 additions & 2 deletions src/library/assistant/ui/settings/SettingsController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@

import com.jfoenix.controls.JFXCheckBox;
import com.jfoenix.controls.JFXPasswordField;
import com.jfoenix.controls.JFXSpinner;
import com.jfoenix.controls.JFXTextField;
import java.io.File;
import java.net.URL;
import java.security.InvalidParameterException;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;
import library.assistant.alert.AlertMaker;
import library.assistant.data.model.MailServerInfo;
import library.assistant.database.DataHelper;
import library.assistant.database.DatabaseHandler;
import library.assistant.database.export.DatabaseExporter;
import library.assistant.ui.mail.TestMailController;
import library.assistant.util.LibraryAssistantUtil;
import org.apache.logging.log4j.Level;
Expand Down Expand Up @@ -42,6 +46,8 @@ public class SettingsController implements Initializable {
private JFXPasswordField emailPassword;
@FXML
private JFXCheckBox sslCheckbox;
@FXML
private JFXSpinner progressSpinner;

@Override
public void initialize(URL url, ResourceBundle rb) {
Expand Down Expand Up @@ -116,8 +122,7 @@ private MailServerInfo readMailSererInfo() {

private void loadMailServerConfigurations() {
MailServerInfo mailServerInfo = DataHelper.loadMailServerInfo();
if(mailServerInfo!=null)
{
if (mailServerInfo != null) {
LOGGER.log(Level.INFO, "Mail server info loaded from DB");
serverName.setText(mailServerInfo.getMailServer());
smtpPort.setText(String.valueOf(mailServerInfo.getPort()));
Expand All @@ -126,4 +131,18 @@ private void loadMailServerConfigurations() {
sslCheckbox.setSelected(mailServerInfo.getSslEnabled());
}
}

@FXML
private void handleDatabaseExportAction(ActionEvent event) {
DirectoryChooser directoryChooser = new DirectoryChooser();
directoryChooser.setTitle("Select Location to Create Backup");
File selectedDirectory = directoryChooser.showDialog(getStage());
if (selectedDirectory == null) {
AlertMaker.showErrorMessage("Export cancelled", "No Valid Directory Found");
} else {
DatabaseExporter databaseExporter = new DatabaseExporter(selectedDirectory);
progressSpinner.visibleProperty().bind(databaseExporter.runningProperty());
new Thread(databaseExporter).start();
}
}
}
26 changes: 19 additions & 7 deletions src/library/assistant/ui/settings/settings.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@
<?import com.jfoenix.controls.JFXButton?>
<?import com.jfoenix.controls.JFXCheckBox?>
<?import com.jfoenix.controls.JFXPasswordField?>
<?import com.jfoenix.controls.JFXSpinner?>
<?import com.jfoenix.controls.JFXTextField?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>

<AnchorPane prefHeight="422.0" prefWidth="585.0" stylesheets="@../../../../resources/dark-theme.css" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="library.assistant.ui.settings.SettingsController">
<StackPane fx:id="rootContainer" prefHeight="459.0" prefWidth="581.0" stylesheets="@../../../../resources/dark-theme.css" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="library.assistant.ui.settings.SettingsController">
<children>
<TabPane prefHeight="414.0" prefWidth="400.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<TabPane nodeOrientation="LEFT_TO_RIGHT" prefHeight="414.0" prefWidth="400.0">
<tabs>
<Tab closable="false" styleClass="settings-tab" text="Basic">
<content>
<AnchorPane id="AnchorPane" prefHeight="304.0" prefWidth="402.0" styleClass="custom-pane" stylesheets="@../../../resources/dark-theme.css">
<AnchorPane id="AnchorPane" prefHeight="304.0" prefWidth="402.0" styleClass="custom-pane">
<children>
<VBox prefHeight="354.0" prefWidth="400.0" spacing="10.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
Expand Down Expand Up @@ -59,8 +61,8 @@
</content>
</Tab>
<Tab closable="false" styleClass="settings-tab" text="Mail Server">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" styleClass="custom-pane">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" styleClass="custom-pane">
<children>
<VBox prefHeight="385.0" prefWidth="585.0" spacing="10.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
Expand Down Expand Up @@ -101,9 +103,19 @@
</VBox>
</children>
</AnchorPane>
</content>
</content>
</Tab>
<Tab closable="false" styleClass="settings-tab" text="Export">
<content>
<AnchorPane id="AnchorPane" prefHeight="304.0" prefWidth="402.0" styleClass="custom-pane">
<children>
<JFXButton layoutX="200.0" layoutY="129.0" onAction="#handleDatabaseExportAction" prefHeight="60.0" prefWidth="185.0" text="Export Database" />
<JFXSpinner fx:id="progressSpinner" layoutX="268.0" layoutY="207.0" visible="false" />
</children>
</AnchorPane>
</content>
</Tab>
</tabs>
</TabPane>
</children>
</AnchorPane>
</StackPane>
9 changes: 9 additions & 0 deletions src/library/assistant/util/LibraryAssistantUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,13 @@ public static boolean validateEmailAddress(String emailID) {
Pattern pattern = Pattern.compile(regex);
return pattern.matcher(emailID).matches();
}

public static void openFileWithDesktop(File file) {
try {
Desktop desktop = Desktop.getDesktop();
desktop.open(file);
} catch (IOException ex) {
Logger.getLogger(LibraryAssistantUtil.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
31 changes: 29 additions & 2 deletions src/resources/dark-theme.css
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@

.wrong-credentials
{
-jfx-unfocus-color: -fx-red;
-jfx-focus-color: -fx-red;
-jfx-unfocus-color: #ef5350;
-jfx-focus-color: #ef5350;
}

/*Main Screen CSS Styling starts here*/
Expand Down Expand Up @@ -320,4 +320,31 @@
-fx-background-color: transparent;
-fx-text-fill: -fx-secondary;
-fx-font-size: 12pt;
}

/*Alert Dialog*/

.custom-alert{
-fx-background-color: -fx-primary;
-fx-text-fill: white;
}
.custom-alert > *.button-bar > *.container{
-fx-background-color: -fx-primary;
}
.custom-alert > *.label.content{
-fx-font-size: 14px;
-fx-font-weight: bold;
-fx-text-fill: white;
}
.custom-alert:header *.header-panel{
-fx-background-color: -fx-blue;
}
.custom-alert:header .header-panel .label {
-fx-font-style: normal;
-fx-text-fill: white;
}
.custom-alert:header *.header-panel *.label{
-fx-font-size: 18px;
-fx-font-style: italic;
-fx-fill: -fx-primary;
}

0 comments on commit 49ae2ee

Please sign in to comment.