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

import org.apache.log4j.Logger;
import com.silverwrist.dynamo.except.*;
import com.silverwrist.dynamo.iface.*;
import com.silverwrist.venice.VeniceNamespaces;
import com.silverwrist.venice.iface.*;

public class CommunityMenuInstaller
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  private static Logger logger = Logger.getLogger(CommunityMenuInstaller.class);

  private static final int STD_GAP = 100;

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

  private MenuProvider m_provider;

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

  public CommunityMenuInstaller(ServiceProvider services)
  {
    m_provider = (MenuProvider)(services.queryService(MenuProvider.class));

  } // end constructor

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

  public void installCommunityServiceEntry(DynamoUser caller, String text, String linktype, String link,
					   String shortvar) throws DatabaseException, DynamoSecurityException
  {
    MenuEditObject menu = m_provider.getEditMenu(caller,VeniceNamespaces.COMMUNITY_GLOBALS_NAMESPACE,"community.menu");
    MenuEditItemObject end_marker = menu.getItemByTypeText("MARKER","end-services");
    if (logger.isDebugEnabled())
      logger.debug("located end marker, sequence is: " + end_marker.getSequence());
    MenuEditItemObject insert_after = menu.getPreviousItem(end_marker);
    if (logger.isDebugEnabled())
      logger.debug("insert-after item is " + insert_after.getItemType() + ":" + insert_after.getText() + ", seq = "
		   + insert_after.getSequence());
    int new_sequence = insert_after.getSequence() + STD_GAP;
    if (logger.isDebugEnabled())
      logger.debug("new item sequence is " + new_sequence);
    if (new_sequence>=end_marker.getSequence())
    { // need to renumber the menu items
      if (logger.isDebugEnabled())
	logger.debug("renumbering existing items...");
      MenuEditItemObject item = menu.getItemByTypeText("MARKER","begin-services");
      new_sequence = item.getSequence();
      item = menu.getNextItem(item);
      new_sequence += STD_GAP;
      while (!(item.getItemType().equals("MARKER")))
      { // reset the numbers for the sequence
	if (logger.isDebugEnabled())
	  logger.debug("new sequence for item " + item.getItemType() + ":" + item.getText() + " is " + new_sequence);
	item.setSequence(new_sequence);
	item = menu.getNextItem(item);
	new_sequence += STD_GAP;

      } // end while

      if (logger.isDebugEnabled())
	logger.debug("new item sequence is " + new_sequence);

    } // end if

    MenuEditItemObject new_object = menu.addItem("TEXT",new_sequence,text,linktype,link);
    new_object.setIfDefVar(shortvar);
    menu.addVariable(shortvar,null);

  } // end installCommunityServiceEntry

  public void removeCommunityServiceEntry(DynamoUser caller, String link_text, String shortvar)
      throws DatabaseException, DynamoSecurityException
  {
    MenuEditObject menu = m_provider.getEditMenu(caller,VeniceNamespaces.COMMUNITY_GLOBALS_NAMESPACE,"community.menu");
    MenuEditItemObject item = menu.getItemContainingLinkText(link_text);
    if (item!=null)
      item.delete();
    menu.removeVariable(shortvar);

  } // end removeCommunityServiceEntry

  public void installCommunityServiceAdminEntry(DynamoUser caller, String text, String linktype, String link,
						String perm_nsid, String perm_name, String shortvar)
      throws DatabaseException, DynamoSecurityException
  {
    MenuEditObject menu = m_provider.getEditMenu(caller,VeniceNamespaces.COMMUNITY_GLOBALS_NAMESPACE,
						 "community.admin.service");
    MenuEditItemObject end_marker = menu.getItemByTypeText("MARKER","end-service-admin");
    if (logger.isDebugEnabled())
      logger.debug("located end marker, sequence is: " + end_marker.getSequence());
    MenuEditItemObject insert_after = menu.getPreviousItem(end_marker);
    if (logger.isDebugEnabled())
      logger.debug("insert-after item is " + insert_after.getItemType() + ":" + insert_after.getText() + ", seq = "
		   + insert_after.getSequence());
    int new_sequence = insert_after.getSequence() + STD_GAP;
    if (logger.isDebugEnabled())
      logger.debug("new item sequence is " + new_sequence);
    if (new_sequence>=end_marker.getSequence())
    { // need to renumber the menu items
      if (logger.isDebugEnabled())
	logger.debug("renumbering existing items...");
      MenuEditItemObject item = menu.getItemByTypeText("MARKER","begin-service-admin");
      new_sequence = item.getSequence();
      item = menu.getNextItem(item);
      new_sequence += STD_GAP;
      while (!(item.getItemType().equals("MARKER")))
      { // reset the numbers for the sequence
	if (logger.isDebugEnabled())
	  logger.debug("new sequence for item " + item.getItemType() + ":" + item.getText() + " is " + new_sequence);
	item.setSequence(new_sequence);
	item = menu.getNextItem(item);
	new_sequence += STD_GAP;

      } // end while

      if (logger.isDebugEnabled())
	logger.debug("new item sequence is " + new_sequence);

    } // end if

    MenuEditItemObject new_object = menu.addItem("TEXT",new_sequence,text,linktype,link);
    new_object.setPermission(perm_nsid,perm_name);
    new_object.setIfDefVar(shortvar);
    menu.addVariable(shortvar,null);

  } // end installCommunityServiceAdminEntry

  public void removeCommunityServiceAdminEntry(DynamoUser caller, String link_text, String shortvar)
      throws DatabaseException, DynamoSecurityException
  {
    MenuEditObject menu = m_provider.getEditMenu(caller,VeniceNamespaces.COMMUNITY_GLOBALS_NAMESPACE,
						 "community.admin.service");
    MenuEditItemObject item = menu.getItemContainingLinkText(link_text);
    if (item!=null)
      item.delete();
    menu.removeVariable(shortvar);

  } // end removeCommunityServiceAdminEntry

} // end class CommunityMenuInstaller
