KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > poker > presentation > login > CreationProcessorPresentation


1 package poker.presentation.login;
2
3 import java.io.*;
4 import java.net.*;
5 import com.lutris.http.*;
6 import com.lutris.appserver.server.httpPresentation.*;
7 import com.lutris.appserver.server.user.*;
8 import com.lutris.util.*;
9 import poker.spec.*;
10 import poker.Poker;
11
12 import poker.presentation.main.FirstDealHTML;
13
14 /**
15  * Presentation Object that processes the creation request and redirects
16  * to the appropriate page.
17  *
18  *
19  * EnhyDraw!, beta 4, 5/21/99
20  *
21  * Copyright 1999, Larry Wolcot & Daryl Tempesta
22  * ALL rights reserved. Not for commercial use
23  * without written permission from both authors.
24  *
25  * @author Larry Wolcott
26  * @version $Revision: 1.1 $
27  */

28 public class CreationProcessorPresentation implements HttpPresentation{
29
30
31     private final String JavaDoc homePage = "main/PlaceBetPresentation.po";
32     private final String JavaDoc loginPage = "login/CreatePlayerPresentation.po";
33     private GameManager gameManager;
34
35   public void run(HttpPresentationComms comms) throws Exception JavaDoc{
36
37
38     Poker application = (Poker)comms.application;
39     gameManager = application.getGameManager();
40
41     String JavaDoc username = comms.request.getParameter("username");
42     String JavaDoc pw = comms.request.getParameter("pw");
43     String JavaDoc email = comms.request.getParameter("email");
44     String JavaDoc cashAsString = comms.request.getParameter("cash");
45
46     String JavaDoc redirect = loginPage + "?" + "password=" + pw + "&" +
47                                         "username=" + username + "&" +
48                                         "cash=" + cashAsString;
49
50     String JavaDoc deck = comms.request.getParameter("deck");
51     String JavaDoc thisDeck = "deck1";
52     if (deck.equals("2")) {
53       thisDeck = "deck2";
54     }
55     else if (deck.equals("3")) {
56       thisDeck = "deck3";
57     }
58     else if (deck.equals("4")) {
59       thisDeck = "deck4";
60     }
61     else if (deck.equals("5")) {
62       thisDeck = "deck5";
63     }
64
65     if( username==null || pw==null ) {
66         comms.session.getSessionData().set("error",
67                       "You must specify a username and password to login.");
68         throw new ClientPageRedirectException(
69                   comms.request.getAppFileURIPath(redirect));
70     }
71
72     int cash = 0;
73     if(cashAsString != null && !cashAsString.equals("")) {
74       try {
75        cash = Integer.parseInt(cashAsString);
76       }
77       catch( NumberFormatException JavaDoc e ) {
78         comms.session.getSessionData().set("error",
79                                             "Invalid input in cash field!");
80         throw new ClientPageRedirectException(
81                   comms.request.getAppFileURIPath(redirect));
82       }
83     }
84
85     if( cash < 0 || cash > 1000000 ) {
86         comms.session.getSessionData().set("error",
87                       "Cash amount can't be negative or greather than 1000000!");
88         throw new ClientPageRedirectException(
89                   comms.request.getAppFileURIPath(redirect));
90     }
91
92     boolean auth = true;
93
94     String JavaDoc id = comms.session.getSessionKey();
95 /*
96  *catch Null pointer exception ( we canot make a instances of classes from business layer when we run poker_pres )
97  *We need to allow poker_pres to be dynamic , so if the requested url is /poker_pres/..... we dont need to chack user
98  */

99
100 try{
101     if ((!username.equals("")) && (!pw.equals(""))){
102
103
104        auth = gameManager.getIsNameUsed(username);
105
106        if (auth)
107          comms.session.getSessionData().set("error", "Name is already used!");
108        else {
109
110          PokerGame newGame = PokerGameFactory.getPokerGame("poker.business.PokerGameImpl",username, pw, id, cash);
111          
112          GameManager g = application.getGameManager();
113          newGame.setDeck(thisDeck);
114          newGame.setEmail(email);
115          gameManager.addGame(newGame);
116          comms.session.setUser((User)newGame.getPlayer());
117          comms.session.getSessionData().set("game", newGame);
118          comms.session.getSessionData().set("error", "");
119          redirect = homePage;
120        }
121     }
122     else
123       comms.session.getSessionData().set("error", "Incorrect username or password!");
124
125    
126  
127 }catch (NullPointerException JavaDoc e){
128    throw new ClientPageRedirectException(
129               comms.request.getAppFileURIPath(homePage));
130 }
131  throw new ClientPageRedirectException(comms.request.getAppFileURIPath(redirect));
132 }
133 }
134
Popular Tags