Added requests history saving

This commit is contained in:
Rohit Awate 2018-02-13 14:40:49 +05:30
parent 118173d574
commit 1829fd3bb0
20 changed files with 210 additions and 21 deletions

1
.gitignore vendored
View file

@ -5,3 +5,4 @@ RESTaurant.iml
classes/
src/main/java/META-INF/
dependency-reduced-pom.xml
!/requests.sqlite

View file

@ -80,5 +80,11 @@
<artifactId>jackson-databind</artifactId>
<version>2.9.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc -->
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.21.0.1</version>
</dependency>
</dependencies>
</project>

View file

@ -18,7 +18,7 @@ package com.rohitawate.restaurant.homewindow;
import com.rohitawate.restaurant.models.requests.DataDispatchRequest;
import com.rohitawate.restaurant.models.requests.RestaurantRequest;
import com.rohitawate.restaurant.util.ThemeManager;
import com.rohitawate.restaurant.util.themes.ThemeManager;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;

View file

@ -26,8 +26,8 @@ import com.rohitawate.restaurant.requestsmanager.DELETERequestManager;
import com.rohitawate.restaurant.requestsmanager.DataDispatchRequestManager;
import com.rohitawate.restaurant.requestsmanager.GETRequestManager;
import com.rohitawate.restaurant.requestsmanager.RequestManager;
import com.rohitawate.restaurant.util.Settings;
import com.rohitawate.restaurant.util.ThemeManager;
import com.rohitawate.restaurant.util.settings.Settings;
import com.rohitawate.restaurant.util.themes.ThemeManager;
import javafx.beans.binding.Bindings;
import javafx.beans.property.StringProperty;
import javafx.fxml.FXML;
@ -172,6 +172,7 @@ public class DashboardController implements Initializable {
loadingLayer.setVisible(false);
promptLayer.setVisible(false);
Throwable exception = requestManager.getException();
exception.printStackTrace();
if (exception.getClass() == UnreliableResponseException.class) {
UnreliableResponseException URE = (UnreliableResponseException) exception;
@ -224,6 +225,7 @@ public class DashboardController implements Initializable {
loadingLayer.setVisible(false);
promptLayer.setVisible(false);
Throwable exception = requestManager.getException();
exception.printStackTrace();
if (exception.getClass() == UnreliableResponseException.class) {
UnreliableResponseException URE = (UnreliableResponseException) exception;
@ -272,6 +274,7 @@ public class DashboardController implements Initializable {
loadingLayer.setVisible(false);
promptLayer.setVisible(false);
Throwable exception = requestManager.getException();
exception.printStackTrace();
if (exception.getClass() == UnreliableResponseException.class) {
UnreliableResponseException URE = (UnreliableResponseException) exception;

View file

@ -16,7 +16,7 @@
package com.rohitawate.restaurant.homewindow;
import com.rohitawate.restaurant.util.ThemeManager;
import com.rohitawate.restaurant.util.themes.ThemeManager;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;

View file

@ -16,7 +16,7 @@
package com.rohitawate.restaurant.homewindow;
import com.rohitawate.restaurant.util.ThemeManager;
import com.rohitawate.restaurant.util.themes.ThemeManager;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;

View file

@ -37,7 +37,7 @@ public class HomeWindowController implements Initializable {
@FXML
private TabPane homeWindowTabPane;
private KeyCombination ctrlN = new KeyCodeCombination(KeyCode.T, KeyCombination.CONTROL_DOWN);
private KeyCombination newTab = new KeyCodeCombination(KeyCode.T, KeyCombination.CONTROL_DOWN);
@Override
public void initialize(URL location, ResourceBundle resources) {
@ -45,7 +45,7 @@ public class HomeWindowController implements Initializable {
Platform.runLater(() -> {
Scene thisScene = homeWindowTabPane.getScene();
thisScene.setOnKeyPressed(e -> {
if (ctrlN.match(e))
if (newTab.match(e))
addTab();
});
});

View file

@ -16,7 +16,7 @@
package com.rohitawate.restaurant.homewindow;
import com.rohitawate.restaurant.util.ThemeManager;
import com.rohitawate.restaurant.util.themes.ThemeManager;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;

View file

@ -15,8 +15,9 @@
*/
package com.rohitawate.restaurant.main;
import com.rohitawate.restaurant.util.SettingsLoader;
import com.rohitawate.restaurant.util.ThemeManager;
import com.rohitawate.restaurant.util.Services;
import com.rohitawate.restaurant.util.settings.SettingsLoader;
import com.rohitawate.restaurant.util.themes.ThemeManager;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
@ -30,6 +31,8 @@ public class Main extends Application {
SettingsLoader settingsLoader = new SettingsLoader();
settingsLoader.SettingsLoaderThread.join();
new Services();
Parent dashboard = FXMLLoader.load(getClass().getResource("/fxml/homewindow/HomeWindow.fxml"));
Stage dashboardStage = new Stage();
ThemeManager.setTheme(dashboard);

View file

@ -22,6 +22,7 @@ import com.fasterxml.jackson.databind.SerializationFeature;
import com.rohitawate.restaurant.exceptions.UnreliableResponseException;
import com.rohitawate.restaurant.models.requests.DELETERequest;
import com.rohitawate.restaurant.models.responses.RestaurantResponse;
import com.rohitawate.restaurant.util.Services;
import javafx.concurrent.Task;
import javax.ws.rs.client.Invocation;
@ -38,6 +39,8 @@ public class DELETERequestManager extends RequestManager {
protected RestaurantResponse call() throws Exception {
DELETERequest deleteRequest = (DELETERequest) request;
Services.historyManager.saveHistory(deleteRequest);
RestaurantResponse response = new RestaurantResponse();
WebTarget target = client.target(deleteRequest.getTarget().toString());
Map.Entry<String, String> mapEntry;

View file

@ -22,6 +22,7 @@ import com.fasterxml.jackson.databind.SerializationFeature;
import com.rohitawate.restaurant.exceptions.UnreliableResponseException;
import com.rohitawate.restaurant.models.requests.DataDispatchRequest;
import com.rohitawate.restaurant.models.responses.RestaurantResponse;
import com.rohitawate.restaurant.util.Services;
import javafx.concurrent.Task;
import org.glassfish.jersey.media.multipart.FormDataMultiPart;
import org.glassfish.jersey.media.multipart.file.FileDataBodyPart;
@ -53,6 +54,8 @@ public class DataDispatchRequestManager extends RequestManager {
DataDispatchRequest dataDispatchRequest = (DataDispatchRequest) request;
String requestType = dataDispatchRequest.getRequestType();
Services.historyManager.saveHistory(dataDispatchRequest);
RestaurantResponse response = new RestaurantResponse();
WebTarget target = client.target(dataDispatchRequest.getTarget().toString());
Map.Entry<String, String> mapEntry;

View file

@ -21,6 +21,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.rohitawate.restaurant.exceptions.UnreliableResponseException;
import com.rohitawate.restaurant.models.responses.RestaurantResponse;
import com.rohitawate.restaurant.util.Services;
import javafx.concurrent.Task;
import javax.ws.rs.client.Invocation.Builder;
@ -38,6 +39,8 @@ public class GETRequestManager extends RequestManager {
RestaurantResponse response = new RestaurantResponse();
WebTarget target = client.target(request.getTarget().toString());
Services.historyManager.saveHistory(request);
Builder requestBuilder = target.request();
HashMap<String, String> headers = request.getHeaders();

View file

@ -17,7 +17,7 @@ package com.rohitawate.restaurant.requestsmanager;
import com.rohitawate.restaurant.models.requests.RestaurantRequest;
import com.rohitawate.restaurant.models.responses.RestaurantResponse;
import com.rohitawate.restaurant.util.Settings;
import com.rohitawate.restaurant.util.settings.Settings;
import javafx.concurrent.Service;
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.media.multipart.MultiPartFeature;

View file

@ -0,0 +1,27 @@
/*
* Copyright 2018 Rohit Awate.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.rohitawate.restaurant.util;
import com.rohitawate.restaurant.util.history.HistoryManager;
public class Services {
public static HistoryManager historyManager;
static {
historyManager = new HistoryManager();
}
}

View file

@ -0,0 +1,104 @@
/*
* Copyright 2018 Rohit Awate.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.rohitawate.restaurant.util.history;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rohitawate.restaurant.models.requests.DELETERequest;
import com.rohitawate.restaurant.models.requests.DataDispatchRequest;
import com.rohitawate.restaurant.models.requests.GETRequest;
import com.rohitawate.restaurant.models.requests.RestaurantRequest;
import com.rohitawate.restaurant.util.json.JSONUtils;
import java.io.File;
import java.sql.*;
import java.time.LocalDate;
import java.util.Map;
public class HistoryManager {
private Connection conn;
private JsonNode queries;
private PreparedStatement statement;
public HistoryManager() {
try {
conn = DriverManager.getConnection("jdbc:sqlite:requests.sqlite");
// Read all queries from Queries.json
File queriesFile = new File(getClass().getResource("/sql/Queries.json").toURI());
ObjectMapper mapper = new ObjectMapper();
queries = mapper.readTree(queriesFile);
statement =
conn.prepareStatement(JSONUtils.trimString(queries.get("createRequestsTable").toString()));
statement.execute();
statement =
conn.prepareStatement(JSONUtils.trimString(queries.get("createHeadersTable").toString()));
statement.execute();
} catch (Exception E) {
E.printStackTrace();
} finally {
System.out.println("Connected to database.");
}
}
// Method is made synchronized to allow only one database transaction at a time.
public synchronized void saveHistory(RestaurantRequest request) {
try {
statement =
conn.prepareStatement(JSONUtils.trimString(queries.get("saveRequest").toString()));
// Determines the request type
if (request.getClass() == GETRequest.class)
statement.setString(1, "GET");
else if (request.getClass() == DataDispatchRequest.class) {
if (((DataDispatchRequest) request).getRequestType().equals("POST"))
statement.setString(1, "POST");
else
statement.setString(1, "PUT");
} else if (request.getClass() == DELETERequest.class)
statement.setString(1, "DELETE");
statement.setString(2, String.valueOf(request.getTarget()));
statement.setString(3, LocalDate.now().toString());
statement.executeUpdate();
if (request.getHeaders().size() > 0) {
// Get latest RequestID to insert into Headers table
statement = conn.prepareStatement("SELECT MAX(ID) AS MaxID FROM Requests");
ResultSet RS = statement.executeQuery();
int requestID = -1;
if (RS.next())
requestID = RS.getInt("MaxID");
statement = conn.prepareStatement(JSONUtils.trimString(queries.get("saveHeader").toString()));
for (Map.Entry entry : request.getHeaders().entrySet()) {
statement.setInt(1, requestID);
statement.setString(2, entry.getKey().toString());
statement.setString(3, entry.getValue().toString());
statement.executeUpdate();
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}

View file

@ -0,0 +1,29 @@
/*
* Copyright 2018 Rohit Awate.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.rohitawate.restaurant.util.json;
public class JSONUtils {
/**
* Removes leading and trailing quotation marks from strings.
*
* @param input String with leading and trailing quotation marks.
* @return trimmedString - String with leading and trailing quotation marks removed.
*/
public static String trimString(String input) {
return input.replaceAll("\"", "");
}
}

View file

@ -14,7 +14,7 @@
* limitations under the License.
*/
package com.rohitawate.restaurant.util;
package com.rohitawate.restaurant.util.settings;
/**
* Holds default settings values which may

View file

@ -14,10 +14,11 @@
* limitations under the License.
*/
package com.rohitawate.restaurant.util;
package com.rohitawate.restaurant.util.settings;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rohitawate.restaurant.util.json.JSONUtils;
import java.io.File;
@ -51,9 +52,11 @@ public class SettingsLoader implements Runnable {
if (Settings.connectionReadTimeOutEnable)
Settings.connectionReadTimeOut = nodes.get("connectionReadTimeOut").asInt();
Settings.theme = nodes.get("theme").toString();
Settings.theme = JSONUtils.trimString(nodes.get("theme").toString());
} catch (Exception E) {
System.out.println("Settings file not found. Loading default values...");
} finally {
System.out.println("Settings loaded.");
}
}
}

View file

@ -14,8 +14,9 @@
* limitations under the License.
*/
package com.rohitawate.restaurant.util;
package com.rohitawate.restaurant.util.themes;
import com.rohitawate.restaurant.util.settings.Settings;
import javafx.scene.Parent;
import java.io.File;
@ -26,7 +27,7 @@ public class ThemeManager {
private static List<Parent> parentNodes = new ArrayList<>();
public static void refreshTheme() {
File themeFile = new File("themes/" + Settings.theme.replaceAll("\"", "") + ".css");
File themeFile = new File("themes/" + Settings.theme + ".css");
if (themeFile.exists()) {
String themePath = themeFile.toURI().toString();
@ -38,11 +39,7 @@ public class ThemeManager {
}
public static void setTheme(Parent parent) {
/*
Removes leading and trailing quotation marks since it causes a problem while
instantiating a file object from this path.
*/
File themeFile = new File("themes/" + Settings.theme.replaceAll("\"", "") + ".css");
File themeFile = new File("themes/" + Settings.theme + ".css");
if (themeFile.exists()) {
parent.getStylesheets().add(themeFile.toURI().toString());
parentNodes.add(parent);

View file

@ -0,0 +1,7 @@
{
"createRequestsTable": "CREATE TABLE IF NOT EXISTS Requests(ID INTEGER PRIMARY KEY, Type TEXT NOT NULL, Target TEXT NOT NULL, Date TEXT NOT NULL)",
"createHeadersTable": "CREATE TABLE IF NOT EXISTS Headers(ID INTEGER, Key TEXT NOT NULL, Value TEXT NOT NULL, FOREIGN KEY(ID) REFERENCES Requests(ID))",
"selectAllRequests": "SELECT * FROM Requests, Headers",
"saveRequest": "INSERT INTO Requests(Type, Target, Date) VALUES(?, ?, ?)",
"saveHeader": "INSERT INTO Headers(ID, Key, Value) VALUES(?, ?, ?)"
}