001: package poker.business;
002:
003: import java.util.Vector;
004: import java.sql.SQLException;
005: import poker.data.DODS_GameData.*;
006: import poker.data.DODS_PitBoss.*;
007: import poker.data.game.*;
008: import com.lutris.appserver.server.sql.*;
009: import com.lutris.appserver.server.Enhydra;
010: import com.lutris.logging.Logger;
011: import com.lutris.util.*; //import dods.builder.sourceGenerators.Query.*;
012: import com.lutris.dods.builder.generator.query.*;
013: import poker.spec.*;
014:
015: /**
016: *
017: * EnhyDraw!, beta 4, 5/21/99
018: *
019: * Copyright 1999, Larry Wolcot & Daryl Tempesta
020: * ALL rights reserved. Not for commercial use
021: * without written permission from both authors.
022: *
023: * FIX: This thing only runs in "Vector" mode for now.
024: * I ran outta time to finish :(
025: * Hopefully, I can get some time to finish in the next few months.
026: *
027: * This is the PitBossList object that does all of the database dirtywork.
028: * specific to each game module like Poker, BlackJack21 and Slots.
029: *
030: */
031: public class PitBossListImpl extends Vector implements PitBossList,
032: java.io.Serializable {
033: private boolean useDB = false;
034: private String appName = "Poker";
035:
036: /**
037: * Public Constructor
038: */
039: public PitBossListImpl() {
040: super (0);
041: }
042:
043: /**
044: * This method returns a copy of the PitBoss object.
045: */
046: protected PitBossList getCopy() {
047: PitBossListImpl newCopy = new PitBossListImpl();
048: newCopy = this ;
049: return newCopy;
050: }
051:
052: /**
053: * Here is where we determine how to store our data.. db or memory.
054: */
055: public void setUseDB(boolean useDB) throws Exception {
056: //FIX
057: // This is all bunk. PitBoss DODS Object needs
058: // to be finished.. It's 95% done but I ran out of time
059: // for now.
060: try {
061: this .useDB = useDB;
062: if (!getDoesExist(this .appName)) {
063: PitBossVDO newPitBossVDO = new PitBossVDO();
064: newPitBossVDO.setGameName(appName);
065: addPitBoss(newPitBossVDO);
066: }
067:
068: } catch (Exception e) {
069: e.printStackTrace();
070: throw e;
071: }
072: }
073:
074: /**
075: * Send to the Vector() or the DB depending on the useDB flag
076: * This is where we hook into the DODS_GameData Object
077: */
078: public synchronized void addPitBoss(PitBossVDO this PitBoss) {
079: if (!useDB) {
080: super .addElement(this PitBoss);
081: } else {
082: try {
083: addPitBossToDB(this PitBoss);
084: } catch (Exception e) {
085: //There was a problem adding the game! bummer..
086: }
087: }
088: }
089:
090: /**
091: * This is a private method that maps the PokerGame to
092: * a GameDataDO Object so it can be inserted into the database.
093: */
094: public synchronized void addPitBossToDB(PitBossVDO this PitBossVDO)
095: throws Exception {
096: //remap the PokerGame Object into a GameDataDO built by DODS.
097: PitBossDO nullDO = null;
098: PitBossDO newPitBossDO = mapToDO(nullDO, this PitBossVDO);
099: try {
100: DBTransaction db = Enhydra.getDatabaseManager()
101: .createTransaction();
102: try {
103: //Insert the newly mapped object into the DB
104: db.insert(newPitBossDO);
105: db.commit();
106: } catch (SQLException se) {
107: //Something went wrong, so roll it back and pass it on!
108: db.rollback();
109: throw se;
110: } finally {
111: //This releases all resources allocated in last trans
112: db.release();
113: }
114: } catch (Exception e) {
115: throw e;
116: }
117: }
118:
119: /**
120: * This private method takes a PitBossVDO (Vector) object and maps it
121: * into a DODS PitBossDO object. Ideally, they would be the same
122: * but this is used to illustrate adding a DODS object to
123: * a completed application.
124: */
125: private synchronized PitBossDO mapToDO(PitBossDO newPitBossDO,
126: PitBossVDO newPitBossVDO) {
127: try {
128: newPitBossDO = PitBossDO.createVirgin();
129: newPitBossDO.setGameName(newPitBossVDO.getGameName());
130: newPitBossDO.setTotalDollars(newPitBossVDO
131: .getTotalDollars());
132: newPitBossDO.setTotalHandsDealt(newPitBossVDO
133: .getTotalHandsDealt());
134: newPitBossDO.setTotalHandsWon(newPitBossVDO
135: .getTotalHandsWon());
136: newPitBossDO.setTotalBankrupt(newPitBossVDO
137: .getTotalBankrupt());
138: } catch (Exception e) {
139: //This is a DODS sourceGenetaror exception
140: }
141: return newPitBossDO;
142: }
143:
144: /**
145: * This private method takes a GameDataDO object and maps it
146: * into a PokerGame object. Ideally, they would be the same
147: * but this is used to illustrate adding a DODS objects to
148: * a completed application.
149: */
150: private synchronized PitBossVDO mapNewPitBossVDO(
151: PitBossDO this PitBossDO) {
152: PitBossVDO newPitBossVDO = new PitBossVDO();
153:
154: try {
155: newPitBossVDO.setGameName(this PitBossDO.getGameName());
156: newPitBossVDO.setTotalDollars(this PitBossDO
157: .getTotalDollars());
158: newPitBossVDO.setTotalBankrupt(this PitBossDO
159: .getTotalBankrupt());
160: newPitBossVDO.setTotalHandsDealt(this PitBossDO
161: .getTotalHandsDealt());
162: newPitBossVDO.setTotalHandsWon(this PitBossDO
163: .getTotalHandsWon());
164: } catch (Exception e) {
165: //There really shouldn't be an exception
166: }
167: return newPitBossVDO;
168: }
169:
170: /**
171: * This is a provate method that returns the GameDataDO
172: * from the database where oId = id
173: */
174: private PitBossDO getPitBossDOByName(String name) throws Exception {
175: PitBossDO gotPitBossDO = null;
176: PitBossQuery dq = new PitBossQuery();
177: dq.setQueryGameName(name);
178:
179: try {
180: gotPitBossDO = (PitBossDO) dq.getNextDO();
181: } finally {
182: // Free up any resources
183: //dq.release();
184: }
185: return gotPitBossDO;
186: }
187:
188: /**
189: * This method ddtermines which storage medium is ised, then
190: * doles the getGameByName request to the right place.
191: */
192: public PitBossVDO getPitBossVDO(String name) {
193: PitBossVDO this PitBossVDO = new PitBossVDO();
194:
195: if (useDB) {
196: try {
197: PitBossDO this DO = null;
198: this DO = getPitBossDOByName(name);
199: this PitBossVDO = mapNewPitBossVDO(this DO);
200:
201: } catch (Exception e) {
202: //FIX: Still need to take care of some exceptions :)
203: }
204: } else {
205: this PitBossVDO = getPitBossInMemory(name);
206: }
207:
208: return this PitBossVDO;
209: }
210:
211: /**
212: * Return the game that belongs to user name
213: * from the Vector in memory
214: */
215: public synchronized PitBossVDO getPitBossInMemory(String name) {
216:
217: PitBossVDO this PitBossVDO = new PitBossVDO();
218:
219: //Get copy of the PitBossList object so we dont have to
220: //worry about locking it for threadsafe opps
221: PitBossListImpl PitBossListCopy = (PitBossListImpl) this
222: .getCopy();
223: //Yeah, I could have done this another way, but I didn't
224: for (int i = 0; i < PitBossListCopy.size(); i++) {
225: this PitBossVDO = (PitBossVDO) PitBossListCopy.elementAt(i);
226: if (this PitBossVDO.getGameName().equals(name)) {
227: return this PitBossVDO;
228: }
229: }
230: return this PitBossVDO;
231: }
232:
233: /**
234: * Check to see if the DO changes need to be updated to the DB
235: */
236: public synchronized void updatePitBoss(PitBossVDO this VDO) {
237: if (this .useDB) {
238: try {
239: PitBossDO pitBossDO = getPitBossDOByName(this VDO
240: .getGameName());
241: pitBossDO.setGameName(this VDO.getGameName());
242: pitBossDO.setTotalDollars(this VDO.getTotalDollars());
243: pitBossDO.setTotalBankrupt(this VDO.getTotalBankrupt());
244: pitBossDO.setTotalHandsDealt(this VDO
245: .getTotalHandsDealt());
246: pitBossDO.setTotalHandsWon(this VDO.getTotalHandsWon());
247:
248: DBTransaction db = Enhydra.getDatabaseManager()
249: .createTransaction();
250: try {
251: db.update(pitBossDO);
252: db.commit();
253: } catch (SQLException sqle) {
254: db.rollback();
255: throw sqle;
256: } finally {
257: db.release();
258: }
259: } catch (Exception e) {
260: Enhydra.getLogChannel().write(Logger.DEBUG,
261: "ERROR! " + e.toString());
262: //FIX: Still need to take care of some exceptions :)
263: }
264: }
265: }
266:
267: /**
268: * This method ddtermines which storage medium is ised, then
269: * doles the exists request to the right place.
270: */
271: public boolean getDoesExist(String name) {
272: boolean exists = false;
273: PitBossVDO this VDO = getPitBossVDO(name);
274:
275: if ((this VDO != null) && (!this VDO.getGameName().equals(""))) {
276: exists = true;
277: }
278:
279: return exists;
280: }
281:
282: }
|