// 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): 

importPackage(java.util);
importPackage(Packages.com.silverwrist.venice.core);
importPackage(Packages.com.silverwrist.venice.except);
importPackage(Packages.com.silverwrist.venice.ui);
importPackage(Packages.com.silverwrist.venice.ui.conf);
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");

// get the current conference
currc = new CurrentConference(rinput);
conf = currc.getConference(true,"conf/conferences.js.vs?cc=" + comm.communityID);

// get the view option
view_opt = currc.viewOption;
if (rinput.hasParameter("view"))
{ // get the new view option
  view_opt = rinput.getParameterInt("view",view_opt);
  if (   (view_opt!=ConferenceContext.DISPLAY_NEW) && (view_opt!=ConferenceContext.DISPLAY_ACTIVE)
      && (view_opt!=ConferenceContext.DISPLAY_ALL) && (view_opt!=ConferenceContext.DISPLAY_HIDDEN)
      && (view_opt!=ConferenceContext.DISPLAY_ARCHIVED))
  { // the view option is invalid
    vlib.output(new ErrorBox(null,"Invalid view parameter.","conf/conferences.js.vs?cc=" + comm.communityID));
    vlib.done();

  } // end if

  currc.viewOption = view_opt;  // save the changed view option

} // end if

// get the sort option
sort_opt = currc.sortOption;
if (rinput.hasParameter("sort"))
{ // get and test the new sort option
  sort_opt = rinput.getParameterInt("sort",sort_opt);
  tmp_sort = ((sort_opt<0) ? -sort_opt : sort_opt);
  if (   (tmp_sort!=ConferenceContext.SORT_NUMBER) && (tmp_sort!=ConferenceContext.SORT_NAME)
      && (tmp_sort!=ConferenceContext.SORT_UNREAD) && (tmp_sort!=ConferenceContext.SORT_TOTAL)
      && (tmp_sort!=ConferenceContext.SORT_DATE))
  { // the sort option is invalid
    vlib.output(new ErrorBox(null,"Invalid sort parameter.","conf/conferences.js.vs?cc=" + comm.communityID));
    vlib.done();

  } // end if

  currc.sortOption = sort_opt;  // save the changed sort option

} // end if

rc = null;
try
{ // get the topic list
  topic_list = conf.getTopicList(view_opt,sort_opt);

  // initialize the visit order
  ord = currc.newTopicVisitOrder(topic_list);

  // get the aliases list...
  aliases = conf.aliases;

  // set the attributes we want to carry into the page
  rinput.setRequestAttribute("topics.list",topic_list);  
  tmp = conf.getCustomBlock(ConferenceContext.CUST_BLOCK_TOP);
  if (tmp==null)
    tmp = "";
  rinput.setRequestAttribute("conference.custom.top",tmp);
  tmp = conf.getCustomBlock(ConferenceContext.CUST_BLOCK_BOTTOM);
  if (tmp==null)
    tmp = "";
  rinput.setRequestAttribute("conference.custom.bottom",tmp);
  if (ord.isNext())
  { // precalculate the "Read New" link and pass it to the page
    tmp = "conf/posts.js.vs?cc=" + comm.communityID + "&conf=" + conf.confID + "&top=" + ord.getNext()
                                 + "&rnm=1";
    rinput.setRequestAttribute("conference.readnew",tmp);

  } // end if

  // create a JSPView to return
  rc = new JSPView("Topics in " + conf.name,"conf/topics.jsp");
  rc.menuSelector = Content.MENU_SELECTOR_COMMUNITY;
  rc.pageQID = "go/" + comm.alias + "!" + aliases.get(0);

} // end try
catch (e)
{ // error generating the topic list
  etype = vlib.exceptionType(e) + "";
  if (etype.match("DataException"))
    rc = new ErrorBox("Database Error","Database error listing topics: " + e.message,
                      "conf/conferences.js.vs?cc=" + comm.communityID);
  else if (etype.match("AccessError"))
  { // they might not be logged in, that's all
    if (rinput.user.isLoggedIn())
      rc = new ErrorBox("Access Error",e.message,"conf/conferences.js.vs?cc=" + comm.communityID);
    else
      rc = new LogInOrCreate();

  } // end else if
  else
    rc = e;

} // end catch

vlib.output(rc);

