Skip to content
Snippets Groups Projects
Commit 8e0e3929 authored by Leonard's avatar Leonard
Browse files

persistence done so far

parent 5d5aa17d
No related branches found
No related tags found
No related merge requests found
Showing
with 256 additions and 109 deletions
No preview for this file type
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 */
......
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;
......
......@@ -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 */
......
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;
}
}
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
......
......@@ -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();
}
}
}
......@@ -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();
}
}
}
......@@ -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();
}
}
}
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();
}
}
}
......@@ -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();
}
}
}
......@@ -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);
}
}
......@@ -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();
}
}
}
......@@ -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();
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment