venice-main-classic/src/com/silverwrist/venice/servlets/TopicOperations.java

358 lines
11 KiB
Java
Raw Normal View History

/*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
*
* Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
* WARRANTY OF ANY KIND, either express or implied. See the License for the specific
* language governing rights and limitations under the License.
*
* The Original Code is the Venice Web Communities System.
*
* The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
* for Silverwrist Design Studios. Portions created by Eric J. Bowersox are
* Copyright (C) 2001 Eric J. Bowersox/Silverwrist Design Studios. All Rights Reserved.
*
* Contributor(s):
*/
package com.silverwrist.venice.servlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.log4j.*;
import com.silverwrist.util.StringUtil;
import com.silverwrist.venice.ValidationException;
import com.silverwrist.venice.core.*;
import com.silverwrist.venice.servlets.format.*;
public class TopicOperations extends VeniceServlet
{
/*--------------------------------------------------------------------------------
* Static data members
*--------------------------------------------------------------------------------
*/
private static final String DELETE_CONFIRM_ATTR = "servlets.TopicOperations.delete.confirm";
private static final String DELETE_CONFIRM_PARAM = "confirm";
private static Category logger = Category.getInstance(TopicOperations.class.getName());
/*--------------------------------------------------------------------------------
* Internal functions
*--------------------------------------------------------------------------------
*/
private static SIGContext getSIGParameter(ServletRequest request, UserContext user)
throws ValidationException, DataException
{
String str = request.getParameter("sig");
if (str==null)
{ // no SIG parameter - bail out now!
logger.error("SIG parameter not specified!");
throw new ValidationException("No SIG specified.");
} // end if
try
{ // turn the string into a SIGID, and thence to a SIGContext
int sigid = Integer.parseInt(str);
return user.getSIGContext(sigid);
} // end try
catch (NumberFormatException nfe)
{ // error in Integer.parseInt
logger.error("Cannot convert SIG parameter '" + str + "'!");
throw new ValidationException("Invalid SIG parameter.");
} // end catch
} // end getSIGParameter
private static ConferenceContext getConferenceParameter(ServletRequest request, SIGContext sig)
throws ValidationException, DataException, AccessError
{
String str = request.getParameter("conf");
if (str==null)
{ // no conference parameter - bail out now!
logger.error("Conference parameter not specified!");
throw new ValidationException("No conference specified.");
} // end if
try
{ // turn the string into a ConfID, and thence to a ConferenceContext
int confid = Integer.parseInt(str);
return sig.getConferenceContext(confid);
} // end try
catch (NumberFormatException nfe)
{ // error in Integer.parseInt
logger.error("Cannot convert conference parameter '" + str + "'!");
throw new ValidationException("Invalid conference parameter.");
} // end catch
} // end getConferenceParameter
private static TopicContext getTopicParameter(ServletRequest request, ConferenceContext conf)
throws ValidationException, DataException, AccessError
{
String str = request.getParameter("top");
if (StringUtil.isStringEmpty(str))
{ // no topic parameter - bail out now!
logger.error("Topic parameter not specified!");
throw new ValidationException("No topic specified.");
} // end if
try
{ // turn the string into a TopicID, and thence to a TopicContext
short topicid = Short.parseShort(str);
return conf.getTopic(topicid);
} // end try
catch (NumberFormatException nfe)
{ // error in Integer.parseInt
logger.error("Cannot convert topic parameter '" + str + "'!");
throw new ValidationException("Invalid topic parameter.");
} // end catch
} // end getTopicParameter
/*--------------------------------------------------------------------------------
* Overrides from class HttpServlet
*--------------------------------------------------------------------------------
*/
public String getServletInfo()
{
String rc = "TopicOperations servlet - General topic operations (freeze, archive, etc.)\n"
+ "Part of the Venice Web Communities System\n";
return rc;
} // end getServletInfo
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
UserContext user = getUserContext(request);
RenderData rdat = createRenderData(request,response);
String location = "top";
String locator = null;
String page_title = null;
Object content = null;
SIGContext sig = null; // SIG context
ConferenceContext conf = null; // conference context
TopicContext topic = null; // topic context
try
{ // this outer try is to catch ValidationException
try
{ // all commands require a SIG parameter
sig = getSIGParameter(request,user);
changeMenuSIG(request,sig);
if (logger.isDebugEnabled())
logger.debug("found SIG #" + String.valueOf(sig.getSIGID()));
locator = "sig=" + String.valueOf(sig.getSIGID());
location = "sigprofile?" + locator;
} // end try
catch (DataException de)
{ // error looking up the SIG
page_title = "Database Error";
content = new ErrorBox(page_title,"Database error finding SIG: " + de.getMessage(),location);
} // end catch
if (content==null)
{ // we got the SIG parameter OK
try
{ // all commands require a conference parameter
conf = getConferenceParameter(request,sig);
if (logger.isDebugEnabled())
logger.debug("found conf #" + String.valueOf(conf.getConfID()));
locator += "&conf=" + String.valueOf(conf.getConfID());
location = "confdisp?" + locator;
} // end try
catch (DataException de)
{ // error looking up the conference
page_title = "Database Error";
content = new ErrorBox(page_title,"Database error finding conference: " + de.getMessage(),location);
} // end catch
} // end if
if (content==null)
{ // we got the conference parameter OK
try
{ // now we need a topic parameter
topic = getTopicParameter(request,conf);
if (logger.isDebugEnabled())
logger.debug("found topic #" + String.valueOf(topic.getTopicID()));
locator += "&top=" + String.valueOf(topic.getTopicNumber());
location = "confdisp?" + locator;
} // end try
catch (DataException de)
{ // error looking up the conference
page_title = "Database Error";
content = new ErrorBox(page_title,"Database error finding topic: " + de.getMessage(),location);
} // end catch
} // end if
} // end try
catch (ValidationException ve)
{ // these all get handled in pretty much the same way
page_title = "Error";
content = new ErrorBox(null,ve.getMessage(),location);
} // end catch
catch (AccessError ae)
{ // these all get handled in pretty much the same way
page_title = "Access Error";
content = new ErrorBox(page_title,ae.getMessage(),location);
} // end catch
if (content==null)
{ // figure out what command we want to perform...
String cmd = request.getParameter("cmd");
if (cmd==null)
cmd = "???";
if (cmd.equals("HY") || cmd.equals("HN"))
{ // we want to set the hide status of the topic
try
{ // call down to set the topic!
topic.setHidden(cmd.equals("HY"));
// go back to the topic view
rdat.redirectTo(location);
return;
} // end try
catch (DataException de)
{ // there was a database error
page_title = "Database Error";
content = new ErrorBox(page_title,"Database error setting hide status: " + de.getMessage(),location);
} // end catch
} // end if ("hide" or "show")
else if (cmd.equals("FY") || cmd.equals("FN"))
{ // we want to set the frozen status of the topic
try
{ // call down to set the topic!
topic.setFrozen(cmd.equals("FY"));
// go back to the topic view
rdat.redirectTo(location);
return;
} // end try
catch (DataException de)
{ // there was a database error
page_title = "Database Error";
content = new ErrorBox(page_title,"Database error setting freeze status: " + de.getMessage(),
location);
} // end catch
catch (AccessError ae)
{ // naughty naughty = you can't do this!
page_title = "Access Error";
content = new ErrorBox(page_title,ae.getMessage(),location);
} // end catch
} // end else if ("freeze" or "unfreeze")
else if (cmd.equals("AY") || cmd.equals("AN"))
{ // we want to change the archived status of the topic
try
{ // call down to set the topic!
topic.setArchived(cmd.equals("AY"));
// go back to the topic view
rdat.redirectTo(location);
return;
} // end try
catch (DataException de)
{ // there was a database error
page_title = "Database Error";
content = new ErrorBox(page_title,"Database error setting archive status: " + de.getMessage(),
location);
} // end catch
catch (AccessError ae)
{ // naughty naughty = you can't do this!
page_title = "Access Error";
content = new ErrorBox(page_title,ae.getMessage(),location);
} // end catch
} // end else if ("archive" or "unarchive")
else if (cmd.equals("DEL"))
{ // we need confirmation on this operation!
if (ConfirmBox.isConfirmed(request,DELETE_CONFIRM_ATTR,DELETE_CONFIRM_PARAM))
{ // OK, go ahead, delete the topic!
location = "confdisp?sig=" + String.valueOf(sig.getSIGID()) + "&conf="
+ String.valueOf(conf.getConfID());
try
{ // delete the bloody topic!
topic.delete();
// after which, redirect to conference view
rdat.redirectTo(location);
return;
} // end try
catch (DataException de)
{ // there was a database error
page_title = "Database Error";
content = new ErrorBox(page_title,"Database error deleting topic: " + de.getMessage(),location);
} // end catch
catch (AccessError ae)
{ // naughty naughty = you can't do this!
page_title = "Access Error";
content = new ErrorBox(page_title,ae.getMessage(),location);
} // end catch
} // end if
else
{ // not a proper confirmation - better display one
String message = "You are about to delete topic " + String.valueOf(topic.getTopicNumber())
+ " from the \"" + conf.getName() + "\" conference! Are you sure you want to do this?";
String confirm_url = "topicops?" + locator + "&cmd=DEL";
page_title = "Delete Topic";
content = new ConfirmBox(request,DELETE_CONFIRM_ATTR,DELETE_CONFIRM_PARAM,page_title,
message,confirm_url,location);
} // end else
} // end else if ("delete")
else
{ // unrecognized command
page_title = "Internal Error";
logger.error("invalid command to TopicOperations.doGet: " + cmd);
content = new ErrorBox(page_title,"Invalid command to TopicOperations.doGet",location);
} // end else
} // end if
BaseJSPData basedat = new BaseJSPData(page_title,location,content);
basedat.transfer(getServletContext(),rdat);
} // end doGet
} // end class TopicOperations