Shifted state maintenance logic to JSON file

This commit is contained in:
Rohit Awate 2018-06-29 09:34:15 +05:30
parent f65d2083ff
commit 1efaf40a3c
No known key found for this signature in database
GPG key ID: 1051D7B79CF2EE25
3 changed files with 27 additions and 29 deletions

View file

@ -16,7 +16,9 @@
package com.rohitawate.everest.controllers; package com.rohitawate.everest.controllers;
import com.fasterxml.jackson.core.type.TypeReference;
import com.jfoenix.controls.JFXButton; import com.jfoenix.controls.JFXButton;
import com.rohitawate.everest.misc.EverestUtilities;
import com.rohitawate.everest.misc.KeyMap; import com.rohitawate.everest.misc.KeyMap;
import com.rohitawate.everest.misc.Services; import com.rohitawate.everest.misc.Services;
import com.rohitawate.everest.misc.ThemeManager; import com.rohitawate.everest.misc.ThemeManager;
@ -39,7 +41,8 @@ import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox; import javafx.scene.layout.VBox;
import javafx.stage.Stage; import javafx.stage.Stage;
import java.io.*; import java.io.File;
import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.*; import java.util.*;
@ -243,39 +246,36 @@ public class HomeWindowController implements Initializable {
} }
private void saveState() { private void saveState() {
List<DashboardState> dashboardStates = new ArrayList<>(); ArrayList<DashboardState> dashboardStates = new ArrayList<>();
// Get the states of all the tabs // Get the states of all the tabs
for (DashboardController controller : tabControllerMap.values()) for (DashboardController controller : tabControllerMap.values())
dashboardStates.add(controller.getState()); dashboardStates.add(controller.getState());
try { try {
File stateFile = new File("Everest/config/state.json");
File configFolder = new File("Everest/config/"); EverestUtilities.jsonMapper.writeValue(stateFile, dashboardStates);
if (!configFolder.exists()) Services.loggingService.logInfo("Application state saved.", LocalDateTime.now());
configFolder.mkdirs();
OutputStream fileStream = new FileOutputStream("Everest/config/everest.state");
ObjectOutputStream objectStream = new ObjectOutputStream(fileStream);
objectStream.writeObject(dashboardStates);
objectStream.close();
fileStream.close();
Services.loggingService.logInfo("Application state was saved successfully.", LocalDateTime.now());
} catch (IOException e) { } catch (IOException e) {
Services.loggingService.logSevere("Failed to save the application's state.", e, LocalDateTime.now()); Services.loggingService.logSevere("Failed to save application state.", e, LocalDateTime.now());
} }
} }
private void recoverState() { private void recoverState() {
try { try {
InputStream fileStream = new FileInputStream("Everest/config/everest.state"); File stateFile = new File("Everest/config/state.json");
ObjectInputStream objectStream = new ObjectInputStream(fileStream);
Services.loggingService.logInfo("Application state file found.", LocalDateTime.now()); if (!stateFile.exists()) {
Services.loggingService.logInfo("Application state file not found. Loading default state.", LocalDateTime.now());
addTab();
return;
}
List<DashboardState> dashboardStates = (List<DashboardState>) objectStream.readObject(); ArrayList<DashboardState> dashboardStates = EverestUtilities.jsonMapper
objectStream.close(); .reader()
fileStream.close(); .forType(new TypeReference<ArrayList<DashboardState>>() {
})
.readValue(stateFile);
if (dashboardStates.size() > 0) { if (dashboardStates.size() > 0) {
for (DashboardState dashboardState : dashboardStates) for (DashboardState dashboardState : dashboardStates)
@ -283,15 +283,12 @@ public class HomeWindowController implements Initializable {
} else { } else {
addTab(); addTab();
} }
} catch (FileNotFoundException e) { } catch (IOException e) {
Services.loggingService.logWarning("Application state file not found. Loading default state.", e, LocalDateTime.now()); Services.loggingService.logWarning("Application state file is possibly corrupted. State recovery failed. Loading default state.", e, LocalDateTime.now());
addTab();
} catch (IOException | ClassNotFoundException e) {
Services.loggingService.logWarning("Application state file is possibly corrupted. Could not recover the state.\nLoading default state.", e, LocalDateTime.now());
addTab();
} finally { } finally {
Services.loggingService.logInfo("Application loaded.", LocalDateTime.now()); Services.loggingService.logInfo("Application loaded.", LocalDateTime.now());
} }
} }
public void addHistoryItem(DashboardState state) { public void addHistoryItem(DashboardState state) {

View file

@ -18,17 +18,17 @@ package com.rohitawate.everest.models;
import com.rohitawate.everest.models.requests.DataDispatchRequest; import com.rohitawate.everest.models.requests.DataDispatchRequest;
import java.io.Serializable;
import java.util.HashMap; import java.util.HashMap;
/** /**
* Convenience class to abstract the state of the application. * Convenience class to abstract the state of the application.
*/ */
public class DashboardState extends DataDispatchRequest implements Serializable { public class DashboardState extends DataDispatchRequest {
private HashMap<String, String> params; private HashMap<String, String> params;
private String httpMethod; private String httpMethod;
public DashboardState() { public DashboardState() {
} }
/* /*

View file

@ -41,7 +41,8 @@ public class SettingsLoader implements Runnable {
try { try {
File settingsFile = new File("Everest/config/settings.json"); File settingsFile = new File("Everest/config/settings.json");
System.out.println("Settings file found. Loading settings... "); if (settingsFile.exists())
System.out.println("Settings file found. Loading settings... ");
nodes = EverestUtilities.jsonMapper.readTree(settingsFile); nodes = EverestUtilities.jsonMapper.readTree(settingsFile);