Simplification de l'utilisation de l'ORM (cast inutile retiré)

This commit is contained in:
Marc Baloup 2015-05-29 11:00:22 +02:00
parent 2c90cc3610
commit 29c68c2687
6 changed files with 22 additions and 36 deletions

View File

@ -52,7 +52,7 @@ public class MPGroupElement extends SQLElement {
public List<MPGroupUserElement> getUsers() throws SQLException { public List<MPGroupUserElement> getUsers() throws SQLException {
return ((MPGroupUserTable)ORM.getTable("mp_group_user")) return ORM.getTable(MPGroupUserTable.class)
.getAll("groupId = "+getId(), "id ASC", null, null); .getAll("groupId = "+getId(), "id ASC", null, null);
} }

View File

@ -60,7 +60,7 @@ public class MPGroupUserElement extends SQLElement {
public MPWebSessionElement getWebSessionData() throws SQLException { public MPWebSessionElement getWebSessionData() throws SQLException {
return ((MPWebSessionTable)ORM.getTable("mp_web_session")) return ORM.getTable(MPWebSessionTable.class)
.getFirst("playerName LIKE '"+getPlayerName()+"'", "id ASC"); .getFirst("playerName LIKE '"+getPlayerName()+"'", "id ASC");
} }

View File

@ -159,7 +159,7 @@ public class MPMessageElement extends SQLElement {
public MPGroupElement getDestGroupElement() throws SQLException { public MPGroupElement getDestGroupElement() throws SQLException {
if (getDestGroup() == null) return null; if (getDestGroup() == null) return null;
return ((MPGroupTable)ORM.getTable("mp_group")).get(getDestGroup()); return ORM.getTable(MPGroupTable.class).get(getDestGroup());
} }
} }

View File

@ -1,8 +1,8 @@
package net.mc_pandacraft.java.plugin.pandacraftutils.data_model; package net.mc_pandacraft.java.plugin.pandacraftutils.data_model;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.HashMap; import java.util.ArrayList;
import java.util.Map; import java.util.List;
/** /**
* <b>ORM = Object-Relational Mapping</b><br/> * <b>ORM = Object-Relational Mapping</b><br/>
@ -21,7 +21,7 @@ import java.util.Map;
public final class ORM { public final class ORM {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
private static Map<String, SQLTable> tables = new HashMap<String, SQLTable>(); private static List<SQLTable> tables = new ArrayList<SQLTable>();
public synchronized static void init() { public synchronized static void init() {
@ -31,14 +31,14 @@ public final class ORM {
* Les tables SQL sont à instancier ici ! * Les tables SQL sont à instancier ici !
*/ */
tables.put("modo_history", new ModoHistoryTable()); tables.add(new ModoHistoryTable());
tables.put("staff_ticket", new StaffTicketTable()); tables.add(new StaffTicketTable());
tables.put("mp_message", new MPMessageTable()); tables.add(new MPMessageTable());
tables.put("mp_group", new MPGroupTable()); tables.add(new MPGroupTable());
tables.put("mp_group_user", new MPGroupUserTable()); tables.add(new MPGroupUserTable());
tables.put("mp_web_session", new MPWebSessionTable()); tables.add(new MPWebSessionTable());
} catch (SQLException e) { } catch (SQLException e) {
@ -50,22 +50,10 @@ public final class ORM {
/**
* Récupère l'instance de la classe associé à la table demandé en paramètre.<br/>
* <b>Il est conseillé de caster le retour de cette méthode par la classe associé à la table demandé.</b>
* @param name nom de la table telle qu'elle est enregistrée dans la classe ORM
* @return ta table dont le nom est passé en paramètre
*/
@SuppressWarnings("rawtypes")
public synchronized static SQLTable getTable(String name) {
return tables.get(name);
}
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
public synchronized static <T extends SQLTable> T getTable(Class<T> c) { public synchronized static <T extends SQLTable> T getTable(Class<T> c) {
if (c == null) return null; if (c == null) return null;
for (SQLTable table : tables.values()) { for (SQLTable table : tables) {
if (c.isAssignableFrom(table.getClass())) { if (c.isAssignableFrom(table.getClass())) {
return c.cast(table); return c.cast(table);

View File

@ -105,7 +105,7 @@ public class PrivateMessagesManager extends BukkitRunnable {
List<MPMessageElement> messages = ((MPMessageTable)ORM.getTable("mp_message")).getAllUnsyncMessage(); List<MPMessageElement> messages = ORM.getTable(MPMessageTable.class).getAllUnsyncMessage();
@ -173,14 +173,14 @@ public class PrivateMessagesManager extends BukkitRunnable {
MPGroupElement groupEl = null; MPGroupElement groupEl = null;
try { try {
groupEl = ((MPGroupTable)ORM.getTable("mp_group")).getFirst("groupName LIKE '"+groupe+ "'", "id"); groupEl = ORM.getTable(MPGroupTable.class).getFirst("groupName LIKE '"+groupe+ "'", "id");
if (groupEl == null) if (groupEl == null)
throw new MessageSendingException("Ce groupe n'existe pas"); throw new MessageSendingException("Ce groupe n'existe pas");
// on vérifie si le joueur source est dans le groupe // on vérifie si le joueur source est dans le groupe
if (sender.isPlayer()) { if (sender.isPlayer()) {
MPGroupUserElement userInGroup = ((MPGroupUserTable)ORM.getTable("mp_group_user")).getPlayerInGroup(groupEl, sender.senderName); MPGroupUserElement userInGroup = ORM.getTable(MPGroupUserTable.class).getPlayerInGroup(groupEl, sender.senderName);
if (userInGroup == null) if (userInGroup == null)
throw new MessageSendingException("Vous n'êtes pas dans ce groupe"); throw new MessageSendingException("Vous n'êtes pas dans ce groupe");
@ -593,7 +593,7 @@ public class PrivateMessagesManager extends BukkitRunnable {
public void onxAuthLogin(xAuthLoginEvent event) { public void onxAuthLogin(xAuthLoginEvent event) {
try { try {
List<MPMessageElement> messages = ((MPMessageTable)ORM.getTable("mp_message")).getAllUnreadForPlayer(event.getPlayer().getName()); List<MPMessageElement> messages = ORM.getTable(MPMessageTable.class).getAllUnreadForPlayer(event.getPlayer().getName());
int nbMess = messages.size(); int nbMess = messages.size();
@ -640,7 +640,7 @@ public class PrivateMessagesManager extends BukkitRunnable {
} }
try { try {
MPWebSessionElement webSession = ((MPWebSessionTable)ORM.getTable("mp_web_session")).getFirst("playerName LIKE '"+playerName+"'", null); MPWebSessionElement webSession = ORM.getTable(MPWebSessionTable.class).getFirst("playerName LIKE '"+playerName+"'", null);
if (webSession != null) { if (webSession != null) {
long lastWebActivity = webSession.getLastWebActivity(); long lastWebActivity = webSession.getLastWebActivity();

View File

@ -39,9 +39,7 @@ public class StaffQueueManager {
public StaffQueueManager() { public StaffQueueManager() {
try { try {
StaffTicketTable dbTable = (StaffTicketTable) ORM.getTable("staff_ticket"); List<StaffTicketElement> elmts = ORM.getTable(StaffTicketTable.class).getAll(null, "creationTime ASC, id ASC", null, null);
List<StaffTicketElement> elmts = dbTable.getAll(null, "creationTime ASC, id ASC", null, null);
for (StaffTicketElement el : elmts) for (StaffTicketElement el : elmts)
playerQueue.add(new WaitingPlayer(el)); playerQueue.add(new WaitingPlayer(el));
@ -322,7 +320,7 @@ public class StaffQueueManager {
message = m; message = m;
try { try {
StaffTicketElement el = ((StaffTicketTable)ORM.getTable("staff_ticket")).get(dbId); StaffTicketElement el = ORM.getTable(StaffTicketTable.class).get(dbId);
el.setMessage(message); el.setMessage(message);
el.save(); el.save();
@ -342,7 +340,7 @@ public class StaffQueueManager {
else staffPlayer = p.getName(); else staffPlayer = p.getName();
try { try {
StaffTicketElement el = ((StaffTicketTable)ORM.getTable("staff_ticket")).get(dbId); StaffTicketElement el = ORM.getTable(StaffTicketTable.class).get(dbId);
el.setStaffPlayer(staffPlayer); el.setStaffPlayer(staffPlayer);
el.save(); el.save();
@ -357,7 +355,7 @@ public class StaffQueueManager {
public void deleteFromDB() { public void deleteFromDB() {
try { try {
((StaffTicketTable)ORM.getTable("staff_ticket")).get(dbId).delete(); ORM.getTable(StaffTicketTable.class).get(dbId).delete();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }