/*
 * 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.core.*;
import com.silverwrist.venice.except.*;

public class CreateCommunityDialog extends ContentDialog
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private VeniceEngine engine;

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

  public CreateCommunityDialog()
  {
    super("Create New Community",null,"createcommform","sigops");
    setHiddenField("cmd","C");

    Vector vec_pubpriv = new Vector(2);
    vec_pubpriv.add("0|Public");
    vec_pubpriv.add("1|Private");
    vec_pubpriv.trimToSize();

    Vector vec_hidemode = new Vector(3);
    vec_hidemode.add(String.valueOf(CommunityContext.HIDE_NONE) + "|Show in both directory and search");
    vec_hidemode.add(String.valueOf(CommunityContext.HIDE_DIRECTORY)
		     + "|Hide in directory, but not in search");
    vec_hidemode.add(String.valueOf(CommunityContext.HIDE_BOTH) + "|Hide in both directory and search");
    vec_hidemode.trimToSize();

    addFormField(new CDFormCategoryHeader("Basic Information"));
    addFormField(new CDTextFormField("name","Community Name",null,true,32,128));
    addFormField(new CDVeniceIDFormField("alias","Community Alias",null,true,32,32));
    addFormField(new CDTextFormField("synopsis","Synopsis",null,false,32,255));
    addFormField(new CDTextFormField("rules","Rules",null,false,32,255));
    addFormField(new CDLanguageListFormField("language","Primary language",null,true));
    addFormField(new CDFormCategoryHeader("Location"));
    addFormField(new CDTextFormField("loc","City",null,false,32,64));
    addFormField(new CDTextFormField("reg","State/Province",null,false,32,64));
    addFormField(new CDTextFormField("pcode","Zip/Postal Code",null,true,32,64));
    addFormField(new CDCountryListFormField("country","Country",null,true));
    addFormField(new CDFormCategoryHeader("Security"));
    addFormField(new CDSimplePickListFormField("comtype","Community type:",null,true,vec_pubpriv,'|'));
    addFormField(new CDTextFormField("joinkey","Join key","(for private communities)",false,32,64));
    addFormField(new CDSimplePickListFormField("hidemode","Community visibility:",null,true,vec_hidemode,'|'));
    addCommandButton(new CDImageButton("create","bn_create.gif","Create",80,24));
    addCommandButton(new CDImageButton("cancel","bn_cancel.gif","Cancel",80,24));

  } // end constructor

  protected CreateCommunityDialog(CreateCommunityDialog other)
  {
    super(other);

  } // end CreateCommunityDialog

  /*--------------------------------------------------------------------------------
   * Overrides from class ContentDialog
   *--------------------------------------------------------------------------------
   */

  protected void validateWholeForm() throws ValidationException
  {
    if (engine.aliasExists(getFieldValue("alias"),-1))
      throw new ValidationException("That alias is already used by another community on the system.");

    if (getFieldValue("comtype").equals("1"))
    { // make sure if they flagged it as Private that they specified a join key
      if (StringUtil.isStringEmpty(getFieldValue("joinkey")))
	throw new ValidationException("Private communities must specify a join key value.");

    } // end if

  } // end validateWholeForm

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

  public void setupDialog(VeniceEngine engine)
  {
    this.engine = engine;

  } // end setupDialog

  public CommunityContext doDialog(UserContext user) throws ValidationException, DataException, AccessError
  {
    validate();  // validate the dialog entries

    int hidemode;

    try
    { // attempt to get the hide mode first...
      hidemode = Integer.parseInt(getFieldValue("hidemode"));

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

    } // end catch

    // Determine what the "join key" should be.
    String jkey;
    if (getFieldValue("comtype").equals("1"))
      jkey = getFieldValue("joinkey");
    else
      jkey = null;

    // Create the new community context.
    CommunityContext rc = user.createCommunity(getFieldValue("name"),getFieldValue("alias"),
					       getFieldValue("language"),getFieldValue("synopsis"),
					       getFieldValue("rules"),jkey,hidemode);

    // Get the new community's contact record and fill in the pieces of info we know.
    ContactInfo ci = rc.getContactInfo();
    ci.setLocality(getFieldValue("loc"));
    ci.setRegion(getFieldValue("reg"));
    ci.setPostalCode(getFieldValue("pcode"));
    ci.setCountry(getFieldValue("country"));
    rc.putContactInfo(ci);

    return rc;  // all done!

  } // end doDialog

  public void resetOnError(String message)
  {
    setErrorMessage(message);

  } // end resetOnError

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

  } // end clone

} // end class CreateCommunityDialog
