// 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-02 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.helpers);
importPackage(Packages.com.silverwrist.venice.ui.view);

// get the request object and the community
rinput = bsf.lookupBean("request");
comm = rinput.getCommunity(true,"top.js.vs");

if (!(comm.canModifyProfile()))
{ // no access - sorry, dude
  logger.error("tried to call up community profile screen without access...naughty naughty!");
  vlib.output(new ErrorBox("Unauthorized","You do not have access to modify this community's profile.",
			   "comm/admin_menu.js.vs?cc=" + comm.communityID));
  vlib.done();

} // end if

rc = null;
if (rinput.hasParameter("set"))
{ // OK...they're actually setting the category here - change it and bail out
  catid = rinput.getParameterInt("set",-1);
  if (rinput.engine.isValidCategoryID(catid))
  { // try setting the category ID
    try
    { // set the category ID and bounce back to the menu
      comm.categoryID = catid;
      rc = new Redirect("comm/admin_menu.js.vs?cc=" + comm.communityID,LinkTypes.SERVLET);

    } // end try
    catch (e)
    { // exception setting category
      etype = vlib.exceptionType(e) + "";
      if (etype.match("DataException"))
        rc = new ErrorBox("Database Error","Database error updating community: " + e.message,
			  "comm/admin_menu.js.vs?cc=" + comm.communityID);
      else if (etype.match("AccessError"))
        rc = new ErrorBox("Access Error",e.message,"comm/admin_menu.js.vs?cc=" + comm.communityID);
      else
        rc = e;

    } // end catch

  } // end if
  else  // invalid category ID
    rc = new ErrorBox("Invalid Input","Invalid category ID passed to category browser.",
                      "comm/admin_menu.js.vs?cc=" + comm.communityID);

  vlib.output(rc);
  vlib.done();

} // end if

// we must be browsing - try and figure out what category we're supposed to be browsing
curr_catid = 0;
if (rinput.hasParameter("go"))
  curr_catid = rinput.getParameterInt("go",-1);
else
  curr_catid = comm.categoryID;
if (!(rinput.engine.isValidCategoryID(curr_catid)))
{ // this is not a valid category ID
  vlib.output(new ErrorBox("Invalid Input","Invalid category ID passed to category browser.",
                           "comm/admin_menu.js.vs?cc=" + comm.communityID));
  vlib.done();

} // end if

try
{ // get the categories
  prev_cat = comm.category;
  rinput.setRequestAttribute("category.previous",prev_cat);
  curr_cat = null;
  if (prev_cat.categoryID!=curr_catid)
    curr_cat = rinput.user.getCategoryDescriptor(curr_catid);
  else
    curr_cat = prev_cat;
  rinput.setRequestAttribute("category.current",curr_cat);
  rinput.setRequestAttribute("category.subcats",curr_cat.getSubCategories());

  // create a view and return it
  rc = new JSPView("Set Community Category","comm/category.jsp");
  rc.menuSelector = Content.MENU_SELECTOR_COMMUNITY;

} // end try
catch (e)
{ // caught an exception here...
  etype = vlib.exceptionType(e) + "";
  if (etype.match("DataException"))
    rc = new ErrorBox("Database Error","Database error browsing categories: " + e.message,
                      "comm/admin_menu.js.vs?cc=" + comm.communityID);
  else
    rc = e;

} // end catch

vlib.output(rc);

