// 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@ricochet.com>,
// for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
// Copyright (C) 2001-2004 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
// 
// Contributor(s): 

importPackage(Packages.com.silverwrist.venice.core);
importPackage(Packages.com.silverwrist.venice.except);
importPackage(Packages.com.silverwrist.venice.ui);
importPackage(Packages.com.silverwrist.venice.ui.dlg);
importPackage(Packages.com.silverwrist.venice.ui.helpers);
importPackage(Packages.com.silverwrist.venice.ui.view);

// get the request object
rinput = bsf.lookupBean("request");

if (!(rinput.user.canCreateCommunity()))
{ // can't create communities - bye!
  if (rinput.user.isLoggedIn())
    vlib.output(new ErrorBox("Community Error","You are not permitted to create communities.","top.js.vs"));
  else
    vlib.output(new LogInOrCreate());
  vlib.done();

} // end if

dlg = rinput.getDialog("create.community");
if ("GET"==rinput.verb)
{ // just output the dialog
  dlg.setValue("language","en-US");
  dlg.setValue("country","US");
  vlib.output(dlg);
  vlib.done();

} // end if

// everything following is for a POST
op = dlg.whichButton(rinput) + "";
if (op=="cancel")
{ // user cancelled - bail out
  vlib.output(new Redirect("top.js.vs",LinkTypes.SERVLET));
  vlib.done();

} // end if

rc = null;
if (op=="create")
{ // actually creating a community
  dlg.load(rinput);  // load the dialog values

  try
  { // start by validating the dialog
    dlg.validate();

    // validate some extra stuff
    my_alias = dlg.getValue("alias");
    if (rinput.engine.aliasExists(my_alias,-1))
      throw new ValidationException("That alias is already used by another community on the system.");
    my_comtype = dlg.getValue("comtype") + "";
    my_joinkey = null;
    if (my_comtype=="1")
    { // get the join key for this private community
      my_joinkey = dlg.getValue("joinkey");
      if (vlib.emptyString(my_joinkey))
        throw new ValidationException("Private communities must specify a join key value.");

    } // end if

    // Create the new community context.
    new_comm = rinput.user.createCommunity(dlg.getValue("name"),my_alias,dlg.getValue("language"),
                                           dlg.getValue("synopsis"),dlg.getValue("rules"),my_joinkey,
                                           dlg.getValue("hidemode"));

    // Get the new community's contact record and fill in the pieces of info we know.
    ci = new_comm.getContactInfo();
    ci.locality = dlg.getValue("loc");
    ci.region = dlg.getValue("reg");
    ci.postalCode = dlg.getValue("pcode");
    ci.country = dlg.getValue("country");
    new_comm.putContactInfo(ci);
    
    // create the return view
    rinput.setRequestAttribute("new.community",new_comm);
    rc = new JSPView("New Community Created","comm/new.jsp");
    rc.menuSelector = Content.MENU_SELECTOR_COMMUNITY;

  } // end try
  catch (e)
  { // exception creating the community
    etype = vlib.exceptionType(e) + "";
    if (etype.match("ValidationException") || etype.match("AccessError"))
    { // reset the dialog and let them try again
      dlg.setErrorMessage(e.message + " Please try again.");
      rc = dlg;

    } // end if
    else if (etype.match("DataException"))
      rc = new ErrorBox("Database Error","Database error creating community: " + e.message,"top.js.vs");
    else
      rc = e;

  } // end catch

} // end if
else
{ // don't know what button was pressed
  logger.error("no known button click on POST to comm/create.js.vs");
  rc = new ErrorBox("Internal Error","Unknown command button pressed","top.js.vs");

} // end else

vlib.output(rc);  // all done!
