/*
 * 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.io.*;
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 EditGlobalPropertiesDialog extends ContentDialog
{
  /*--------------------------------------------------------------------------------
   * Constructors
   *--------------------------------------------------------------------------------
   */

  public EditGlobalPropertiesDialog(SecurityInfo sinf)
  {
    super("Edit Global Properties",null,"globpropform","sysadmin");
    setHiddenField("cmd","G");

    addFormField(new CDFormCategoryHeader("System Properties"));
    addFormField(new CDIntegerFormField("search_items","Number of search items to display per page",
					null,5,100));
    addFormField(new CDIntegerFormField("fp_posts","Number of published posts to display on front page",
					null,1,50));
    addFormField(new CDIntegerFormField("audit_recs","Number of audit records to display per page",
					null,10,500));
    addFormField(new CDRoleListFormField("create_lvl","Security level required to create a new community",
					 null,true,sinf.getRoleList("Global.CreateCommunity")));

    addFormField(new CDFormCategoryHeader("Community Properties"));
    addFormField(new CDIntegerFormField("comm_mbrs","Number of community members to display per page",
					null,10,100));

    addFormField(new CDFormCategoryHeader("Conferencing Properties"));
    addFormField(new CDIntegerFormField("posts_page","Maximum number of posts to display per page",
					null,5,100));
    addFormField(new CDIntegerFormField("old_posts","Number of \"old\" posts to display at top of page",
					null,1,5));
    addFormField(new CDIntegerFormField("conf_mbrs","Number of conference members to display per page",
					null,10,100));
    addFormField(new CDCheckBoxFormField("pic_in_post","Display user pictures next to posts in conferences",
					 "(by default; user can override)"));

    addCommandButton(new CDImageButton("update","bn_update.gif","Update",80,24));
    addCommandButton(new CDImageButton("cancel","bn_cancel.gif","Cancel",80,24));

  } // end constructor

  protected EditGlobalPropertiesDialog(EditGlobalPropertiesDialog other)
  {
    super(other);

  } // end constructor

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

  public void setupDialog(AdminOperations adm)
  {
    GlobalProperties props = adm.getProperties();
    setFieldObjValue("search_items",new Integer(props.getSearchItemsPerPage()));
    setFieldObjValue("fp_posts",new Integer(props.getPostsOnFrontPage()));
    setFieldObjValue("audit_recs",new Integer(props.getAuditRecordsPerPage()));
    setFieldObjValue("create_lvl",new Integer(props.getCommunityCreateLevel()));
    setFieldObjValue("comm_mbrs",new Integer(props.getCommunityMembersPerPage()));
    setFieldObjValue("posts_page",new Integer(props.getPostsPerPage()));
    setFieldObjValue("old_posts",new Integer(props.getOldPostsAtTop()));
    setFieldObjValue("conf_mbrs",new Integer(props.getConferenceMembersPerPage()));
    setFieldObjValue("pic_in_post",new Boolean(props.getDisplayPostPictures()));

  } // end setupDialog

  public void doDialog(AdminOperations adm) throws ValidationException, DataException
  {
    validate();  // validate the dialog

    // Reset the global properties.
    GlobalProperties props = adm.getProperties();
    Integer iv = (Integer)(getFieldObjValue("search_items"));
    props.setSearchItemsPerPage(iv.intValue());
    iv = (Integer)(getFieldObjValue("fp_posts"));
    props.setPostsOnFrontPage(iv.intValue());
    iv = (Integer)(getFieldObjValue("audit_recs"));
    props.setAuditRecordsPerPage(iv.intValue());
    iv = (Integer)(getFieldObjValue("create_lvl"));
    props.setCommunityCreateLevel(iv.intValue());
    iv = (Integer)(getFieldObjValue("comm_mbrs"));
    props.setCommunityMembersPerPage(iv.intValue());
    iv = (Integer)(getFieldObjValue("posts_page"));
    props.setPostsPerPage(iv.intValue());
    iv = (Integer)(getFieldObjValue("old_posts"));
    props.setOldPostsAtTop(iv.intValue());
    iv = (Integer)(getFieldObjValue("conf_mbrs"));
    props.setConferenceMembersPerPage(iv.intValue());
    Boolean bv = (Boolean)(getFieldObjValue("pic_in_post"));
    props.setDisplayPostPictures(bv.booleanValue());
    adm.setProperties(props);

  } // end doDialog

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

  } // end clone

} // end class EditGlobalPropertiesDialog
