From 8e0e392982adb3862d6dd18518ef31b630acf3bf Mon Sep 17 00:00:00 2001 From: Leonard <Leonard@Leonard.Leo> Date: Wed, 10 Jun 2020 18:11:51 +0300 Subject: [PATCH] persistence done so far --- core/database.mv.db | Bin 32768 -> 36864 bytes .../Model/Map/Overworld.java | 24 ++++--- .../galaxytruckerreloaded/Model/Ship.java | 29 +++----- .../galaxytruckerreloaded/Model/User.java | 7 +- .../Server/Persistence/CrewDAO.java | 10 +-- .../Server/Persistence/ObjectDAO.java | 4 +- .../Server/Persistence/OverworldDAO.java | 38 +++++++++-- .../Server/Persistence/PlanetDAO.java | 30 ++++++++- .../Server/Persistence/RoomDAO.java | 33 ++++++++-- .../Server/Persistence/ShipDAO.java | 59 +++++++++++------ .../Server/Persistence/TraderDAO.java | 33 ++++++++-- .../Server/Persistence/UserDAO.java | 62 ++++++++++++------ .../Server/Persistence/WeaponDAO.java | 33 ++++++++-- .../Test/Server/Persistence/CrewDAOTest.java | 3 +- 14 files changed, 256 insertions(+), 109 deletions(-) diff --git a/core/database.mv.db b/core/database.mv.db index 7e2e1de2540f765ebf278401a0ed46fd69b52434..9dbdffc5c5d72fd1cf5f84151e6d3282d8f5cb4f 100644 GIT binary patch delta 1552 zcmc(fL2DC17=|;Own<ZL1p|?WwXxAkVM%soXEvKfOGL1QLWzhui7?qrn%Fc+lWpnE zc=4>6lV0rIiy{BRqu|Yx|3d0)b~l@X(UuDCVZMQx-EZf8-go+asedZHT?hDG`d%C> zwI;KzI_sK-&Qx46tk{r#T;V@g?*47ZTC9wfO0#Y4n@DN0mI;*u+A)z+PE4$-N{gM? zW=`qQJ=PPe-FDkHi8_8z4r0Y_9555pidw7Ym1EZJHQM4Um=CjC4`ZQF_(k|@=td}N z)sI^H83_Fe@+VrvU1Ia1aRx3pBU0T0IXJw@>C@`sg3~vny#@m{!M|)jNglP1daUX- zt_F=^9}Ef!Zahd1o{`mAyHIaXO7Q3P=rv6ovDr9gCFguRUfKq)`0)PT^7%LU2Rj91 zo+&275Uue{T9%;3Ps@d61edVos-fr6kiJ<Oq#_%Erhy^>rMw0$gHc^*w97lGxzSlR zQb<7JfykW$!R3;iCW3@`Dzz!WA!J5yZsG_&dS9wakG}ym6#tXrzV!INNPp-`zsi%P z!eVa>-|<Ufd-KuNZFgySp0dKq0^X;n125n(fk&p`VN8u0wS0K|EE_HB9y((hoj%Tx zFo&C=yq6*5Wyo-dLIXj4k|C%uD?sY_V=E#uG~b;6F*+^OMODHmBwvWF51IT9vKKSt z#!Pbyb_;QfF0wcL=ee5=f9LA(eKQd*uWw2SI?=gvf#}RM%{b{{sCmgJGT$jCZ$l!t zsJQfOT4}OOPO-X-+N|tfePY-3mq`f{{=Ik$5OGVO4GHNIB0vWi5CDNDAtWI&V)2(t hWb6bL<wl>=yK{~M13OI|s82bN5G$`z<~tC?egUZ*+Ry+1 delta 439 zcmZozz|_#dG(m~cV4||Rh=ER8PHIVVMrx6jQEIAziLpg8*T%&8{FDFks}fQn&o$Z2 zMqqM|jmzYX`gUAg3=GT?%##?T7$z&aSuiF|uC<n*+~78IvQ0WaW8vhDHgf!C#s)e$ znPsT~iRr1uRz{N_lv+;?NZ_2TX!~ojPL|DNOFPrabF(BFQ#NnUI?1xJK$d-y0=t@l zPEt;Oa<-L`PFZSEab|v=l_8R|EmG4AED{rqfNt=cT%f?R`M*B9!{qIu0+ao0ok0%c z1Uih>l64(}++?FPO%C?tjMBVpz0}EuZW^1L)9jfTYbGDgNRungD=kh<u`;zZFie75 zlw1Q=ZIv>4zq{7tGameti!;^4u_{f$@5KmZvB|ogB|vX3-h452E2nB2)K}(k%hC{W Sk!qP@WMYzL4)qipBNqS{Ae2o2 diff --git a/core/src/com/galaxytrucker/galaxytruckerreloaded/Model/Map/Overworld.java b/core/src/com/galaxytrucker/galaxytruckerreloaded/Model/Map/Overworld.java index 76714081..e1a23586 100644 --- a/core/src/com/galaxytrucker/galaxytruckerreloaded/Model/Map/Overworld.java +++ b/core/src/com/galaxytrucker/galaxytruckerreloaded/Model/Map/Overworld.java @@ -1,33 +1,39 @@ package com.galaxytrucker.galaxytruckerreloaded.Model.Map; -import com.j256.ormlite.field.DatabaseField; -import lombok.Data; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; import lombok.NonNull; +import lombok.RequiredArgsConstructor; +import javax.persistence.*; import java.io.Serializable; import java.util.HashMap; - +import java.util.Map; + +@Entity +@NoArgsConstructor(access = AccessLevel.PUBLIC) +@RequiredArgsConstructor(access = AccessLevel.PUBLIC) +@NamedQueries({ + @NamedQuery(name = "Overworld.getByUsername", query = "select o from Overworld o where o.associatedUser =: name") +}) public class Overworld implements Serializable { /** ID */ @NonNull - @DatabaseField(id = true,columnName = "ID") + @Id private int id; /** Username used as ID */ @NonNull - @DatabaseField(columnName = "user") private String associatedUser; /** Stores planet and their location on the map */ - @NonNull - @DatabaseField(columnName = "planetMap") - private HashMap<float[],Planet> planetMap; + @ElementCollection + private Map<String,Planet> planetMap; /** The start planet */ @NonNull - @DatabaseField(columnName = "startPlanet") private Planet startPlanet; /** Constructor */ diff --git a/core/src/com/galaxytrucker/galaxytruckerreloaded/Model/Ship.java b/core/src/com/galaxytrucker/galaxytruckerreloaded/Model/Ship.java index 08417b50..9926bb33 100644 --- a/core/src/com/galaxytrucker/galaxytruckerreloaded/Model/Ship.java +++ b/core/src/com/galaxytrucker/galaxytruckerreloaded/Model/Ship.java @@ -1,25 +1,27 @@ package com.galaxytrucker.galaxytruckerreloaded.Model; -import com.badlogic.gdx.scenes.scene2d.ui.List; -import com.galaxytrucker.galaxytruckerreloaded.Model.Crew.Crew; import com.galaxytrucker.galaxytruckerreloaded.Model.Map.Planet; import com.galaxytrucker.galaxytruckerreloaded.Model.ShipLayout.Room; import com.galaxytrucker.galaxytruckerreloaded.Model.Weapons.Weapon; import com.j256.ormlite.field.DatabaseField; -import com.j256.ormlite.table.DatabaseTable; import lombok.*; +import javax.persistence.*; import java.io.Serializable; +import java.util.List; @AllArgsConstructor(access = AccessLevel.PUBLIC) @NoArgsConstructor(access = AccessLevel.PUBLIC) @Getter @Setter -@DatabaseTable(tableName = "ship") +@Entity +@NamedQueries({ + @NamedQuery(name = "Ship.getByUsername", query = "select s from Ship s where s.associatedUser =: username") +}) public class Ship implements Serializable { /** ID */ - @DatabaseField(id = true,columnName = "ID") + @Id @NonNull private int id; @@ -27,7 +29,6 @@ public class Ship implements Serializable { * The user this ship belongs to * (uses the user's username) */ - @DatabaseField(columnName = "associatedUser") @NonNull private String associatedUser; @@ -35,84 +36,72 @@ public class Ship implements Serializable { * HP */ @NonNull - @DatabaseField(columnName = "HP") private int hp; /** Coins */ @NonNull - @DatabaseField(columnName = "coins") private int coins; /** Amount of available missiles */ @NonNull - @DatabaseField(columnName = "missles") private int missiles; /** Amount of fuel left*/ @NonNull - @DatabaseField(columnName = "fuel") private int fuel; /** * Energy to be distributed */ @NonNull - @DatabaseField(columnName = "energy") private int energy; /** * Shields that are currently active */ @NonNull - @DatabaseField(columnName = "shieldCharge") private int shieldCharge; /** * Total number of Shields that are powered. Possibly redundant through Shield.getEnergy/2 */ @NonNull - @DatabaseField(columnName = "maximumShieldCharge") private int maxShieldCharge; /** * chance for the ship to dodge incoming attacks */ @NonNull - @DatabaseField(columnName = "evasionChance") private float evasionChance; /** * The planet the ship is currently at */ @NonNull - @DatabaseField(foreign = true,columnName = "planet") private Planet planet; /** Shields */ @NonNull - @DatabaseField(columnName = "shields") private int shields; /** * time needed until position can be changed again */ @NonNull - @DatabaseField(columnName = "FTLCharge") private int FTLCharge; /** This ship's systems */ @NonNull - @DatabaseField(columnName = "systems",foreign = true) + @ElementCollection private List<Room> systems; /** Inventory */ @NonNull - @DatabaseField(foreign = true, columnName = "inventory") + @ElementCollection private List<Weapon> inventory; /** Whether or not the ship is in combat */ - @DatabaseField(columnName = "inCombat") @NonNull private boolean inCombat = false; diff --git a/core/src/com/galaxytrucker/galaxytruckerreloaded/Model/User.java b/core/src/com/galaxytrucker/galaxytruckerreloaded/Model/User.java index 93448a58..bc0748dc 100644 --- a/core/src/com/galaxytrucker/galaxytruckerreloaded/Model/User.java +++ b/core/src/com/galaxytrucker/galaxytruckerreloaded/Model/User.java @@ -2,8 +2,7 @@ package com.galaxytrucker.galaxytruckerreloaded.Model; import lombok.*; -import javax.persistence.Entity; -import javax.persistence.Id; +import javax.persistence.*; import java.io.Serializable; @RequiredArgsConstructor(access = AccessLevel.PUBLIC) @@ -11,6 +10,9 @@ import java.io.Serializable; @Getter @Setter @Entity +@NamedQueries({ + @NamedQuery(name = "User.getByUsername",query = "select u from User u where u.username =: username") +}) public class User implements Serializable { /** @@ -23,6 +25,7 @@ public class User implements Serializable { /** * The user's ship */ + @OneToOne private Ship userShip; /** Whether or not the user is logged in */ diff --git a/core/src/com/galaxytrucker/galaxytruckerreloaded/Server/Persistence/CrewDAO.java b/core/src/com/galaxytrucker/galaxytruckerreloaded/Server/Persistence/CrewDAO.java index 30012c2d..c18bc10c 100644 --- a/core/src/com/galaxytrucker/galaxytruckerreloaded/Server/Persistence/CrewDAO.java +++ b/core/src/com/galaxytrucker/galaxytruckerreloaded/Server/Persistence/CrewDAO.java @@ -1,6 +1,7 @@ package com.galaxytrucker.galaxytruckerreloaded.Server.Persistence; import com.galaxytrucker.galaxytruckerreloaded.Model.Crew.Crew; +import com.galaxytrucker.galaxytruckerreloaded.Server.Database.Database; import com.galaxytrucker.galaxytruckerreloaded.Server.Exception.CrewNotFoundException; import com.galaxytrucker.galaxytruckerreloaded.Server.Exception.DuplicateCrewException; @@ -60,13 +61,4 @@ public class CrewDAO extends ObjectDAO<Crew> { throw new CrewNotFoundException(); } } - - /** - * Constructor - * - * @param entityManager - the EntityManager - */ - public CrewDAO(EntityManager entityManager) { - this.entityManager = entityManager; - } } diff --git a/core/src/com/galaxytrucker/galaxytruckerreloaded/Server/Persistence/ObjectDAO.java b/core/src/com/galaxytrucker/galaxytruckerreloaded/Server/Persistence/ObjectDAO.java index 42f5bc45..7947b5ba 100644 --- a/core/src/com/galaxytrucker/galaxytruckerreloaded/Server/Persistence/ObjectDAO.java +++ b/core/src/com/galaxytrucker/galaxytruckerreloaded/Server/Persistence/ObjectDAO.java @@ -1,5 +1,7 @@ package com.galaxytrucker.galaxytruckerreloaded.Server.Persistence; +import com.galaxytrucker.galaxytruckerreloaded.Server.Database.Database; + import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; @@ -8,7 +10,7 @@ public abstract class ObjectDAO<T> { /** EntityManager */ @PersistenceContext(name = "database") - public EntityManager entityManager; + public EntityManager entityManager = Database.getEntityManager(); /** * Save the object to the database diff --git a/core/src/com/galaxytrucker/galaxytruckerreloaded/Server/Persistence/OverworldDAO.java b/core/src/com/galaxytrucker/galaxytruckerreloaded/Server/Persistence/OverworldDAO.java index fe6702a0..4354f905 100644 --- a/core/src/com/galaxytrucker/galaxytruckerreloaded/Server/Persistence/OverworldDAO.java +++ b/core/src/com/galaxytrucker/galaxytruckerreloaded/Server/Persistence/OverworldDAO.java @@ -5,11 +5,12 @@ import com.galaxytrucker.galaxytruckerreloaded.Server.Exception.DuplicateOverwor import com.galaxytrucker.galaxytruckerreloaded.Server.Exception.OverworldNotFoundException; import com.galaxytrucker.galaxytruckerreloaded.Server.Persistence.ObjectDAO; import com.j256.ormlite.dao.Dao; +import lombok.NonNull; -public class OverworldDAO extends ObjectDAO<Overworld> { +import javax.persistence.NamedQuery; +import javax.persistence.Query; - /** OverworldDAO */ - private Dao<Overworld,String> overworldDAO; +public class OverworldDAO extends ObjectDAO<Overworld> { /** * Add a new OverWorld to the database @@ -19,7 +20,15 @@ public class OverworldDAO extends ObjectDAO<Overworld> { */ @Override public void persist(Overworld o) throws DuplicateOverworldException { - + try { + entityManager.getTransaction().begin(); + entityManager.persist(o); + entityManager.getTransaction().commit(); + } + catch (Exception e){ + e.printStackTrace(); + throw new DuplicateOverworldException(); + } } /** @@ -30,7 +39,15 @@ public class OverworldDAO extends ObjectDAO<Overworld> { */ @Override public void remove(Overworld o) throws OverworldNotFoundException { - + try { + entityManager.getTransaction().begin(); + entityManager.remove(o); + entityManager.getTransaction().commit(); + } + catch (Exception e){ + e.printStackTrace(); + throw new OverworldNotFoundException(); + } } /** Get the overworld of a designated user @@ -38,6 +55,15 @@ public class OverworldDAO extends ObjectDAO<Overworld> { * @return the user's world map * @throws OverworldNotFoundException if the overworld couldn't be found */ public Overworld getOverworldByUser(String username) throws OverworldNotFoundException{ - return null; + try { + entityManager.getTransaction().begin(); + @NonNull Overworld o = entityManager.createNamedQuery("Overworld.getByUsername",Overworld.class).setParameter("name",username).getSingleResult(); + entityManager.getTransaction().commit(); + return o; + } + catch (Exception e){ + e.printStackTrace(); + throw new OverworldNotFoundException(); + } } } diff --git a/core/src/com/galaxytrucker/galaxytruckerreloaded/Server/Persistence/PlanetDAO.java b/core/src/com/galaxytrucker/galaxytruckerreloaded/Server/Persistence/PlanetDAO.java index 7c2f4279..a9d2294d 100644 --- a/core/src/com/galaxytrucker/galaxytruckerreloaded/Server/Persistence/PlanetDAO.java +++ b/core/src/com/galaxytrucker/galaxytruckerreloaded/Server/Persistence/PlanetDAO.java @@ -18,7 +18,15 @@ public class PlanetDAO extends ObjectDAO<Planet> { */ @Override public void persist(Planet p) throws DuplicatePlanetException { - + try { + entityManager.getTransaction().begin(); + entityManager.persist(p); + entityManager.getTransaction().commit(); + } + catch (Exception e){ + e.printStackTrace(); + throw new DuplicatePlanetException(); + } } /** @@ -28,7 +36,15 @@ public class PlanetDAO extends ObjectDAO<Planet> { * @throws PlanetNotFoundException if the planet cannot be found in the database */ public void update(Planet p) throws PlanetNotFoundException { - + try { + entityManager.getTransaction().begin(); + entityManager.merge(p); + entityManager.getTransaction().commit(); + } + catch (Exception e){ + e.printStackTrace(); + throw new PlanetNotFoundException(); + } } /** @@ -39,6 +55,14 @@ public class PlanetDAO extends ObjectDAO<Planet> { */ @Override public void remove(Planet p) throws PlanetNotFoundException { - + try { + entityManager.getTransaction().begin(); + entityManager.remove(p); + entityManager.getTransaction().commit(); + } + catch (Exception e){ + e.printStackTrace(); + throw new PlanetNotFoundException(); + } } } diff --git a/core/src/com/galaxytrucker/galaxytruckerreloaded/Server/Persistence/RoomDAO.java b/core/src/com/galaxytrucker/galaxytruckerreloaded/Server/Persistence/RoomDAO.java index 9fbbb566..ce7c3b52 100644 --- a/core/src/com/galaxytrucker/galaxytruckerreloaded/Server/Persistence/RoomDAO.java +++ b/core/src/com/galaxytrucker/galaxytruckerreloaded/Server/Persistence/RoomDAO.java @@ -8,9 +8,6 @@ import com.j256.ormlite.dao.Dao; /** This class manages room objects in the database */ public class RoomDAO extends ObjectDAO<Room> { - /** RoomDAO */ - private Dao<Room,String> roomDAO; - /** * Add a new room to the database * @@ -19,7 +16,15 @@ public class RoomDAO extends ObjectDAO<Room> { */ @Override public void persist(Room r) throws DuplicateRoomException { - + try { + entityManager.getTransaction().begin(); + entityManager.persist(r); + entityManager.getTransaction().commit(); + } + catch (Exception e){ + e.printStackTrace(); + throw new DuplicateRoomException(); + } } /** @@ -29,7 +34,15 @@ public class RoomDAO extends ObjectDAO<Room> { * @throws RoomNotFoundException if the room cannot be found in the database */ public void update(Room r) throws RoomNotFoundException { - + try { + entityManager.getTransaction().begin(); + entityManager.merge(r); + entityManager.getTransaction().commit(); + } + catch (Exception e){ + e.printStackTrace(); + throw new RoomNotFoundException(); + } } /** @@ -40,6 +53,14 @@ public class RoomDAO extends ObjectDAO<Room> { */ @Override public void remove(Room r) throws RoomNotFoundException { - + try { + entityManager.getTransaction().begin(); + entityManager.remove(r); + entityManager.getTransaction().commit(); + } + catch (Exception e){ + e.printStackTrace(); + throw new RoomNotFoundException(); + } } } diff --git a/core/src/com/galaxytrucker/galaxytruckerreloaded/Server/Persistence/ShipDAO.java b/core/src/com/galaxytrucker/galaxytruckerreloaded/Server/Persistence/ShipDAO.java index 2519a489..eafb79ba 100644 --- a/core/src/com/galaxytrucker/galaxytruckerreloaded/Server/Persistence/ShipDAO.java +++ b/core/src/com/galaxytrucker/galaxytruckerreloaded/Server/Persistence/ShipDAO.java @@ -1,28 +1,16 @@ package com.galaxytrucker.galaxytruckerreloaded.Server.Persistence; import com.galaxytrucker.galaxytruckerreloaded.Model.Ship; +import com.galaxytrucker.galaxytruckerreloaded.Model.User; import com.galaxytrucker.galaxytruckerreloaded.Server.Exception.DuplicateShipException; import com.galaxytrucker.galaxytruckerreloaded.Server.Exception.ShipNotFoundException; import com.galaxytrucker.galaxytruckerreloaded.Server.Exception.UserNotFoundException; import com.j256.ormlite.dao.Dao; import com.j256.ormlite.support.ConnectionSource; +import lombok.NonNull; public class ShipDAO extends ObjectDAO<Ship> { - /** - * ShipDAO - */ - private Dao<Ship, String> shipDAO; - - /** - * Constructor - * - * @param source - the database connection source - */ - public ShipDAO(ConnectionSource source) { - - } - /** * Add a new ship to the database * @@ -30,14 +18,30 @@ public class ShipDAO extends ObjectDAO<Ship> { */ @Override public void persist(Ship s) throws DuplicateShipException { - + try { + entityManager.getTransaction().begin(); + entityManager.persist(s); + entityManager.getTransaction().commit(); + } + catch (Exception e){ + e.printStackTrace(); + throw new DuplicateShipException(); + } } /** Update a ship in the database * @param s - the ship to update * @throws ShipNotFoundException if the ship cannot be found in the database */ public void update(Ship s) throws ShipNotFoundException{ - + try { + entityManager.getTransaction().begin(); + entityManager.merge(s); + entityManager.getTransaction().commit(); + } + catch (Exception e){ + e.printStackTrace(); + throw new ShipNotFoundException(); + } } /** @@ -45,8 +49,17 @@ public class ShipDAO extends ObjectDAO<Ship> { * * @param user - the ship's associated user */ - private Ship getShipByUser(String user) throws ShipNotFoundException, UserNotFoundException { - return null; + private Ship getShipByUser(String user) throws ShipNotFoundException { + try { + entityManager.getTransaction().begin(); + @NonNull Ship s = entityManager.createNamedQuery("Ship.getByUsername",Ship.class).setParameter("username",user).getSingleResult(); + entityManager.getTransaction().commit(); + return s; + } + catch (Exception e){ + e.printStackTrace(); + throw new ShipNotFoundException(); + } } /** @@ -56,6 +69,14 @@ public class ShipDAO extends ObjectDAO<Ship> { */ @Override public void remove(Ship s) throws ShipNotFoundException{ - + try { + entityManager.getTransaction().begin(); + entityManager.remove(s); + entityManager.getTransaction().commit(); + } + catch (Exception e){ + e.printStackTrace(); + throw new ShipNotFoundException(); + } } } diff --git a/core/src/com/galaxytrucker/galaxytruckerreloaded/Server/Persistence/TraderDAO.java b/core/src/com/galaxytrucker/galaxytruckerreloaded/Server/Persistence/TraderDAO.java index 0388a839..a490d0a7 100644 --- a/core/src/com/galaxytrucker/galaxytruckerreloaded/Server/Persistence/TraderDAO.java +++ b/core/src/com/galaxytrucker/galaxytruckerreloaded/Server/Persistence/TraderDAO.java @@ -10,9 +10,6 @@ import com.j256.ormlite.dao.Dao; */ public class TraderDAO extends ObjectDAO<Trader> { - /** TraderDAO */ - private Dao<Trader,String> traderDAO; - /** * Add a new trader to the database * @@ -21,7 +18,15 @@ public class TraderDAO extends ObjectDAO<Trader> { */ @Override public void persist(Trader t) throws DuplicateTraderException { - + try { + entityManager.getTransaction().begin(); + entityManager.persist(t); + entityManager.getTransaction().commit(); + } + catch (Exception e){ + e.printStackTrace(); + throw new DuplicateTraderException(); + } } /** @@ -31,7 +36,15 @@ public class TraderDAO extends ObjectDAO<Trader> { * @throws TraderNotFoundException if the trader cannot be found in the database */ public void update(Trader t) throws TraderNotFoundException { - + try { + entityManager.getTransaction().begin(); + entityManager.merge(t); + entityManager.getTransaction().commit(); + } + catch (Exception e){ + e.printStackTrace(); + throw new TraderNotFoundException(); + } } /** @@ -42,6 +55,14 @@ public class TraderDAO extends ObjectDAO<Trader> { */ @Override public void remove(Trader t) throws TraderNotFoundException { - + try { + entityManager.getTransaction().begin(); + entityManager.remove(t); + entityManager.getTransaction().commit(); + } + catch (Exception e){ + e.printStackTrace(); + throw new TraderNotFoundException(); + } } } diff --git a/core/src/com/galaxytrucker/galaxytruckerreloaded/Server/Persistence/UserDAO.java b/core/src/com/galaxytrucker/galaxytruckerreloaded/Server/Persistence/UserDAO.java index 45c79cb5..5861eff0 100644 --- a/core/src/com/galaxytrucker/galaxytruckerreloaded/Server/Persistence/UserDAO.java +++ b/core/src/com/galaxytrucker/galaxytruckerreloaded/Server/Persistence/UserDAO.java @@ -5,23 +5,10 @@ import com.galaxytrucker.galaxytruckerreloaded.Server.Exception.DuplicateUserExc import com.galaxytrucker.galaxytruckerreloaded.Server.Exception.UserNotFoundException; import com.j256.ormlite.dao.Dao; import com.j256.ormlite.support.ConnectionSource; +import lombok.NonNull; public class UserDAO extends ObjectDAO<User> { - /** - * UserDAO - */ - private Dao<User, String> userDAO; - - /** - * Constructor - * - * @param source - database connection source - */ - public UserDAO(ConnectionSource source) { - - } - /** * Add a new user to the database * @@ -29,14 +16,31 @@ public class UserDAO extends ObjectDAO<User> { */ @Override public void persist(User u) throws DuplicateUserException { - + try { + entityManager.getTransaction().begin(); + entityManager.persist(u); + entityManager.getTransaction().commit(); + } catch (Exception e) { + e.printStackTrace(); + throw new DuplicateUserException(); + } } - /** Update a user in the database + /** + * Update a user in the database + * * @param u - the user to update - * @throws UserNotFoundException if the user cannot be found in the database */ + * @throws UserNotFoundException if the user cannot be found in the database + */ public void update(User u) throws UserNotFoundException { - + try { + entityManager.getTransaction().begin(); + entityManager.merge(u); + entityManager.getTransaction().commit(); + } catch (Exception e) { + e.printStackTrace(); + throw new UserNotFoundException(); + } } /** @@ -45,7 +49,15 @@ public class UserDAO extends ObjectDAO<User> { * @param username - the username of the user */ private User getUserByUsername(String username) throws UserNotFoundException { - return null; + try { + entityManager.getTransaction().begin(); + @NonNull User u = entityManager.createNamedQuery("User.getByUsername",User.class).setParameter("username",username).getSingleResult(); + entityManager.getTransaction().commit(); + return u; + } catch (Exception e) { + e.printStackTrace(); + throw new UserNotFoundException(); + } } /** @@ -55,7 +67,14 @@ public class UserDAO extends ObjectDAO<User> { */ @Override public void remove(User u) throws UserNotFoundException { - + try { + entityManager.getTransaction().begin(); + entityManager.remove(u); + entityManager.getTransaction().commit(); + } catch (Exception e) { + e.printStackTrace(); + throw new UserNotFoundException(); + } } /** @@ -64,7 +83,8 @@ public class UserDAO extends ObjectDAO<User> { * @param username - the username of the user to delete */ private void removeUserByUsername(String username) throws UserNotFoundException { - + User u = getUserByUsername(username); + remove(u); } } diff --git a/core/src/com/galaxytrucker/galaxytruckerreloaded/Server/Persistence/WeaponDAO.java b/core/src/com/galaxytrucker/galaxytruckerreloaded/Server/Persistence/WeaponDAO.java index 01872911..52c16f0f 100644 --- a/core/src/com/galaxytrucker/galaxytruckerreloaded/Server/Persistence/WeaponDAO.java +++ b/core/src/com/galaxytrucker/galaxytruckerreloaded/Server/Persistence/WeaponDAO.java @@ -10,9 +10,6 @@ import com.j256.ormlite.dao.Dao; */ public class WeaponDAO extends ObjectDAO<Weapon> { - /** WeaponDAO */ - private Dao<Weapon,String> weaponDAO; - /** * Add a new weapon to the database * @@ -21,7 +18,15 @@ public class WeaponDAO extends ObjectDAO<Weapon> { */ @Override public void persist(Weapon w) throws DuplicateWeaponException { - + try { + entityManager.getTransaction().begin(); + entityManager.persist(w); + entityManager.getTransaction().commit(); + } + catch (Exception e){ + e.printStackTrace(); + throw new DuplicateWeaponException(); + } } /** @@ -31,7 +36,15 @@ public class WeaponDAO extends ObjectDAO<Weapon> { * @throws WeaponNotFoundException if the weapon doesn't exist in the database */ public void update(Weapon w) throws WeaponNotFoundException { - + try { + entityManager.getTransaction().begin(); + entityManager.merge(w); + entityManager.getTransaction().commit(); + } + catch (Exception e){ + e.printStackTrace(); + throw new WeaponNotFoundException(); + } } /** @@ -42,7 +55,15 @@ public class WeaponDAO extends ObjectDAO<Weapon> { */ @Override public void remove(Weapon w) throws WeaponNotFoundException { - + try { + entityManager.getTransaction().begin(); + entityManager.remove(w); + entityManager.getTransaction().commit(); + } + catch (Exception e){ + e.printStackTrace(); + throw new WeaponNotFoundException(); + } } } diff --git a/core/src/com/galaxytrucker/galaxytruckerreloaded/Test/Server/Persistence/CrewDAOTest.java b/core/src/com/galaxytrucker/galaxytruckerreloaded/Test/Server/Persistence/CrewDAOTest.java index 2443c7b9..4d91f5c2 100644 --- a/core/src/com/galaxytrucker/galaxytruckerreloaded/Test/Server/Persistence/CrewDAOTest.java +++ b/core/src/com/galaxytrucker/galaxytruckerreloaded/Test/Server/Persistence/CrewDAOTest.java @@ -9,6 +9,7 @@ import org.junit.Assert; import org.junit.Test; import javax.persistence.EntityManager; +import javax.xml.crypto.Data; import java.util.UUID; public class CrewDAOTest { @@ -22,7 +23,7 @@ public class CrewDAOTest { /** * CrewDAO */ - private CrewDAO crewDAO = new CrewDAO(entityManager); + private CrewDAO crewDAO = new CrewDAO(); /** -- GitLab