/*
 * 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): 
 */
package com.silverwrist.venice.core.internals;

import com.silverwrist.venice.except.AccessError;
import com.silverwrist.venice.security.AuditRecord;
import com.silverwrist.venice.security.Role;
import com.silverwrist.venice.security.SecurityMonitor;

public class EnvCommunity extends EnvUser
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  public static final String PERM_SHOWADMIN = "Community.ShowAdmin";
  public static final String PERM_NOKEYREQUIRED = "Community.NoKeyRequired";
  public static final String PERM_SHOWHIDDENMEMBERS = "Community.ShowHiddenMembers";
  public static final String PERM_SHOWHIDDENOBJECTS = "Community.ShowHiddenObjects";
  public static final String PERM_READ = "Community.Read";
  public static final String PERM_WRITE = "Community.Write";
  public static final String PERM_CREATE = "Community.Create";
  public static final String PERM_DELETE = "Community.Delete";
  public static final String PERM_JOIN = "Community.Join";

  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private CommunityBackend comm;   // the community back end
  private SecurityMonitor sm;      // the security monitor

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

  public EnvCommunity(EnvUser parent, CommunityBackend comm)
  {
    super(parent);
    this.comm = comm;
    this.sm = (SecurityMonitor)(getGlobalSite().queryService(SecurityMonitor.class,"Community"));

  } // end constructor

  protected EnvCommunity(EnvCommunity other)
  {
    super(other);
    this.comm = other.comm;
    this.sm = other.sm;

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Overrides from class EnvEngine
   *--------------------------------------------------------------------------------
   */

  protected SecurityMonitor getStaticMonitor()
  {
    return sm;

  } // end getStaticMonitor

  /*--------------------------------------------------------------------------------
   * Overrides from class EnvUser
   *--------------------------------------------------------------------------------
   */

  public AuditRecord newAudit(int type, String data1, String data2, String data3, String data4)
  {
    return new AuditRecord(type,super.getUserID(),super.getRemoteAddress(),getCommunityID(),data1,data2,
			   data3,data4);

  } // end newAudit

  public AuditRecord newAudit(int type, String data1, String data2, String data3)
  {
    return new AuditRecord(type,super.getUserID(),super.getRemoteAddress(),getCommunityID(),data1,data2,data3);

  } // end newAudit

  public AuditRecord newAudit(int type, String data1, String data2)
  {
    return new AuditRecord(type,super.getUserID(),super.getRemoteAddress(),getCommunityID(),data1,data2);

  } // end newAudit

  public AuditRecord newAudit(int type, String data1)
  {
    return new AuditRecord(type,super.getUserID(),super.getRemoteAddress(),getCommunityID(),data1);

  } // end newAudit

  public AuditRecord newAudit(int type)
  {
    return new AuditRecord(type,super.getUserID(),super.getRemoteAddress(),getCommunityID());

  } // end newAudit

  public boolean testPermission(String symbol, String errormsg) throws AccessError
  {
    if (   symbol.equals(PERM_READ) || symbol.equals(PERM_WRITE) || symbol.equals(PERM_CREATE)
	|| symbol.equals(PERM_DELETE) || symbol.equals(PERM_JOIN))
    { // fall back to the backend object to test this
      if (comm.env_testPermission(symbol))
	return true;
      if (errormsg==null)
	errormsg = "Permission denied.";
      throw new AccessError(errormsg);

    } // end if

    if (sm.testPermission(symbol,comm.realCommunityLevel(),errormsg))
      return true;
    return super.testPermission(symbol,errormsg);

  } // end testPermission

  public boolean satisfy(Role role)
  {
    return role.isSatisfiedBy(comm.realCommunityLevel());

  } // end satisfy

  public boolean testPermission(String symbol)
  {
    if (   symbol.equals(PERM_READ) || symbol.equals(PERM_WRITE) || symbol.equals(PERM_CREATE)
	|| symbol.equals(PERM_DELETE) || symbol.equals(PERM_JOIN))
      return comm.env_testPermission(symbol);
    if (sm.testPermission(symbol,comm.realCommunityLevel()))
      return true;
    return super.testPermission(symbol);

  } // end testPermission

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

  public final CommunityBackend getCommunity()
  {
    return comm;

  } // end getCommunity

  public final int getCommunityID()
  {
    return comm.realCommunityID();

  } // end getCommunityID()

  public final boolean isLevelAdmin(int level)
  {
    return sm.testPermission(PERM_SHOWADMIN,level);

  } // end isLevelAdmin

  public final Role getConferenceDefaultRole(String symbol)
  {
    SecurityMonitor smon = (SecurityMonitor)(getGlobalSite().queryService(SecurityMonitor.class,"Conference"));
    return smon.getDefaultRole(symbol);

  } // end getConferenceDefaultRole

  public final String getCommunityName()
  {
    return comm.env_getCommunityName();

  } // end getCommunityName

  public final String getCommunityAlias()
  {
    return comm.realCommunityAlias();

  } // end getCommunityAlias

} // end class EnvCommunity
