Skip to content

Commit

Permalink
implement database storage/saving
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasstarsz committed Jul 29, 2024
1 parent 772f9bb commit d06cf6e
Show file tree
Hide file tree
Showing 17 changed files with 486 additions and 75 deletions.
3 changes: 3 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ dependencies {
implementation("jakarta.inject:jakarta.inject-api:2.0.1")
implementation("io.github.mkpaz:atlantafx-base:2.0.1")
implementation("org.apache.commons:commons-text:1.12.0")
implementation("org.xerial:sqlite-jdbc:3.46.0.0")
}

javafx {
Expand Down Expand Up @@ -69,6 +70,8 @@ val distributionName = "FXDex ${version}"
jlink {
addExtraDependencies("javafx")
options.addAll("--strip-debug", "--compress", "2", "--no-header-files", "--no-man-pages")



launcher {
name = distributionName
Expand Down
61 changes: 42 additions & 19 deletions src/main/java/io/github/lucasstarsz/fxdex/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,22 @@
package io.github.lucasstarsz.fxdex;

import java.io.IOException;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Objects;
import java.util.concurrent.ExecutorService;

import com.google.inject.Guice;
import com.google.inject.Injector;

import atlantafx.base.theme.PrimerDark;
import io.github.lucasstarsz.fxdex.database.DatabaseSetup;
import io.github.lucasstarsz.fxdex.misc.DexModule;
import io.github.lucasstarsz.fxdex.misc.DexViewModelCache;
import io.github.lucasstarsz.fxdex.misc.FileLinks;
import io.github.lucasstarsz.fxdex.service.UiService;
import javafx.application.Application;
import javafx.beans.property.Property;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.property.*;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
Expand All @@ -43,31 +44,53 @@ public class App extends Application {

public static final StringProperty CurrentDexEntry = new SimpleStringProperty();
public static final StringProperty CurrentScene = new SimpleStringProperty();
public static final IntegerProperty CurrentDexNumber = new SimpleIntegerProperty();
public static final Property<ExecutorService> DexThreadHandler = new SimpleObjectProperty<>();
private static final DexViewModelCache dexVMCache = new DexViewModelCache();

private Stage primaryStage;

@Override
public void start(Stage stage) {
Application.setUserAgentStylesheet(new PrimerDark().getUserAgentStylesheet());
CurrentScene.addListener((c, o, n) -> {
try {
switchSceneInfo(n);
} catch (Exception ex) {
Alert errorAlert = UiService.createErrorAlert("Unable to open Pokédex", ex);
errorAlert.initOwner(stage);
try {

try (var dbConnection = DriverManager.getConnection(FileLinks.JDBCConnectionUrl);
var statementObj = dbConnection.createStatement()) {
var meta = dbConnection.getMetaData();
System.out.println("The driver name is " + meta.getDriverName());
System.out.println("Setting up...");

for (String sqlStatement : DatabaseSetup.getStartupStatements()) {
System.out.println(sqlStatement);
statementObj.execute(sqlStatement);
}
} catch (SQLException e) {
Alert errorAlert = UiService.createErrorAlert("Unable to open Pokédex Database", e);
errorAlert.showAndWait();
}
});

this.primaryStage = stage;

Scene scene = new Scene(DefaultParent, 640, 480);
stage.setScene(scene);

CurrentScene.set("main.fxml");
stage.show();
Application.setUserAgentStylesheet(new PrimerDark().getUserAgentStylesheet());
CurrentScene.addListener((c, o, n) -> {
try {
switchSceneInfo(n);
} catch (Exception ex) {
Alert errorAlert = UiService.createErrorAlert("Unable to open Pokédex", ex);
ex.printStackTrace();
errorAlert.initOwner(stage);
errorAlert.showAndWait();
}
});

this.primaryStage = stage;

Scene scene = new Scene(DefaultParent, 640, 480);
stage.setScene(scene);

CurrentScene.set("main.fxml");
stage.show();
} catch (Exception e) {
e.printStackTrace();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package io.github.lucasstarsz.fxdex.database;

public class DatabaseSetup {

public static final String CreateDexes = """
CREATE TABLE IF NOT EXISTS `dexes` (
`apiName` tinytext PRIMARY KEY NOT NULL,
`uiName` tinytext NOT NULL,
`apiUrl` tinytext NOT NULL
);""";

public static final String CreateDexPokemon = """
CREATE TABLE IF NOT EXISTS `dexPokemon` (
`nationalDexNumber` int PRIMARY KEY NOT NULL,
`apiDexName` tinytext UNIQUE NOT NULL
);""";

public static final String CreatePokemonNamesToDex = """
CREATE TABLE IF NOT EXISTS `pokemonNamesToDex` (
`apiDexName` tinytext PRIMARY KEY NOT NULL,
`nationalDexNumber` int UNIQUE NOT NULL,
FOREIGN KEY (`apiDexName`) REFERENCES `dexPokemon` (`apiDexName`),
FOREIGN KEY (`nationalDexNumber`) REFERENCES `dexPokemon` (`nationalDexNumber`)
);""";

public static final String CreateDexEntries = """
CREATE TABLE IF NOT EXISTS `dexEntries` (
`nationalDexNumber` int PRIMARY KEY NOT NULL,
`name` tinytext NOT NULL,
`speciesUrl` tinytext NOT NULL,
`genus` tinytext NOT NULL,
`generation` int NOT NULL,
FOREIGN KEY (`nationalDexNumber`) REFERENCES `dexPokemon` (`nationalDexNumber`),
FOREIGN KEY (`name`) REFERENCES `dexPokemon` (`apiDexName`)
);""";

public static final String CreateEggGroups = """
CREATE TABLE IF NOT EXISTS `eggGroups` (
`nationalDexNumber` int PRIMARY KEY NOT NULL,
`eggGroupsString` tinytext NOT NULL,
FOREIGN KEY (`nationalDexNumber`) REFERENCES `dexEntries` (`nationalDexNumber`)
);""";

public static final String CreateFlavorTexts = """
CREATE TABLE IF NOT EXISTS `flavorTexts` (
`nationalDexNumber` int NOT NULL,
`region` tinytext NOT NULL,
`regionText` tinytext NOT NULL,
PRIMARY KEY (`nationalDexNumber`, `region`),
FOREIGN KEY (`nationalDexNumber`) REFERENCES `dexEntries` (`nationalDexNumber`),
FOREIGN KEY (`region`) REFERENCES `dexes` (`apiName`)
);""";

public static String[] getStartupStatements() {
return new String[]{
CreateDexes,
CreateDexPokemon,
CreatePokemonNamesToDex,
CreateDexEntries,
CreateEggGroups,
CreateFlavorTexts
};
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.lucasstarsz.fxdex.misc;

import java.util.List;
import java.util.Map;

public class ApiConversionTables {
Expand Down Expand Up @@ -50,6 +51,52 @@ public class ApiConversionTables {
public static final String Kitakami = "kitakami";
public static final String Blueberry = "blueberry";

public static final List<String> GameVersions = List.of(
"red",
"blue",
"yellow",
"gold",
"silver",
"crystal",
"ruby",
"sapphire",
"emerald",
"firered",
"leafgreen",
"diamond",
"pearl",
"platinum",
"heartgold",
"soulsilver",
"black",
"white",
"colosseum",
"xd",
"black-2",
"white-2",
"x",
"y",
"omega-ruby",
"alpha-sapphire",
"sun",
"moon",
"ultra-sun",
"ultra-moon",
"lets-go-pikachu",
"lets-go-eevee",
"sword",
"shield",
"the-isle-of-armor",
"the-crown-tundra",
"brilliant-diamond",
"shining-pearl",
"legends-arceus",
"scarlet",
"violet",
"the-teal-mask",
"the-indigo-disk"
);

public static final Map<String, String> DexNameMap = Map.ofEntries(
Map.entry(National, "National Dex"),
Map.entry(Kanto, "Kanto (Red/Blue/Yellow/Green)"),
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/io/github/lucasstarsz/fxdex/misc/DexModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@
import com.google.inject.AbstractModule;
import com.google.inject.name.Names;

import io.github.lucasstarsz.fxdex.persistence.DexInfoHandler;
import io.github.lucasstarsz.fxdex.persistence.SqlDexInfoHandler;
import io.github.lucasstarsz.fxdex.service.*;

public class DexModule extends AbstractModule {

@Override
protected void configure() {
bind(DexInfoHandler.class).toInstance(new SqlDexInfoHandler());
bind(JsonParserService.class).to(PokeApiJsonParserService.class);
bind(UiService.class).to(DefaultUIService.class);
bind(DexService.class).to(PokeApiDexService.class);
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/io/github/lucasstarsz/fxdex/misc/FileLinks.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.github.lucasstarsz.fxdex.misc;

public class FileLinks {
public static final String DatabaseLocation = System.getProperty("user.home") + "/database.db";
public static final String JDBCConnectionUrl = "jdbc:sqlite:" + DatabaseLocation;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.json.JSONObject;

import java.util.List;
import java.util.Map;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

public class JsonDexEntryItem {

private final String name;
private final int nationalDexNumber;
private final String genus;
private final String generation;
private final List<String> eggGroups;
Expand All @@ -21,6 +24,17 @@ public JsonDexEntryItem(JSONObject dexEntry, JsonParserService jsonParserService
generation = jsonParserService.getGenerationIntroducedIn(dexEntry);
eggGroups = jsonParserService.getEggGroups(dexEntry);
flavorTexts = jsonParserService.getFlavorTexts(dexEntry);
name = jsonParserService.getPokemonNameFromDexEntry(dexEntry);
nationalDexNumber = jsonParserService.getNationalDexNumber(dexEntry);
}

public JsonDexEntryItem(ResultSet mainDexEntry, ResultSet eggGroups, Map<String, String> flavorTexts) throws SQLException {
nationalDexNumber = mainDexEntry.getInt("nationalDexNumber");
name = Objects.requireNonNull(mainDexEntry.getString("name"));
genus = Objects.requireNonNull(mainDexEntry.getString("genus"));
generation = Objects.requireNonNull(mainDexEntry.getString("generation"));
this.eggGroups = Arrays.stream(eggGroups.getString("eggGroupsString").split(",")).toList();
this.flavorTexts = flavorTexts;
}

public String getGenus() {
Expand All @@ -39,6 +53,14 @@ public Map<String, String> getFlavorTexts() {
return flavorTexts;
}

public String getName() {
return name;
}

public int getNationalDexNumber() {
return nationalDexNumber;
}

@Override
public boolean equals(Object other) {
if (this == other) return true;
Expand All @@ -47,6 +69,8 @@ public boolean equals(Object other) {
JsonDexEntryItem dexEntryItem = (JsonDexEntryItem) other;

return new EqualsBuilder()
.append(name, dexEntryItem.name)
.append(nationalDexNumber, dexEntryItem.nationalDexNumber)
.append(genus, dexEntryItem.genus)
.append(generation, dexEntryItem.generation)
.append(eggGroups, dexEntryItem.eggGroups)
Expand All @@ -57,6 +81,8 @@ public boolean equals(Object other) {
@Override
public int hashCode() {
return new HashCodeBuilder()
.append(name)
.append(nationalDexNumber)
.append(genus)
.append(generation)
.append(eggGroups)
Expand All @@ -67,6 +93,8 @@ public int hashCode() {
@Override
public String toString() {
return new ToStringBuilder(this)
.append("name", name)
.append("nationalDexNumber", nationalDexNumber)
.append("genus", genus)
.append("generation", generation)
.append("eggGroups", eggGroups)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class JsonDexItem {

private final String apiDexName;
private final String apiDexUrl;

public JsonDexItem(JSONObject dex) {
this.apiDexName = dex.getString("name");
this.apiDexUrl = dex.getString("url");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.github.lucasstarsz.fxdex.persistence;

import io.github.lucasstarsz.fxdex.model.JsonDexEntryItem;
import io.github.lucasstarsz.fxdex.model.JsonDexItem;
import io.github.lucasstarsz.fxdex.model.JsonDexListItem;

import java.sql.SQLException;
import java.util.List;

public interface DexInfoHandler {
void saveDexItems(List<JsonDexItem> jsonDexItems);

void saveDexEntry(JsonDexEntryItem jsonDexEntry, String dexEntryLink);

JsonDexEntryItem loadDexEntry(int nationalDexNumber) throws SQLException;

void saveDexPokemon(JsonDexListItem dexEntryFromList);

int loadPokemonNumber(String apiPokemonName);
}
Loading

0 comments on commit d06cf6e

Please sign in to comment.