2001-04-12 05:53:26 +00:00

385 lines
13 KiB
Java

/*
* 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.venice.ValidationException;
import com.silverwrist.venice.core.*;
import com.silverwrist.venice.servlets.format.*;
public class SIGOperations extends VeniceServlet
{
/*--------------------------------------------------------------------------------
* Static data members
*--------------------------------------------------------------------------------
*/
private static final String UNJOIN_CONFIRM_ATTR = "servlets.SIGOperations.unjoin.confirm";
private static final String UNJOIN_CONFIRM_PARAM = "confirm";
private static Category logger = Category.getInstance(SIGOperations.class.getName());
/*--------------------------------------------------------------------------------
* Internal functions
*--------------------------------------------------------------------------------
*/
private JoinKeyDialog makeJoinKeyDialog()
{
final String desired_name = "JoinKeyDialog";
DialogCache cache = DialogCache.getDialogCache(getServletContext());
if (!(cache.isCached(desired_name)))
{ // create a template and save it off
JoinKeyDialog template = new JoinKeyDialog();
cache.saveTemplate(template);
} // end if
// return a new copy
return (JoinKeyDialog)(cache.getNewDialog(desired_name));
} // end makeJoinKeyDialog
private CreateSIGDialog makeCreateSIGDialog() throws ServletException
{
final String desired_name = "CreateSIGDialog";
DialogCache cache = DialogCache.getDialogCache(getServletContext());
if (!(cache.isCached(desired_name)))
{ // create a template and save it off
CreateSIGDialog template = new CreateSIGDialog(getCountryList(),getLanguageList());
cache.saveTemplate(template);
} // end if
// return a new copy
return (CreateSIGDialog)(cache.getNewDialog(desired_name));
} // end makeCreateSIGDialog
/*--------------------------------------------------------------------------------
* Overrides from class HttpServlet
*--------------------------------------------------------------------------------
*/
public String getServletInfo()
{
String rc = "SIGOperations servlet - General SIG operations (join, unjoin, etc.)\n"
+ "Part of the Venice Web Communities System\n";
return rc;
} // end getServletInfo
/*--------------------------------------------------------------------------------
* Overrides from class VeniceServlet
*--------------------------------------------------------------------------------
*/
protected VeniceContent doVeniceGet(HttpServletRequest request, VeniceEngine engine,
UserContext user, RenderData rdat)
throws ServletException, IOException, VeniceServletResult
{
// get the command we want to use
String cmd = getStandardCommandParam(request);
setMyLocation(request,"sigops?" + request.getQueryString());
if (cmd.equals("J"))
{ // "J" = "Join" (requires SIG parameter)
SIGContext sig = getSIGParameter(request,user,true,"top");
if (!(sig.canJoin())) // not permitted to join!
return new ErrorBox("SIG Error","You are not permitted to join this SIG.","top");
if (sig.isPublicSIG())
{ // attempt to join right now! (no join key required)
try
{ // call down to join the SIG
sig.join(null);
// success! display the "welcome" page
clearMenu(request); // force the clear to regen the menus
changeMenuSIG(request,sig);
return new SIGWelcome(sig);
} // end try
catch (AccessError ae)
{ // access error
return new ErrorBox("Access Error","Unable to join SIG: " + ae.getMessage(),"top");
} // end catch
catch (DataException de)
{ // data exception doing something
return new ErrorBox("Database Error","Database error joining SIG: " + de.getMessage(),"top");
} // end catch
} // end if (public SIG)
else
{ // we need to prompt them for the join key....
JoinKeyDialog dlg = makeJoinKeyDialog();
dlg.setupDialog(sig);
changeMenuSIG(request,sig);
return dlg;
} // end else (private SIG)
} // end if ("J" command)
if (cmd.equals("U"))
{ // "U" = "Unjoin (requires SIG parameter)
SIGContext sig = getSIGParameter(request,user,true,"top");
if (!(sig.canUnjoin()))
return new ErrorBox("SIG Error","You cannot unjoin this SIG.","top");
// OK, let's test for a confirmation...
if (ConfirmBox.isConfirmed(request,UNJOIN_CONFIRM_ATTR,UNJOIN_CONFIRM_PARAM))
{ // OK, if you say so, let's unjoin!
try
{ // do the unjoin now...
sig.unjoin();
} // end try
catch (AccessError ae)
{ // access error
return new ErrorBox("Access Error","Unable to unjoin SIG: " + ae.getMessage(),"top");
} // end catch
catch (DataException de)
{ // data exception doing something
return new ErrorBox("Database Error","Database error unjoining SIG: " + de.getMessage(),"top");
} // end catch
// after which, redirect back to the top
clearMenu(request);
throw new RedirectResult("top");
} // end if
else
{ // not a proper confirmation - display the confirm box
String message = "Are you sure you want to unjoin the '" + sig.getName() + "' SIG?";
return new ConfirmBox(request,UNJOIN_CONFIRM_ATTR,UNJOIN_CONFIRM_PARAM,"Unjoining SIG",
message,"sigops?cmd=U&sig=" + sig.getSIGID(),"sig/" + sig.getAlias());
} // end else
} // end if ("U" command)
if (cmd.equals("C"))
{ // "C" - Create SIG (no parameters)
if (!(user.canCreateSIG()))
return new ErrorBox("SIG Error","You are not permitted to create SIGs.","top");
// present the "Create New SIG" dialog
CreateSIGDialog dlg = makeCreateSIGDialog();
dlg.setupDialog(engine);
dlg.setFieldValue("language","en-US");
dlg.setFieldValue("country","US");
changeMenuTop(request);
return dlg;
} // end if ("C" command)
if (cmd.equals("I"))
{ // "I" - Send Invitation (requires SIG parameter)
SIGContext sig = getSIGParameter(request,user,true,"top");
if (!(sig.canSendInvitation()))
return new ErrorBox("SIG Error","You are not permitted to send an invitation.",
"sig/" + sig.getAlias());
// present the "Invitation" dialog
return new Invitation(sig);
} // end if ("I" command)
// this is an error!
logger.error("invalid command to SIGOperations.doGet: " + cmd);
return new ErrorBox("Internal Error","Invalid command to SIGOperations.doGet","top");
} // end doVeniceGet
protected VeniceContent doVenicePost(HttpServletRequest request, VeniceEngine engine,
UserContext user, RenderData rdat)
throws ServletException, IOException, VeniceServletResult
{
// get the command we want to use
String cmd = getStandardCommandParam(request);
setMyLocation(request,"sigops?cmd=" + cmd);
if (cmd.equals("J"))
{ // "J" = Join SIG (requires SIG parameter)
SIGContext sig = getSIGParameter(request,user,true,"top");
JoinKeyDialog dlg = makeJoinKeyDialog();
if (dlg.isButtonClicked(request,"cancel")) // cancel - go back to SIG opening page
throw new RedirectResult("sig/" + sig.getAlias());
if (!(sig.canJoin())) // not permitted to join!
return new ErrorBox("SIG Error","You are not permitted to join this SIG.","top");
if (dlg.isButtonClicked(request,"join"))
{ // OK, go join the SIG
dlg.loadValues(request); // load the dialog
try
{ // attempt to join the SIG!
dlg.doDialog(sig);
// success! display the "welcome" page
clearMenu(request); // force the clear to regen the menus
changeMenuSIG(request,sig);
return new SIGWelcome(sig);
} // end try
catch (ValidationException ve)
{ // here, a validation exception causes us to recycle and retry
dlg.resetOnError(ve.getMessage() + " Please try again.");
changeMenuSIG(request,sig);
} // end catch
catch (AccessError ae)
{ // this is probably a bogus key - let them retry
dlg.resetOnError(ae.getMessage() + " Please try again.");
changeMenuSIG(request,sig);
} // end catch
catch (DataException de)
{ // database error joining something
return new ErrorBox("Database Error","Database error joining SIG: " + de.getMessage(),"top");
} // end catch
return dlg; // recycle and try aagin
} // end if ("join" clicked)
// error - don't know what button was clicked
logger.error("no known button click on SIGOperations.doPost, cmd=J");
return new ErrorBox("Internal Error","Unknown command button pressed","top");
} // end if ("J" command)
if (cmd.equals("C"))
{ // "C" = Create New SIG
if (!(user.canCreateSIG()))
return new ErrorBox("SIG Error","You are not permitted to create SIGs.","top");
// load the "Create SIG" dialog
CreateSIGDialog dlg = makeCreateSIGDialog();
dlg.setupDialog(engine);
if (dlg.isButtonClicked(request,"cancel")) // cancel - go back to top
throw new RedirectResult("top");
if (dlg.isButtonClicked(request,"create"))
{ // OK, they actually want to create the new SIG...
dlg.loadValues(request); // load the form data
try
{ // attempt to create the SIG!
SIGContext sig = dlg.doDialog(user);
// created successfully - display a "new SIG welcome" page
changeMenuSIG(request,sig); // display menus for the first time!
return new NewSIGWelcome(sig);
} // end try
catch (ValidationException ve)
{ // here, a validation exception causes us to recycle and retry
dlg.resetOnError(ve.getMessage() + " Please try again.");
changeMenuTop(request);
} // end catch
catch (AccessError ae)
{ // this is probably a bogus key - let them retry
dlg.resetOnError(ae.getMessage() + " Please try again.");
changeMenuTop(request);
} // end catch
catch (DataException de)
{ // database error doing something
return new ErrorBox("Database Error","Database error creating SIG: " + de.getMessage(),"top");
} // end catch
return dlg; // put the dialog back up
} // end if ("create" pressed)
// error - don't know what button was clicked
logger.error("no known button click on SIGOperations.doPost, cmd=C");
return new ErrorBox("Internal Error","Unknown command button pressed","top");
} // end if ("C" command)
if (cmd.equals("I"))
{ // "I" = Send invitation (requires SIG parameter)
SIGContext sig = getSIGParameter(request,user,true,"top");
String on_error = "sig/" + sig.getAlias();
if (isImageButtonClicked(request,"cancel")) // cancel - go back to SIG opening page
throw new RedirectResult(on_error);
if (isImageButtonClicked(request,"send"))
{ // the "send" button was pressed
try
{ // send out the invitation
sig.sendInvitation(request.getParameter("addr"),request.getParameter("pb"));
} // end try
catch (AccessError ae)
{ // access error - display error box
return new ErrorBox("Access Error",ae.getMessage(),on_error);
} // end catch
catch (DataException de)
{ // database error doing something
return new ErrorBox("Database Error","Database error creating SIG: " + de.getMessage(),on_error);
} // end catch
catch (EmailException ee)
{ // error sending the email message
return new ErrorBox("E-Mail Error","Error sending e-mail: " + ee.getMessage(),on_error);
} // end catch
// all sent - go back to SIG profile display
throw new RedirectResult(on_error);
} // end if ("send" pressed)
// error - don't know what button was clicked
logger.error("no known button click on SIGOperations.doPost, cmd=I");
return new ErrorBox("Internal Error","Unknown command button pressed",on_error);
} // end if ("I" command)
// this is an error!
logger.error("invalid command to SIGOperations.doPost: " + cmd);
return new ErrorBox("Internal Error","Invalid command to SIGOperations.doPost","top");
} // end doVenicePost
} // end class SIGOperations