KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > rubbos > servlets > StoreStory


1 /**
2  * RUBBoS: Rice University Bulletin Board System.
3  * Copyright (C) 2001-2004 Rice University and French National Institute For
4  * Research In Computer Science And Control (INRIA).
5  * Contact: jmob@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Emmanuel Cecchet.
22  * Contributor(s): Niraj Tolia.
23  */

24
25 package edu.rice.rubbos.servlets;
26
27 import java.io.IOException JavaDoc;
28 import java.sql.Connection JavaDoc;
29 import java.sql.PreparedStatement JavaDoc;
30 import java.sql.ResultSet JavaDoc;
31 import java.sql.SQLException JavaDoc;
32
33 import javax.servlet.ServletException JavaDoc;
34 import javax.servlet.http.HttpServletRequest JavaDoc;
35 import javax.servlet.http.HttpServletResponse JavaDoc;
36
37 /**
38  * Builds the html page with the list of all categories and provides links to
39  * browse all items in a category or items in a category for a given region
40  */

41 public class StoreStory extends RubbosHttpServlet
42 {
43
44   public int getPoolSize()
45   {
46     return Config.BrowseCategoriesPoolSize;
47   }
48
49   private void closeConnection(PreparedStatement JavaDoc stmt, Connection JavaDoc conn)
50   {
51     try
52     {
53       if (stmt != null)
54         stmt.close(); // close statement
55
}
56     catch (Exception JavaDoc ignore)
57     {
58     }
59
60     try
61     {
62       if (conn != null)
63           releaseConnection(conn);
64     }
65     catch (Exception JavaDoc ignore)
66     {
67     }
68
69   }
70
71   /** Build the html page for the response */
72   public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
73       throws IOException JavaDoc, ServletException JavaDoc
74   {
75
76     ServletPrinter sp = null;
77     PreparedStatement JavaDoc stmt = null;
78     Connection JavaDoc conn = null;
79
80     String JavaDoc categoryName, nickname, title, body, category, table;
81     String JavaDoc password = null;
82     int userId, access;
83     ResultSet JavaDoc rs = null;
84     int updateResult;
85
86     sp = new ServletPrinter(response, "StoreStory");
87
88     nickname = request.getParameter("nickname");
89     password = request.getParameter("password");
90     title = request.getParameter("title");
91     body = request.getParameter("body");
92     category = request.getParameter("category");
93
94     if (title == null)
95     {
96       sp.printHTML("You must provide a story title!<br>");
97       return;
98     }
99
100     if (body == null)
101     {
102       sp.printHTML("<h3>You must provide a story body!<br></h3>");
103       return;
104     }
105
106     if (category == null)
107     {
108       sp.printHTML("<h3>You must provide a category!<br></h3>");
109       return;
110     }
111
112     sp.printHTMLheader("RUBBoS: Story submission result");
113
114     sp.printHTML("<center><h2>Story submission result:</h2></center><p>\n");
115
116     //Authenticate the user
117
userId = 0;
118     access = 0;
119
120     conn = getConnection();
121
122     if ((nickname != null) && (password != null))
123     {
124       try
125       {
126         stmt = conn
127             .prepareStatement("SELECT id,access FROM users WHERE nickname=\""
128                 + nickname + "\" AND password=\"" + password + "\"");
129         rs = stmt.executeQuery();
130       }
131       catch (Exception JavaDoc e)
132       {
133         sp.printHTML("ERROR: Authentification query failed" + e);
134         closeConnection(stmt, conn);
135         return;
136       }
137       try
138       {
139         if (rs.first())
140         {
141           userId = rs.getInt("id");
142           access = rs.getInt("access");
143         }
144     stmt.close();
145       }
146       catch (Exception JavaDoc e)
147       {
148         sp.printHTML("Exception storing story " + e + "<br>");
149         closeConnection(stmt, conn);
150         return;
151       }
152     }
153
154     table = "submissions";
155     if (userId == 0)
156       sp.printHTML("Story stored by the 'Anonymous Coward'<br>\n");
157     else
158     {
159       if (access == 0)
160         sp.printHTML("Story submitted by regular user " + userId + "<br>\n");
161       else
162       {
163         sp.printHTML("Story posted by author " + userId + "<br>\n");
164         table = "stories";
165       }
166     }
167
168     // Add story to database
169

170     try
171     {
172       stmt = conn.prepareStatement("INSERT INTO " + table
173           + " VALUES (NULL, \"" + title + "\", \"" + body + "\", NOW(), \""
174           + userId + "\", " + category + ")");
175
176       updateResult = stmt.executeUpdate();
177       if (updateResult != 1)
178       {
179         sp.printHTML(" ERROR: Failed to insert new story in database. Number of rows updated == " + updateResult +".");
180         closeConnection(stmt, conn);
181         return;
182       }
183     }
184     catch (SQLException JavaDoc e)
185     {
186       sp.printHTML("Failed to execute Query for StoreStory: " + e);
187       closeConnection(stmt, conn);
188       return;
189     }
190
191     closeConnection(stmt, conn);
192
193     sp.printHTML("Your story has been successfully stored in the " + table
194         + " database table<br>\n");
195
196     sp.printHTMLfooter();
197
198   }
199
200   public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
201       throws IOException JavaDoc, ServletException JavaDoc
202   {
203     doGet(request, response);
204   }
205
206 }
207
Popular Tags