|  | @@ -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");
 | 
	
		
			
				|  |  |  		}
 | 
	
		
			
				|  |  |  	}
 | 
	
		
			
				|  |  |  
 |