KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > cofax > module > CofaxToolsExtPoll


1 /*
2  * CofaxToolsExtPoll is part of the Cofax content management system library.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Please see http://www.cofax.org for contact information and other related informaion.
19  *
20  * $Header: /cvsroot/cofax/cofax/src/org/cofax/module/CofaxToolsExtPoll.java,v 1.4.2.1 2006/12/11 16:31:50 fxrobin Exp $
21  */

22
23 package org.cofax.module;
24
25 import org.cofax.*;
26 import org.cofax.cms.*;
27 import javax.servlet.http.*;
28 import java.util.*;
29
30 /**
31  * CofaxToolsPoll utilities for the polls
32  *
33  * @author Badr Chentouf
34  *
35  */

36
37 public class CofaxToolsExtPoll extends CofaxToolsExtModule {
38
39     /**
40      * navigate throught the different modes for polls
41      *
42      */

43     public String JavaDoc navigate(DataStore db, CofaxPage page, HttpServletRequest req, HttpServletResponse res, HttpSession session) {
44         String JavaDoc mode = (String JavaDoc) req.getParameter("mode");
45         HashMap ht = new HashMap();
46
47         // delete an answer
48
if (mode.equals("poll_delete_answer")) {
49             String JavaDoc answerID = (String JavaDoc) req.getParameter("ANSWERID");
50             String JavaDoc pollID = (String JavaDoc) req.getParameter("POLLID");
51             if ((answerID != null) && !(answerID.equals(""))) {
52                 String JavaDoc tag = "delete from tblpollanswers where answerID=" + answerID;
53                 List list = CofaxToolsDbUtils.getPackageData(db, ht, tag);
54                 page.putGlossaryValue("system:message", getI18NMessage("message_answerdeleted"));
55                 page.putGlossaryValue("request:pollID", pollID);
56             } else {
57                 page.putGlossaryValue("system:message", getI18NMessage("message_errorAnswerID"));
58             }
59         }
60
61         // modify an answer
62
if (mode.equals("poll_set_answer")) {
63             String JavaDoc pollID = (String JavaDoc) req.getParameter("POLLID");
64             String JavaDoc answerID = (String JavaDoc) req.getParameter("ANSWERID");
65             String JavaDoc rank = (String JavaDoc) req.getParameter("rank");
66             String JavaDoc answer = (String JavaDoc) req.getParameter("answer");
67             if ((answerID != null) && !(answerID.equals(""))) {
68                 int intRank = 99;
69                 try {
70                     intRank = Integer.parseInt(rank);
71                 } catch (Exception JavaDoc e) {
72                     intRank = 99;
73                 }
74                 String JavaDoc tag = "update tblpollanswers set answer='" + formatString(answer) + "', rank=" + intRank + " where answerID=" + answerID;
75                 List list = CofaxToolsDbUtils.getPackageData(db, ht, tag);
76                 page.putGlossaryValue("system:message", getI18NMessage("message_answermodified"));
77                 page.putGlossaryValue("request:pollID", pollID);
78             } else {
79                 page.putGlossaryValue("system:message", getI18NMessage("message_errorAnswerID"));
80             }
81         }
82
83         // add an answer
84
if (mode.equals("poll_add_answer")) {
85             String JavaDoc pollID = (String JavaDoc) req.getParameter("POLLID");
86             String JavaDoc rank = (String JavaDoc) req.getParameter("rank");
87             if (rank.equals(""))
88                 rank = "0";
89             int intRank = 99;
90             try {
91                 intRank = Integer.parseInt(rank);
92             } catch (Exception JavaDoc e) {
93                 intRank = 99;
94             }
95             String JavaDoc answer = (String JavaDoc) req.getParameter("answer");
96             if (answer.equals(""))
97                 answer = getI18NMessage("message_errorTitle");
98             if ((pollID != null) && !(pollID.equals(""))) {
99                 String JavaDoc tag = "insert into tblpollanswers(pollID, answer, rank) values(" + pollID + ",'" + formatString(answer) + "', " + intRank + ");";
100                 List list = CofaxToolsDbUtils.getPackageData(db, ht, tag);
101                 page.putGlossaryValue("system:message", getI18NMessage("message_answeradded"));
102                 page.putGlossaryValue("request:pollID", pollID);
103             } else {
104                 page.putGlossaryValue("system:message", getI18NMessage("message_errorPollID"));
105             }
106
107         }
108
109         // modify a poll
110
if (mode.equals("poll_set_poll")) {
111             String JavaDoc pollID = (String JavaDoc) req.getParameter("POLLID");
112             String JavaDoc question = (String JavaDoc) req.getParameter("QUESTION");
113             if ((pollID != null) && !(pollID.equals(""))) {
114                 String JavaDoc tag = "update tblpollquestions set question='" + formatString(question) + "' where pollID=" + pollID;
115                 List list = CofaxToolsDbUtils.getPackageData(db, ht, tag);
116                 page.putGlossaryValue("system:message", getI18NMessage("message_questionmodified"));
117                 page.putGlossaryValue("request:pollID", pollID);
118             } else {
119                 // new poll
120
CofaxToolsUser user = (CofaxToolsUser) (session.getAttribute("user"));
121                 String JavaDoc workingPubName = ((CofaxToolsUser) session.getAttribute("user")).workingPubName;
122                 String JavaDoc tag = "insert into tblpollquestions(question, pubName, active, createDate) values('" + formatString(question) + "','" + workingPubName
123                         + "',0,now())";
124                 List list = CofaxToolsDbUtils.getPackageData(db, ht, tag);
125                 page.putGlossaryValue("system:message", getI18NMessage("message_polladded"));
126             }
127         }
128
129         // delete a poll
130
if (mode.equals("poll_delete_poll")) {
131             String JavaDoc pollID = (String JavaDoc) req.getParameter("POLLID");
132             if ((pollID != null) && !(pollID.equals(""))) {
133                 String JavaDoc tag = "delete from tblpollanswers where pollID=" + pollID;
134                 List list = CofaxToolsDbUtils.getPackageData(db, ht, tag);
135                 String JavaDoc tag2 = "delete from tblpollquestions where pollID=" + pollID;
136                 List list2 = CofaxToolsDbUtils.getPackageData(db, ht, tag2);
137                 page.putGlossaryValue("system:message", getI18NMessage("message_polldeleted"));
138             } else {
139                 page.putGlossaryValue("system:message", getI18NMessage("message_errorPollID"));
140             }
141         }
142
143         // initialize a poll
144
if (mode.equals("poll_init_poll")) {
145             String JavaDoc pollID = (String JavaDoc) req.getParameter("POLLID");
146             if ((pollID != null) && !(pollID.equals(""))) {
147                 String JavaDoc tag = "update tblpollanswers set countVotes=0 where pollID=" + pollID;
148                 List list = CofaxToolsDbUtils.getPackageData(db, ht, tag);
149                 String JavaDoc tag2 = "update tblpollquestions set countVotes=0 where pollID=" + pollID;
150                 List list2 = CofaxToolsDbUtils.getPackageData(db, ht, tag2);
151                 page.putGlossaryValue("system:message", getI18NMessage("message_countersreset"));
152             } else {
153                 page.putGlossaryValue("system:message", getI18NMessage("message_errorPollID"));
154             }
155         }
156
157         // activate a poll
158
if (mode.equals("poll_activate_poll")) {
159             String JavaDoc pollID = (String JavaDoc) req.getParameter("POLLID");
160             String JavaDoc pubName = (String JavaDoc) req.getParameter("PUBNAME");
161             String JavaDoc active = (String JavaDoc) req.getParameter("active");
162             String JavaDoc value1 = "1";
163             String JavaDoc value2 = "0";
164             if ((active != null) && (active.equals("0"))) {
165                 value1 = "0";
166                 value2 = "1";
167             }
168
169             if ((pollID != null) && !(pollID.equals(""))) {
170                 String JavaDoc tag = "update tblpollquestions set active=" + value1 + " where pollID=" + pollID;
171                 List list = CofaxToolsDbUtils.getPackageData(db, ht, tag);
172                 String JavaDoc tag2 = "update tblpollquestions set active=" + value2 + " where pollID!=" + pollID + " and pubName='" + pubName + "'";
173                 List list2 = CofaxToolsDbUtils.getPackageData(db, ht, tag2);
174                 page.putGlossaryValue("system:message", getI18NMessage("message_pollmodified"));
175             } else {
176                 page.putGlossaryValue("system:message", getI18NMessage("message_errorPollID"));
177             }
178         }
179
180         page.putGlossaryValue("system:highLightTab", "admin");
181         page.putGlossaryValue("system:message", getI18NMessage("message_welcome"));
182         CofaxToolsNavigation.includeResource(page, "" + CofaxToolsServlet.templatePath + "module/editPoll.jsp", req, res, session); // FX :
183
// added
184
// templatePath
185

186         return "";
187     }
188
189     public String JavaDoc formatString(String JavaDoc input) {
190         String JavaDoc retVal = CofaxToolsUtil.replace(input, "'", "''");
191         retVal = CofaxToolsUtil.replace(retVal, "\\", "\\\\");
192         return retVal;
193     }
194
195     public String JavaDoc getI18NMessage(String JavaDoc message) {
196         ResourceBundle messages;
197         String JavaDoc returnMessage = "";
198         Locale lcl = CofaxToolsServlet.lcl;
199         try {
200             messages = ResourceBundle.getBundle("org.cofax.module.poll", lcl);
201             returnMessage = messages.getString(message);
202         } catch (Exception JavaDoc e) {
203             CofaxToolsUtil.log("CofaxToolsExtPoll : getI18NMessage : error while reading " + message);
204         }
205         return (returnMessage);
206     }
207
208 }
209
Popular Tags