KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > poker > business > PitBossListImpl


1 package poker.business;
2
3 import java.util.Vector JavaDoc;
4 import java.sql.SQLException JavaDoc;
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 dods.builder.sourceGenerators.Query.*;
13
import com.lutris.dods.builder.generator.query.*;
14 import poker.spec.*;
15 /**
16  *
17  * EnhyDraw!, beta 4, 5/21/99
18  *
19  * Copyright 1999, Larry Wolcot & Daryl Tempesta
20  * ALL rights reserved. Not for commercial use
21  * without written permission from both authors.
22  *
23  * FIX: This thing only runs in "Vector" mode for now.
24  * I ran outta time to finish :(
25  * Hopefully, I can get some time to finish in the next few months.
26  *
27  * This is the PitBossList object that does all of the database dirtywork.
28  * specific to each game module like Poker, BlackJack21 and Slots.
29  *
30  */

31 public class PitBossListImpl extends Vector JavaDoc implements PitBossList,java.io.Serializable JavaDoc {
32     private boolean useDB = false;
33     private String JavaDoc appName = "Poker";
34
35    /**
36     * Public Constructor
37     */

38     public PitBossListImpl(){
39         super(0);
40     }
41
42     /**
43      * This method returns a copy of the PitBoss object.
44      */

45     protected PitBossList getCopy() {
46       PitBossListImpl newCopy = new PitBossListImpl();
47       newCopy = this;
48       return newCopy;
49     }
50
51     /**
52      * Here is where we determine how to store our data.. db or memory.
53      */

54     public void setUseDB(boolean useDB) throws Exception JavaDoc{
55       //FIX
56
// This is all bunk. PitBoss DODS Object needs
57
// to be finished.. It's 95% done but I ran out of time
58
// for now.
59
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 JavaDoc e) {
69         e.printStackTrace();
70         throw e;
71       }
72     }
73
74    /**
75     * Send to the Vector() or the DB depending on the useDB flag
76     * This is where we hook into the DODS_GameData Object
77     */

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 JavaDoc e) {
87       //There was a problem adding the game! bummer..
88
}
89       }
90     }
91
92    /**
93     * This is a private method that maps the PokerGame to
94     * a GameDataDO Object so it can be inserted into the database.
95     */

96     public synchronized void addPitBossToDB(PitBossVDO thisPitBossVDO)
97                             throws Exception JavaDoc{
98       //remap the PokerGame Object into a GameDataDO built by DODS.
99
PitBossDO nullDO = null;
100       PitBossDO newPitBossDO = mapToDO(nullDO, thisPitBossVDO);
101       try {
102           DBTransaction db =Enhydra.getDatabaseManager().createTransaction();
103           try {
104         //Insert the newly mapped object into the DB
105
db.insert(newPitBossDO);
106         db.commit();
107           } catch (SQLException JavaDoc se) {
108         //Something went wrong, so roll it back and pass it on!
109
db.rollback();
110               throw se;
111           } finally {
112         //This releases all resources allocated in last trans
113
db.release();
114           }
115       } catch (Exception JavaDoc e) {
116         throw e;
117       }
118     }
119
120     /**
121      * This private method takes a PitBossVDO (Vector) object and maps it
122      * into a DODS PitBossDO object. Ideally, they would be the same
123      * but this is used to illustrate adding a DODS object to
124      * a completed application.
125      */

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 JavaDoc e){
137        //This is a DODS sourceGenetaror exception
138
}
139        return newPitBossDO;
140     }
141
142     /**
143      * This private method takes a GameDataDO object and maps it
144      * into a PokerGame object. Ideally, they would be the same
145      * but this is used to illustrate adding a DODS objects to
146      * a completed application.
147      */

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 JavaDoc e){
160            //There really shouldn't be an exception
161
}
162       return newPitBossVDO;
163     }
164
165     /**
166      * This is a provate method that returns the GameDataDO
167      * from the database where oId = id
168      */

169     private PitBossDO getPitBossDOByName(String JavaDoc name)
170                         throws Exception JavaDoc{
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           // Free up any resources
180
//dq.release();
181
}
182       return gotPitBossDO;
183     }
184
185     /**
186      * This method ddtermines which storage medium is ised, then
187      * doles the getGameByName request to the right place.
188      */

189     public PitBossVDO getPitBossVDO(String JavaDoc 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 JavaDoc e) {
200              //FIX: Still need to take care of some exceptions :)
201
}
202       }
203       else {
204           thisPitBossVDO = getPitBossInMemory(name);
205       }
206
207         return thisPitBossVDO;
208     }
209
210     /**
211      * Return the game that belongs to user name
212      * from the Vector in memory
213      */

214     public synchronized PitBossVDO getPitBossInMemory(String JavaDoc name){
215
216       PitBossVDO thisPitBossVDO = new PitBossVDO();
217
218       //Get copy of the PitBossList object so we dont have to
219
//worry about locking it for threadsafe opps
220
PitBossListImpl PitBossListCopy = (PitBossListImpl)this.getCopy();
221       //Yeah, I could have done this another way, but I didn't
222
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     /**
232      * Check to see if the DO changes need to be updated to the DB
233      */

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 JavaDoc sqle) {
250              db.rollback();
251              throw sqle;
252          } finally {
253              db.release();
254          }
255         } catch (Exception JavaDoc e) {
256                 Enhydra.getLogChannel().write(Logger.DEBUG, "ERROR! " +
257                         e.toString());
258                 //FIX: Still need to take care of some exceptions :)
259
}
260     }
261     }
262
263     /**
264      * This method ddtermines which storage medium is ised, then
265      * doles the exists request to the right place.
266      */

267     public boolean getDoesExist(String JavaDoc 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