/*
 * 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) 2003 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.sidebox;

import java.util.*;
import com.silverwrist.dynamo.Namespaces;
import com.silverwrist.dynamo.except.*;
import com.silverwrist.dynamo.iface.*;
import com.silverwrist.dynamo.security.SecurityReferenceMonitor;
import com.silverwrist.dynamo.util.*;
import com.silverwrist.dynamo.velocity.VelocityParamSupplier;
import com.silverwrist.dynamo.velocity.VelocityRenderable;
import com.silverwrist.venice.VeniceNamespaces;
import com.silverwrist.venice.community.CommunityService;
import com.silverwrist.venice.except.*;
import com.silverwrist.venice.iface.*;
import com.silverwrist.venice.session.SessionInfoParams;

class CommunityListSidebox implements SideboxFactory
{
  /*--------------------------------------------------------------------------------
   * Internal class that implements the actual sidebox
   *--------------------------------------------------------------------------------
   */

  private class MyBox implements Sidebox, VelocityRenderable
  {
    /*====================================================================
     * Attributes
     *====================================================================
     */

    private HashMap m_params = new HashMap();
    private String m_title_keyname;

    /*====================================================================
     * Constructor
     *====================================================================
     */

    MyBox(DynamoUser user, List communities, boolean can_create)
    {
      m_params.put("user",user);
      m_params.put("communities",communities);
      if (can_create)
	m_params.put("can_create",Boolean.TRUE);
      if (user.isAnonymous())
	m_title_keyname = "anon.title";
      else
	m_title_keyname = "normal.title";

    } // end constructor

    /*====================================================================
     * Implementations from interface Sidebox
     *====================================================================
     */

    public String getSideboxTitle()
    {
      return m_props.getObject(VeniceNamespaces.CONTENT_LAF_NAMESPACE,m_title_keyname).toString();

    } // end getSideboxTitle

    /*====================================================================
     * Implementations from interface VelocityParamSupplier
     *====================================================================
     */

    /**
     * Returns the value of a parameter set on the object.
     *
     * @param key The name of the parameter to look up.
     * @return The parameter's value, or <CODE>null</CODE> if the parameter was not set.
     */
    public Object getParameter(String key)
    {
      return m_params.get(key);

    } // end getParameter

    /**
     * Returns a <CODE>java.util.Collection</CODE> of all parameter names currently defined on this object.
     *
     * @return A collection of all parameter names currently defined.
     */
    public Collection getParameterNames()
    {
      return Collections.unmodifiableSet(m_params.keySet());

    } // end getParameterNames

    /*====================================================================
     * Implementations from interface VelocityRenderable
     *====================================================================
     */

    /**
     * Returns the MIME type of the output to be rendered.  Usually, this will be "text/html".
     *
     * @return The MIME type of the output.
     */
    public String getMimeType()
    {
      return "text/html";

    } // end getMimeType

    /**
     * Returns the resource name of the Velocity template to be used in rendering this object.  This pathname
     * is interpreted relative to the "resource root path" specified in the Velocity renderer's configuration.
     * The template engine loads it via the standard
     * {@link com.silverwrist.dynamo.iface.ResourceProvider ResourceProvider}, and then applies the parameters
     * contained in this object to it.
     *
     * @return The resource pathname of the Velocity template to use.
     */
    public String getTemplateName()
    {
      return m_props.getObject(VeniceNamespaces.CONTENT_LAF_NAMESPACE,"velocity.template").toString();

    } // end getTemplateName

  } // end class MyBox

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

  private ObjectProvider m_props;      // properties provider

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

  CommunityListSidebox(ObjectProvider properties)
  {
    m_props = properties;

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Implementations from interface SideboxFactory
   *--------------------------------------------------------------------------------
   */

  public Sidebox createSidebox(Request req) throws DynamoException
  {
    // Get the user associated with this request.
    SessionInfoProvider prov = (SessionInfoProvider)(req.queryService(SessionInfoProvider.class));
    SessionInfo session = prov.getSessionInfo();
    DynamoUser user = (DynamoUser)(session.getObject(SessionInfoParams.NAMESPACE,SessionInfoParams.ATTR_USER));

    // Get the list of the user's communities.
    String tmp_name = m_props.getObject(VeniceNamespaces.SESSION_CONTROL_NAMESPACE,"community.provider").toString();
    ObjectProvider op = (ObjectProvider)(req.queryService(ObjectProvider.class));
    CommunityService commsvc = (CommunityService)(op.getObject(Namespaces.DYNAMO_OBJECT_NAMESPACE,tmp_name));
    List communities = commsvc.getMemberCommunities(user,user);

    // Determine whether the user can actually create a community.
    tmp_name = m_props.getObject(VeniceNamespaces.SESSION_CONTROL_NAMESPACE,"security.provider").toString();
    SecurityReferenceMonitor srm = (SecurityReferenceMonitor)(op.getObject(Namespaces.DYNAMO_OBJECT_NAMESPACE,
									   tmp_name));
    boolean can_create = false;
    if (user.equals(srm.getAdminUser()))
      can_create = true;
    else
      can_create = srm.getGlobalAcl().testPermission(user,VeniceNamespaces.COMMUNITY_PERMS_NAMESPACE,"create.new");

    // We now have all the info we need to create the sidebox.
    return new MyBox(user,communities,can_create);
    
  } // end createSidebox

} // end class CommunityListSidebox
