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

import org.w3c.dom.*;
import com.silverwrist.util.*;
import com.silverwrist.venice.core.ServiceToken;
import com.silverwrist.venice.except.*;
import com.silverwrist.venice.security.*;
import com.silverwrist.venice.svc.*;

class StaticCommService implements CommService
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private ServiceToken token;
  private boolean is_default;
  private boolean is_locked;
  private SecurityMonitor secmon;
  private String permission;
  private Role role;

  /*--------------------------------------------------------------------------------
   * Constructor
   *--------------------------------------------------------------------------------
   */

  public StaticCommService()
  { // do nothing
  } // end constructor

  /*--------------------------------------------------------------------------------
   * Implementations from interface Service
   *--------------------------------------------------------------------------------
   */

  public void shutdown()
  { // do nothing
  } // end shutdown

  public ServiceToken getToken()
  {
    return token;

  } // end getToken

  public boolean isDefault()
  {
    return is_default;

  } // end isDefault

  public boolean isLocked()
  {
    return is_locked;

  } // end isLocked

  /*--------------------------------------------------------------------------------
   * Implementations from interface CommunityService
   *--------------------------------------------------------------------------------
   */

  public void initialize(ServiceToken token, Element root, SecurityMonitor sm, CommServiceSite site)
      throws ConfigException
  {
    this.token = token;

    // Get the setup from the subelement.
    DOMElementHelper h = new DOMElementHelper(root);
    Element sub = h.getSubElement("setup");
    if (sub!=null)
    { // read the values of the "default" and "locked" flags
      DOMElementHelper h2 = new DOMElementHelper(sub);
      is_default = h2.hasAttribute("default");
      is_locked = h2.hasAttribute("locked");

    } // end if
    else
    { // default those two flags
      is_default = false;
      is_locked = false;

    } // end else

    // Get the access control element from the subelement.
    sub = h.getSubElement("access");
    if (sub!=null)
    { // get the attributes...
      DOMElementHelper h3 = new DOMElementHelper(sub);
      if (h3.hasAttribute("permission"))
      { // get the permission and verify that it exists
	permission = sub.getAttribute("permission");
	if (!(sm.permissionDefined(permission,false)))
	  throw new ConfigException("permission \"" + permission + "\" is not defined",sub);

      } // end if
      else  // just null out the permission
	permission = null;

      if (h3.hasAttribute("role"))
      { // get the role and verify that it exists
	role = sm.getRole(sub.getAttribute("role"));
	if (role==null)
	  throw new ConfigException("role \"" + sub.getAttribute("role") + "\" is not defined",sub);

      } // end if
      else // default the role
	role = sm.getRole("NotInList");

    } // end if
    else
    { // default the values
      permission = null;
      role = sm.getRole("NotInList");

    } // end else

    // Save the security monitor reference.
    secmon = sm;

  } // end initialize

  public String getPermission()
  {
    return permission;

  } // end getPermission

  public Role getRole()
  {
    return role;

  } // end getRole

} // end class StaticCommService
