Simplification de l'utilisation de l'ORM (cast inutile retiré)
This commit is contained in:
parent
2c90cc3610
commit
29c68c2687
@ -52,7 +52,7 @@ public class MPGroupElement extends SQLElement {
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
@ -60,7 +60,7 @@ public class MPGroupUserElement extends SQLElement {
|
||||
|
||||
|
||||
public MPWebSessionElement getWebSessionData() throws SQLException {
|
||||
return ((MPWebSessionTable)ORM.getTable("mp_web_session"))
|
||||
return ORM.getTable(MPWebSessionTable.class)
|
||||
.getFirst("playerName LIKE '"+getPlayerName()+"'", "id ASC");
|
||||
}
|
||||
|
||||
|
@ -159,7 +159,7 @@ public class MPMessageElement extends SQLElement {
|
||||
public MPGroupElement getDestGroupElement() throws SQLException {
|
||||
if (getDestGroup() == null) return null;
|
||||
|
||||
return ((MPGroupTable)ORM.getTable("mp_group")).get(getDestGroup());
|
||||
return ORM.getTable(MPGroupTable.class).get(getDestGroup());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,8 +1,8 @@
|
||||
package net.mc_pandacraft.java.plugin.pandacraftutils.data_model;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <b>ORM = Object-Relational Mapping</b><br/>
|
||||
@ -21,7 +21,7 @@ import java.util.Map;
|
||||
public final class ORM {
|
||||
|
||||
@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() {
|
||||
@ -31,14 +31,14 @@ public final class ORM {
|
||||
* 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.put("mp_group", new MPGroupTable());
|
||||
tables.put("mp_group_user", new MPGroupUserTable());
|
||||
tables.put("mp_web_session", new MPWebSessionTable());
|
||||
tables.add(new MPMessageTable());
|
||||
tables.add(new MPGroupTable());
|
||||
tables.add(new MPGroupUserTable());
|
||||
tables.add(new MPWebSessionTable());
|
||||
|
||||
|
||||
} 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")
|
||||
public synchronized static <T extends SQLTable> T getTable(Class<T> c) {
|
||||
if (c == null) return null;
|
||||
for (SQLTable table : tables.values()) {
|
||||
for (SQLTable table : tables) {
|
||||
|
||||
if (c.isAssignableFrom(table.getClass())) {
|
||||
return c.cast(table);
|
||||
|
@ -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;
|
||||
|
||||
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)
|
||||
throw new MessageSendingException("Ce groupe n'existe pas");
|
||||
|
||||
// on vérifie si le joueur source est dans le groupe
|
||||
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)
|
||||
throw new MessageSendingException("Vous n'êtes pas dans ce groupe");
|
||||
@ -593,7 +593,7 @@ public class PrivateMessagesManager extends BukkitRunnable {
|
||||
|
||||
public void onxAuthLogin(xAuthLoginEvent event) {
|
||||
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();
|
||||
|
||||
@ -640,7 +640,7 @@ public class PrivateMessagesManager extends BukkitRunnable {
|
||||
}
|
||||
|
||||
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) {
|
||||
long lastWebActivity = webSession.getLastWebActivity();
|
||||
|
@ -39,9 +39,7 @@ public class StaffQueueManager {
|
||||
public StaffQueueManager() {
|
||||
|
||||
try {
|
||||
StaffTicketTable dbTable = (StaffTicketTable) ORM.getTable("staff_ticket");
|
||||
|
||||
List<StaffTicketElement> elmts = dbTable.getAll(null, "creationTime ASC, id ASC", null, null);
|
||||
List<StaffTicketElement> elmts = ORM.getTable(StaffTicketTable.class).getAll(null, "creationTime ASC, id ASC", null, null);
|
||||
|
||||
for (StaffTicketElement el : elmts)
|
||||
playerQueue.add(new WaitingPlayer(el));
|
||||
@ -322,7 +320,7 @@ public class StaffQueueManager {
|
||||
message = m;
|
||||
|
||||
try {
|
||||
StaffTicketElement el = ((StaffTicketTable)ORM.getTable("staff_ticket")).get(dbId);
|
||||
StaffTicketElement el = ORM.getTable(StaffTicketTable.class).get(dbId);
|
||||
|
||||
el.setMessage(message);
|
||||
el.save();
|
||||
@ -342,7 +340,7 @@ public class StaffQueueManager {
|
||||
else staffPlayer = p.getName();
|
||||
|
||||
try {
|
||||
StaffTicketElement el = ((StaffTicketTable)ORM.getTable("staff_ticket")).get(dbId);
|
||||
StaffTicketElement el = ORM.getTable(StaffTicketTable.class).get(dbId);
|
||||
|
||||
el.setStaffPlayer(staffPlayer);
|
||||
el.save();
|
||||
@ -357,7 +355,7 @@ public class StaffQueueManager {
|
||||
|
||||
public void deleteFromDB() {
|
||||
try {
|
||||
((StaffTicketTable)ORM.getTable("staff_ticket")).get(dbId).delete();
|
||||
ORM.getTable(StaffTicketTable.class).get(dbId).delete();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user