1 package poker.business; 2 3 import java.util.Vector ; 4 import java.sql.SQLException ; 5 import poker.data.DODS_GameData.*; 6 import poker.data.DODS_PitBoss.*; 7 import poker.data.game.*; 8 import com.lutris.appserver.server.sql.*; 9 import com.lutris.appserver.server.Enhydra; 10 import com.lutris.logging.Logger; 11 import com.lutris.util.*; 12 import com.lutris.dods.builder.generator.query.*; 14 import poker.spec.*; 15 31 public class PitBossListImpl extends Vector implements PitBossList,java.io.Serializable { 32 private boolean useDB = false; 33 private String appName = "Poker"; 34 35 38 public PitBossListImpl(){ 39 super(0); 40 } 41 42 45 protected PitBossList getCopy() { 46 PitBossListImpl newCopy = new PitBossListImpl(); 47 newCopy = this; 48 return newCopy; 49 } 50 51 54 public void setUseDB(boolean useDB) throws Exception { 55 try { 60 this.useDB = useDB; 61 if (!getDoesExist(this.appName)){ 62 PitBossVDO newPitBossVDO = new PitBossVDO(); 63 newPitBossVDO.setGameName(appName); 64 addPitBoss(newPitBossVDO); 65 } 66 67 } 68 catch(Exception e) { 69 e.printStackTrace(); 70 throw e; 71 } 72 } 73 74 78 public synchronized void addPitBoss(PitBossVDO thisPitBoss){ 79 if (!useDB) { 80 super.addElement(thisPitBoss); 81 } 82 else { 83 try { 84 addPitBossToDB(thisPitBoss); 85 } 86 catch (Exception e) { 87 } 89 } 90 } 91 92 96 public synchronized void addPitBossToDB(PitBossVDO thisPitBossVDO) 97 throws Exception { 98 PitBossDO nullDO = null; 100 PitBossDO newPitBossDO = mapToDO(nullDO, thisPitBossVDO); 101 try { 102 DBTransaction db =Enhydra.getDatabaseManager().createTransaction(); 103 try { 104 db.insert(newPitBossDO); 106 db.commit(); 107 } catch (SQLException se) { 108 db.rollback(); 110 throw se; 111 } finally { 112 db.release(); 114 } 115 } catch (Exception e) { 116 throw e; 117 } 118 } 119 120 126 private synchronized PitBossDO mapToDO( 127 PitBossDO newPitBossDO, PitBossVDO newPitBossVDO){ 128 try { 129 newPitBossDO = PitBossDO.createVirgin(); 130 newPitBossDO.setGameName(newPitBossVDO.getGameName()); 131 newPitBossDO.setTotalDollars(newPitBossVDO.getTotalDollars()); 132 newPitBossDO.setTotalHandsDealt(newPitBossVDO.getTotalHandsDealt()); 133 newPitBossDO.setTotalHandsWon(newPitBossVDO.getTotalHandsWon()); 134 newPitBossDO.setTotalBankrupt(newPitBossVDO.getTotalBankrupt()); 135 } 136 catch (Exception e){ 137 } 139 return newPitBossDO; 140 } 141 142 148 private synchronized PitBossVDO 149 mapNewPitBossVDO(PitBossDO thisPitBossDO){ 150 PitBossVDO newPitBossVDO = new PitBossVDO(); 151 152 try { 153 newPitBossVDO.setGameName(thisPitBossDO.getGameName()); 154 newPitBossVDO.setTotalDollars(thisPitBossDO.getTotalDollars()); 155 newPitBossVDO.setTotalBankrupt(thisPitBossDO.getTotalBankrupt()); 156 newPitBossVDO.setTotalHandsDealt(thisPitBossDO.getTotalHandsDealt()); 157 newPitBossVDO.setTotalHandsWon(thisPitBossDO.getTotalHandsWon()); 158 } 159 catch (Exception e){ 160 } 162 return newPitBossVDO; 163 } 164 165 169 private PitBossDO getPitBossDOByName(String name) 170 throws Exception { 171 PitBossDO gotPitBossDO = null; 172 PitBossQuery dq = new PitBossQuery(); 173 dq.setQueryGameName(name); 174 175 try { 176 gotPitBossDO = (PitBossDO) dq.getNextDO(); 177 } 178 finally { 179 } 182 return gotPitBossDO; 183 } 184 185 189 public PitBossVDO getPitBossVDO(String name){ 190 PitBossVDO thisPitBossVDO = new PitBossVDO(); 191 192 if (useDB){ 193 try { 194 PitBossDO thisDO = null; 195 thisDO = getPitBossDOByName(name); 196 thisPitBossVDO = mapNewPitBossVDO(thisDO); 197 198 } 199 catch (Exception e) { 200 } 202 } 203 else { 204 thisPitBossVDO = getPitBossInMemory(name); 205 } 206 207 return thisPitBossVDO; 208 } 209 210 214 public synchronized PitBossVDO getPitBossInMemory(String name){ 215 216 PitBossVDO thisPitBossVDO = new PitBossVDO(); 217 218 PitBossListImpl PitBossListCopy = (PitBossListImpl)this.getCopy(); 221 for (int i = 0; i < PitBossListCopy.size(); i++){ 223 thisPitBossVDO = (PitBossVDO)PitBossListCopy.elementAt(i); 224 if (thisPitBossVDO.getGameName().equals(name)){ 225 return thisPitBossVDO; 226 } 227 } 228 return thisPitBossVDO; 229 } 230 231 234 public synchronized void updatePitBoss(PitBossVDO thisVDO){ 235 if (this.useDB) { 236 try { 237 PitBossDO pitBossDO = getPitBossDOByName(thisVDO.getGameName()); 238 pitBossDO.setGameName(thisVDO.getGameName()); 239 pitBossDO.setTotalDollars(thisVDO.getTotalDollars()); 240 pitBossDO.setTotalBankrupt(thisVDO.getTotalBankrupt()); 241 pitBossDO.setTotalHandsDealt(thisVDO.getTotalHandsDealt()); 242 pitBossDO.setTotalHandsWon(thisVDO.getTotalHandsWon()); 243 244 DBTransaction db = Enhydra.getDatabaseManager().createTransaction(); 245 try { 246 db.update(pitBossDO); 247 db.commit(); 248 } 249 catch (SQLException sqle) { 250 db.rollback(); 251 throw sqle; 252 } finally { 253 db.release(); 254 } 255 } catch (Exception e) { 256 Enhydra.getLogChannel().write(Logger.DEBUG, "ERROR! " + 257 e.toString()); 258 } 260 } 261 } 262 263 267 public boolean getDoesExist(String name){ 268 boolean exists = false; 269 PitBossVDO thisVDO = getPitBossVDO(name); 270 271 if( (thisVDO != null) && (!thisVDO.getGameName().equals("") ) ){ 272 exists = true; 273 } 274 275 return exists; 276 } 277 278 } 279 | Popular Tags |