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

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

  private static final int STD_GAP = 100;

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

  private MenuProvider m_provider;

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

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

  } // end constructor

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

  public void installSystemAdminEntry(DynamoUser caller, String text, String linktype, String link, String perm_nsid,
				      String perm_name) throws DatabaseException, DynamoSecurityException
  {
    MenuEditObject menu = m_provider.getEditMenu(caller,VeniceNamespaces.SYSTEM_PERMS_NAMESPACE,
						 "system.admin.module");
    MenuEditItemObject end_marker = menu.getItemByTypeText("MARKER","end-module-sysadmin");
    MenuEditItemObject insert_after = menu.getPreviousItem(end_marker);
    int new_sequence = insert_after.getSequence() + STD_GAP;
    if (new_sequence>=end_marker.getSequence())
    { // need to renumber the menu items
      MenuEditItemObject item = menu.getItemByTypeText("MARKER","begin-module-sysadmin");
      new_sequence = item.getSequence();
      item = menu.getNextItem(item);
      new_sequence += STD_GAP;
      while (!(item.getItemType().equals("MARKER")))
      { // reset the numbers for the sequence
	item.setSequence(new_sequence);
	item = menu.getNextItem(item);
	new_sequence += STD_GAP;

      } // end while

    } // end if

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

  } // end installSystemAdminEntry

  public void removeSystemAdminEntry(DynamoUser caller, String link_text)
      throws DatabaseException, DynamoSecurityException
  {
    MenuEditObject menu = m_provider.getEditMenu(caller,VeniceNamespaces.SYSTEM_PERMS_NAMESPACE,
						 "system.admin.module");
    MenuEditItemObject item = menu.getItemContainingLinkText(link_text);
    if (item!=null)
      item.delete();

  } // end removeSystemAdminEntry

} // end class SystemAdminMenuInstaller
