Browse Source

Création execption database

Arthur Brandao 5 years ago
parent
commit
28a60d16fb
3 changed files with 76 additions and 37 deletions
  1. 4 4
      src/db/Database.java
  2. 23 0
      src/db/DatabaseException.java
  3. 49 33
      src/db/DatabaseTable.java

+ 4 - 4
src/db/Database.java

@@ -31,25 +31,25 @@ public class Database {
 		try{
 		    Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
 		} catch(ClassNotFoundException e){
-		    LOGGER.warning("Unable to find Derby driver");
+		    LOGGER.severe("Unable to find Derby driver");
 		    throw new IllegalStateException("Unable to connect to the database", e);
 		}
 		try {
 			db = DriverManager.getConnection(DatabaseProperties.get("url"));
 		} catch (SQLException e) {
-			LOGGER.warning(e.getMessage());
+			LOGGER.severe(e.getMessage());
 			throw new IllegalStateException("Unable to connect to the database", e);
 		}
 	}
 	
-	public static void close() {
+	public static void close() throws DatabaseException {
 		if(db != null) {
 			try {
 				db.close();
 				db = null;
 			} catch (SQLException e) {
 				LOGGER.warning(e.getMessage());
-				throw new IllegalStateException("Unable to close database connection", e);
+				throw new DatabaseException("Unable to close database connection", e);
 			}	
 		}
 	}

+ 23 - 0
src/db/DatabaseException.java

@@ -0,0 +1,23 @@
+package db;
+
+public class DatabaseException extends Exception {
+
+	private static final long serialVersionUID = 7964850487474410989L;
+
+	public DatabaseException() {
+		super();
+	}
+
+	public DatabaseException(String message, Throwable cause) {
+		super(message, cause);
+	}
+
+	public DatabaseException(String message) {
+		super(message);
+	}
+
+	public DatabaseException(Throwable cause) {
+		super(cause);
+	}
+
+}

+ 49 - 33
src/db/DatabaseTable.java

@@ -7,6 +7,7 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Optional;
+import java.util.logging.Logger;
 
 import db.annotation.DbField;
 import db.annotation.DbId;
@@ -16,6 +17,8 @@ import db.mapper.DatabaseMapper;
 
 public abstract class DatabaseTable<T extends Persistable> {
 	
+	private static final Logger LOGGER = Logger.getLogger(DatabaseTable.class.getName());
+	
 	private Map<Long, T> cacheMap = new HashMap<>();
 	
 	public T get(T obj) {
@@ -63,53 +66,66 @@ public abstract class DatabaseTable<T extends Persistable> {
 		return list;
 	}
 	
-	public T refresh(long id) {
+	public T refresh(long id) throws DatabaseException {
 		if (!DatabaseProperties.getBool("cache")) {
-			throw new IllegalArgumentException("Cache is not enabled, can't refresh");
+			throw new DatabaseException("Cache is not enabled, can't refresh");
 		}
 		if (!cacheMap.containsKey(id)) {
-			throw new IllegalArgumentException("Entity is not load, can't refresh");
+			throw new DatabaseException("Entity is not load, can't refresh");
 		}
 		Optional<T> optObj = getFromDbById(id);
 		if(!optObj.isPresent()) {
-			throw new IllegalStateException("Unable to find entity in the database");
+			throw new DatabaseException("Unable to find entity in the database");
 		}
 		return cache(optObj.get());
 	}
 	
-	public T refresh(T obj) {
+	public T refresh(T obj) throws DatabaseException {
 		return refresh(obj.getId());
 	}
 	
 	public T save(T obj) {
-		// Si le cache est activé on se base dessus
-		if (DatabaseProperties.getBool("cache")) {
-			if (cacheMap.containsKey(obj.getId())) {
-				update(obj);
-			} else {
-				insert(obj);
-			}
-		} 
-		// Sinon on regarde en base
-		else {
-			if (findById(obj.getId()).isPresent()) {
-				update(obj);
-			} else {
-				insert(obj);
+		try {
+			// Si le cache est activé on se base dessus
+			if (DatabaseProperties.getBool("cache")) {
+				if (cacheMap.containsKey(obj.getId())) {
+					update(obj);
+				} else {
+					insert(obj);
+				}
+			} 
+			// Sinon on regarde en base
+			else {
+				if (findById(obj.getId()).isPresent()) {
+					update(obj);
+				} else {
+					insert(obj);
+				}
 			}
+			return cache(obj);
+		} catch (DatabaseException e) {
+			LOGGER.severe(e.getMessage());
+			return null;
 		}
-		return cache(obj);
 	}
 	
-	public void del(long id) {
-		// Suppr de la base
-		delete(id);
-		// Retire du cache
-		remove(id);
+	public boolean del(long id) {
+		try {
+			// Suppr de la base
+			delete(id);
+			// Retire du cache
+			remove(id);
+			// TOut est ok
+			return true;
+		} catch (DatabaseException e) {
+			LOGGER.severe(e.getMessage());
+			return false;
+		}
+		
 	}
 	
-	public void del(T obj) {
-		del(obj.getId());
+	public boolean del(T obj) {
+		return del(obj.getId());
 	}
 	
 	private T cache(T obj) {
@@ -195,7 +211,7 @@ public abstract class DatabaseTable<T extends Persistable> {
 		return new ArrayList<>();	
 	}
 	
-	private void insert(T obj) {
+	private void insert(T obj) throws DatabaseException {
 		DbTable dbTable = getDbTableAnnotation();
 		// Récupération du champ id
 		Field id = DatabaseUtils.getIdField(obj.getClass());
@@ -226,7 +242,7 @@ public abstract class DatabaseTable<T extends Persistable> {
 			String attrIdName = id.getName();
 			Map<String, Object> newId = Database.insert(sql.toString(), sql.getParams(), new String[]{dbIdName});
 			if (newId.isEmpty()) {
-				throw new IllegalStateException("Unable to save data");
+				throw new DatabaseException("Unable to save data");
 			}
 			// Récupération de l'id
 			Object val = newId.get(dbIdName);
@@ -241,7 +257,7 @@ public abstract class DatabaseTable<T extends Persistable> {
 		}
 	}
 	
-	private void update(T obj) {
+	private void update(T obj) throws DatabaseException {
 		DbTable dbTable = getDbTableAnnotation();
 		// Récupération du champ id
 		Field id = DatabaseUtils.getIdField(obj.getClass());
@@ -271,14 +287,14 @@ public abstract class DatabaseTable<T extends Persistable> {
 			sql.addId(DatabaseUtils.getDbField(id), id.get(obj));
 			// Execution de la requete
 			if(!Database.execute(sql.toString(), sql.getParams())) {
-				throw new IllegalStateException("Unable to save data");
+				throw new DatabaseException("Unable to save data");
 			}
 		} catch (IllegalArgumentException | IllegalAccessException | SecurityException e) {
 			throw new IllegalStateException("Unable to save data", e);
 		}
 	}
 	
-	private void delete(long id) {
+	private void delete(long id) throws DatabaseException {
 		DbTable dbTable = getDbTableAnnotation();
 		// Récupération du champ id
 		Field idField = DatabaseUtils.getIdField(dbTable.entity());
@@ -290,7 +306,7 @@ public abstract class DatabaseTable<T extends Persistable> {
 		sql.addId(DatabaseUtils.getDbField(idField), id);
 		// Execution de la requete
 		if (!Database.execute(sql.toString(), sql.getParams())) {
-			throw new IllegalStateException("Unable to delete data");
+			throw new DatabaseException("Unable to delete data");
 		}
 	}