/*
 * 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.format;

import java.util.*;
import com.silverwrist.util.StringUtil;
import com.silverwrist.venice.security.Role;
import com.silverwrist.venice.core.*;
import com.silverwrist.venice.except.*;

public class EditConferenceDialog extends ContentDialog
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  private static final String YES = "Y";

  /*--------------------------------------------------------------------------------
   * Constructors
   *--------------------------------------------------------------------------------
   */

  public EditConferenceDialog(SecurityInfo sinf)
  {
    super("Edit Conference:",null,"editconfform","confops");
    setHiddenField("cmd","E");
    setHiddenField("sig","");
    setHiddenField("conf","");

    addFormField(new CDFormCategoryHeader("Basic Information"));
    addFormField(new CDTextFormField("name","Conference Name",null,true,32,128));
    addFormField(new CDTextFormField("descr","Description",null,false,32,255));
    addFormField(new CDCheckBoxFormField("hide","Hide conference in the community's conference list",
					 null,YES));
    addFormField(new CDFormCategoryHeader("Security Information"));
    addFormField(new CDRoleListFormField("read_lvl","Security level required to read conference",null,true,
					 sinf.getRoleList("Conference.Read")));
    addFormField(new CDRoleListFormField("post_lvl","Security level required to post to conference",null,true,
					 sinf.getRoleList("Conference.Post")));
    addFormField(new CDRoleListFormField("create_lvl",
					 "Security level required to create new topics in conference",null,
					 true,sinf.getRoleList("Conference.Create")));
    addFormField(new CDRoleListFormField("hide_lvl",
					 "Security level required to archive or freeze topics",
					 "(or to hide posts of which you are not the owner)",true,
					 sinf.getRoleList("Conference.Hide")));
    addFormField(new CDRoleListFormField("nuke_lvl",
					 "Security level required to delete topics or nuke posts",
					 "(or to scribble posts of which you are not the owner)",true,
					 sinf.getRoleList("Conference.Nuke")));
    addFormField(new CDRoleListFormField("change_lvl",
					 "Security level required to change conference attributes",null,true,
					 sinf.getRoleList("Conference.Change")));
    addFormField(new CDRoleListFormField("delete_lvl",
					 "Security level required to delete conference",null,true,
					 sinf.getRoleList("Conference.Delete")));
    addFormField(new CDFormCategoryHeader("Conference Properties"));
    addFormField(new CDCheckBoxFormField("pic_in_post","Display users' pictures next to their posts",
					 "(user can override)",YES));
    addCommandButton(new CDImageButton("update","bn_update.gif","Update",80,24));
    addCommandButton(new CDImageButton("cancel","bn_cancel.gif","Cancel",80,24));

  } // end constructor

  protected EditConferenceDialog(EditConferenceDialog other)
  {
    super(other);

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Internal operations
   *--------------------------------------------------------------------------------
   */

  private final void doDisable(CommunityContext comm, ConferenceContext conf)
  {
    setFieldEnabled("hide",conf.canSetHideList());

  } // end doDisable

  /*--------------------------------------------------------------------------------
   * External operations
   *--------------------------------------------------------------------------------
   */

  public void setupDialog(CommunityContext comm, ConferenceContext conf) throws DataException, AccessError
  {
    doDisable(comm,conf);
    setHiddenField("sig",String.valueOf(comm.getCommunityID()));
    setHiddenField("conf",String.valueOf(conf.getConfID()));
    setTitle("Edit Conference: " + conf.getName());
    setFieldValue("name",conf.getName());
    setFieldValue("descr",conf.getDescription());
    if (conf.canSetHideList())
    { // this is only valid at community level
      if (conf.getHideList())
	setFieldValue("hide",YES);
      else
	setFieldValue("hide","");

    } // end if

    setFieldValue("read_lvl",String.valueOf(conf.getReadLevel()));
    setFieldValue("post_lvl",String.valueOf(conf.getPostLevel()));
    setFieldValue("create_lvl",String.valueOf(conf.getCreateLevel()));
    setFieldValue("hide_lvl",String.valueOf(conf.getHideLevel()));
    setFieldValue("nuke_lvl",String.valueOf(conf.getNukeLevel()));
    setFieldValue("change_lvl",String.valueOf(conf.getChangeLevel()));
    setFieldValue("delete_lvl",String.valueOf(conf.getDeleteLevel()));
    ConferenceProperties props = conf.getProperties();
    if (props.getDisplayPostPictures())
      setFieldValue("pic_in_post",YES);
    else
      setFieldValue("pic_in_post","");

  } // end setupDialog

  public void doDialog(ConferenceContext conf) throws ValidationException, DataException, AccessError
  {
    validate();  // validate the dialog entries

    ConferenceProperties props = conf.getProperties();
    int read_lvl, post_lvl, create_lvl, hide_lvl, nuke_lvl, change_lvl, delete_lvl;
    try
    { // get all the security levels out of their form fields
      read_lvl = Integer.parseInt(getFieldValue("read_lvl"));
      post_lvl = Integer.parseInt(getFieldValue("post_lvl"));
      create_lvl = Integer.parseInt(getFieldValue("create_lvl"));
      hide_lvl = Integer.parseInt(getFieldValue("hide_lvl"));
      nuke_lvl = Integer.parseInt(getFieldValue("nuke_lvl"));
      change_lvl = Integer.parseInt(getFieldValue("change_lvl"));
      delete_lvl = Integer.parseInt(getFieldValue("delete_lvl"));

    } // end try
    catch (NumberFormatException nfe)
    { // how rude!
      throw new InternalStateError("somehow we got a non-numeric result from a numeric dropdown list!");

    } // end catch

    // sweep through the conference and set the appropriate changes
    conf.setName(getFieldValue("name"));
    conf.setDescription(getFieldValue("descr"));
    if (conf.canSetHideList())
      conf.setHideList(YES.equals(getFieldValue("hide")));
    conf.setSecurityLevels(read_lvl,post_lvl,create_lvl,hide_lvl,nuke_lvl,change_lvl,delete_lvl);

    // reset the properties
    props.setDisplayPostPictures(YES.equals(getFieldValue("pic_in_post")));
    conf.setProperties(props);

  } // end doDialog

  public void resetOnError(CommunityContext comm, ConferenceContext conf, String message)
  {
    doDisable(comm,conf);
    setHiddenField("sig",String.valueOf(comm.getCommunityID()));
    setHiddenField("conf",String.valueOf(conf.getConfID()));
    setTitle("Edit Conference: " + conf.getName());
    setErrorMessage(message);

  } // end resetOnError

  public Object clone()
  {
    return new EditConferenceDialog(this);

  } // end clone

} // end class EditConferenceDialog
